@sirhc77/postman-sdk-gen
Version:
Generate a fully-typed TypeScript SDK from a Postman collection, with support for Axios or Fetch, folder-based namespacing, and auto-inferred types.
76 lines (75 loc) • 3.71 kB
JavaScript
;
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.loadCollection = loadCollection;
exports.extractEndpoints = extractEndpoints;
const postman_collection_1 = require("postman-collection");
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const utils_1 = require("./utils");
function loadCollection(collectionPath) {
return __awaiter(this, void 0, void 0, function* () {
let json;
if (collectionPath.startsWith("http://") || collectionPath.startsWith("https://")) {
const res = yield (0, node_fetch_1.default)(collectionPath);
json = yield res.json();
}
else {
const fullPath = path_1.default.resolve(collectionPath);
const content = yield promises_1.default.readFile(fullPath, "utf-8");
json = JSON.parse(content);
}
return new postman_collection_1.Collection(json);
});
}
function extractEndpoints(collection) {
const endpoints = [];
const collectionAuth = collection.auth || { type: "noauth" };
const recurse = (items, path = []) => {
if (Array.isArray(items)) {
items.forEach(item => handleItem(item, path));
}
else {
items.items.each(item => handleItem(item, path)); // ItemGroup has an .items property, and .each method
}
};
const handleItem = (item, path) => {
var _a, _b, _c;
if (item instanceof postman_collection_1.ItemGroup) {
recurse(item, [...path, (0, utils_1.camelCase)(item.name || "unnamed")]);
}
else {
const req = item.request;
const firstResponse = (_b = (_a = item.responses) === null || _a === void 0 ? void 0 : _a.all()) === null || _b === void 0 ? void 0 : _b.filter(r => r.code === 200)[0];
const sampleBody = (firstResponse === null || firstResponse === void 0 ? void 0 : firstResponse.body) || undefined;
const effectiveAuth = req.auth || collectionAuth;
endpoints.push({
name: item.name || req.url.getPath(),
method: req.method,
url: req.url,
pathParams: req.url.variables.map((v) => v.key),
queryParams: req.url.query.map((q) => q.key),
requestBody: ((_c = req.body) === null || _c === void 0 ? void 0 : _c.raw) || undefined,
headers: Object.fromEntries(req.headers.map((h) => [h.key, h.value])),
responseSample: sampleBody,
namespacePath: path,
effectiveAuth
});
}
};
const itemsArray = collection.items.map((i) => i);
recurse(itemsArray);
return { endpoints, auth: collection.auth || { type: "noauth" } };
}