UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

195 lines 7.98 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.Insomnia = void 0; const storage_1 = require("../storage"); const yaml = __importStar(require("../yaml")); const util = __importStar(require("../util")); /** * TODO: values */ class Insomnia { constructor(logger) { this.logger = logger; } 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}`); } let obj; if (contents.startsWith('{')) { obj = JSON.parse(contents); } else { obj = yaml.load(from.location.toString(), contents); } const workspaces = this.loadWorkspaces(obj, options); for (const workspace of workspaces) { this.writeRequests(workspace, opts); this.writeValues(workspace, opts); } } loadWorkspaces(obj, options) { const resources = obj.resources; if (!Array.isArray(resources)) throw new Error(`Bad format: 'resources' array not found`); const wss = resources.filter((res) => res._type === 'workspace'); if (wss.length === 0) throw new Error('Workspace not found'); const workspaces = []; for (const ws of wss) { const workspace = { id: ws._id, name: ws.name, path: options.testsLocation + '/' + util.writeablePath(ws.name), environments: [] }; this.loadRequests(workspace, resources); this.loadEnvironments(workspace, resources); workspaces.push(workspace); } return workspaces; } loadRequests(container, resources) { var _b; const children = resources.filter((res) => res.parentId === container.id); const reqGroups = children.filter((c) => c._type === 'request_group'); for (const reqGroup of reqGroups) { if (!container.requestGroups) container.requestGroups = []; const requestGroup = { id: reqGroup._id, name: reqGroup.name, path: container.path + '/' + util.writeablePath(reqGroup.name) }; container.requestGroups.push(requestGroup); this.loadRequests(requestGroup, resources); } const reqs = children.filter((c) => c._type === 'request'); for (const req of reqs) { if (!container.requests) container.requests = {}; if (req.url) { const request = { url: this.replaceExpressions(req.url), method: this.replaceExpressions(req.method) }; if (Array.isArray(req.headers) && req.headers.length > 0) { request.headers = {}; for (const hdr of req.headers) { if (hdr.value) request.headers[hdr.name] = this.replaceExpressions(hdr.value); } } if ((_b = req.body) === null || _b === void 0 ? void 0 : _b.text) { if (req.body.mimeType === 'application/graphql') { const graphql = JSON.parse(req.body.text); if (graphql.query) { request.body = this.replaceExpressions(graphql.query); } else { request.body = this.replaceExpressions(req.body.text); } } else { request.body = this.replaceExpressions(req.body.text); } } const existing = container.requests[req.name]; if (existing && existing.method !== request.method) { // qualify both with method container.requests[`${existing.method} ${req.name}`] = existing; delete container.requests[req.name]; req.name = `${req.method} ${req.name}`; } container.requests[req.name] = request; } else { this.logger.info(`Skipping request ${req.name} with empty url`); } } } writeRequests(container, options) { if (container.requests) { if (options.importToSuite) { const reqsYaml = yaml.dump(container.requests, options.indent || 2); this.writeStorage(`${container.path}.ply.yaml`, reqsYaml); } else { for (const requestName of Object.keys(container.requests)) { const reqObj = { [requestName]: container.requests[requestName] }; const reqYaml = yaml.dump(reqObj, options.indent || 2); this.writeStorage(container.path + '/' + util.writeableFileName(requestName) + '.ply', reqYaml); } } } if (container.requestGroups) { for (const requestGroup of container.requestGroups) { this.writeRequests(requestGroup, options); } } } loadEnvironments(workspace, resources) { const baseEnvs = resources.filter((res) => { return res._type === 'environment' && res.parentId === workspace.id; }); for (const baseEnv of baseEnvs) { workspace.environments.push({ name: baseEnv.name, data: baseEnv.data || {} }); const subEnvs = resources.filter((res) => { return res._type === 'environment' && res.parentId === baseEnv._id; }); for (const subEnv of subEnvs) { workspace.environments.push({ name: subEnv.name, data: subEnv.data || {} }); } } } writeValues(workspace, options) { if (workspace.environments) { for (const environment of workspace.environments) { const file = `${options.valuesLocation}/${util.writeableFileName(environment.name)}.json`; this.writeStorage(file, JSON.stringify(environment.data, null, options.indent)); } } } replaceExpressions(input) { return input.replace(/\{\{(.*?)}}/g, function (_a, b) { return '${' + b + '}'; }); } 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); } } exports.Insomnia = Insomnia; //# sourceMappingURL=insomnia.js.map