UNPKG

webserv

Version:

a quick, flexible, fully typed development server

42 lines (41 loc) 1.41 kB
import { Service } from '../app'; import { RouteProperties } from '../interface'; export interface CrudServiceProperties extends RouteProperties { data?: Record[]; operations?: Operation[]; dataLoader?: DataLoader; } export interface Record { id: string; [key: string]: any; } export interface DataLoader { (id: string): Promise<Record> | Record | undefined; (): Promise<Record[]> | Record[]; } export declare type Operation = 'list' | 'create' | 'read' | 'update' | 'delete'; export declare function isRecord(value: any): value is Record; /** * The CRUD service provides basic in-memory Create Read Update Delete and List support on a record * centered around the provided path. * * - supported operations may be defined in properties * - a set of records may be provided to initialize the store * - a loader function can be used to load in records from another source (e.g. disk) * * List: GET {path}/ * Returns a list of all records * * Create: POST {path}/ * Create a new record to be stored in-memory * * Read: GET {path}/{id} * Returns a record if found; or returns a 404 status * * Update: PUT {path}/ * Updates a record if found; or returns a 404 status * * Delete: DELETE {path}/{id} * Deletes a record from the in-memory store */ export declare function crudService(props: CrudServiceProperties): Service;