Comparing version 3.0.9 to 3.0.10
@@ -14,9 +14,11 @@ import type { Context } from "koa"; | ||
declare type MethodContext<RequestSchema extends ValidStructure, URLParams extends Record<string, string> = Record<string, never>> = ExtendedContext<RequestSchema, URLParams>; | ||
declare type MethodHandlerFunctionResponse<ResponseSchema> = { | ||
declare type MethodHandlerFunctionResponse<ResponseSchema> = ResponseSchema extends NonNullableValidStructure ? { | ||
headers?: Record<string, string>; | ||
status?: number; | ||
} & (ResponseSchema extends NonNullableValidStructure ? { | ||
body: ZodInfer<ResponseSchema>; | ||
} : Record<string, never>); | ||
} : { | ||
headers?: Record<string, string>; | ||
status?: number; | ||
}; | ||
export declare type MethodHandlerFunction<RequestSchema extends ValidStructure, ResponseSchema extends ValidStructure, URLParams extends Record<string, string>> = (context: MethodContext<RequestSchema, URLParams>) => Promise<MethodHandlerFunctionResponse<ResponseSchema>>; | ||
export {}; |
@@ -26,8 +26,10 @@ "use strict"; | ||
const { handler, accepts = [], schema, middleware: { pre = [], post = [] } = {}, } = controller; | ||
const checkIfResponded = (context, next) => { | ||
if (context.headerSent) | ||
return; | ||
else | ||
return next(); | ||
}; | ||
const handlerResolver = (context, next) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
const continueToPost = () => { | ||
if (post.length > 0) | ||
next(); | ||
}; | ||
var _a, _b; | ||
const requestValidation = yield schema.request.safeParseAsync((_a = context.request.body) !== null && _a !== void 0 ? _a : null); | ||
@@ -37,3 +39,3 @@ if (!requestValidation.success) { | ||
context.status = 400; | ||
yield continueToPost(); | ||
yield next(); | ||
return; | ||
@@ -43,18 +45,19 @@ } | ||
context.request.body = (requestValidation.data); | ||
const { status = 200, headers = {}, body: responseBody, } = yield handler(context); | ||
const responseValidation = yield schema.response.safeParseAsync(responseBody !== null && responseBody !== void 0 ? responseBody : null); | ||
const response = yield handler(context); | ||
const responseValidation = yield schema.response.safeParseAsync("body" in response ? response.body : null); | ||
if (!responseValidation.success) { | ||
console.warn(errors_1.Errors.RESPONSE_MISMATCH); | ||
context.status = 500; | ||
yield continueToPost(); | ||
yield next(); | ||
return; | ||
} | ||
context.body = responseValidation.data; | ||
context.status = status; | ||
for (const [key, value] of Object.entries(headers)) | ||
context.status = (_b = response.status) !== null && _b !== void 0 ? _b : 200; | ||
for (const [key, value] of Object.entries(response.headers || {})) | ||
context.set(key, value.toString()); | ||
continueToPost(); | ||
return next(); | ||
}); | ||
const methodRegistrar = router[method]; | ||
const chain = [ | ||
checkIfResponded, | ||
...(0, middleware_1.getParsingMiddleware)(accepts), | ||
@@ -61,0 +64,0 @@ ...pre, |
@@ -32,10 +32,13 @@ import type { Context } from "koa"; | ||
type MethodHandlerFunctionResponse<ResponseSchema> = { | ||
headers?: Record<string, string>; | ||
status?: number; | ||
} & (ResponseSchema extends NonNullableValidStructure | ||
? { | ||
body: ZodInfer<ResponseSchema>; | ||
} | ||
: Record<string, never>); | ||
type MethodHandlerFunctionResponse<ResponseSchema> = | ||
ResponseSchema extends NonNullableValidStructure | ||
? { | ||
headers?: Record<string, string>; | ||
status?: number; | ||
body: ZodInfer<ResponseSchema>; | ||
} | ||
: { | ||
headers?: Record<string, string>; | ||
status?: number; | ||
}; | ||
@@ -42,0 +45,0 @@ export type MethodHandlerFunction< |
@@ -33,7 +33,8 @@ import Router from "koa-router"; | ||
const checkIfResponded = (context: ExtendedContext, next: Next) => { | ||
if (context.headerSent) return; | ||
else return next(); | ||
}; | ||
const handlerResolver = async (context: ExtendedContext, next: Next) => { | ||
const continueToPost = () => { | ||
if (post.length > 0) next(); | ||
}; | ||
const requestValidation = await schema.request.safeParseAsync( | ||
@@ -46,3 +47,3 @@ context.request.body ?? null | ||
context.status = 400; | ||
await continueToPost(); | ||
await next(); | ||
return; | ||
@@ -54,10 +55,6 @@ } else | ||
const { | ||
status = 200, | ||
headers = {}, | ||
body: responseBody, | ||
} = await handler(context); | ||
const response = await handler(context); | ||
const responseValidation = await schema.response.safeParseAsync( | ||
responseBody ?? null | ||
"body" in response ? response.body : null | ||
); | ||
@@ -68,3 +65,3 @@ | ||
context.status = 500; | ||
await continueToPost(); | ||
await next(); | ||
return; | ||
@@ -74,8 +71,8 @@ } | ||
context.body = responseValidation.data; | ||
context.status = status; | ||
context.status = response.status ?? 200; | ||
for (const [key, value] of Object.entries(headers)) | ||
for (const [key, value] of Object.entries(response.headers || {})) | ||
context.set(key, value.toString()); | ||
continueToPost(); | ||
return next(); | ||
}; | ||
@@ -88,2 +85,3 @@ | ||
const chain = [ | ||
checkIfResponded, | ||
...getParsingMiddleware(accepts), | ||
@@ -90,0 +88,0 @@ ...pre, |
{ | ||
"name": "18h", | ||
"description": "A Next.js style dynamic API router for Koa-based APIs.", | ||
"version": "3.0.9", | ||
"version": "3.0.10", | ||
"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
40476
740