universal-autorouter
Version:
An universal plugin that scans the file system and automatically loads to a server all routes in a target directory.
59 lines (54 loc) • 1.79 kB
text/typescript
import { ViteDevServer } from 'vite';
declare const toPosix: (filepath: string) => string;
declare const filepathToRoute: (filepath: string) => string;
declare const DEFAULT_ROUTES_DIR = "./api";
declare const DEFAULT_METHOD = "get";
type Method = 'get' | 'post' | 'put' | 'delete' | 'options' | 'patch' | 'all';
type App<T> = Record<Method | string, ((route: string, handler: (req: unknown, res: unknown) => void) => void) | any> & T;
type AutoloadRoutesOptions = {
/**
* Pattern to search files of routes
* @example pattern only .ts files
* ```ts
* pattern: '**\/*.ts'
* ```
* @default '**\/*.{ts,tsx,mjs,js,jsx,cjs}'
*/
pattern?: string;
/**
* Prefix to add to routes
* @example prefix for APIs
* ```ts
* prefix: '/api'
* ```
* @default ''
*/
prefix?: string;
/**
* Directory to search routes
* @default '/routes'
*/
routesDir?: string;
/**
* Default method to use when the route filename doesn't use the (<METHOD>) pattern
* @default 'get'
*/
defaultMethod?: Method | string;
/**
* Vite dev server instance
* @default undefined
*/
viteDevServer?: ViteDevServer;
/**
* Skip the throw error when no routes are found
* @default false
*/
skipNoRoutes?: boolean;
/**
* Skip the import errors with the `default export` of a rotue file
* @default false
*/
skipImportErrors?: boolean;
};
declare const _default: <T>(app: App<T>, { pattern, prefix, routesDir, defaultMethod, viteDevServer, skipNoRoutes, skipImportErrors }: AutoloadRoutesOptions) => Promise<App<T>>;
export { type AutoloadRoutesOptions, DEFAULT_METHOD, DEFAULT_ROUTES_DIR, _default as default, filepathToRoute, toPosix };