UNPKG

cassava

Version:
151 lines 6.39 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BuildableRoute = void 0; const Negotiator = require("negotiator"); const RouterEvent_1 = require("../RouterEvent"); const RouterResponse_1 = require("../RouterResponse"); class BuildableRoute { constructor() { this.settings = {}; } matches(evt) { if (this.settings.method && this.settings.method !== evt.httpMethod) { return false; } if (this.settings.pathRegex && !this.settings.pathRegex.test(evt.path)) { return false; } if (this.settings.serializers) { const negotiator = new Negotiator({ headers: evt.headersLowerCase }); if (!negotiator.mediaType(Object.keys(this.settings.serializers))) { return false; } } return true; } handle(evt) { return __awaiter(this, void 0, void 0, function* () { if (this.settings.handler) { const calculatedPathParameters = Object.assign({}, evt.pathParameters); // Map regex groups to pathParameters. const pathRegexExec = this.settings.pathRegex.exec(evt.path); for (let i = 1; i < pathRegexExec.length; i++) { const pathValue = decodeURIComponent(pathRegexExec[i]); if (this.settings.regexGroupToPathParamMap && this.settings.regexGroupToPathParamMap[i]) { calculatedPathParameters[this.settings.regexGroupToPathParamMap[i]] = pathValue; } calculatedPathParameters[i.toString()] = pathValue; } const pathedRouterEvent = Object.assign(new RouterEvent_1.RouterEvent(), evt); pathedRouterEvent.pathParameters = calculatedPathParameters; const resp = yield this.settings.handler(pathedRouterEvent); if (!resp) { return resp; } if (this.settings.serializers) { const negotiator = new Negotiator({ headers: evt.headersLowerCase }); const mediaType = negotiator.mediaType(Object.keys(this.settings.serializers)); resp.body = yield this.settings.serializers[mediaType](resp.body); if (!resp.headers) { resp.headers = {}; } RouterResponse_1.RouterResponse.setHeader(resp, "Content-Type", mediaType); } return resp; } return null; }); } postProcess(evt, resp, handlingRoutes) { if (this.settings.postProcessor) { return this.settings.postProcessor(evt, resp, handlingRoutes); } return null; } path(path) { if (!path) { throw new Error("path cannot be null"); } if (this.settings.pathRegex) { throw new Error("path is already defined"); } if (typeof path === "string") { // Turn path into a regex, replace {pathParam}s with regex groups // and build the map that maps the group index to the path param name. this.settings.regexGroupToPathParamMap = [""]; const sanitizedPathRegex = path .replace(/[#-.]|[[-^]|[?|{}]/g, "\\$&") .replace(/\\{[a-zA-Z][a-zA-Z0-9]*\\}/g, substr => { const pathParamName = substr.replace(/^\\{/, "").replace(/\\}/, ""); this.settings.regexGroupToPathParamMap.push(pathParamName); return "([0-9a-zA-Z._~!$&'()*+,;=:@%-]+)"; }); path = new RegExp(`^${sanitizedPathRegex}$`, "i"); } if (path instanceof RegExp) { this.settings.pathRegex = path; } else { throw new Error("unknown path type"); } return this; } method(method) { if (!method) { throw new Error("method cannot be null"); } if (this.settings.method) { throw new Error("method is already defined"); } this.settings.method = method; return this; } serializers(serializers) { if (!serializers) { throw new Error("serializers cannot be null"); } if (this.settings.serializers) { throw new Error("serializers is already defined"); } // For text mime-types without a charset specified add a default utf-8 version. const serializersWithCharsets = Object.assign({}, serializers); for (const type in serializers) { if (typeof serializers[type] === "function" && /^(text\/[^;]+|application\/json)$/.test(type)) { serializersWithCharsets[`${type}; charset=utf-8`] = serializers[type]; } } this.settings.serializers = serializersWithCharsets; return this; } handler(handler) { if (!handler) { throw new Error("handler cannot be null"); } if (this.settings.handler) { throw new Error("handler is already defined"); } this.settings.handler = handler; return this; } postProcessor(postProcessor) { if (!postProcessor) { throw new Error("postProcessor cannot be null"); } if (this.settings.postProcessor) { throw new Error("postProcessor is already defined"); } this.settings.postProcessor = postProcessor; return this; } } exports.BuildableRoute = BuildableRoute; //# sourceMappingURL=BuildableRoute.js.map