UNPKG

@chubbyts/chubbyts-framework

Version:

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

151 lines (150 loc) 4.45 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 { ResponseFactory } from '@chubbyts/chubbyts-http-types/dist/message-factory'; * import type { Method, Response, ServerRequest } from '@chubbyts/chubbyts-http-types/dist/message'; * import type { Route } from '@chubbyts/chubbyts-framework/dist/router/route'; * import { createRoute } from '@chubbyts/chubbyts-framework/dist/router/route'; * * const responseFactory: ResponseFactory = ...; * * const route: Route = createRoute({ * method: 'GET' * path: '/hello/:name([a-z]+)', * name: 'hello', * handler: async (request: ServerRequest): Promise<Response> => { * const response = responseFactory(200); * response.body.end(`Hello, ${request.attributes.name}`); * * return { * ...response, * headers: { ...response.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' }); };