UNPKG

openapi-modifier

Version:

This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules

48 lines (47 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseAnyEndpointDescriptor = exports.parseSimpleEndpointDescriptor = void 0; const parseSimpleEndpointDescriptor = (endpointDescriptor) => { if (typeof endpointDescriptor === 'string') { const clearEndpointDescriptor = endpointDescriptor.trim(); if (!clearEndpointDescriptor) { return null; } const lastSpaceIndex = clearEndpointDescriptor.lastIndexOf(' '); if (lastSpaceIndex === -1) { return null; } const method = clearEndpointDescriptor.slice(0, lastSpaceIndex); const clearMethod = method.replace(/[^a-zA-Z]/g, ""); const path = clearEndpointDescriptor.slice(lastSpaceIndex + 1); const clearPath = path.trim(); return { path: clearPath, method: clearMethod, }; } return null; }; exports.parseSimpleEndpointDescriptor = parseSimpleEndpointDescriptor; const parseAnyEndpointDescriptor = (endpointDescriptor, logger) => { if (typeof endpointDescriptor === 'object' && 'path' in endpointDescriptor) { return { path: endpointDescriptor.path, method: endpointDescriptor.method, }; } if (typeof endpointDescriptor === 'string') { const parsedEndpointDescriptor = (0, exports.parseSimpleEndpointDescriptor)(endpointDescriptor); if (!parsedEndpointDescriptor) { logger.errorMessage(` Failed to parse endpoint descriptor: "${endpointDescriptor}". Use format: "{method} {path}", for example: "GET /foo/bar" `); return null; } return parsedEndpointDescriptor; } logger.errorMessage(`Wrong endpoint descriptor: ${JSON.stringify(endpointDescriptor)}`); return null; }; exports.parseAnyEndpointDescriptor = parseAnyEndpointDescriptor;