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
110 lines
4.0 KiB
Plaintext
110 lines
4.0 KiB
Plaintext
import { getDataLoader } from '../collections/dataloader.js';
|
|
import { getLocalI18n } from '../translations/getLocalI18n.js';
|
|
import { sanitizeFallbackLocale } from '../utilities/sanitizeFallbackLocale.js';
|
|
function getRequestContext(req = {
|
|
context: null
|
|
}, context = {}) {
|
|
if (req.context) {
|
|
if (Object.keys(req.context).length === 0 && req.context.constructor === Object) {
|
|
// if req.context is `{}` avoid unnecessary spread
|
|
return context;
|
|
} else {
|
|
return {
|
|
...req.context,
|
|
...context
|
|
};
|
|
}
|
|
} else {
|
|
return context;
|
|
}
|
|
}
|
|
const attachFakeURLProperties = (req, urlSuffix)=>{
|
|
/**
|
|
* *NOTE*
|
|
* If no URL is provided, the local API was called outside
|
|
* the context of a request. Therefore we create a fake URL object.
|
|
* `ts-expect-error` is used below for properties that are 'read-only'.
|
|
* Since they do not exist yet we can safely ignore the error.
|
|
*/ let urlObject;
|
|
function getURLObject() {
|
|
if (urlObject) {
|
|
return urlObject;
|
|
}
|
|
const fallbackURL = `http://${req.host || 'localhost'}${urlSuffix || ''}`;
|
|
const urlToUse = req?.url || (req.payload?.config?.serverURL ? `${req.payload?.config.serverURL}${urlSuffix || ''}` : fallbackURL);
|
|
try {
|
|
urlObject = new URL(urlToUse);
|
|
} catch (_err) {
|
|
req.payload?.logger.error(`Failed to create URL object from URL: ${urlToUse}, falling back to ${fallbackURL}`);
|
|
urlObject = new URL(fallbackURL);
|
|
}
|
|
return urlObject;
|
|
}
|
|
if (!req.host) {
|
|
req.host = getURLObject().host;
|
|
}
|
|
if (!req.protocol) {
|
|
req.protocol = getURLObject().protocol;
|
|
}
|
|
if (!req.pathname) {
|
|
req.pathname = getURLObject().pathname;
|
|
}
|
|
if (!req.searchParams) {
|
|
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
|
req.searchParams = getURLObject().searchParams;
|
|
}
|
|
if (!req.origin) {
|
|
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
|
req.origin = getURLObject().origin;
|
|
}
|
|
if (!req?.url) {
|
|
// @ts-expect-error eslint-disable-next-line no-param-reassign
|
|
req.url = getURLObject().href;
|
|
}
|
|
};
|
|
export const createLocalReq = async ({ context, depth, fallbackLocale, locale: localeArg, req = {}, urlSuffix, user }, payload)=>{
|
|
const localization = payload.config?.localization;
|
|
if (localization) {
|
|
const locale = localeArg === '*' ? 'all' : localeArg;
|
|
const defaultLocale = localization.defaultLocale;
|
|
const localeCandidate = locale || req?.locale || req?.query?.locale;
|
|
req.locale = localeCandidate && typeof localeCandidate === 'string' ? localeCandidate : defaultLocale;
|
|
const sanitizedFallback = sanitizeFallbackLocale({
|
|
fallbackLocale: fallbackLocale,
|
|
locale: req.locale,
|
|
localization
|
|
});
|
|
req.fallbackLocale = sanitizedFallback;
|
|
}
|
|
const i18n = req?.i18n || await getLocalI18n({
|
|
config: payload.config,
|
|
language: payload.config.i18n.fallbackLanguage
|
|
});
|
|
if (!req.headers) {
|
|
req.headers = new Headers();
|
|
}
|
|
req.context = getRequestContext(req, context);
|
|
req.payloadAPI = req?.payloadAPI || 'local';
|
|
req.payload = payload;
|
|
req.i18n = i18n;
|
|
req.t = i18n.t;
|
|
req.user = user || req?.user || null;
|
|
// Ensure user.collection is set for auth-related access control
|
|
// TODO (4.0): Instead of silently falling back, throw an error if user.collection is missing
|
|
if (req.user && !req.user.collection) {
|
|
req.user = {
|
|
...req.user,
|
|
collection: payload.config.admin.user
|
|
};
|
|
}
|
|
req.payloadDataLoader = req?.payloadDataLoader || getDataLoader(req);
|
|
req.routeParams = req?.routeParams || {};
|
|
req.query = req?.query || {};
|
|
if (typeof depth !== 'undefined') {
|
|
req.query.depth = depth;
|
|
}
|
|
attachFakeURLProperties(req, urlSuffix);
|
|
return req;
|
|
};
|
|
|
|
//# sourceMappingURL=createLocalReq.js.map |