UNPKG

cassava

Version:
81 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RouterEvent = void 0; const jsonschema = require("jsonschema"); const RestError_1 = require("./RestError"); /** * Input to the HTTP router. Based on the ProxyEvent but enriched. */ class RouterEvent { requireQueryStringParameter(param, valuesOrValidator, explanation) { if (!this.queryStringParameters[param]) { throw new RestError_1.RestError(400, explanation || `Required query parameter '${param}' is not set.`); } if (valuesOrValidator && Array.isArray(valuesOrValidator) && valuesOrValidator.indexOf(this.queryStringParameters[param]) === -1) { throw new RestError_1.RestError(400, explanation || `Required query parameter '${param}=${this.queryStringParameters[param]}' must be one of: ${valuesOrValidator.join(", ")}.`); } if (valuesOrValidator && typeof valuesOrValidator === "function" && !valuesOrValidator(this.queryStringParameters[param])) { throw new RestError_1.RestError(400, explanation || `Required query parameter '${param}=${this.queryStringParameters[param]}' is not a legal value.`); } } /** * Require that the given query parameters are *not* set. * If any parameter in this list is set a RestError is thrown. */ blacklistQueryStringParameters(...params) { blacklistKeys(this.queryStringParameters || {}, params, "query parameter"); } /** * Require that only the given query parameters are set. No parameter * in this list has to be set, but if any parameter is set that is not in * this list a RestError will be thrown. */ whitelistQueryStringParameters(...params) { whitelistKeys(this.queryStringParameters || {}, params, "query parameter"); } requireHeader(field, valuesOrValidator, explanation) { const fieldLowerCase = field.toLowerCase(); if (!this.headersLowerCase[fieldLowerCase]) { throw new RestError_1.RestError(400, explanation || `Required header '${field}' is not set.`); } if (valuesOrValidator && Array.isArray(valuesOrValidator) && valuesOrValidator.indexOf(this.headersLowerCase[fieldLowerCase]) === -1) { throw new RestError_1.RestError(400, explanation || `Required header '${field}=${this.headersLowerCase[fieldLowerCase]}' must be one of: ${valuesOrValidator.join(", ")}.`); } if (valuesOrValidator && typeof valuesOrValidator === "function" && !valuesOrValidator(this.headersLowerCase[fieldLowerCase])) { throw new RestError_1.RestError(400, explanation || `Required header '${field}=${this.headersLowerCase[fieldLowerCase]}' is not a legal value.`); } } /** * Validate the body of the request using JSON Schema. * * JSON Schema is a concise way to define a valid JSON object. * The spec and examples can be found at http://json-schema.org/ * with additional help at * https://spacetelescope.github.io/understanding-json-schema/index.html . * * The actual implementation comes from https://github.com/tdegrunt/jsonschema . */ validateBody(schema, options) { var _a; const result = jsonschema.validate((_a = this.body) !== null && _a !== void 0 ? _a : null, schema, Object.assign({ propertyName: "requestBody" }, options)); if (result.errors.length) { throw new RestError_1.RestError(options && typeof options.httpStatusCode === "number" ? options.httpStatusCode : 422, `The ${this.httpMethod} body has ${result.errors.length} validation error(s): ${result.errors.map(e => e.toString()).join(", ")}.`); } } } exports.RouterEvent = RouterEvent; function blacklistKeys(o, keys, part = "member") { for (const key of Object.keys(o)) { if (keys.indexOf(key) !== -1) { throw new RestError_1.RestError(400, `Unexpected ${part} '${key}'.`); } } } function whitelistKeys(o, keys, part = "member") { for (const key of Object.keys(o)) { if (keys.indexOf(key) === -1) { throw new RestError_1.RestError(400, `Unexpected ${part} '${key}'.`); } } } //# sourceMappingURL=RouterEvent.js.map