@redocly/respect-core
Version:
API testing framework core
89 lines • 3.92 kB
JavaScript
import * as path from 'node:path';
const KNOWN_BINARY_CONTENT_TYPES_REGEX = /^image\/(png|jpeg|gif|bmp|webp|svg\+xml)|application\/pdf$/;
export function stripFileDecorator(payload) {
return payload.startsWith('$file(') && payload.endsWith(')')
? payload.substring(7, payload.length - 2)
: payload;
}
const appendFileToFormData = async (formData, key, item, workflowFilePath, ctx) => {
const currentArazzoFileFolder = path.dirname(workflowFilePath);
const filePath = path.resolve(currentArazzoFileFolder, stripFileDecorator(item));
formData.append(key, await ctx.options.requestFileLoader.getFileBody(filePath));
};
const appendObjectToFormData = (promises, formData, payload, workflowFilePath, ctx, parentKey) => {
Object.entries(payload).forEach(([key, item]) => {
const formKey = parentKey ? `${parentKey}[${key}]` : key;
if (typeof item === 'string' && item.startsWith('$file(') && item.endsWith(')')) {
promises.push(appendFileToFormData(formData, formKey, item, workflowFilePath, ctx));
}
else if (Array.isArray(item)) {
item.forEach((i) => {
if (typeof i === 'string' && i.startsWith('$file(') && i.endsWith(')')) {
promises.push(appendFileToFormData(formData, formKey, i, workflowFilePath, ctx));
}
else {
formData.append(formKey, i.toString());
}
});
}
else if (typeof item === 'object' && item !== null) {
appendObjectToFormData(promises, formData, item, workflowFilePath, ctx, formKey);
}
else if (typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean') {
formData.append(formKey, item.toString());
}
});
};
const getRequestBodyMultipartFormData = async (payload, formData, workflowFilePath, ctx) => {
if (payload && typeof payload === 'object' && !Array.isArray(payload)) {
const promises = [];
appendObjectToFormData(promises, formData, payload, workflowFilePath, ctx);
await Promise.all(promises);
}
};
const getRequestBodyOctetStream = async (payload, ctx) => {
if (typeof payload === 'string' && payload.startsWith('$file(') && payload.endsWith(')')) {
const filePath = path.resolve(path.dirname(ctx.options.filePath), stripFileDecorator(payload));
return ctx.options.requestFileLoader.getFileBody(filePath);
}
else {
return payload;
}
};
export async function parseRequestBody(stepRequestBody, ctx) {
if (!stepRequestBody) {
return {
payload: undefined,
contentType: undefined,
encoding: undefined,
replacements: undefined,
};
}
const { payload, contentType } = stepRequestBody;
if (contentType === 'multipart/form-data') {
const formData = new FormData();
const workflowFilePath = path.resolve(ctx.options.filePath);
await getRequestBodyMultipartFormData(payload, formData, workflowFilePath, ctx);
return {
...stepRequestBody,
payload: formData,
};
}
else if (contentType === 'application/octet-stream' ||
(typeof contentType === 'string' && KNOWN_BINARY_CONTENT_TYPES_REGEX.test(contentType))) {
return {
...stepRequestBody,
payload: await getRequestBodyOctetStream(payload, ctx),
contentType: 'application/octet-stream',
};
}
else if (contentType === 'application/x-www-form-urlencoded' && typeof payload === 'string') {
return {
...stepRequestBody,
payload: Object.fromEntries(new URLSearchParams(payload).entries()),
contentType: 'application/x-www-form-urlencoded',
};
}
return stepRequestBody;
}
//# sourceMappingURL=parse-request-body.js.map