@oselvar/c4
Version:
Test helpers for Cloudflare Workers
53 lines (52 loc) • 1.47 kB
JavaScript
// src/openapi/index.ts
import { match } from "path-to-regexp";
function addOpenApiComponents(builder, openapi, container) {
for (const path in openapi.paths) {
const method = openapi.paths[path];
for (const httpMethod in method) {
const operation = method[httpMethod];
if (!operation.operationId) {
continue;
}
builder.addComponent(operation.operationId, { container });
}
}
}
function addOpenapiCall(builder, openapi, callerName, httpMethod, path) {
let basePath = "";
const server = openapi.servers?.[0]?.url;
if (server) {
try {
const url = new URL(path, server);
basePath = url.pathname;
} catch {
basePath = server;
}
}
for (const [pattern, pathObject] of Object.entries(openapi.paths)) {
if (!pathObject) {
continue;
}
const pathItemObject = pathObject[httpMethod];
if (!pathItemObject?.operationId) {
continue;
}
const colonPattern = basePath + pattern.replace(/{([^}]+)}/g, ":$1");
const matcher = match(colonPattern, { decode: decodeURIComponent });
const matched = matcher(path);
if (matched) {
const calleeName = pathItemObject.operationId;
const dependencyName = `${httpMethod.toUpperCase()} ${pattern}`;
builder.addCall(
callerName,
calleeName,
dependencyName
);
}
}
}
export {
addOpenApiComponents,
addOpenapiCall
};
//# sourceMappingURL=chunk-YHO3FLIS.js.map