@mintlify/common
Version:
Commonly shared code within Mintlify
60 lines (59 loc) • 2.08 kB
JavaScript
import { findOpenApiPath } from './findOpenApiPath.js';
import { potentiallyParseOpenApiString } from './parseOpenApiString.js';
export const getOpenApiOperationMethodAndEndpoint = (endpointStr, openApiFiles) => {
const potentiallyParsedOpenApiString = potentiallyParseOpenApiString(endpointStr);
if (potentiallyParsedOpenApiString == undefined) {
return undefined;
}
const { endpoint, method, filename } = potentiallyParsedOpenApiString;
let path = undefined;
let apiFile = undefined;
let matchedEndpoint = endpoint;
for (const file of openApiFiles) {
const openApiFile = file.spec;
const match = findOpenApiPath(openApiFile.paths, endpoint);
const filenameMatches = !filename || filename === file.filename || filename === file.originalFileLocation;
if (match && filenameMatches) {
path = match.pathItem;
apiFile = openApiFile;
matchedEndpoint = match.pathKey;
}
}
if (path == null || apiFile == null) {
return undefined;
}
const operationResponse = getOperation(method, path);
if (operationResponse == null) {
return undefined;
}
const { method: finalMethod, operation } = operationResponse;
return {
method: finalMethod,
endpoint: matchedEndpoint,
operation,
path,
};
};
const getOperation = (method, pathObject) => {
const methods = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
if (method !== undefined) {
const lowerMethod = method.toLowerCase();
if (methods.includes(lowerMethod) && lowerMethod in pathObject) {
return {
method: lowerMethod,
operation: pathObject[lowerMethod],
};
}
}
else {
for (const method of methods) {
if (method in pathObject) {
return {
method,
operation: pathObject[method],
};
}
}
}
return undefined;
};