UNPKG

@mgilangjanuar/ex-router

Version:

A lightweight, automatic file-based routing, inspired by Next.js-style dynamic routes.

77 lines (76 loc) 3.38 kB
/** * ex-router * * @license MIT * (c) 2025 Pradeep Kumar * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction... */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import fs from 'fs'; import path from 'path'; function loadRoutes(app, { routeDir = '', prefixUrl = '' }) { return __awaiter(this, void 0, void 0, function* () { if (!app) { throw new Error('App is not defined'); } if (!routeDir) { throw new Error('Route directory is not defined'); } if (!fs.existsSync(routeDir)) { throw new Error(`Route directory ${routeDir} does not exist`); } yield compile(app, prefixUrl, routeDir, ''); }); } function compile(app, prefixUrl, dirPath, baseRoute) { return __awaiter(this, void 0, void 0, function* () { const files = yield fs.promises.readdir(dirPath); files.sort(); for (const file of files) { const filePath = path.join(dirPath, file); const stat = yield fs.promises.stat(filePath); if (stat.isDirectory()) { yield compile(app, prefixUrl, filePath, baseRoute + '/' + file); } else if (file.endsWith('.ts') || file.endsWith('.js')) { yield registerFileRoutes(app, prefixUrl, filePath, baseRoute, path.extname(file)); } } }); } function registerFileRoutes(app, prefixUrl, filePath, baseRoute, extension) { return __awaiter(this, void 0, void 0, function* () { const module = yield import(filePath); let pathRoute = path.basename(filePath, extension); let routePath = baseRoute + '/' + pathRoute; if (routePath.endsWith('/index') || routePath.endsWith('/api')) { routePath = baseRoute; } // here we can check if routePath include [] like - user/[id] if yes then remove [] and add user:id routePath = routePath.replace(/\[(.*?)\]/g, ':$1'); const supportedMethods = [ 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'ANY', 'HEAD', 'OPTIONS', 'PROPFIND', 'PROPPATCH', 'MKCOL', 'COPY', 'MOVE', 'LOCK', 'UNLOCK', 'TRACE', 'SEARCH', 'ALL', 'USE', 'TRACE', 'CONNECT' // fastify ]; for (const method of supportedMethods) { if (module[method] && typeof app[method.toLowerCase()] === 'function') { app[method.toLocaleLowerCase()](prefixUrl + routePath, ...Array.isArray(module[method]) ? module[method] : [module[method]]); } } }); } export { loadRoutes };