UNPKG

@apica-io/url-xi

Version:

URL Check for integrations and API monitoring

365 lines (364 loc) 15.2 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PMConverter = void 0; const testbase_1 = require("../lib/testbase"); const fs_1 = __importDefault(require("fs")); const util_1 = __importDefault(require("util")); const path_1 = __importDefault(require("path")); class PMConverter extends testbase_1.TestBase { constructor(debug) { super(debug, 'PMConverter'); this.pmCollection = {}; this.envVariables = []; } save(testConfig, outDir) { return __awaiter(this, void 0, void 0, function* () { const fileName = testConfig.name.replace(/\s/g, '_'); const testFile = path_1.default.resolve(outDir, fileName + '.json'); this._logger.info('Saving converted script in file=%s', testFile); const writeFile = util_1.default.promisify(fs_1.default.writeFile); const test = JSON.stringify(testConfig, null, 2); yield writeFile(testFile, test); return test; }); } initFromFile(pathName, envPath) { return __awaiter(this, void 0, void 0, function* () { try { let envContent = ''; const readFile = util_1.default.promisify(fs_1.default.readFile); const content = (yield readFile(pathName)).toString(); if (envPath) { envContent = (yield readFile(envPath)).toString(); } return this.setup(content, envContent); } catch (error) { this._logger.error(error); return false; } }); } setup(content, envContent) { this.pmCollection = JSON.parse(content); this.envVariables = []; if (envContent) { const envVars = JSON.parse(envContent); this.envVariables = envVars.values; } this._logger.info('PM collection [%s] initialized. Env variables=%d', this.pmCollection.info.name, this.envVariables.length); return true; } toIVariable(variable) { const toVariable = {}; toVariable.usage = ''; toVariable.key = variable.key ? variable.key : variable.name ? variable.name : ''; toVariable.value = variable.value || undefined; switch (variable.type) { case 'number': toVariable.type = 'number'; break; default: toVariable.type = 'string'; } return toVariable; } convertScriptLine(line) { line = line.replace(/pm.collectionVariables.set/gm, 'uxs.setVar'); line = line.replace(/pm.collectionVariables.get/gm, 'uxs.getVar'); line = line.replace(/pm.expect/gm, 'uxs.expect'); line = line.replace(/postman.setEnvironmentVariable/gm, 'uxs.setVar'); line = line.replace(/postman.getEnvironmentVariable/gm, 'uxs.getVar'); return line; } convertScripts(events, step, request) { if (request) { if (!request.scripts) request.scripts = []; } if (step) { if (!step.scripts) step.scripts = []; } const scriptName = step.name; let scriptNumber = 0; events.forEach((event) => { var _a, _b; if (!event.disabled) { const convertedScript = {}; const script = event.script; scriptNumber++; convertedScript.name = `${scriptName}_${scriptNumber}`; let convertedCode; if (Array.isArray(script.exec)) { const t = this.convertScriptLine(script.exec.join('\n')); convertedCode = t.split('\n'); } else { convertedCode = this.convertScriptLine(script === null || script === void 0 ? void 0 : script.exec); } convertedScript.script = convertedCode; switch (event.listen) { case 'prerequest': convertedScript.scope = request ? 'before' : 'beforeEach'; break; case 'test': convertedScript.scope = request ? 'after' : 'afterEach'; break; } if (request) { (_a = request.scripts) === null || _a === void 0 ? void 0 : _a.push(convertedScript); } else { (_b = step.scripts) === null || _b === void 0 ? void 0 : _b.push(convertedScript); } } }); } convertAuth(auth, config) { const authType = auth.type.toString(); switch (authType) { case 'apikey': const keys = auth.apikey || []; let key = ''; let value = ''; let _in = ''; keys.forEach((apiKey) => { switch (apiKey.key) { case 'key': key = apiKey.value; break; case 'value': value = apiKey.value; break; case 'in': _in = apiKey.value; break; } }); if (_in === 'query') { if (!config.params) config.params = {}; config.params[key] = value; } else { if (!config.headers) config.headers = {}; config.headers[key] = value; } break; case 'basic': const basic_keys = auth.basic || []; let username = ''; let password = ''; basic_keys.forEach((key) => { switch (key.key) { case 'username': username = key.value; break; case 'password': password = key.value; break; } }); config.auth = { username: username, password: password }; break; } return config; } convertRequest(step, item, config) { var _a, _b, _c, _d; const req = item.request; const request = {}; request.config = Object.assign({}, config); if (typeof req !== 'string') { const ro = req; if (ro.auth) request.config = this.convertAuth(ro.auth, request.config); switch (ro.method) { case 'POST': request.config.method = 'post'; break; case 'DELETE': request.config.method = 'delete'; break; case 'PATCH': request.config.method = 'patch'; break; case 'PUT': request.config.method = 'put'; break; case 'OPTIONS': request.config.method = 'options'; break; case 'LINK': request.config.method = 'link'; break; case 'UNLINK': request.config.method = 'unlink'; break; case 'HEAD': request.config.method = 'head'; break; default: request.config.method = 'get'; } if (typeof (ro === null || ro === void 0 ? void 0 : ro.url) === 'string') { request.config.url = ro.url; } else { const url = ro.url; request.config.url = url.raw; const pos = ((_a = url.raw) === null || _a === void 0 ? void 0 : _a.indexOf('?')) || 0; if (pos > 0) request.config.url = (_b = url === null || url === void 0 ? void 0 : url.raw) === null || _b === void 0 ? void 0 : _b.substr(0, pos); if (url.query) { url.query.forEach((param) => { if (!param.disabled) { if (!request.config.params) request.config.params = {}; const key = param.key; request.config.params[key] = param.value; } }); } } if (ro.header) { if (typeof (ro === null || ro === void 0 ? void 0 : ro.header) !== 'string') { const headers = ro.header; headers.forEach((header) => { if (!header.disabled) { if (!request.config.headers) request.config.headers = {}; const key = header.key; request.config.headers[key] = header.value; } }); } if (ro.body) { const body = ro.body; const bodyAny = ro.body; const lang = ((_d = (_c = bodyAny === null || bodyAny === void 0 ? void 0 : bodyAny.options) === null || _c === void 0 ? void 0 : _c.raw) === null || _d === void 0 ? void 0 : _d.language) || ''; let contentType = ''; if (request.config.headers) contentType = request.config.headers['Content-Type'] || ''; switch (body.mode) { case 'raw': if (lang === 'json' || contentType.includes('xxjson')) { /* let strJson:string=body.raw || '' strJson=strJson.replace('\n',' ') strJson=strJson.replace('\t',' ') */ const data = JSON.parse(body.raw); request.config.data = data; } else { request.config.data = body.raw; } break; case 'urlencoded': const urlencoded = body.urlencoded; urlencoded.forEach((element) => { if (!element.disabled) { if (!request.config.data) request.config.data = {}; const key = element.key; request.config.data[key] = element.value; } }); break; } } } } if (item.event) { this.convertScripts(item.event, step, request); } this._logger.debug(`converting request ${item.name} : ${request.config.url} ${request.config.method}`); return request; } convertStep(item, params) { let config = {}; if (params) config.params = params; const step = {}; if (item.auth) config = this.convertAuth(item.auth, config); step.name = item.name != undefined ? item.name : ''; step.requests = []; if (item.request) { step.requests.push(this.convertRequest(step, item, config)); } else if (item.item) { item.item.forEach((elem) => { if (elem.request) { const request = this.convertRequest(step, elem, config); step.requests.push(request); } }); } /* if (item.event) { this.convertScripts(item.event, step) } */ return step; } convert() { var _a, _b, _c; const varMap = new Map(); const testConfig = {}; testConfig.name = this.pmCollection.info.name; testConfig.baseURL = ''; if (this.pmCollection.info.description) { const description = ((_a = this.pmCollection.info) === null || _a === void 0 ? void 0 : _a.description) || ''; testConfig.description = description; } (_b = this.pmCollection.variable) === null || _b === void 0 ? void 0 : _b.forEach((v) => { this._logger.debug('Reading variable [%s] value=%s from collection', v.key || v.name, v.value || ''); if (!v.disabled) { const variable = this.toIVariable(v); varMap.set(variable.key, variable); } }); this.envVariables.forEach((v) => { this._logger.debug('Reading variable [%s] value=%s from environment', v.key || v.name, v.value || ''); if (!v.disabled) { const variable = this.toIVariable(v); varMap.set(variable.key, variable); } }); testConfig.variables = []; for (const variable of varMap.values()) { testConfig.variables.push(variable); } let config = {}; if (this.pmCollection.auth) { config = this.convertAuth(this.pmCollection.auth, config); if (!config.params) testConfig.config = config; } testConfig.steps = []; (_c = this.pmCollection.item) === null || _c === void 0 ? void 0 : _c.forEach((item) => { this._logger.debug('Reading Item [%s]', item.name, item.description); const step = this.convertStep(item, config.params); testConfig.steps.push(step); }); return testConfig; } } exports.PMConverter = PMConverter; //# sourceMappingURL=pmConverter.js.map