UNPKG

@chubbyts/chubbyts-framework

Version:

A minimal, highly performant middleware PSR-15 inspired function based micro framework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

146 lines (145 loc) 4.26 kB
/** * ```ts * import type { Group } from '@chubbyts/chubbyts-framework/dist/router/group'; * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const group: Group = { ..., _group: 'Group' }; * const route: Route = { ..., _route: 'Route' }; * * isRoute(group) // false * isRoute(route) // true * ``` */ export const isRoute = (route) => { return typeof route === 'object' && null !== route && '_route' in route; }; /** * ```ts * import type { Response, ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server'; * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createRoute({ * method: 'GET' * path: '/hello/:name([a-z]+)', * name: 'hello', * handler: async (serverRequest: ServerRequest): Promise<Response> => { * return new Response(`Hello, ${serverRequest.attributes.get('name')}`, { * status: 200, * statusText: 'OK', * headers: { 'content-type': 'text/plain' }, * }); * }, * }); * ``` */ export const createRoute = ({ method, path, name, handler, middlewares, pathOptions }) => { return { method, path, name, handler, middlewares: middlewares ?? [], pathOptions: pathOptions ?? {}, attributes: {}, _route: 'Route', }; }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createDeleteRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createDeleteRoute({ * path: '/api/users/:id', * handler: userDeleteHandler, * }); * ``` */ export const createDeleteRoute = (args) => { return createRoute({ ...args, method: 'DELETE' }); }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createGetRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createGetRoute({ * path: '/api/users/:id', * handler: userReadHandler, * }); * ``` */ export const createGetRoute = (args) => { return createRoute({ ...args, method: 'GET' }); }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createHeadRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createHeadRoute({ * path: '/api/users/:id', * handler: userExistsHandler, * }); * ``` */ export const createHeadRoute = (args) => { return createRoute({ ...args, method: 'HEAD' }); }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createOptionsRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createOptionsRoute({ * path: '/api/users/:id', * handler: corsHandler, * }); * ``` */ export const createOptionsRoute = (args) => { return createRoute({ ...args, method: 'OPTIONS' }); }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createPatchRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createPatchRoute({ * path: '/api/users/:id', * handler: userUpdateHandler, * }); * ``` */ export const createPatchRoute = (args) => { return createRoute({ ...args, method: 'PATCH' }); }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createPostRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createPostRoute({ * path: '/api/users', * handler: userCreateHandler, * }); * ``` */ export const createPostRoute = (args) => { return createRoute({ ...args, method: 'POST' }); }; /** * ```ts * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createPutRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const route: Route = createPutRoute({ * path: '/api/users/:id', * handler: userReplaceHandler, * }); * ``` */ export const createPutRoute = (args) => { return createRoute({ ...args, method: 'PUT' }); };