Comparing version 3.0.2 to 3.0.3
@@ -18,14 +18,4 @@ "use strict"; | ||
const koa_router_1 = __importDefault(require("koa-router")); | ||
const koa_bodyparser_1 = __importDefault(require("koa-bodyparser")); | ||
const filesystem_1 = require("../util/filesystem"); | ||
const getParsingMiddleware = (accepts) => { | ||
return accepts.length | ||
? [ | ||
(0, koa_bodyparser_1.default)({ | ||
enableTypes: accepts, | ||
onerror: (_error, context) => context.throw(422), | ||
}), | ||
] | ||
: []; | ||
}; | ||
const routes_1 = require("../util/routes"); | ||
const router = ({ routesFolder, middleware = [], port, hostname, }) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -36,44 +26,5 @@ const app = new koa_1.default(); | ||
middleware.forEach(app.use); | ||
const registerRoute = (path, routes) => { | ||
for (const [method, controller] of Object.entries(routes)) { | ||
if (!controller) | ||
continue; | ||
const { handler, accepts = [], schema, middleware: { pre = [], post = [] } = {}, } = controller; | ||
const handlerResolver = (context, next) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
const requestValidation = yield schema.request.safeParseAsync((_a = context.request.body) !== null && _a !== void 0 ? _a : null); | ||
if (!requestValidation.success) { | ||
context.status = 400; | ||
context.body = requestValidation.error; | ||
yield next(); | ||
return; | ||
} | ||
const { status = 200, headers = {}, body: responseBody, } = yield handler(context); | ||
const responseValidation = yield schema.response.safeParseAsync(responseBody !== null && responseBody !== void 0 ? responseBody : null); | ||
if (!responseValidation.success) { | ||
context.status = 500; | ||
yield next(); | ||
return; | ||
} | ||
context.status = status; | ||
context.body = responseBody; | ||
for (const [key, value] of Object.entries(headers)) | ||
context.set(key, value.toString()); | ||
yield next(); | ||
}); | ||
const methodRegistrar = router[method]; | ||
const chain = [ | ||
...getParsingMiddleware(accepts), | ||
...pre, | ||
handlerResolver, | ||
...post, | ||
]; | ||
if (typeof methodRegistrar !== "function") | ||
throw Error("UNSUPPORTED_METHOD_NAME"); | ||
router[method](path, ...chain); | ||
} | ||
}; | ||
for (const { routes, path } of (0, filesystem_1.mapDirectoryToRoutes)(routesFolder)) { | ||
const resolvedRoutes = yield routes; | ||
yield registerRoute(path, resolvedRoutes); | ||
yield (0, routes_1.registerRoute)(router, path, resolvedRoutes); | ||
} | ||
@@ -80,0 +31,0 @@ return new Promise((resolve) => { |
@@ -91,4 +91,14 @@ "use strict"; | ||
const directoryMap = mapDirectory(origin); | ||
return directoryMap.map((pathArray) => pathArrayToRoute(origin, pathArray)); | ||
const paths = directoryMap | ||
.map((pathArray) => pathArrayToRoute(origin, pathArray)) | ||
.sort((current, comparison) => { | ||
if (current.path === comparison.path) | ||
return 0; | ||
if (current.path > comparison.path) | ||
return -1; | ||
else | ||
return 1; | ||
}); | ||
return paths; | ||
}; | ||
exports.mapDirectoryToRoutes = mapDirectoryToRoutes; |
import Koa from "koa"; | ||
import Router from "koa-router"; | ||
import bodyParser from "koa-bodyparser"; | ||
import { route } from "@/src/create-route"; | ||
import { mapDirectoryToRoutes } from "@/util/filesystem"; | ||
import type { Middleware, Next } from "koa"; | ||
import type { ExtendedContext } from "@/@types/method"; | ||
import { registerRoute } from "@/util/routes"; | ||
import type { Middleware } from "koa"; | ||
type MethodRegistrationFunction = ( | ||
route: string, | ||
...middleware: ((context: ExtendedContext, next: Next) => Promise<void>)[] | ||
) => void; | ||
interface CreateRouterOptions { | ||
@@ -21,13 +14,2 @@ routesFolder: string; | ||
const getParsingMiddleware = (accepts: string[]) => { | ||
return accepts.length | ||
? [ | ||
bodyParser({ | ||
enableTypes: accepts, | ||
onerror: (_error, context) => context.throw(422), | ||
}), | ||
] | ||
: []; | ||
}; | ||
export const router = async ({ | ||
@@ -45,72 +27,5 @@ routesFolder, | ||
const registerRoute = (path: string, routes: ReturnType<typeof route>) => { | ||
for (const [method, controller] of Object.entries(routes)) { | ||
if (!controller) continue; | ||
const { | ||
handler, | ||
accepts = [], | ||
schema, | ||
middleware: { pre = [], post = [] } = {}, | ||
} = controller; | ||
const handlerResolver = async (context: ExtendedContext, next: Next) => { | ||
const requestValidation = await schema.request.safeParseAsync( | ||
context.request.body ?? null | ||
); | ||
if (!requestValidation.success) { | ||
context.status = 400; | ||
context.body = requestValidation.error; | ||
await next(); | ||
return; | ||
} | ||
const { | ||
status = 200, | ||
headers = {}, | ||
body: responseBody, | ||
} = await handler(context); | ||
const responseValidation = await schema.response.safeParseAsync( | ||
responseBody ?? null | ||
); | ||
if (!responseValidation.success) { | ||
context.status = 500; | ||
await next(); | ||
return; | ||
} | ||
context.status = status; | ||
context.body = responseBody; | ||
for (const [key, value] of Object.entries(headers)) | ||
context.set(key, value.toString()); | ||
await next(); | ||
}; | ||
const methodRegistrar = router[ | ||
method as keyof typeof router | ||
] as MethodRegistrationFunction; | ||
const chain = [ | ||
...getParsingMiddleware(accepts), | ||
...pre, | ||
handlerResolver, | ||
...post, | ||
] as Parameters<typeof methodRegistrar>[1][]; | ||
if (typeof methodRegistrar !== "function") | ||
throw Error("UNSUPPORTED_METHOD_NAME"); | ||
(router[method as keyof typeof router] as MethodRegistrationFunction)( | ||
path, | ||
...chain | ||
); | ||
} | ||
}; | ||
for (const { routes, path } of mapDirectoryToRoutes(routesFolder)) { | ||
const resolvedRoutes = await routes; | ||
await registerRoute(path, resolvedRoutes); | ||
await registerRoute(router, path, resolvedRoutes); | ||
} | ||
@@ -117,0 +32,0 @@ |
@@ -75,3 +75,12 @@ import { join } from "path"; | ||
const directoryMap = mapDirectory(origin); | ||
return directoryMap.map((pathArray) => pathArrayToRoute(origin, pathArray)); | ||
const paths = directoryMap | ||
.map((pathArray) => pathArrayToRoute(origin, pathArray)) | ||
.sort((current, comparison) => { | ||
if (current.path === comparison.path) return 0; | ||
if (current.path > comparison.path) return -1; | ||
else return 1; | ||
}); | ||
return paths; | ||
}; |
{ | ||
"name": "18h", | ||
"description": "A Next.js style dynamic API router for Koa-based APIs.", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "url": "https://github.com/ridafkih/18h" |
@@ -175,3 +175,3 @@ <img src="https://i.imgur.com/jWLjU1R.png"> | ||
* `"json"`, or both. */ | ||
accepts: ["json"], | ||
// accepts: ["json", "form"], | ||
/** Validation, request, and response schema | ||
@@ -178,0 +178,0 @@ * definition is done in one swoop. Uses "zod" |
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
38829
37
694