Skip to content

Commit

Permalink
refactor: don’t use alias for i18n __ local object
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed May 26, 2023
1 parent bdbb5e3 commit 4b4799d
Show file tree
Hide file tree
Showing 34 changed files with 137 additions and 98 deletions.
8 changes: 4 additions & 4 deletions packages/endpoint-auth/lib/controllers/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ export const authorizationController = {
]) {
if (!request.query[parameter]) {
throw IndiekitError.badRequest(
response.__("BadRequestError.missingParameter", parameter)
response.locals.__("BadRequestError.missingParameter", parameter)
);
}
}

// `response_type` must be `code` (or deprecated `id`)
if (!/^(code|id)$/.test(request.query.response_type)) {
throw IndiekitError.badRequest(
response.__("BadRequestError.invalidValue", "response_type")
response.locals.__("BadRequestError.invalidValue", "response_type")
);
}

// `client_id`, `redirect_uri` and `me` (optional) must be valid URLs
for (const uri of ["client_id", "me", "redirect_uri"]) {
if (request.query[uri] && !isUrl(request.query[uri])) {
throw IndiekitError.badRequest(
response.__("BadRequestError.invalidValue", uri)
response.locals.__("BadRequestError.invalidValue", uri)
);
}

Expand All @@ -65,7 +65,7 @@ export const authorizationController = {
const validRedirect = validateRedirect(redirect_uri, client_id);
if (!validRedirect) {
throw IndiekitError.badRequest(
response.__("BadRequestError.invalidValue", "redirect_uri")
response.locals.__("BadRequestError.invalidValue", "redirect_uri")
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/endpoint-auth/lib/controllers/consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const consentController = {
get(request, response) {
if (!request.query.request_uri) {
throw IndiekitError.badRequest(
response.__("BadRequestError.missingParameter", "request_uri")
response.locals.__("BadRequestError.missingParameter", "request_uri")
);
}

Expand All @@ -24,7 +24,7 @@ export const consentController = {

if (process.env.PASSWORD_SECRET) {
response.render("consent", {
title: response.__(`auth.consent.${authType}.title`),
title: response.locals.__(`auth.consent.${authType}.title`),
authType,
me,
redirect_uri,
Expand All @@ -35,7 +35,7 @@ export const consentController = {
}
} catch {
throw IndiekitError.badRequest(
response.__("BadRequestError.invalidValue", "request_uri")
response.locals.__("BadRequestError.invalidValue", "request_uri")
);
}
},
Expand All @@ -60,7 +60,7 @@ export const consentController = {
const errors = validationResult(request);
if (!errors.isEmpty()) {
return response.status(422).render("consent", {
title: response.__(`auth.consent.${authType}.title`),
title: response.locals.__(`auth.consent.${authType}.title`),
authType,
errors: errors.mapped(),
me,
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-auth/lib/controllers/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export const documentationController = (error, request, response, next) => {
if (request.accepts("html")) {
response.render("auth", {
title: response.__("auth.guidance.title"),
title: response.locals.__("auth.guidance.title"),
error: error.message,
});
} else if (request.accepts("json")) {
Expand Down
8 changes: 4 additions & 4 deletions packages/endpoint-auth/lib/controllers/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export const passwordController = {
const { name } = request.app.locals.application;

response.render("new-password", {
title: response.__("auth.newPassword.title"),
title: response.locals.__("auth.newPassword.title"),
notice: request.query.setup
? response.__("auth.newPassword.setup.text", { app: name })
? response.locals.__("auth.newPassword.setup.text", { app: name })
: false,
});
},
Expand All @@ -27,7 +27,7 @@ export const passwordController = {
const errors = validationResult(request);
if (!errors.isEmpty()) {
return response.status(422).render("new-password", {
title: response.__("auth.newPassword.title"),
title: response.locals.__("auth.newPassword.title"),
errors: errors.mapped(),
});
}
Expand All @@ -36,7 +36,7 @@ export const passwordController = {
const secret = await createPasswordHash(password);

response.render("new-password", {
title: response.__("auth.newPassword.title"),
title: response.locals.__("auth.newPassword.title"),
password,
secret,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-auth/lib/controllers/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const tokenController = {
accessToken = verifyToken(bearerToken);
} catch {
throw IndiekitError.unauthorized(
response.__("UnauthorizedError.invalidToken")
response.locals.__("UnauthorizedError.invalidToken")
);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/endpoint-auth/lib/middleware/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ export const codeValidator = (request, response, next) => {
]) {
if (!Object.keys(parameters).includes(parameter)) {
throw IndiekitError.badRequest(
response.__("BadRequestError.missingParameter", parameter)
response.locals.__("BadRequestError.missingParameter", parameter)
);
}
}

// `grant_type` must equal `authorization_code`
if (grant_type !== "authorization_code") {
throw IndiekitError.badRequest(
response.__("BadRequestError.invalidValue", "grant_type")
response.locals.__("BadRequestError.invalidValue", "grant_type")
);
}

// Validate `client_id` against that provided in authorization request
if (getCanonicalUrl(client_id) !== client.url) {
throw IndiekitError.unauthorized(
response.__("BadRequestError.invalidValue", "client_id")
response.locals.__("BadRequestError.invalidValue", "client_id")
);
}

// Validate `redirect_uri`
const validRedirect = validateRedirect(redirect_uri, client_id);
if (!validRedirect) {
throw IndiekitError.badRequest(
response.__("BadRequestError.invalidValue", "redirect_uri")
response.locals.__("BadRequestError.invalidValue", "redirect_uri")
);
}

Expand All @@ -56,7 +56,7 @@ export const codeValidator = (request, response, next) => {
request.verifiedToken = verifyToken(code);
} catch {
throw IndiekitError.unauthorized(
response.__("UnauthorizedError.invalidToken")
response.locals.__("UnauthorizedError.invalidToken")
);
}

Expand All @@ -66,7 +66,7 @@ export const codeValidator = (request, response, next) => {
const verifiedCode = verifyCode(code_verifier, code_challenge);
if (!verifiedCode) {
throw IndiekitError.unauthorized(
response.__("BadRequestError.invalidValue", "code_verifier")
response.locals.__("BadRequestError.invalidValue", "code_verifier")
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-auth/lib/middleware/secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IndiekitError } from "@indiekit/error";
export const hasSecret = (request, response, next) => {
if (!process.env.SECRET) {
const error = IndiekitError.notImplemented(
response.__("NotImplementedError.secret")
response.locals.__("NotImplementedError.secret")
);

next(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-auth/lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const scopes = ["create", "update", "draft", "media", "delete"];
*/
export function getScopeItems(scope, response) {
return scopes.map((value) => ({
text: response.__(`scope.${value}.label`),
text: response.locals.__(`scope.${value}.label`),
value,
checked: scope?.includes(value),
}));
Expand Down
4 changes: 3 additions & 1 deletion packages/endpoint-auth/tests/unit/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import test from "ava";
import { getScopeItems } from "../../lib/scope.js";

test("Gets `items` object for checkboxes component", (t) => {
const response = { __: (value) => value };
const response = {
locals: { __: (value) => value },
};
const result = getScopeItems("create update", response);

t.is(result.length, 5);
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint-files/lib/controllers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const deleteController = {

if (scope.includes("delete")) {
return response.render("file-delete", {
title: response.__("files.delete.title"),
title: response.locals.__("files.delete.title"),
back,
parent: { text: fileName },
});
Expand Down Expand Up @@ -39,7 +39,7 @@ export const deleteController = {
} catch (error) {
response.status(error.status || 500);
response.render("file-delete", {
title: response.__("files.delete.title"),
title: response.locals.__("files.delete.title"),
parent: { text: fileName },
error: error.message,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint-files/lib/controllers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const fileController = async (request, response, next) => {
file,
parent: {
href: back,
text: response.__("files.files.title"),
text: response.locals.__("files.files.title"),
},
actions: [
scope.includes("delete")
? {
classes: "actions__link--warning",
href: path.join(request.baseUrl + request.path, "/delete"),
icon: "delete",
text: response.__("files.delete.action"),
text: response.locals.__("files.delete.action"),
}
: {},
],
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint-files/lib/controllers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export const filesController = async (request, response, next) => {
* @todo Remove requirement for private `_count` parameter
*/
response.render("files", {
title: response.__("files.files.title"),
title: response.locals.__("files.files.title"),
actions: [
scope.includes("create") || scope.includes("media")
? {
href: path.join(request.baseUrl + request.path, "/upload"),
icon: "uploadFile",
text: response.__("files.upload.action"),
text: response.locals.__("files.upload.action"),
}
: {},
],
Expand Down
6 changes: 3 additions & 3 deletions packages/endpoint-files/lib/controllers/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const formController = {

if (scope.includes("create") || scope.includes("media")) {
return response.render("file-form", {
title: response.__("files.upload.title"),
title: response.locals.__("files.upload.title"),
});
}

Expand All @@ -31,7 +31,7 @@ export const formController = {
const errors = validationResult(request);
if (!errors.isEmpty()) {
return response.status(422).render("file-form", {
title: response.__("files.upload.title"),
title: response.locals.__("files.upload.title"),
errors: errors.mapped(),
});
}
Expand All @@ -52,7 +52,7 @@ export const formController = {
} catch (error) {
response.status(error.status || 500);
response.render("file-form", {
title: response.__("files.upload.title"),
title: response.locals.__("files.upload.title"),
error: error.message,
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/endpoint-files/lib/middleware/file-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const fileData = {
let nextError = error;

if (error.message === "Invalid URL") {
nextError = IndiekitError.notFound(response.__("NotFoundError.page"));
nextError = IndiekitError.notFound(
response.locals.__("NotFoundError.page")
);
}

next(nextError);
Expand Down
12 changes: 6 additions & 6 deletions packages/endpoint-media/lib/controllers/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const actionController = async (request, response, next) => {
const hasScope = checkScope(scope);
if (!hasScope) {
throw IndiekitError.insufficientScope(
response.__("ForbiddenError.insufficientScope"),
response.locals.__("ForbiddenError.insufficientScope"),
{ scope: action }
);
}
Expand All @@ -31,7 +31,7 @@ export const actionController = async (request, response, next) => {
// Check for file in request
if (!files || !files.file || files.file.truncated) {
throw IndiekitError.badRequest(
response.__("BadRequestError.missingProperty", "file")
response.locals.__("BadRequestError.missingProperty", "file")
);
}

Expand All @@ -44,7 +44,7 @@ export const actionController = async (request, response, next) => {
// Check for URL if deleting a file
if (action === "delete" && !url) {
throw IndiekitError.badRequest(
response.__("BadRequestError.missingParameter", "url")
response.locals.__("BadRequestError.missingParameter", "url")
);
}

Expand All @@ -69,21 +69,21 @@ export const actionController = async (request, response, next) => {
// Hoist not found error to controller to localise response
if (error.name === "NotFoundError") {
nextError = IndiekitError.notFound(
response.__("NotFoundError.record", error.message)
response.locals.__("NotFoundError.record", error.message)
);
}

// Hoist unsupported media type error to controller to localise response
if (error.name === "UnsupportedMediaTypeError") {
nextError = IndiekitError.unsupportedMediaType(
response.__("UnsupportedMediaTypeError.type", error.message)
response.locals.__("UnsupportedMediaTypeError.type", error.message)
);
}

// Hoist unsupported post type error to controller to localise response
if (error.name === "NotImplementedError") {
nextError = IndiekitError.notImplemented(
response.__("NotImplementedError.postType", error.message),
response.locals.__("NotImplementedError.postType", error.message),
{ uri: "https://getindiekit.com/configuration/post-types" }
);
}
Expand Down
11 changes: 7 additions & 4 deletions packages/endpoint-media/lib/controllers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export const queryController = async (request, response, next) => {
const { q, url } = request.query;
if (!q) {
throw IndiekitError.badRequest(
response.__("BadRequestError.missingParameter", "q")
response.locals.__("BadRequestError.missingParameter", "q")
);
}

switch (q) {
case "source": {
if (!application.hasDatabase) {
throw IndiekitError.notImplemented(
response.__("NotImplementedError.database")
response.locals.__("NotImplementedError.database")
);
}

Expand All @@ -44,7 +44,7 @@ export const queryController = async (request, response, next) => {

if (!item) {
throw IndiekitError.notFound(
response.__("NotFoundError.resource", "file")
response.locals.__("NotFoundError.resource", "file")
);
}

Expand Down Expand Up @@ -73,7 +73,10 @@ export const queryController = async (request, response, next) => {

default: {
throw IndiekitError.notImplemented(
response.__("NotImplementedError.query", { key: "q", value: q })
response.locals.__("NotImplementedError.query", {
key: "q",
value: q,
})
);
}
}
Expand Down
Loading

0 comments on commit 4b4799d

Please sign in to comment.