UNPKG

parse-openapi

Version:

OpenAPI v3 parser

54 lines (53 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getServices = void 0; const unique_1 = require("../utils/unique"); const getOperation_1 = require("./getOperation"); const getOperationParameters_1 = require("./getOperationParameters"); /** * Get the OpenAPI services */ const getServices = (openApi) => { const services = new Map(); for (const url in openApi.paths) { if (openApi.paths.hasOwnProperty(url)) { // Grab path and parse any global path parameters const path = openApi.paths[url]; const pathParams = (0, getOperationParameters_1.getOperationParameters)(openApi, path?.parameters || []); // Parse all the methods for this path for (const method in path) { if (path.hasOwnProperty(method)) { switch (method) { case 'get': case 'put': case 'post': case 'delete': case 'options': case 'head': case 'patch': // Each method contains an OpenAPI operation, we parse the operation const op = path[method]; const tags = op.tags?.length ? op.tags.filter(unique_1.unique) : ['Default']; tags.forEach(tag => { const operation = (0, getOperation_1.getOperation)(openApi, url, method, tag, op, pathParams); // If we have already declared a service, then we should fetch that and // append the new method to it. Otherwise we should create a new service object. const service = services.get(operation.service) || { name: operation.service, operations: [], imports: [], }; // Push the operation in the service service.operations.push(operation); service.imports.push(...operation.imports); services.set(operation.service, service); }); break; } } } } } return Array.from(services.values()); }; exports.getServices = getServices;