UNPKG

@nestia/fetcher

Version:

Fetcher library of Nestia SDK

45 lines (42 loc) 1.73 kB
import { HttpError } from '@typia/utils'; var NestiaSimulator; (function (NestiaSimulator) { NestiaSimulator.assert = (props) => { return { param: param(props), query: query(props), body: body(props), }; }; const param = (props) => (name) => (task) => { validate((exp) => `URL parameter "${name}" is not ${exp.expected} type.`)(props)(task); }; const query = (props) => (task) => validate(() => "Request query parameters are not following the promised type.")(props)(task); const body = (props) => (task) => validate(() => "Request body is not following the promised type.")(props)(task); const validate = (message, path) => (props) => (task) => { try { task(); } catch (exp) { if (isTypeGuardError(exp)) throw new HttpError(props.method, props.host + props.path, 400, { "Content-Type": props.contentType, }, JSON.stringify({ method: exp.method, path: exp.path, expected: exp.expected, value: exp.value, message: message(exp), })); throw exp; } }; })(NestiaSimulator || (NestiaSimulator = {})); const isTypeGuardError = (input) => "string" === typeof input.method && (undefined === input.path || "string" === typeof input.path) && "string" === typeof input.expected && "string" === typeof input.name && "string" === typeof input.message && (undefined === input.stack || "string" === typeof input.stack); export { NestiaSimulator }; //# sourceMappingURL=NestiaSimulator.mjs.map