Files
klz-cables.com/.pnpm-store/v10/files/d5/a30f32ffd170e684eac4e87e2976fc4586cb411addf710f5c4886d1183edc975ea5179f4e2c87dc0603986b9189740efeea1f2cd658d9c3f5e5900d45de0d8
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

87 lines
3.5 KiB
Plaintext

import { APIError } from '../errors/APIError.js';
import { processMultipartFormdata } from '../uploads/fetchAPI-multipart/index.js';
/**
* Mutates the Request, appending 'data' and 'file' if found
*/ export const addDataAndFileToRequest = async (req)=>{
const { body, headers, method, payload } = req;
if (method && [
'PATCH',
'POST',
'PUT'
].includes(method.toUpperCase()) && body) {
const [contentType] = (headers.get('Content-Type') || '').split(';', 1);
const bodyByteSize = parseInt(req.headers.get('Content-Length') || '0', 10);
if (contentType === 'application/json') {
let data = {};
try {
const text = await req.text?.();
data = text ? JSON.parse(text) : {};
} catch (error) {
req.payload.logger.error(error);
} finally{
req.data = data;
// @ts-expect-error attach json method to request
req.json = ()=>Promise.resolve(data);
}
} else if (bodyByteSize && contentType?.includes('multipart/')) {
const { error, fields, files } = await processMultipartFormdata({
options: {
...payload.config.bodyParser || {},
...payload.config.upload || {}
},
request: req
});
if (error) {
throw new APIError(error.message);
}
if (files?.file) {
req.file = files.file;
}
if (fields?._payload && typeof fields._payload === 'string') {
req.data = JSON.parse(fields._payload);
}
if (!req.file && fields?.file && typeof fields?.file === 'string') {
const { clientUploadContext, collectionSlug, filename, mimeType, size } = JSON.parse(fields.file);
const uploadConfig = req.payload.collections[collectionSlug].config.upload;
if (!uploadConfig.handlers) {
throw new APIError('uploadConfig.handlers is not present for ' + collectionSlug);
}
let response = null;
let error;
for (const handler of uploadConfig.handlers){
try {
const result = await handler(req, {
doc: null,
params: {
clientUploadContext,
collection: collectionSlug,
filename
}
});
if (result) {
response = result;
}
// If we couldn't get the file from that handler, save the error and try other.
} catch (err) {
error = err;
}
}
if (!response) {
if (error) {
payload.logger.error(error);
}
throw new APIError('Expected response from the upload handler.');
}
req.file = {
name: filename,
clientUploadContext,
data: Buffer.from(await response.arrayBuffer()),
mimetype: response.headers.get('Content-Type') || mimeType,
size
};
}
}
}
};
//# sourceMappingURL=addDataAndFileToRequest.js.map