UNPKG

@tunframework/tun-rest-router

Version:
43 lines (42 loc) 1.88 kB
import { HttpMethod } from '@tunframework/tun'; import type { TunComposable, TunContext } from '@tunframework/tun'; import type { Route } from './route-utils.js'; /** * @example * ```js * // router/index.js * export const productRouter = new RestifyRouter('product') * .get('/', (ctx, next) => `Hi, world!`) * .post('/', (ctx, next) => JSON.stringify(ctx.req.body)) * .put('/{id}', (ctx, next) => `${ctx.req.slugs.id}`) * .delete('/:id', (ctx, next) => `${ctx.req.slugs.id}`) * ``` */ export interface RestifyRouterOption { prefix?: string; methods?: Array<keyof typeof HttpMethod>; } export declare class RestifyRouter { _routes: Route[]; _prefix: string; _methods?: Array<keyof typeof HttpMethod>; constructor({ prefix, methods }?: RestifyRouterOption); prefix(_prefix: string): this; routes(): TunComposable<TunContext>; addRoute(methods: keyof typeof HttpMethod | Array<keyof typeof HttpMethod>, pathname: string, handler: TunComposable<TunContext>): this; get(pathname: string, handler: TunComposable<TunContext>): this; head(pathname: string, handler: TunComposable<TunContext>): this; post(pathname: string, handler: TunComposable<TunContext>): this; put(pathname: string, handler: TunComposable<TunContext>): this; delete(pathname: string, handler: TunComposable<TunContext>): this; options(pathname: string, handler: TunComposable<TunContext>): this; patch(pathname: string, handler: TunComposable<TunContext>): this; /** * Fufill response header if all middleware resolved and `ctx.status` is empty or 404(not_found). */ allowedMethods(options?: { throw?: boolean; notImplemented?: () => Error; }): TunComposable<TunContext>; } export declare let loadRestifyRoutes: (pathname: string) => Promise<RestifyRouter>;