feat: add context to auth and globals local API (#4449)

* feat: add context to auth and globals local API

* chore: add int test
This commit is contained in:
Alessio Gravili
2023-12-11 20:27:34 +01:00
committed by GitHub
parent b9c0248823
commit 168d629697
12 changed files with 67 additions and 23 deletions

View File

@@ -67,6 +67,10 @@ export const DataHooksGlobal: GlobalConfig = {
afterRead: [
({ global, field, context }) => {
if (context['field_beforeChange_GlobalAndField_override']) {
return context['field_beforeChange_GlobalAndField_override']
}
return (
(context['field_beforeChange_GlobalAndField'] as string) +
JSON.stringify(global) +

View File

@@ -251,6 +251,22 @@ describe('Hooks', () => {
expect(retrievedDoc.value).toEqual('data from local API')
})
it('should pass context from local API to global hooks', async () => {
const globalDocument = await payload.findGlobal({
slug: dataHooksGlobalSlug,
})
expect(globalDocument.field_globalAndField).not.toEqual('data from local API context')
const globalDocumentWithContext = await payload.findGlobal({
slug: dataHooksGlobalSlug,
context: {
field_beforeChange_GlobalAndField_override: 'data from local API context',
},
})
expect(globalDocumentWithContext.field_globalAndField).toEqual('data from local API context')
})
it('should pass context from rest API to hooks', async () => {
const params = new URLSearchParams({
context_secretValue: 'data from rest API',