UNPKG

@nestia/fetcher

Version:

Fetcher library of Nestia SDK

44 lines 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestiaSimulator = void 0; const HttpError_1 = require("./HttpError"); 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_1.HttpError(props.method, props.host + props.path, 400, { "Content-Type": props.contentType, }, JSON.stringify({ method: exp.method, path: path !== null && path !== void 0 ? path : exp.path, expected: exp.expected, value: exp.value, message: message(exp), })); throw exp; } }; })(NestiaSimulator || (exports.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); //# sourceMappingURL=NestiaSimulator.js.map