UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

156 lines 6.15 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Postman = void 0; const storage_1 = require("../storage"); const yaml = __importStar(require("../yaml")); const util = __importStar(require("../util")); class Postman { constructor(logger) { this.logger = logger; this.storagePathToRequestsObj = new Map(); } async import(from, options) { const opts = { indent: 2, importToSuite: false, ...options }; const contents = await from.read(); if (!contents) { throw new Error(`Import source not found: ${from.location}`); } const obj = JSON.parse(contents); this.storagePathToRequestsObj.clear(); if (obj.values) { // values const name = this.baseName(from.location); const values = {}; for (const value of obj.values) { if (value.enabled) { values[value.key] = value.value; } } this.writeStorage(`${opts.valuesLocation}/${name}.json`, JSON.stringify(values, null, opts.indent)); } else if (obj.item) { // requests const name = this.baseName(from.location); this.processItem(`${opts.testsLocation}/${name}`, obj.item, options.importToSuite || false); for (const [path, requestsObj] of this.storagePathToRequestsObj) { if (opts.importToSuite) { this.writeStorage(path, yaml.dump(requestsObj, opts.indent || 2)); } else { for (const name of Object.keys(requestsObj)) { const reqObj = { [name]: requestsObj[name] }; const reqPath = path + '/' + util.writeableFileName(name) + '.ply'; this.writeStorage(reqPath, yaml.dump(reqObj, opts.indent || 2)); } } } } } baseName(location) { let name = location.base; const dot = name.indexOf('.'); if (dot > 0) { name = name.substring(0, dot); } return name; } writeStorage(path, content) { const storage = new storage_1.Storage(path); if (storage.exists) { this.logger.info(`Overwriting: ${storage}`); } else { this.logger.info(`Creating: ${storage}`); } storage.write(content); } processItem(path, item, importToSuite) { for (const it of item) { if (it.request) { // write the request const name = it.name; try { const request = this.translateRequest(it.request); let storagePath = path; if (importToSuite) storagePath += '.ply.yaml'; const reqsObj = this.storagePathToRequestsObj.get(storagePath); if (reqsObj) { reqsObj[name] = request; } else { this.storagePathToRequestsObj.set(storagePath, { [name]: request }); } } catch (err) { this.logger.error(`Request ${path}/${it.name} not imported due to: ${err}`); this.logger.debug(`${err}`, err); } } if (it.item) { // windows doesn't support : in file names this.processItem(`${path}/${util.writeablePath(it.name)}`, it.item, importToSuite); } } } translateRequest(postmanRequest) { const request = { url: this.replaceExpressions(postmanRequest.url.raw), method: this.replaceExpressions(postmanRequest.method) }; if (postmanRequest.header && postmanRequest.header.length > 0) { request.headers = {}; for (const head of postmanRequest.header) { request.headers[head.key] = this.replaceExpressions(head.value); } } if (postmanRequest.body) { if (typeof postmanRequest.body === 'string') { request.body = this.replaceExpressions(postmanRequest.body); } else { const mode = postmanRequest.body.mode; if (mode === 'graphql') { request.body = this.replaceExpressions(postmanRequest.body.graphql.query); } else if (mode === 'raw') { request.body = this.replaceExpressions(postmanRequest.body.raw); } else { throw new Error(`Unsupported request body mode: ${postmanRequest.body.mode}`); } } } return request; } replaceExpressions(input) { return input.replace(/\{\{(.*?)}}/g, function (_a, b) { return '${' + b + '}'; }); } } exports.Postman = Postman; //# sourceMappingURL=postman.js.map