Comparing version 2.2.1 to 2.2.2
@@ -5,3 +5,2 @@ import { Context, Middleware } from "koa"; | ||
import { ObjectShape } from "yup/lib/object"; | ||
import bodyParser from "koa-bodyparser"; | ||
declare type RecursivePartial<T> = { | ||
@@ -28,3 +27,3 @@ [P in keyof T]?: RecursivePartial<T[P]>; | ||
} : { | ||
accept: NonNullable<bodyParser.Options["enableTypes"]>; | ||
accept: ("json" | "form")[]; | ||
validation: ObjectSchema<RequestBody extends ObjectShape ? RequestBody : Record<string, never>>; | ||
@@ -31,0 +30,0 @@ }); |
@@ -51,14 +51,14 @@ "use strict"; | ||
const { pre = [], post = [] } = middleware || {}; | ||
const parsingMiddleware = accept ? [body_parser_1.handleParseError, (0, body_parser_1.parseBody)()] : []; | ||
const parsingMiddleware = accept | ||
? [body_parser_1.handleParseError, (0, body_parser_1.bodyParser)({ enableTypes: accept })] | ||
: []; | ||
const middlewareChain = [ | ||
...parsingMiddleware, | ||
...pre, | ||
internalHandler, | ||
(context, next) => __awaiter(void 0, void 0, void 0, function* () { | ||
yield internalHandler(context); | ||
yield next(); | ||
}), | ||
...post, | ||
].map((func) => { | ||
return (context, next) => __awaiter(void 0, void 0, void 0, function* () { | ||
yield func(context, next); | ||
yield next(); | ||
}); | ||
}); | ||
]; | ||
if (typeof router[method] === "function") | ||
@@ -65,0 +65,0 @@ router[method](path, ...middlewareChain); |
import { Context, Next } from "koa"; | ||
import bodyParser from "koa-bodyparser"; | ||
export declare const handleParseError: () => (context: Context, next: Next) => void; | ||
export declare const parseBody: typeof bodyParser; | ||
export declare const handleParseError: (context: Context, next: Next) => void; | ||
export { bodyParser }; |
@@ -6,14 +6,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseBody = exports.handleParseError = void 0; | ||
exports.bodyParser = exports.handleParseError = void 0; | ||
const koa_bodyparser_1 = __importDefault(require("koa-bodyparser")); | ||
const handleParseError = () => { | ||
return (context, next) => { | ||
next().catch((error) => { | ||
if (!(error instanceof SyntaxError)) | ||
return; | ||
context.throw(500); | ||
}); | ||
}; | ||
exports.bodyParser = koa_bodyparser_1.default; | ||
const handleParseError = (context, next) => { | ||
next().catch((error) => { | ||
if (!(error instanceof SyntaxError)) | ||
return; | ||
context.throw(500); | ||
}); | ||
}; | ||
exports.handleParseError = handleParseError; | ||
exports.parseBody = koa_bodyparser_1.default; |
@@ -48,3 +48,3 @@ import { Context, Middleware } from "koa"; | ||
: { | ||
accept: NonNullable<bodyParser.Options["enableTypes"]>; | ||
accept: ("json" | "form")[]; | ||
validation: ObjectSchema< | ||
@@ -51,0 +51,0 @@ RequestBody extends ObjectShape ? RequestBody : Record<string, never> |
import Koa, { Next } from "koa"; | ||
import Router from "koa-router"; | ||
import { mapDirectoryToRoutes } from "@/util/filesystem"; | ||
import { handleParseError, parseBody } from "@/middleware/body-parser"; | ||
import { handleParseError, bodyParser } from "@/middleware/body-parser"; | ||
@@ -42,14 +42,15 @@ import { ExtendedContext } from "@/@types/http-method"; | ||
const { pre = [], post = [] } = middleware || {}; | ||
const parsingMiddleware = accept ? [handleParseError, parseBody()] : []; | ||
const parsingMiddleware = accept | ||
? [handleParseError, bodyParser({ enableTypes: accept })] | ||
: []; | ||
const middlewareChain = [ | ||
...parsingMiddleware, | ||
...pre, | ||
internalHandler, | ||
async (context: ExtendedContext, next: Next) => { | ||
await internalHandler(context); | ||
await next(); | ||
}, | ||
...post, | ||
].map((func) => { | ||
return async (context: ExtendedContext, next: Next) => { | ||
await func(context, next); | ||
await next(); | ||
}; | ||
}); | ||
]; | ||
@@ -56,0 +57,0 @@ if (typeof router[method as keyof typeof router] === "function") |
import { Context, Next } from "koa"; | ||
import bodyParser from "koa-bodyparser"; | ||
export const handleParseError = () => { | ||
return (context: Context, next: Next) => { | ||
next().catch((error: Error) => { | ||
if (!(error instanceof SyntaxError)) return; | ||
context.throw(500); | ||
}); | ||
}; | ||
export const handleParseError = (context: Context, next: Next) => { | ||
next().catch((error: Error) => { | ||
if (!(error instanceof SyntaxError)) return; | ||
context.throw(500); | ||
}); | ||
}; | ||
export const parseBody = bodyParser; | ||
export { bodyParser }; |
{ | ||
"name": "18h", | ||
"description": "A Next.js style dynamic API router for Koa-based APIs.", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "url": "https://github.com/ridafkih/18h" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30156
633