UNPKG

server-mockup-api

Version:

To get full fake REST API with no efforts. Add/Modify API endpoint and response through JSON file.

91 lines (90 loc) 3.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-explicit-any */ const json_server_1 = __importDefault(require("json-server")); const utils_1 = __importDefault(require("./utils")); const middleware_1 = require("./middleware"); const https_1 = __importDefault(require("https")); const fs_1 = __importDefault(require("fs")); const cors_1 = __importDefault(require("cors")); // import { middlewareA } from './../../sample-middleware'; class ServerMockApi { constructor(port, dbPath, routePath, delayInResponse, enableHttps, certFilePath, keyFilePath, customMiddlewares) { this.port = port; this.dbPath = dbPath; this.routePath = routePath; this.delayInResponse = delayInResponse; this.enableHttps = enableHttps; this.certFilePath = certFilePath; this.keyFilePath = keyFilePath; this.customMiddlewares = customMiddlewares; this.expressApp = json_server_1.default.create(); const mergedDBJson = new utils_1.default().readAllFiles(this.dbPath, JSON.parse('{}')); const mergedRoutesJson = new utils_1.default().readAllFiles(this.routePath, JSON.parse('{}')); const router = json_server_1.default.router(mergedDBJson); this.expressApp.use((0, cors_1.default)({ origin: '*' })); this.expressApp.use(json_server_1.default.rewriter(mergedRoutesJson)); this.expressApp.use((req, res, next) => { setTimeout(next, this.delayInResponse); }); this.expressApp.use(middleware_1.middleware); if (customMiddlewares) { const arPr = []; for (const index in customMiddlewares) { arPr[index] = Promise.resolve().then(() => __importStar(require(customMiddlewares[index]))); } Promise.all(arPr).then((values) => { values.map(item => { for (const i in item) { this.expressApp.use(item[i]); } }); this.includeAll(router); }); } else { this.includeAll(router); } } includeAll(router) { this.expressApp.use(router); if (this.enableHttps && this.keyFilePath && this.certFilePath) { https_1.default.createServer({ key: fs_1.default.readFileSync(this.keyFilePath), cert: fs_1.default.readFileSync(this.certFilePath) }, this.expressApp).listen(this.port, () => { console.info(`Started ServerMockApi at https://localhost:${this.port}/`); }); } else { this.expressApp.listen(this.port, () => { console.info(`Started ServerMockApi at http://localhost:${this.port}/`); }); } } } exports.default = ServerMockApi;