Skip to content

Commit

Permalink
feat: update to koa-jsonapi-zod 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Mar 4, 2024
1 parent 1679dbd commit e1a293b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .lefthook/commit-msg/commitlint.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
echo $(head -n1 $1) | npx commitlint --color
echo $(head -n1 $1) | pnpm exec commitlint --color
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pre-commit:
commands:
check:
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
run: npx biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} && git update-index --again
run: pnpm exec biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} && git update-index --again

commit-msg:
scripts:
Expand Down
2 changes: 1 addition & 1 deletion skeleton/base/.lefthook/commit-msg/commitlint.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
echo $(head -n1 $1) | npx commitlint --color
echo $(head -n1 $1) | pnpm exec commitlint --color
2 changes: 1 addition & 1 deletion skeleton/base/lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pre-commit:
commands:
check:
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
run: npx biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} && git update-index --again
run: pnpm exec biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} && git update-index --again

commit-msg:
scripts:
Expand Down
2 changes: 1 addition & 1 deletion skeleton/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"koa-bodyparser": "^4.3.0",
"koa-cache-control": "^2.0.0",
"koa-compress": "^5.0.1",
"koa-jsonapi-zod": "^1.1.0",
"koa-jsonapi-zod": "^1.4.0",
"koa-tree-router": "^0.12.1",
"source-map-support": "^0.5.21",
"winston": "^3.6.0",
Expand Down
50 changes: 12 additions & 38 deletions skeleton/templates/src/index.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import bodyParser from "koa-bodyparser";
import cacheControl from "koa-cache-control";
import compress from "koa-compress";
import {
JsonApiErrorBody,
jsonApiErrorMiddleware,
jsonApiRequestMiddleware,
methodNotAllowedHandler,
} from "koa-jsonapi-zod";
import Router from "koa-tree-router";
import "source-map-support/register.js";
Expand All @@ -20,9 +20,9 @@ import { logger } from "./util/winston.js";
import { ZodValidationError } from "./util/zod.js";
{{#if (has features "appconfig")}}

const result = await appConfigHandler.start();
const appConfigStartResult = await appConfigHandler.start();

if (result.error) {
if (appConfigStartResult.error) {
throw result.error;
}
{{/if}}
Expand All @@ -31,10 +31,16 @@ const app = new Koa();

app.use(cacheControl({ noStore: true }));
app.use(
jsonApiRequestMiddleware({
excludedPaths: ["/health"],
compress({
br: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 1,
},
},
}),
);
app.use(cors({ maxAge: 86400 }));
app.use(jsonApiRequestMiddleware());
app.use(
jsonApiErrorMiddleware({
logError: (error, exposed) => {
Expand All @@ -45,16 +51,6 @@ app.use(
}),
);
app.use(bodyParser());
app.use(cors({ maxAge: 86400 }));
app.use(
compress({
br: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 1,
},
},
}),
);

app.use(async (context, next) => {
if (context.url === "/health") {
Expand All @@ -69,29 +65,7 @@ app.use(async (context, next) => {
app.use(requestContextMiddleware);
{{/if}}

const router = new Router({
onMethodNotAllowed: (context) => {
if (context.response.headers.allow === "") {
context.remove("allow");
context.status = 404;
context.body = new JsonApiErrorBody({
status: "404",
code: "not_found",
title: "Resource not found",
});
return;
}

context.status = 405;
context.body = new JsonApiErrorBody({
status: "405",
code: "method_not_allowed",
title: "Method not allowed",
detail: `Allowed methods: ${context.response.headers.allow}`,
});
},
});

const router = new Router({ onMethodNotAllowed: methodNotAllowedHandler });
registerRoutes(router);
app.use(router.routes());

Expand Down

0 comments on commit e1a293b

Please sign in to comment.