feat: ability to add context to payload's request object (#2796)
Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
@@ -34,6 +34,7 @@ async function forgotPassword(incomingArgs: Arguments): Promise<string | null> {
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'forgotPassword',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -122,7 +123,7 @@ async function forgotPassword(incomingArgs: Arguments): Promise<string | null> {
|
||||
|
||||
await collectionConfig.hooks.afterForgotPassword.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
await hook({ args });
|
||||
await hook({ args, context: req.context });
|
||||
}, Promise.resolve());
|
||||
|
||||
return token;
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Payload } from '../../../payload';
|
||||
import { getDataLoader } from '../../../collections/dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -27,6 +28,7 @@ async function localForgotPassword<T extends keyof GeneratedTypes['collections']
|
||||
disableEmail,
|
||||
req = {} as PayloadRequest,
|
||||
} = options;
|
||||
setRequestContext(options.req);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Payload } from '../../../payload';
|
||||
import { getDataLoader } from '../../../collections/dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<TSlug extends keyof GeneratedTypes['collections']> = {
|
||||
collection: TSlug
|
||||
@@ -37,6 +38,8 @@ async function localLogin<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
overrideAccess = true,
|
||||
showHiddenFields,
|
||||
} = options;
|
||||
setRequestContext(options.req);
|
||||
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { PayloadRequest } from '../../../express/types';
|
||||
import { getDataLoader } from '../../../collections/dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -26,6 +27,7 @@ async function localResetPassword<T extends keyof GeneratedTypes['collections']>
|
||||
overrideAccess,
|
||||
req = {} as PayloadRequest,
|
||||
} = options;
|
||||
setRequestContext(options.req);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import unlock from '../unlock';
|
||||
import { getDataLoader } from '../../../collections/dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -25,6 +26,7 @@ async function localUnlock<T extends keyof GeneratedTypes['collections']>(
|
||||
overrideAccess = true,
|
||||
req = {} as PayloadRequest,
|
||||
} = options;
|
||||
setRequestContext(options.req);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'login',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -133,6 +134,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
user = (await hook({
|
||||
user,
|
||||
req: args.req,
|
||||
context: req.context,
|
||||
})) || user;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -172,6 +174,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
user,
|
||||
req: args.req,
|
||||
token,
|
||||
context: req.context,
|
||||
}) || user;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -186,6 +189,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
overrideAccess,
|
||||
req,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -198,6 +202,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
user = await hook({
|
||||
req,
|
||||
doc: user,
|
||||
context: req.context,
|
||||
}) || user;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ async function logout(incomingArgs: Arguments): Promise<string> {
|
||||
args = (await hook({
|
||||
req,
|
||||
res,
|
||||
context: req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ async function me({
|
||||
response = await hook({
|
||||
req,
|
||||
response,
|
||||
context: req.context,
|
||||
}) || response;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ async function refresh(incomingArgs: Arguments): Promise<Result> {
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'refresh',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -114,6 +115,7 @@ async function refresh(incomingArgs: Arguments): Promise<Result> {
|
||||
res: args.res,
|
||||
exp,
|
||||
token: refreshedToken,
|
||||
context: args.req.context,
|
||||
})) || response;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import { Response } from 'express';
|
||||
import { Config as GeneratedTypes } from 'payload/generated-types';
|
||||
import { Access, Endpoint, EntityDescription, GeneratePreviewURL } from '../../config/types';
|
||||
import { Field } from '../../fields/config/types';
|
||||
import { PayloadRequest } from '../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../express/types';
|
||||
import { Auth, IncomingAuthType, User } from '../../auth/types';
|
||||
import { IncomingUploadType, Upload } from '../../uploads/types';
|
||||
import { IncomingCollectionVersions, SanitizedCollectionVersions } from '../../versions/types';
|
||||
@@ -49,6 +49,7 @@ export type BeforeOperationHook = (args: {
|
||||
* Hook operation being performed
|
||||
*/
|
||||
operation: HookOperationType;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type BeforeValidateHook<T extends TypeWithID = any> = (args: {
|
||||
@@ -64,6 +65,7 @@ export type BeforeValidateHook<T extends TypeWithID = any> = (args: {
|
||||
* `undefined` on 'create' operation
|
||||
*/
|
||||
originalDoc?: T;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type BeforeChangeHook<T extends TypeWithID = any> = (args: {
|
||||
@@ -79,6 +81,7 @@ export type BeforeChangeHook<T extends TypeWithID = any> = (args: {
|
||||
* `undefined` on 'create' operation
|
||||
*/
|
||||
originalDoc?: T;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterChangeHook<T extends TypeWithID = any> = (args: {
|
||||
@@ -89,53 +92,62 @@ export type AfterChangeHook<T extends TypeWithID = any> = (args: {
|
||||
* Hook operation being performed
|
||||
*/
|
||||
operation: CreateOrUpdateOperation;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type BeforeReadHook<T extends TypeWithID = any> = (args: {
|
||||
doc: T;
|
||||
req: PayloadRequest;
|
||||
query: { [key: string]: any };
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterReadHook<T extends TypeWithID = any> = (args: {
|
||||
doc: T;
|
||||
req: PayloadRequest;
|
||||
query?: { [key: string]: any };
|
||||
findMany?: boolean
|
||||
findMany?: boolean;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type BeforeDeleteHook = (args: {
|
||||
req: PayloadRequest;
|
||||
id: string | number;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterDeleteHook<T extends TypeWithID = any> = (args: {
|
||||
doc: T;
|
||||
req: PayloadRequest;
|
||||
id: string | number;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterErrorHook = (err: Error, res: unknown) => { response: any, status: number } | void;
|
||||
export type AfterErrorHook = (err: Error, res: unknown, context: RequestContext) => { response: any, status: number } | void;
|
||||
|
||||
export type BeforeLoginHook<T extends TypeWithID = any> = (args: {
|
||||
req: PayloadRequest;
|
||||
user: T
|
||||
user: T;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterLoginHook<T extends TypeWithID = any> = (args: {
|
||||
req: PayloadRequest;
|
||||
user: T;
|
||||
token: string;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterLogoutHook<T extends TypeWithID = any> = (args: {
|
||||
req: PayloadRequest;
|
||||
res: Response;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterMeHook<T extends TypeWithID = any> = (args: {
|
||||
req: PayloadRequest;
|
||||
response: unknown;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterRefreshHook<T extends TypeWithID = any> = (args: {
|
||||
@@ -143,10 +155,12 @@ export type AfterRefreshHook<T extends TypeWithID = any> = (args: {
|
||||
res: Response;
|
||||
token: string;
|
||||
exp: number;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
export type AfterForgotPasswordHook = (args: {
|
||||
args?: any;
|
||||
context: RequestContext;
|
||||
}) => any;
|
||||
|
||||
type BeforeDuplicateArgs<T> = {
|
||||
|
||||
@@ -54,6 +54,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'create',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -130,6 +131,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
operation: 'create',
|
||||
overrideAccess,
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -143,6 +145,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
data,
|
||||
req,
|
||||
operation: 'create',
|
||||
context: req.context,
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -165,6 +168,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
data,
|
||||
req,
|
||||
operation: 'create',
|
||||
context: req.context,
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -180,6 +184,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
operation: 'create',
|
||||
req,
|
||||
skipValidation: shouldSaveDraft,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -203,7 +208,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
doc: resultWithLocales,
|
||||
payload: req.payload,
|
||||
password: data.password as string,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
doc = await Model.create(resultWithLocales);
|
||||
@@ -266,6 +271,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
overrideAccess,
|
||||
req,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -278,6 +284,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
result = await hook({
|
||||
req,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -292,6 +299,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
entityConfig: collectionConfig,
|
||||
operation: 'create',
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -306,6 +314,7 @@ async function create<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
previousDoc: {},
|
||||
req: args.req,
|
||||
operation: 'create',
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'delete',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -116,6 +117,7 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
|
||||
return hook({
|
||||
req,
|
||||
id,
|
||||
context: req.context,
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -150,6 +152,7 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
|
||||
overrideAccess,
|
||||
req,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -162,6 +165,7 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
|
||||
result = await hook({
|
||||
req,
|
||||
doc: result || doc,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -176,6 +180,7 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
|
||||
req,
|
||||
id,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ async function deleteByID<TSlug extends keyof GeneratedTypes['collections']>(inc
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'delete',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -72,6 +73,7 @@ async function deleteByID<TSlug extends keyof GeneratedTypes['collections']>(inc
|
||||
return hook({
|
||||
req,
|
||||
id,
|
||||
context: req.context,
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -145,6 +147,7 @@ async function deleteByID<TSlug extends keyof GeneratedTypes['collections']>(inc
|
||||
overrideAccess,
|
||||
req,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -157,6 +160,7 @@ async function deleteByID<TSlug extends keyof GeneratedTypes['collections']>(inc
|
||||
result = await hook({
|
||||
req,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -167,7 +171,7 @@ async function deleteByID<TSlug extends keyof GeneratedTypes['collections']>(inc
|
||||
await collectionConfig.hooks.afterDelete.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
result = await hook({ req, id, doc: result }) || result;
|
||||
result = await hook({ req, id, doc: result, context: req.context }) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -41,6 +41,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'read',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -180,7 +181,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
await collectionConfig.hooks.beforeRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
docRef = await hook({ req, query, doc: docRef }) || docRef;
|
||||
docRef = await hook({ req, query, doc: docRef, context: req.context }) || docRef;
|
||||
}, Promise.resolve());
|
||||
|
||||
return docRef;
|
||||
@@ -202,6 +203,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
req,
|
||||
showHiddenFields,
|
||||
findMany: true,
|
||||
context: req.context,
|
||||
}))),
|
||||
};
|
||||
|
||||
@@ -217,7 +219,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
docRef = await hook({ req, query, doc: docRef, findMany: true }) || doc;
|
||||
docRef = await hook({ req, query, doc: docRef, findMany: true, context: req.context }) || doc;
|
||||
}, Promise.resolve());
|
||||
|
||||
return docRef;
|
||||
|
||||
@@ -35,6 +35,7 @@ async function findByID<T extends TypeWithID>(
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'read',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -138,6 +139,7 @@ async function findByID<T extends TypeWithID>(
|
||||
req,
|
||||
query,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -153,6 +155,7 @@ async function findByID<T extends TypeWithID>(
|
||||
overrideAccess,
|
||||
req,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -166,6 +169,7 @@ async function findByID<T extends TypeWithID>(
|
||||
req,
|
||||
query,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -98,6 +98,7 @@ async function findVersionByID<T extends TypeWithVersion<T> = any>(args: Argumen
|
||||
req,
|
||||
query,
|
||||
doc: result.version,
|
||||
context: req.context,
|
||||
}) || result.version;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -113,6 +114,7 @@ async function findVersionByID<T extends TypeWithVersion<T> = any>(args: Argumen
|
||||
overrideAccess,
|
||||
req,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -126,6 +128,7 @@ async function findVersionByID<T extends TypeWithVersion<T> = any>(args: Argumen
|
||||
req,
|
||||
query,
|
||||
doc: result.version,
|
||||
context: req.context,
|
||||
}) || result.version;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -110,7 +110,7 @@ async function findVersions<T extends TypeWithVersion<T>>(
|
||||
await collectionConfig.hooks.beforeRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
docRef.version = await hook({ req, query, doc: docRef.version }) || docRef.version;
|
||||
docRef.version = await hook({ req, query, doc: docRef.version, context: req.context }) || docRef.version;
|
||||
}, Promise.resolve());
|
||||
|
||||
return docRef;
|
||||
@@ -133,6 +133,7 @@ async function findVersions<T extends TypeWithVersion<T>>(
|
||||
req,
|
||||
showHiddenFields,
|
||||
findMany: true,
|
||||
context: req.context,
|
||||
}),
|
||||
}))),
|
||||
};
|
||||
@@ -149,7 +150,7 @@ async function findVersions<T extends TypeWithVersion<T>>(
|
||||
await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
docRef.version = await hook({ req, query, doc: doc.version, findMany: true }) || doc.version;
|
||||
docRef.version = await hook({ req, query, doc: doc.version, findMany: true, context: req.context }) || doc.version;
|
||||
}, Promise.resolve());
|
||||
|
||||
return docRef;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Config as GeneratedTypes } from 'payload/generated-types';
|
||||
import { UploadedFile } from 'express-fileupload';
|
||||
import { MarkOptional } from 'ts-essentials';
|
||||
import { Payload } from '../../../payload';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Document } from '../../../types';
|
||||
import getFileByPath from '../../../uploads/getFileByPath';
|
||||
import create from '../create';
|
||||
@@ -10,6 +10,7 @@ import { getDataLoader } from '../../dataloader';
|
||||
import { File } from '../../../uploads/types';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<TSlug extends keyof GeneratedTypes['collections']> = {
|
||||
collection: TSlug
|
||||
@@ -26,6 +27,10 @@ export type Options<TSlug extends keyof GeneratedTypes['collections']> = {
|
||||
overwriteExistingFiles?: boolean
|
||||
req?: PayloadRequest
|
||||
draft?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext
|
||||
}
|
||||
|
||||
export default async function createLocal<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
@@ -47,7 +52,9 @@ export default async function createLocal<TSlug extends keyof GeneratedTypes['co
|
||||
overwriteExistingFiles = false,
|
||||
req = {} as PayloadRequest,
|
||||
draft,
|
||||
context,
|
||||
} = options;
|
||||
setRequestContext(req, context);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
const defaultLocale = payload?.config?.localization ? payload?.config?.localization?.defaultLocale : null;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Config as GeneratedTypes } from '../../../generated-types';
|
||||
import { Document, Where } from '../../../types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Payload } from '../../../payload';
|
||||
import deleteOperation from '../delete';
|
||||
import deleteByID from '../deleteByID';
|
||||
@@ -8,6 +8,7 @@ import { getDataLoader } from '../../dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { BulkOperationResult } from '../../config/types';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type BaseOptions<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -17,6 +18,10 @@ export type BaseOptions<T extends keyof GeneratedTypes['collections']> = {
|
||||
user?: Document
|
||||
overrideAccess?: boolean
|
||||
showHiddenFields?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext
|
||||
}
|
||||
|
||||
export type ByIDOptions<T extends keyof GeneratedTypes['collections']> = BaseOptions<T> & {
|
||||
@@ -45,6 +50,7 @@ async function deleteLocal<TSlug extends keyof GeneratedTypes['collections']>(pa
|
||||
user,
|
||||
overrideAccess = true,
|
||||
showHiddenFields,
|
||||
context,
|
||||
} = options;
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
@@ -63,6 +69,7 @@ async function deleteLocal<TSlug extends keyof GeneratedTypes['collections']>(pa
|
||||
payload,
|
||||
i18n: i18n(payload.config.i18n),
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req, context);
|
||||
|
||||
if (!req.t) req.t = req.i18n.t;
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
@@ -2,11 +2,12 @@ import { Config as GeneratedTypes } from 'payload/generated-types';
|
||||
import { PaginatedDocs } from '../../../mongoose/types';
|
||||
import { Document, Where } from '../../../types';
|
||||
import { Payload } from '../../../payload';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import find from '../find';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -25,6 +26,10 @@ export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
where?: Where
|
||||
draft?: boolean
|
||||
req?: PayloadRequest
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext
|
||||
}
|
||||
|
||||
export default async function findLocal<T extends keyof GeneratedTypes['collections']>(
|
||||
@@ -48,7 +53,9 @@ export default async function findLocal<T extends keyof GeneratedTypes['collecti
|
||||
draft = false,
|
||||
pagination = true,
|
||||
req = {} as PayloadRequest,
|
||||
context,
|
||||
} = options;
|
||||
setRequestContext(options.req, context);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
const defaultLocale = payload?.config?.localization ? payload?.config?.localization?.defaultLocale : null;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Config as GeneratedTypes } from 'payload/generated-types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Document } from '../../../types';
|
||||
import findByID from '../findByID';
|
||||
import { Payload } from '../../../payload';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -20,6 +21,10 @@ export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
disableErrors?: boolean
|
||||
req?: PayloadRequest
|
||||
draft?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext,
|
||||
}
|
||||
|
||||
export default async function findByIDLocal<T extends keyof GeneratedTypes['collections']>(
|
||||
@@ -39,7 +44,9 @@ export default async function findByIDLocal<T extends keyof GeneratedTypes['coll
|
||||
showHiddenFields,
|
||||
req = {} as PayloadRequest,
|
||||
draft = false,
|
||||
context,
|
||||
} = options;
|
||||
setRequestContext(options.req, context);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
const defaultLocale = payload?.config?.localization ? payload?.config?.localization?.defaultLocale : null;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Config as GeneratedTypes } from 'payload/generated-types';
|
||||
import { Payload } from '../../../payload';
|
||||
import { Document } from '../../../types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { TypeWithVersion } from '../../../versions/types';
|
||||
import findVersionByID from '../findVersionByID';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -19,6 +20,11 @@ export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
showHiddenFields?: boolean
|
||||
disableErrors?: boolean
|
||||
req?: PayloadRequest
|
||||
draft?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext,
|
||||
}
|
||||
|
||||
export default async function findVersionByIDLocal<T extends keyof GeneratedTypes['collections']>(
|
||||
@@ -35,7 +41,9 @@ export default async function findVersionByIDLocal<T extends keyof GeneratedType
|
||||
disableErrors = false,
|
||||
showHiddenFields,
|
||||
req = {} as PayloadRequest,
|
||||
context,
|
||||
} = options;
|
||||
setRequestContext(options.req, context);
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
const defaultLocale = payload?.config?.localization ? payload?.config?.localization?.defaultLocale : null;
|
||||
|
||||
@@ -3,11 +3,12 @@ import { Payload } from '../../../payload';
|
||||
import { Document, Where } from '../../../types';
|
||||
import { PaginatedDocs } from '../../../mongoose/types';
|
||||
import { TypeWithVersion } from '../../../versions/types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import findVersions from '../findVersions';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -21,6 +22,11 @@ export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
showHiddenFields?: boolean
|
||||
sort?: string
|
||||
where?: Where
|
||||
draft?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext,
|
||||
}
|
||||
|
||||
export default async function findVersionsLocal<T extends keyof GeneratedTypes['collections']>(
|
||||
@@ -39,6 +45,7 @@ export default async function findVersionsLocal<T extends keyof GeneratedTypes['
|
||||
overrideAccess = true,
|
||||
showHiddenFields,
|
||||
sort,
|
||||
context,
|
||||
} = options;
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
@@ -57,6 +64,7 @@ export default async function findVersionsLocal<T extends keyof GeneratedTypes['
|
||||
payload,
|
||||
i18n,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req, context);
|
||||
|
||||
if (!req.t) req.t = req.i18n.t;
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Config as GeneratedTypes } from 'payload/generated-types';
|
||||
import { Payload } from '../../../payload';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Document } from '../../../types';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import restoreVersion from '../restoreVersion';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
collection: T
|
||||
@@ -16,6 +17,11 @@ export type Options<T extends keyof GeneratedTypes['collections']> = {
|
||||
user?: Document
|
||||
overrideAccess?: boolean
|
||||
showHiddenFields?: boolean
|
||||
draft?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext,
|
||||
}
|
||||
|
||||
export default async function restoreVersionLocal<T extends keyof GeneratedTypes['collections']>(
|
||||
@@ -31,6 +37,7 @@ export default async function restoreVersionLocal<T extends keyof GeneratedTypes
|
||||
user,
|
||||
overrideAccess = true,
|
||||
showHiddenFields,
|
||||
context,
|
||||
} = options;
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
@@ -49,6 +56,7 @@ export default async function restoreVersionLocal<T extends keyof GeneratedTypes
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req, context);
|
||||
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
|
||||
@@ -4,13 +4,14 @@ import { Payload } from '../../../payload';
|
||||
import { Document, Where } from '../../../types';
|
||||
import getFileByPath from '../../../uploads/getFileByPath';
|
||||
import update from '../update';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import { File } from '../../../uploads/types';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import updateByID from '../updateByID';
|
||||
import { BulkOperationResult } from '../../config/types';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type BaseOptions<TSlug extends keyof GeneratedTypes['collections']> = {
|
||||
collection: TSlug
|
||||
@@ -26,6 +27,10 @@ export type BaseOptions<TSlug extends keyof GeneratedTypes['collections']> = {
|
||||
overwriteExistingFiles?: boolean
|
||||
draft?: boolean
|
||||
autosave?: boolean
|
||||
/**
|
||||
* context, which will then be passed to req.context, which can be read by hooks
|
||||
*/
|
||||
context?: RequestContext
|
||||
}
|
||||
|
||||
export type ByIDOptions<TSlug extends keyof GeneratedTypes['collections']> = BaseOptions<TSlug> & {
|
||||
@@ -60,6 +65,7 @@ async function updateLocal<TSlug extends keyof GeneratedTypes['collections']>(pa
|
||||
autosave,
|
||||
id,
|
||||
where,
|
||||
context,
|
||||
} = options;
|
||||
|
||||
const collection = payload.collections[collectionSlug];
|
||||
@@ -82,6 +88,7 @@ async function updateLocal<TSlug extends keyof GeneratedTypes['collections']>(pa
|
||||
file: file ?? await getFileByPath(filePath),
|
||||
},
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req, context);
|
||||
|
||||
if (!req.t) req.t = req.i18n.t;
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
@@ -143,6 +143,7 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -155,6 +156,7 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
result = await hook({
|
||||
req,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -168,6 +170,7 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
previousDoc: prevDocWithLocales,
|
||||
entityConfig: collectionConfig,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
req,
|
||||
});
|
||||
|
||||
@@ -183,6 +186,7 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
req,
|
||||
previousDoc: prevDocWithLocales,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'update',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -149,6 +150,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
overrideAccess: true,
|
||||
showHiddenFields: true,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
await deleteAssociatedFiles({ config, collectionConfig, files: filesToUpload, doc: docWithLocales, t, overrideDelete: false });
|
||||
@@ -165,6 +167,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
operation: 'update',
|
||||
overrideAccess,
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -179,6 +182,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
operation: 'update',
|
||||
originalDoc,
|
||||
context: req.context,
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -202,6 +206,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
originalDoc,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -218,6 +223,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
operation: 'update',
|
||||
req,
|
||||
skipValidation: shouldSaveDraft || data._status === 'draft',
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -275,6 +281,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -287,6 +294,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
result = await hook({
|
||||
req,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -301,6 +309,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
entityConfig: collectionConfig,
|
||||
operation: 'update',
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -315,6 +324,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
previousDoc: originalDoc,
|
||||
req,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
args = (await hook({
|
||||
args,
|
||||
operation: 'update',
|
||||
context: args.req.context,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -130,6 +131,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
overrideAccess: true,
|
||||
showHiddenFields: true,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -165,6 +167,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
operation: 'update',
|
||||
overrideAccess,
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -179,6 +182,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
operation: 'update',
|
||||
originalDoc,
|
||||
context: req.context,
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -202,6 +206,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
originalDoc,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -218,18 +223,19 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
operation: 'update',
|
||||
req,
|
||||
skipValidation: shouldSaveDraft || data._status === 'draft',
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Handle potential password update
|
||||
// /////////////////////////////////////
|
||||
|
||||
const dataToUpdate: Record<string, unknown> = { ...result }
|
||||
const dataToUpdate: Record<string, unknown> = { ...result };
|
||||
|
||||
if (shouldSavePassword && typeof password === 'string') {
|
||||
const { hash, salt } = await generatePasswordSaltHash({ password })
|
||||
dataToUpdate.salt = salt
|
||||
dataToUpdate.hash = hash
|
||||
const { hash, salt } = await generatePasswordSaltHash({ password });
|
||||
dataToUpdate.salt = salt;
|
||||
dataToUpdate.hash = hash;
|
||||
delete data.password;
|
||||
delete result.password;
|
||||
}
|
||||
@@ -287,6 +293,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -299,6 +306,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
result = await hook({
|
||||
req,
|
||||
doc: result,
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -313,6 +321,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
entityConfig: collectionConfig,
|
||||
operation: 'update',
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -327,6 +336,7 @@ async function updateByID<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
previousDoc: originalDoc,
|
||||
req,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
|
||||
10
src/express/middleware/defaultPayload.ts
Normal file
10
src/express/middleware/defaultPayload.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Response, NextFunction } from 'express';
|
||||
import type { PayloadRequest } from '../types';
|
||||
import { setRequestContext } from '../setRequestContext';
|
||||
|
||||
function defaultPayload(req: PayloadRequest, res: Response, next: NextFunction) {
|
||||
setRequestContext(req);
|
||||
next();
|
||||
}
|
||||
|
||||
export default defaultPayload;
|
||||
@@ -21,11 +21,11 @@ const errorHandler = (config: SanitizedConfig, logger: Logger) => async (err: AP
|
||||
}
|
||||
|
||||
if (req.collection && typeof req.collection.config.hooks.afterError === 'function') {
|
||||
({ response, status } = await req.collection.config.hooks.afterError(err, response) || { response, status });
|
||||
({ response, status } = await req.collection.config.hooks.afterError(err, response, req.context) || { response, status });
|
||||
}
|
||||
|
||||
if (typeof config.hooks.afterError === 'function') {
|
||||
({ response, status } = await config.hooks.afterError(err, response) || { response, status });
|
||||
({ response, status } = await config.hooks.afterError(err, response, req.context) || { response, status });
|
||||
}
|
||||
|
||||
res.status(status).send(response);
|
||||
|
||||
@@ -14,6 +14,7 @@ import { PayloadRequest } from '../types';
|
||||
import corsHeaders from './corsHeaders';
|
||||
import convertPayload from './convertPayload';
|
||||
import { i18nMiddleware } from './i18n';
|
||||
import defaultPayload from './defaultPayload';
|
||||
|
||||
const middleware = (payload: Payload): any => {
|
||||
const rateLimitOptions: {
|
||||
@@ -32,6 +33,7 @@ const middleware = (payload: Payload): any => {
|
||||
}
|
||||
|
||||
return [
|
||||
defaultPayload,
|
||||
...(payload.config.express.preMiddleware || []),
|
||||
rateLimit(rateLimitOptions),
|
||||
passport.initialize(),
|
||||
|
||||
18
src/express/setRequestContext.ts
Normal file
18
src/express/setRequestContext.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import type { PayloadRequest, RequestContext } from './types';
|
||||
|
||||
/**
|
||||
* This makes sure that req.context always exists (is {}) and populates it with an optional default context.
|
||||
* This function mutates directly to avoid copying memory. As payloadRequest is not a primitive, the scope of the mutation is not limited to this function but should also be reflected in the calling function.
|
||||
*/
|
||||
export function setRequestContext(req: PayloadRequest = { context: null } as PayloadRequest, context: RequestContext = {}) {
|
||||
if (req.context) {
|
||||
if (Object.keys(req.context).length === 0 && req.context.constructor === Object) { // check if req.context is just {}
|
||||
req.context = context; // Faster - ... is bad for performance
|
||||
} else {
|
||||
req.context = { ...req.context, ...context }; // Merge together
|
||||
}
|
||||
} else {
|
||||
req.context = context;
|
||||
}
|
||||
}
|
||||
@@ -24,10 +24,16 @@ export declare type PayloadRequest<U = any> = Request & {
|
||||
* - Configuration from payload-config.ts
|
||||
* - MongoDB model for this collection
|
||||
* - GraphQL type metadata
|
||||
* */
|
||||
*/
|
||||
collection?: Collection;
|
||||
/** What triggered this request */
|
||||
payloadAPI?: 'REST' | 'local' | 'GraphQL';
|
||||
/** context allows you to pass your own data to the request object as context
|
||||
* This is useful for, for example, passing data from a beforeChange hook to an afterChange hook.
|
||||
* payoadContext can also be fully typed using declare module
|
||||
* {@link https://payloadcms.com/docs/hooks/context More info in the Payload Documentation}.
|
||||
*/
|
||||
context: RequestContext;
|
||||
/** Uploaded files */
|
||||
files?: {
|
||||
/**
|
||||
@@ -49,3 +55,7 @@ export declare type PayloadRequest<U = any> = Request & {
|
||||
[slug: string]: (q: unknown) => Document;
|
||||
};
|
||||
};
|
||||
|
||||
export interface RequestContext {
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { EditorProps } from '@monaco-editor/react';
|
||||
import { Operation, Where } from '../../types';
|
||||
import { SanitizedConfig } from '../../config/types';
|
||||
import { TypeWithID } from '../../collections/config/types';
|
||||
import { PayloadRequest } from '../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../express/types';
|
||||
import { ConditionalDateProps } from '../../admin/components/elements/DatePicker/types';
|
||||
import { Description } from '../../admin/components/forms/FieldDescription/types';
|
||||
import { User } from '../../auth';
|
||||
@@ -33,6 +33,7 @@ export type FieldHookArgs<T extends TypeWithID = any, P = any, S = any> = {
|
||||
/** The value of the field. */
|
||||
value?: P,
|
||||
previousValue?: P,
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export type FieldHook<T extends TypeWithID = any, P = any, S = any> = (args: FieldHookArgs<T, P, S>) => Promise<P> | P;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { SanitizedCollectionConfig } from '../../../collections/config/types';
|
||||
import { SanitizedGlobalConfig } from '../../../globals/config/types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
import deepCopyObject from '../../../utilities/deepCopyObject';
|
||||
|
||||
@@ -11,6 +11,7 @@ type Args<T> = {
|
||||
entityConfig: SanitizedCollectionConfig | SanitizedGlobalConfig
|
||||
operation: 'create' | 'update'
|
||||
req: PayloadRequest
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const afterChange = async <T extends Record<string, unknown>>({
|
||||
@@ -20,6 +21,7 @@ export const afterChange = async <T extends Record<string, unknown>>({
|
||||
entityConfig,
|
||||
operation,
|
||||
req,
|
||||
context,
|
||||
}: Args<T>): Promise<T> => {
|
||||
const doc = deepCopyObject(incomingDoc);
|
||||
|
||||
@@ -33,6 +35,7 @@ export const afterChange = async <T extends Record<string, unknown>>({
|
||||
previousSiblingDoc: previousDoc,
|
||||
siblingDoc: doc,
|
||||
siblingData: data,
|
||||
context,
|
||||
});
|
||||
|
||||
return doc;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Field, fieldAffectsData, TabAsField, tabHasName } from '../../config/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
|
||||
@@ -13,6 +13,7 @@ type Args = {
|
||||
req: PayloadRequest
|
||||
siblingData: Record<string, unknown>
|
||||
siblingDoc: Record<string, unknown>
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
// This function is responsible for the following actions, in order:
|
||||
@@ -28,6 +29,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
}: Args): Promise<void> => {
|
||||
if (fieldAffectsData(field)) {
|
||||
// Execute hooks
|
||||
@@ -45,6 +47,7 @@ export const promise = async ({
|
||||
siblingData,
|
||||
operation,
|
||||
req,
|
||||
context,
|
||||
});
|
||||
|
||||
if (hookedValue !== undefined) {
|
||||
@@ -67,6 +70,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingData: siblingData?.[field.name] as Record<string, unknown> || {},
|
||||
siblingDoc: siblingDoc[field.name] as Record<string, unknown>,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -88,6 +92,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingData: siblingData?.[field.name]?.[i] || {},
|
||||
siblingDoc: { ...row } || {},
|
||||
context,
|
||||
}));
|
||||
});
|
||||
await Promise.all(promises);
|
||||
@@ -114,6 +119,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingData: siblingData?.[field.name]?.[i] || {},
|
||||
siblingDoc: { ...row } || {},
|
||||
context,
|
||||
}));
|
||||
}
|
||||
});
|
||||
@@ -135,6 +141,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingData: siblingData || {},
|
||||
siblingDoc: { ...siblingDoc },
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -161,6 +168,7 @@ export const promise = async ({
|
||||
previousDoc,
|
||||
siblingData: tabSiblingData,
|
||||
siblingDoc: tabSiblingDoc,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -177,6 +185,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingData: siblingData || {},
|
||||
siblingDoc: { ...siblingDoc },
|
||||
context,
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Field, TabAsField } from '../../config/types';
|
||||
import { promise } from './promise';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
|
||||
type Args = {
|
||||
data: Record<string, unknown>
|
||||
@@ -12,6 +12,7 @@ type Args = {
|
||||
req: PayloadRequest
|
||||
siblingData: Record<string, unknown>
|
||||
siblingDoc: Record<string, unknown>
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const traverseFields = async ({
|
||||
@@ -24,6 +25,7 @@ export const traverseFields = async ({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
}: Args): Promise<void> => {
|
||||
const promises = [];
|
||||
|
||||
@@ -38,6 +40,7 @@ export const traverseFields = async ({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { SanitizedCollectionConfig } from '../../../collections/config/types';
|
||||
import { SanitizedGlobalConfig } from '../../../globals/config/types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
import deepCopyObject from '../../../utilities/deepCopyObject';
|
||||
|
||||
@@ -14,6 +14,7 @@ type Args = {
|
||||
req: PayloadRequest
|
||||
overrideAccess: boolean
|
||||
showHiddenFields: boolean
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export async function afterRead<T = any>(args: Args): Promise<T> {
|
||||
@@ -27,6 +28,7 @@ export async function afterRead<T = any>(args: Args): Promise<T> {
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context,
|
||||
} = args;
|
||||
|
||||
const doc = deepCopyObject(incomingDoc);
|
||||
@@ -51,6 +53,7 @@ export async function afterRead<T = any>(args: Args): Promise<T> {
|
||||
req,
|
||||
siblingDoc: doc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
|
||||
await Promise.all(fieldPromises);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { Field, fieldAffectsData, TabAsField, tabHasName } from '../../config/types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
import richTextRelationshipPromise from '../../richText/richTextRelationshipPromise';
|
||||
import relationshipPopulationPromise from './relationshipPopulationPromise';
|
||||
@@ -18,6 +18,7 @@ type Args = {
|
||||
overrideAccess: boolean
|
||||
siblingDoc: Record<string, unknown>
|
||||
showHiddenFields: boolean
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
// This function is responsible for the following actions, in order:
|
||||
@@ -41,6 +42,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
}: Args): Promise<void> => {
|
||||
if (fieldAffectsData(field) && field.hidden && typeof siblingDoc[field.name] !== 'undefined' && !showHiddenFields) {
|
||||
delete siblingDoc[field.name];
|
||||
@@ -161,6 +163,7 @@ export const promise = async ({
|
||||
siblingData: siblingDoc,
|
||||
operation: 'read',
|
||||
req,
|
||||
context,
|
||||
});
|
||||
|
||||
if (hookedValue !== undefined) {
|
||||
@@ -178,6 +181,7 @@ export const promise = async ({
|
||||
siblingData: siblingDoc,
|
||||
req,
|
||||
value: siblingDoc[field.name],
|
||||
context,
|
||||
});
|
||||
|
||||
if (hookedValue !== undefined) {
|
||||
@@ -227,6 +231,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc: groupDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -250,6 +255,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc: row || {},
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
});
|
||||
} else if (!shouldHoistLocalizedValue && typeof rows === 'object' && rows !== null) {
|
||||
@@ -269,6 +275,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc: row || {},
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -298,6 +305,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc: row || {},
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -321,6 +329,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc: row || {},
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -346,6 +355,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -371,6 +381,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc: tabDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -390,6 +401,7 @@ export const promise = async ({
|
||||
req,
|
||||
siblingDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Field, TabAsField } from '../../config/types';
|
||||
import { promise } from './promise';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
|
||||
type Args = {
|
||||
currentDepth: number
|
||||
@@ -15,6 +15,7 @@ type Args = {
|
||||
overrideAccess: boolean
|
||||
siblingDoc: Record<string, unknown>
|
||||
showHiddenFields: boolean
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const traverseFields = ({
|
||||
@@ -30,6 +31,7 @@ export const traverseFields = ({
|
||||
req,
|
||||
siblingDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
}: Args): void => {
|
||||
fields.forEach((field) => {
|
||||
fieldPromises.push(promise({
|
||||
@@ -45,6 +47,7 @@ export const traverseFields = ({
|
||||
req,
|
||||
siblingDoc,
|
||||
showHiddenFields,
|
||||
context,
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { SanitizedCollectionConfig } from '../../../collections/config/types';
|
||||
import { SanitizedGlobalConfig } from '../../../globals/config/types';
|
||||
import { Operation } from '../../../types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
import { ValidationError } from '../../../errors';
|
||||
import deepCopyObject from '../../../utilities/deepCopyObject';
|
||||
@@ -15,6 +15,7 @@ type Args<T> = {
|
||||
operation: Operation
|
||||
req: PayloadRequest
|
||||
skipValidation?: boolean
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const beforeChange = async <T extends Record<string, unknown>>({
|
||||
@@ -26,6 +27,7 @@ export const beforeChange = async <T extends Record<string, unknown>>({
|
||||
operation,
|
||||
req,
|
||||
skipValidation,
|
||||
context,
|
||||
}: Args<T>): Promise<T> => {
|
||||
const data = deepCopyObject(incomingData);
|
||||
const mergeLocaleActions = [];
|
||||
@@ -46,6 +48,7 @@ export const beforeChange = async <T extends Record<string, unknown>>({
|
||||
siblingDocWithLocales: docWithLocales,
|
||||
fields: entityConfig.fields,
|
||||
skipValidation,
|
||||
context,
|
||||
});
|
||||
|
||||
if (errors.length > 0) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import merge from 'deepmerge';
|
||||
import { Field, fieldAffectsData, TabAsField, tabHasName } from '../../config/types';
|
||||
import { Operation } from '../../../types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import getValueWithDefault from '../../getDefaultValue';
|
||||
import { traverseFields } from './traverseFields';
|
||||
import { getExistingRowDoc } from './getExistingRowDoc';
|
||||
@@ -23,6 +23,7 @@ type Args = {
|
||||
siblingDoc: Record<string, unknown>
|
||||
siblingDocWithLocales?: Record<string, unknown>
|
||||
skipValidation: boolean
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
// This function is responsible for the following actions, in order:
|
||||
@@ -49,6 +50,7 @@ export const promise = async ({
|
||||
siblingDoc,
|
||||
siblingDocWithLocales,
|
||||
skipValidation,
|
||||
context,
|
||||
}: Args): Promise<void> => {
|
||||
const passesCondition = (field.admin?.condition) ? field.admin.condition(data, siblingData, { user: req.user }) : true;
|
||||
let skipValidationFromHere = skipValidation || !passesCondition;
|
||||
@@ -96,6 +98,7 @@ export const promise = async ({
|
||||
siblingData,
|
||||
operation,
|
||||
req,
|
||||
context,
|
||||
});
|
||||
|
||||
if (hookedValue !== undefined) {
|
||||
@@ -208,6 +211,7 @@ export const promise = async ({
|
||||
siblingDoc: siblingDoc[field.name] as Record<string, unknown>,
|
||||
siblingDocWithLocales: siblingDocWithLocales[field.name] as Record<string, unknown>,
|
||||
skipValidation: skipValidationFromHere,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -234,6 +238,7 @@ export const promise = async ({
|
||||
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]),
|
||||
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]),
|
||||
skipValidation: skipValidationFromHere,
|
||||
context,
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -267,6 +272,7 @@ export const promise = async ({
|
||||
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]),
|
||||
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]),
|
||||
skipValidation: skipValidationFromHere,
|
||||
context,
|
||||
}));
|
||||
}
|
||||
});
|
||||
@@ -294,6 +300,7 @@ export const promise = async ({
|
||||
siblingDoc,
|
||||
siblingDocWithLocales,
|
||||
skipValidation: skipValidationFromHere,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -331,6 +338,7 @@ export const promise = async ({
|
||||
siblingDoc: tabSiblingDoc,
|
||||
siblingDocWithLocales: tabSiblingDocWithLocales,
|
||||
skipValidation: skipValidationFromHere,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -352,6 +360,7 @@ export const promise = async ({
|
||||
siblingDoc,
|
||||
siblingDocWithLocales,
|
||||
skipValidation: skipValidationFromHere,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Field, TabAsField } from '../../config/types';
|
||||
import { promise } from './promise';
|
||||
import { Operation } from '../../../types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
|
||||
type Args = {
|
||||
data: Record<string, unknown>
|
||||
@@ -18,6 +18,7 @@ type Args = {
|
||||
siblingDoc: Record<string, unknown>
|
||||
siblingDocWithLocales: Record<string, unknown>
|
||||
skipValidation?: boolean
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const traverseFields = async ({
|
||||
@@ -35,6 +36,7 @@ export const traverseFields = async ({
|
||||
siblingDoc,
|
||||
siblingDocWithLocales,
|
||||
skipValidation,
|
||||
context,
|
||||
}: Args): Promise<void> => {
|
||||
const promises = [];
|
||||
|
||||
@@ -54,6 +56,7 @@ export const traverseFields = async ({
|
||||
siblingDoc,
|
||||
siblingDocWithLocales,
|
||||
skipValidation,
|
||||
context,
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { SanitizedCollectionConfig } from '../../../collections/config/types';
|
||||
import { SanitizedGlobalConfig } from '../../../globals/config/types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
import deepCopyObject from '../../../utilities/deepCopyObject';
|
||||
|
||||
@@ -12,6 +12,7 @@ type Args<T> = {
|
||||
operation: 'create' | 'update'
|
||||
overrideAccess: boolean
|
||||
req: PayloadRequest
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const beforeValidate = async <T extends Record<string, unknown>>({
|
||||
@@ -22,6 +23,7 @@ export const beforeValidate = async <T extends Record<string, unknown>>({
|
||||
operation,
|
||||
overrideAccess,
|
||||
req,
|
||||
context,
|
||||
}: Args<T>): Promise<T> => {
|
||||
const data = deepCopyObject(incomingData);
|
||||
|
||||
@@ -35,6 +37,7 @@ export const beforeValidate = async <T extends Record<string, unknown>>({
|
||||
req,
|
||||
siblingData: data,
|
||||
siblingDoc: doc,
|
||||
context,
|
||||
});
|
||||
|
||||
return data;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Field, fieldAffectsData, TabAsField, tabHasName, valueIsValueWithRelation } from '../../config/types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
|
||||
@@ -13,6 +13,7 @@ type Args<T> = {
|
||||
req: PayloadRequest
|
||||
siblingData: Record<string, unknown>
|
||||
siblingDoc: Record<string, unknown>
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
// This function is responsible for the following actions, in order:
|
||||
@@ -30,6 +31,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
}: Args<T>): Promise<void> => {
|
||||
if (fieldAffectsData(field)) {
|
||||
if (field.name === 'id') {
|
||||
@@ -170,6 +172,7 @@ export const promise = async <T>({
|
||||
siblingData,
|
||||
operation,
|
||||
req,
|
||||
context,
|
||||
});
|
||||
|
||||
if (hookedValue !== undefined) {
|
||||
@@ -207,6 +210,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData: groupData,
|
||||
siblingDoc: groupDoc,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -228,6 +232,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData: row,
|
||||
siblingDoc: siblingDoc[field.name]?.[i] || {},
|
||||
context,
|
||||
}));
|
||||
});
|
||||
await Promise.all(promises);
|
||||
@@ -254,6 +259,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData: row,
|
||||
siblingDoc: siblingDoc[field.name]?.[i] || {},
|
||||
context,
|
||||
}));
|
||||
}
|
||||
});
|
||||
@@ -275,6 +281,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -301,6 +308,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData: tabSiblingData,
|
||||
siblingDoc: tabSiblingDoc,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
@@ -317,6 +325,7 @@ export const promise = async <T>({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { PayloadRequest, RequestContext } from '../../../express/types';
|
||||
import { Field, TabAsField } from '../../config/types';
|
||||
import { promise } from './promise';
|
||||
|
||||
@@ -12,6 +12,7 @@ type Args<T> = {
|
||||
req: PayloadRequest
|
||||
siblingData: Record<string, unknown>
|
||||
siblingDoc: Record<string, unknown>
|
||||
context: RequestContext
|
||||
}
|
||||
|
||||
export const traverseFields = async <T>({
|
||||
@@ -24,6 +25,7 @@ export const traverseFields = async <T>({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
}: Args<T>): Promise<void> => {
|
||||
const promises = [];
|
||||
fields.forEach((field) => {
|
||||
@@ -37,6 +39,7 @@ export const traverseFields = async <T>({
|
||||
req,
|
||||
siblingData,
|
||||
siblingDoc,
|
||||
context,
|
||||
}));
|
||||
});
|
||||
await Promise.all(promises);
|
||||
|
||||
@@ -112,6 +112,7 @@ async function findOne<T extends Record<string, unknown>>(args: Args): Promise<T
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -106,6 +106,7 @@ async function findVersionByID<T extends TypeWithVersion<T> = any>(args: Argumen
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -107,6 +107,7 @@ async function findVersions<T extends TypeWithVersion<T>>(
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
findMany: true,
|
||||
context: req.context,
|
||||
}),
|
||||
}))),
|
||||
} as PaginatedDocs<T>;
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Document } from '../../../types';
|
||||
import findOne from '../findOne';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['globals']> = {
|
||||
slug: T
|
||||
@@ -50,6 +51,7 @@ export default async function findOneLocal<T extends keyof GeneratedTypes['globa
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req);
|
||||
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { TypeWithVersion } from '../../../versions/types';
|
||||
import findVersionByID from '../findVersionByID';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['globals']> = {
|
||||
slug: T
|
||||
@@ -52,6 +53,7 @@ export default async function findVersionByIDLocal<T extends keyof GeneratedType
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req);
|
||||
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import { getDataLoader } from '../../../collections/dataloader';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { TypeWithVersion } from '../../../versions/types';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['globals']> = {
|
||||
slug: T
|
||||
@@ -57,6 +58,7 @@ export default async function findVersionsLocal<T extends keyof GeneratedTypes['
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req);
|
||||
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Document } from '../../../types';
|
||||
import restoreVersion from '../restoreVersion';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<T extends keyof GeneratedTypes['globals']> = {
|
||||
slug: string
|
||||
@@ -49,6 +50,7 @@ export default async function restoreVersionLocal<T extends keyof GeneratedTypes
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req);
|
||||
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import update from '../update';
|
||||
import { getDataLoader } from '../../../collections/dataloader';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { APIError } from '../../../errors';
|
||||
import { setRequestContext } from '../../../express/setRequestContext';
|
||||
|
||||
export type Options<TSlug extends keyof GeneratedTypes['globals']> = {
|
||||
slug: TSlug
|
||||
@@ -52,6 +53,7 @@ export default async function updateLocal<TSlug extends keyof GeneratedTypes['gl
|
||||
i18n,
|
||||
t: i18n.t,
|
||||
} as PayloadRequest;
|
||||
setRequestContext(req);
|
||||
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
|
||||
@@ -106,6 +106,7 @@ async function restoreVersion<T extends TypeWithVersion<T> = any>(args: Argument
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -132,6 +133,7 @@ async function restoreVersion<T extends TypeWithVersion<T> = any>(args: Argument
|
||||
entityConfig: globalConfig,
|
||||
operation: 'update',
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -100,6 +100,7 @@ async function update<TSlug extends keyof GeneratedTypes['globals']>(
|
||||
req,
|
||||
overrideAccess: true,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -113,6 +114,7 @@ async function update<TSlug extends keyof GeneratedTypes['globals']>(
|
||||
operation: 'update',
|
||||
overrideAccess,
|
||||
req,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -155,6 +157,7 @@ async function update<TSlug extends keyof GeneratedTypes['globals']>(
|
||||
operation: 'update',
|
||||
req,
|
||||
skipValidation: shouldSaveDraft,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -207,6 +210,7 @@ async function update<TSlug extends keyof GeneratedTypes['globals']>(
|
||||
req,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
context: req.context,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -232,6 +236,7 @@ async function update<TSlug extends keyof GeneratedTypes['globals']>(
|
||||
previousDoc: originalDoc,
|
||||
entityConfig: globalConfig,
|
||||
operation: 'update',
|
||||
context: req.context,
|
||||
req,
|
||||
});
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ const errorHandler = async (
|
||||
};
|
||||
|
||||
if (afterErrorHook) {
|
||||
({ response } = await afterErrorHook(err, response) || { response });
|
||||
({ response } = await afterErrorHook(err, response, null) || { response });
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
@@ -3,6 +3,8 @@ import { InitOptions } from './config/types';
|
||||
import { initHTTP } from './initHTTP';
|
||||
import { Payload as LocalPayload, BasePayload } from './payload';
|
||||
|
||||
import type { RequestContext } from './express/types';
|
||||
|
||||
export { getPayload } from './payload';
|
||||
|
||||
require('isomorphic-fetch');
|
||||
@@ -25,3 +27,5 @@ const payload = new Payload();
|
||||
|
||||
export default payload;
|
||||
module.exports = payload;
|
||||
// Export RequestContext type
|
||||
export type { RequestContext };
|
||||
|
||||
Reference in New Issue
Block a user