Skip to content

Commit

Permalink
move error handling into bodyParser middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ridafkih committed May 12, 2022
1 parent 7be3aea commit 076205c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
13 changes: 10 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Koa, { Next } from "koa";
import Router from "koa-router";
import { mapDirectoryToRoutes } from "@/util/filesystem";
import { handleParseError, bodyParser } from "@/middleware/body-parser";

import { ExtendedContext } from "@/@types/http-method";
import { ParsedRouteController } from "@/@types/route-controller";
import bodyParser from "koa-bodyparser";

interface CreateRouterParams {
routesFolder: string;
Expand Down Expand Up @@ -38,9 +38,16 @@ export const createRouter = async ({
controller: ParsedRouteController
) => {
for (const { internalHandler, method, middleware, accept } of controller) {
const enableTypes: string[] = accept || [];
const { pre = [], post = [] } = middleware || {};
const parsingMiddleware = accept
? [handleParseError, bodyParser({ enableTypes: accept })]

const parsingMiddleware = enableTypes.length
? [
bodyParser({
enableTypes: accept,
onerror: (_error, context) => context.throw(422),
}),
]
: [];

const middlewareChain = [
Expand Down
12 changes: 0 additions & 12 deletions lib/middleware/body-parser.ts

This file was deleted.

0 comments on commit 076205c

Please sign in to comment.