Skip to content

Commit

Permalink
Added named routing (1.0.18)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPlayz0 committed Jan 17, 2024
1 parent aff400e commit c21cfc3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ashmwtech/dapir",
"version": "1.0.17",
"version": "1.0.18",
"type": "module",
"description": "An api wrapper",
"source": "src/index.ts",
Expand Down
16 changes: 14 additions & 2 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ export class Server<Context extends {}, Methods extends AuthenticationMethods<Co
}

runMiddleware('preroutes');
const filteredRoutes = files.filter((x) => !x.directory).filter((x) => /^(get|put|patch|post|delete|head)\.(js|ts)$/.test(x.name));
const keywordRoutes = files.filter((x) => !x.directory).filter((x) => /^(get|put|patch|post|delete|head)\.(js|ts)$/.test(x.name));
const namedRoutes = files.filter((x) => !x.directory).filter((x) => /^(.*)\.(get|put|patch|post|delete|head)\.(js|ts)$/.test(x.name));
const filteredRoutes = [...keywordRoutes, ...namedRoutes];
log('info', `Found ${filteredRoutes.length} route files`);
let routesInit = new Set();
for (const file of filteredRoutes) {
if (file.directory) continue;

Expand All @@ -163,16 +166,25 @@ export class Server<Context extends {}, Methods extends AuthenticationMethods<Co
continue;
}

const routeName = file.name.match(/[ \w-]+?(?=\.)/)?.[0] ?? 'route';
const routeName = file.name.split('.').slice(-2)?.[0] ?? 'route';
const method = routeName as 'get' | 'put' | 'patch' | 'post' | 'delete' | 'head';
let routePath = file.path
.replace(/\\/g, '/')
.replace(/^routes/, '')
.replace(/\(([^\)]+)\)\//g, '');
if (routePath.length == 0) routePath = '/';

const namedPortion = file.name.split('.').slice(0, -2);
if (namedPortion.length > 0) routePath += '/' + namedPortion.join('.');

log('debug', `Loaded route ${method.toUpperCase()} ${routePath}`);

if (routesInit.has(method+routePath)) {
log('error', ` ↳ [Router | Aborted] Duplicate path & method combination found.`);
continue;
}
routesInit.add(method+routePath);

let routeAuth: { method: string; data: object; handle: CtxMiddlewareFunction<Context> }[] = [];
if (this.config.routes.security?.authentication?.enabled)
for (let authMethod of route.configuration?.security?.authentication ?? []) {
Expand Down

0 comments on commit c21cfc3

Please sign in to comment.