Skip to content

Commit

Permalink
feat: add methodNotAllowedHandler to remove downstream boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Mar 4, 2024
1 parent e7180b6 commit 8d55ac5
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 96 deletions.
4 changes: 2 additions & 2 deletions examples/complete/node_modules/.bin/tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/complete/node_modules/@mikro-orm/core

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/complete/node_modules/@mikro-orm/sqlite

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/complete/node_modules/@types/koa

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/complete/node_modules/mikro-orm-js-joda

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/complete/node_modules/tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/complete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"type": "module",
"dependencies": {
"@js-joda/core": "^5.6.1",
"@mikro-orm/core": "^6.1.0",
"@mikro-orm/sqlite": "^6.1.0",
"@mikro-orm/core": "^6.1.7",
"@mikro-orm/sqlite": "^6.1.7",
"flat": "^6.0.1",
"http-errors": "^2.0.0",
"koa": "^2.15.0",
Expand All @@ -16,9 +16,9 @@
},
"devDependencies": {
"@types/http-errors": "^2.0.4",
"@types/koa": "^2.14.0",
"@types/koa": "^2.15.0",
"@types/koa-bodyparser": "^4.3.12",
"tsx": "^4.7.0",
"tsx": "^4.7.1",
"typescript": "^5.3.3"
},
"scripts": {
Expand Down
36 changes: 4 additions & 32 deletions examples/complete/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import Koa from "koa";
import bodyParser from "koa-bodyparser";
import {
JsonApiErrorBody,
jsonApiErrorMiddleware,
jsonApiRequestMiddleware,
methodNotAllowedHandler,
} from "koa-jsonapi-zod";
import Router from "koa-tree-router";
import { registerRoutes } from "./route/index.js";
import { requestContextMiddleware } from "./util/mikro-orm.js";

const app = new Koa();

app.use(
jsonApiRequestMiddleware({
excludedPaths: ["/health"],
}),
);

app.use(jsonApiRequestMiddleware());
app.use(
jsonApiErrorMiddleware({
logError: (error, exposed) => {
Expand All @@ -27,10 +22,8 @@ app.use(
},
}),
);

app.use(bodyParser());

// This is a non-resource endpoint, hence it was excluded from the `jsonApiMiddleware`
app.use(async (context, next) => {
if (context.url === "/health") {
context.body = { status: "alive" };
Expand All @@ -40,30 +33,9 @@ app.use(async (context, next) => {
return next();
});

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(requestContextMiddleware);
app.use(router.routes());

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@tsconfig/node20": "^20.1.2",
"@types/content-type": "^1.1.8",
"@types/http-errors": "^2.0.4",
"@types/koa": "^2.14.0",
"@types/koa": "^2.15.0",
"@types/node": "^20.11.17",
"@types/qs": "^6.9.11",
"@vitest/coverage-v8": "^1.2.2",
Expand Down
Loading

0 comments on commit 8d55ac5

Please sign in to comment.