UNPKG

@micro.ts/core

Version:

Microservice framework with Typescript

78 lines (77 loc) 3.09 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.HttpBroker = void 0; const AbstractBroker_1 = require("../AbstractBroker"); class HttpBroker extends AbstractBroker_1.AbstractBroker { constructor() { super(...arguments); /** * Maps from route definition to broker-specific route * @param def Route definition */ this.routeMapper = (def) => { let basePart = def.base; if (basePart && basePart.indexOf("/") !== 0) { basePart = `/${basePart}`; } let controllerPart = def.controller; if (controllerPart.indexOf("/") !== 0) { controllerPart = `/${controllerPart}`; } let handlerPart = def.handler; if (handlerPart.indexOf("/") !== 0 && handlerPart.length > 0) { handlerPart = `/${handlerPart}`; } let completePath = `${basePart}/${controllerPart}/${handlerPart}`; const params = this.extractParamNames(completePath); completePath = params.map(x => { if (x.param) { return this.paramWrapper(x.name); } return x.name; }).join('/'); let finalPath = `${completePath}`.replace(/\/{2,}/g, "/"); if (finalPath.endsWith("/")) { finalPath = finalPath.slice(0, finalPath.length - 1); } return finalPath; }; } /** * Broker specific server */ getConnection() { return this.server; } registerSingleRoute(value, route) { if (value.length > 0) { const methods = {}; value.forEach(pair => { if (!methods[pair.def.method]) { methods[pair.def.method] = pair; } }); Object.keys(methods).forEach((method) => { this.registerHandler(value, route, method); }); } } getFullPath() { return `http://${this.config.address || 'localhost'}:${this.config.port || 80}`; } registerRoutes() { this.registeredRoutes.forEach((value, route) => __awaiter(this, void 0, void 0, function* () { this.registerSingleRoute(value, route); })); } } exports.HttpBroker = HttpBroker;