UNPKG

faker-api

Version:

A fully customizible rest api faking package that allows you to mock , clone and fake Rest API with fake yet realistic data

181 lines 7.91 kB
"use strict"; /* tslint:disable:variable-name*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.Router = void 0; var path_1 = require("../core/path"); var logger_1 = require("../core/logger"); var Router = /** @class */ (function () { function Router() { /*** * Map that holds various viewset objects with there key been there path prefix */ this.viewsetMap = new Map(); } Object.defineProperty(Router.prototype, "_root", { /** * Sets the prefix of the router * @param root - The path prefix of the router * This method is used by the FakerServer instance when registing a router */ set: function (root) { this.root = root; }, enumerable: false, configurable: true }); /** * To add or register a new viewset class to a router instance * @param root - the prefix of the viewset * @viewset - The class of the actual viewset, the class will be instantiated by the router object here */ Router.prototype.register = function (root, viewset) { var instance = new viewset(); var blueprint = viewset.prototype; if (!blueprint.__paths__) { blueprint.__paths__ = new Map(); blueprint.__actions__ = new Map(); } this.viewsetMap.set(root, { instance: instance, blueprint: blueprint }); }; /** * Handles all request that a directed to the router by the faker server instance * @param request - Express request object of the request * @param response - Express response object for the request * @path : The request path without the faker server path prefix * @returns true if request was handled by any viewset else returns false or undefined */ Router.prototype.on = function (request, response, path) { // first remove the router route from the the request path as actualpath // Already removed by the server when testing the router var actualPath = path.replace(/^\//, ""); var stripFirstSigmentPattern = /^([\w+|\s+]+)\//; // No need to remove it as it already removed by the server // actualPath = actualPath.replace(stripFirstSigmentPattern, ""); if (!actualPath) { return this.displayInfo(request, response); } // second extract the first segment of the actual route and var matchResult = stripFirstSigmentPattern.exec(actualPath); // Could not extract the if (!matchResult) return; if ((matchResult === null || matchResult === void 0 ? void 0 : matchResult.length) < 2) return; // Check for any viewset with that path if (this.viewsetMap.get(matchResult[1])) { var viewsetPath = actualPath.replace(stripFirstSigmentPattern, ""); var viewset = this.viewsetMap.get(matchResult[1]); var actionHandler = this._handleViewsetRequest(viewsetPath, viewset, request, response); if (actionHandler) { return true; } // Todo Call this function } }; /** * To generate a description of the router object */ Router.prototype.displayInfo = function (request, response) { if (request.method === "INFO") { // Provide description here } return false; }; /** * Calls when a viewset root matches the prefix of a request * This method will basically iterate over all the support path the viewset can handle and check if any matches the request path @path * * @param path - The request path * @viewset - The actual viewset Item whose root matches the prefix of the path * @param request - Express request object of the request * @param response - Express response object for the request * @returns true if the viewset can handle the request method or false | undefined if not */ Router.prototype._handleViewsetRequest = function (path, viewset, request, response) { var __instance = viewset.instance; // For get and post request directly to the viewset path if (path.trim() === "") { var method = request.method.toUpperCase(); if (method === "GET" && typeof __instance.get === 'function') { __instance.get(request, response); return true; } else if (method === "POST" && typeof __instance.create === "function") { __instance.create(request, response); return true; } return; } // 1 Get all the support path of the viewset var viewsetPaths = Array.from(viewset.blueprint.__paths__.keys()); // Loop through each viewpath for a match to the path var matchedPath; var matchedId; for (var _i = 0, viewsetPaths_1 = viewsetPaths; _i < viewsetPaths_1.length; _i++) { var handlerPathFormat = viewsetPaths_1[_i]; // run the path matching algorithm var result = path_1.PathUtil.isMatch(handlerPathFormat, path); // path matches exit loop after setting the matched path and id if (result) { matchedPath = handlerPathFormat; matchedId = result.id; break; } } // check if a handler was found, (set during the previous loop) if (matchedPath) { var actionHandlerKey = viewset.blueprint.__paths__.get(matchedPath); var actionHandler = viewset.blueprint.__actions__.get(actionHandlerKey); if (actionHandler) { // Check that the method is supported by the action handler if (!actionHandler.methods.includes(request.method.toUpperCase())) { if (request.method.toUpperCase() === "OPTIONS") { response.setHeader("Accept", actionHandler.methods.join(",")); // response.send() return; } // Handler does not handle this request method type skip it response.status(404); response.setHeader("Accept", actionHandler.methods.join(",")); response.send({ detail: "Method Not allowed", }); return; } // call the handler to handle the request actionHandler.handler.bind(viewset.instance)(request, response, matchedId); (0, logger_1.logRequest)(request); return true; } } else { var params = path_1.PathUtil.isMatch("/:id/", path, true); if (params) { switch (request.method.toUpperCase()) { case "POST": if (typeof __instance.update === "function") { __instance.update(request, response, params.id); return true; } break; case "DELETE": if (typeof __instance.delete === "function") { __instance.delete(request, response, params.id); return true; } break; case "GET": if (typeof __instance.retreive === "function") { __instance.retreive(request, response, params.id); return true; } break; } } } return; }; return Router; }()); exports.Router = Router; //# sourceMappingURL=index.js.map