UNPKG

@sauce-api/core

Version:

Sauce API core functionality

54 lines (53 loc) 2.1 kB
import { Request } from "express"; export type ParamDataTypes = "boolean" | "number" | "string" | "object" | "array" | "enum"; /** * Defines a route param for Sauce to pickup. * Could be a param in the path, body, or a query param * * @example * new RouteParam() .setName("startDate") .setType("string") .setDescription("ISO string of the start date of this thing") .setRequired(true) .setCustomValidator(someCustomValidator) */ export declare class RouteParam { name: string; /** Children route params are typically used for structuring JSON bodies */ children: RouteParam[]; required: Boolean; type: ParamDataTypes; enumValues: Array<string | number | boolean>; description: string; customValidator?: (requestParamValue: any, req: Request) => void; setName(name: string): RouteParam; setChildren(children: Array<RouteParam>): RouteParam; setRequired(required: Boolean): RouteParam; setType(type: ParamDataTypes): RouteParam; setEnumValues(values: Array<string | number | boolean>): RouteParam; setDescription(description: string): RouteParam; /** * Set a custom validator on this route param * @param {Function} validator * @return {RouteParam} * @example * const isIsoDateParam = (requestParamValue :any, req :Request)=>{ // This will enforce that the incoming string for the route param is indeed foramtted as an ISO string const errorRes = formatError(400, "Date string must be in ISO8601:2000 format"); if (!/d{4}-d{2}-d{2}Td{2}:d{2}:d{2}.d{3}Z/.test(requestParamValue)) { throw errorRes; }; const d = new Date(requestParamValue); const isValid = Date && !isNaN(d as any) && d.toISOString()===requestParamValue; // valid date if(!isValid) { throw errorRes; } }; * * new RouteParam() * .setCustomValidator(isIsoDateParam) */ setCustomValidator(validator: (requestParamValue: any, req: Request) => void): RouteParam; }