UNPKG

@scalar/oas-utils

Version:

Open API spec and Yaml handling utilities

68 lines (67 loc) 2.17 kB
import { processServerUrl } from "./process-server-url.js"; import { processParameters } from "./process-parameters.js"; import { processBody } from "./process-body.js"; import { processSecuritySchemes } from "./process-security-schemes.js"; import { isReference } from "@scalar/workspace-store/schemas/v3.1/type-guard"; const operationToHar = ({ operation, contentType, method, path, server, securitySchemes, example }) => { const harRequest = { method, url: path, headers: [], queryString: [], postData: void 0, httpVersion: "HTTP/1.1", cookies: [], headersSize: -1, bodySize: -1 }; if (server?.url) { harRequest.url = processServerUrl(server, path); } if (operation.parameters) { const { url, headers, queryString, cookies } = processParameters(harRequest, operation.parameters, example); harRequest.url = url; harRequest.headers = headers; harRequest.queryString = queryString; harRequest.cookies = cookies; } if (!isReference(operation.requestBody) && operation.requestBody?.content) { const postData = processBody({ operation, contentType, example }); harRequest.postData = postData; harRequest.bodySize = postData.text?.length ?? -1; if (postData.mimeType) { const existingContentTypeHeader = harRequest.headers.find( (header) => header.name.toLowerCase() === "content-type" ); if (existingContentTypeHeader && !existingContentTypeHeader.value) { existingContentTypeHeader.value = postData.mimeType; } else { harRequest.headers.push({ name: "Content-Type", value: postData.mimeType }); } } } if (securitySchemes) { const { headers, queryString, cookies } = processSecuritySchemes(securitySchemes); harRequest.headers.push(...headers); harRequest.queryString.push(...queryString); harRequest.cookies.push(...cookies); } const headerText = harRequest.headers.map((h) => `${h.name}: ${h.value}`).join("\r\n"); harRequest.headersSize = headerText.length; return harRequest; }; export { operationToHar }; //# sourceMappingURL=operation-to-har.js.map