UNPKG

@scalar/oas-utils

Version:

Open API spec and Yaml handling utilities

65 lines (64 loc) 2.1 kB
import { getExampleFromSchema } from "../../spec-getters/get-example-from-schema.js"; import { isReference } from "@scalar/workspace-store/schemas/v3.1/type-guard"; const objectToFormParams = (obj) => { const params = []; for (const [key, value] of Object.entries(obj)) { if (value === void 0 || value === null) { continue; } if (Array.isArray(value)) { for (const item of value) { params.push({ name: key, value: String(item) }); } } else if (typeof value === "object") { const nestedParams = objectToFormParams(value); for (const param of nestedParams) { params.push({ name: `${key}.${param.name}`, value: param.value }); } } else { params.push({ name: key, value: String(value) }); } } return params; }; const processBody = ({ operation, contentType, example }) => { const content = !operation.requestBody || isReference(operation.requestBody) ? {} : operation.requestBody.content; const _contentType = (contentType || Object.keys(content)[0]) ?? ""; const isFormData = _contentType === "multipart/form-data" || _contentType === "application/x-www-form-urlencoded"; if (example) { if (isFormData && typeof example === "object" && example !== null) { return { mimeType: _contentType, params: objectToFormParams(example) }; } return { mimeType: _contentType, text: JSON.stringify(example) }; } const contentSchema = content[_contentType]?.schema; if (contentSchema) { const extractedExample = getExampleFromSchema(contentSchema); if (extractedExample !== void 0) { if (isFormData && typeof extractedExample === "object" && extractedExample !== null) { return { mimeType: _contentType, params: objectToFormParams(extractedExample) }; } return { mimeType: _contentType, text: JSON.stringify(extractedExample) }; } } return { mimeType: _contentType, text: "null" }; }; export { processBody }; //# sourceMappingURL=process-body.js.map