UNPKG

inversify-postman-collection

Version:
74 lines 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPostmanCollection = void 0; const postman_collection_1 = require("postman-collection"); const helpers_1 = require("../helpers"); function getPostmanCollection({ routesDefs, title, baseUrl, headerEnvVariables, routeDefData, }) { var _c; const myCollection = new postman_collection_1.Collection({ name: title }); myCollection.describe(`${title}`); const header = (headerEnvVariables === null || headerEnvVariables === void 0 ? void 0 : headerEnvVariables.length) ? headerEnvVariables.map(([key, value]) => ({ key, value })) : undefined; routesDefs.forEach(({ endpoints, controller }) => { const groupName = controller.split("Controller")[0].replace(/([a-z](?=[A-Z]))/g, "$1 "); const groupItem = new postman_collection_1.ItemGroup({ name: groupName, }); endpoints.forEach(({ route, args }) => { var _c; let [method, urlPath] = route.split(" ").map((x) => x.trim()); if (urlPath.endsWith("/")) { urlPath = urlPath.slice(0, -1); } let url = [baseUrl, urlPath].filter((x) => x).join(""); const { queryParams, routeName, routeRawDefName } = (0, helpers_1.getValidQueryParams)(args); if (queryParams === null || queryParams === void 0 ? void 0 : queryParams.length) { const newUrl = new postman_collection_1.Url(url); queryParams.forEach((key) => { newUrl.addQueryParams(key); }); url = newUrl.toString(); } const { requestBody, responseBody, routeDescribe } = (0, helpers_1.getRouteDefData)({ groupName, method, routeRawDefName, routeDefData, controller, }); const requestDef = { url, method, header, }; if (requestBody !== undefined) { requestDef.body = { mode: "raw", raw: JSON.stringify(requestBody, null, 2), }; } if (responseBody !== undefined) { } groupItem.items.add(new postman_collection_1.Item({ name: (_c = routeDescribe !== null && routeDescribe !== void 0 ? routeDescribe : routeName) !== null && _c !== void 0 ? _c : url, request: requestDef, response: undefined, })); }); myCollection.items.add(groupItem); }); const result = myCollection.toJSON(); result.item = (_c = result === null || result === void 0 ? void 0 : result.item) === null || _c === void 0 ? void 0 : _c.sort((a, b) => { const _a = (a.name || "").toLowerCase(); const _b = (b.name || "").toLowerCase(); if (_a > _b) { return 1; } if (_a < _b) { return -1; } return 0; }); return result; } exports.getPostmanCollection = getPostmanCollection; //# sourceMappingURL=postman-col.js.map