This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
viscord/packages/server/src/lib/router.ts

26 lines
656 B
TypeScript
Raw Normal View History

2022-07-21 15:22:00 -04:00
2022-07-21 23:45:52 -04:00
export default function router(routes: any) {
2022-07-20 16:04:09 -04:00
for(const routeName in routes) {
const route = routes[routeName];
2022-07-21 15:22:00 -04:00
if('routes' in route) {
for(const suffix of route.routes) {
2022-07-20 16:04:09 -04:00
const combinedRouteName = routeName + ':' + suffix;
2022-07-21 15:22:00 -04:00
routes[combinedRouteName] = (_: never, ...args: any[]) => route(suffix, args);
2022-07-20 16:04:09 -04:00
}
delete routes[routeName];
}
}
2022-07-21 15:22:00 -04:00
const sendFn = function(route: any, data: any) {
2022-07-20 16:04:09 -04:00
if(route in routes) {
2022-07-21 04:18:39 -04:00
return routes[route](data);
2022-07-20 16:04:09 -04:00
} else {
console.warn(`route <${route}> not found`);
2022-07-21 15:22:00 -04:00
console.trace();
2022-07-20 16:04:09 -04:00
}
};
2022-07-21 15:22:00 -04:00
sendFn.routes = Object.keys(routes);
return sendFn;
2022-07-20 16:04:09 -04:00
}