@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
49 lines (48 loc) • 1.61 kB
JavaScript
import { APIError } from 'payload';
export const getRequestCollection = (req)=>{
const collectionSlug = req.routeParams?.collection;
if (typeof collectionSlug !== 'string') {
throw new APIError('No collection was specified', 400);
}
const collection = req.payload.collections[collectionSlug];
if (!collection) {
throw new APIError(`Collection with the slug ${collectionSlug} was not found`, 404);
}
return collection;
};
export const getRequestCollectionWithID = (req, { disableSanitize, optionalID } = {})=>{
const collection = getRequestCollection(req);
const id = req.routeParams?.id;
if (typeof id !== 'string') {
if (optionalID) {
return {
id: undefined,
collection
};
}
throw new APIError('ID was not specified', 400);
}
if (disableSanitize === true) {
return {
id,
collection
};
}
let sanitizedID = id;
// If default db ID type is a number, we should sanitize
let shouldSanitize = Boolean(req.payload.db.defaultIDType === 'number');
// UNLESS the customIDType for this collection is text.... then we leave it
if (shouldSanitize && collection.customIDType === 'text') {
shouldSanitize = false;
}
// If we still should sanitize, parse float
if (shouldSanitize) {
sanitizedID = parseFloat(sanitizedID);
}
return {
// @ts-expect-error generic return
id: sanitizedID,
collection
};
};
//# sourceMappingURL=getRequestCollection.js.map