UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

131 lines (130 loc) 5.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockserverService = void 0; const tslib_1 = require("tslib"); const https = tslib_1.__importStar(require("https")); const http = tslib_1.__importStar(require("http")); const file_system_utils_1 = require("../utils/file-system-utils"); const url_utils_1 = require("../utils/url-utils"); const string_utils_1 = require("../utils/string-utils"); class MockserverService { constructor() { } async deployOpenapi(mockserverUrl, apiKey, openApiPathUrl) { return new Promise((resolve, reject) => { let specUrlOrPayload = openApiPathUrl; if (!url_utils_1.UrlUtils.validate(openApiPathUrl)) { specUrlOrPayload = file_system_utils_1.FileSystemUtils.loadFile(openApiPathUrl); } const url = new URL(`${mockserverUrl}/mockserver/openapi`); const requestBody = JSON.stringify({ specUrlOrPayload: specUrlOrPayload }); const options = { hostname: url.hostname, port: url.port || 443, path: `${url.pathname}${apiKey ? `?apiKey=${apiKey}` : ""}`, method: "PUT", headers: { Accept: "application/json", "User-Agent": string_utils_1.StringUtils.getUserAgent(), "Content-Type": "application/json", "Content-Length": Buffer.byteLength(requestBody), }, }; const protocol = url.protocol === "https:" ? https : http; const req = protocol.request(options, res => { res.setEncoding("utf8"); let body = ""; res.on("data", d => { body += d; }); res.on("end", () => { if (res.statusCode === 201) resolve("SUCCESS"); else resolve("ERROR"); reject(new Error(`Error: ${res.statusCode}`)); }); }); req.on("error", error => { reject(error); }); req.write(requestBody); req.end(); }); } async removePath(mockserverUrl, path) { return new Promise((resolve, reject) => { const url = new URL(`${mockserverUrl}/mockserver/clear`); const requestBody = JSON.stringify({ path: path }); console.log(`requestBody: ${requestBody}`); const options = { hostname: url.hostname, port: url.port || 443, path: `${url.pathname}${url.search ? "?" + url.searchParams.toString() : ""}`, method: "PUT", headers: { Accept: "application/json", "User-Agent": string_utils_1.StringUtils.getUserAgent(), "Content-Type": "application/json", "Content-Length": Buffer.byteLength(requestBody), }, }; const protocol = url.protocol === "https:" ? https : http; const req = protocol.request(options, res => { res.setEncoding("utf8"); let body = ""; res.on("data", d => { body += d; }); res.on("end", () => { console.log(`code: ${res.statusCode} | body: ${body}`); if (res.statusCode === 200) resolve("SUCCESS"); else resolve("ERROR"); reject(new Error(`Error: ${res.statusCode}`)); }); }); req.on("error", error => { reject(error); }); req.write(requestBody); req.end(); }); } async isMockserverUp(mockserverUrl) { return new Promise((resolve, reject) => { const url = new URL(mockserverUrl); const options = { hostname: url.hostname, port: url.port || 443, path: url.pathname, method: "GET", headers: { Accept: "application/json", "User-Agent": string_utils_1.StringUtils.getUserAgent(), }, }; const protocol = url.protocol === "https:" ? https : http; const req = protocol.request(options, res => { res.setEncoding("utf8"); let body = ""; res.on("data", d => { body += d; }); res.on("end", () => { resolve((res.statusCode || 0) >= 200 && (res.statusCode || 0) <= 299); }); }); req.on("error", error => { resolve(false); }); req.end(); }); } static get instance() { if (!this._instance) this._instance = new this(); return this._instance; } } exports.MockserverService = MockserverService;