feat: global beforeOperation hook (#13768)

Adds support for the `beforeOperation` hook on globals. Runs before all
other hooks to either modify the arguments that operations receive, or
perform side-effects before an operation begins.

```ts
import type { GlobalConfig } from 'payload'

const MyGlobal: GlobalConfig = {
  // ...
  hooks: {
    beforeOperation: []
  }
}
```

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1211317005907890
This commit is contained in:
Jacob Fletcher
2025-09-10 16:46:49 -04:00
committed by GitHub
parent 4482eaf9ad
commit 3af546eeee
11 changed files with 143 additions and 18 deletions

View File

@@ -277,7 +277,7 @@ describe('Hooks', () => {
const document = await payload.create({
collection: contextHooksSlug,
context: {
secretValue: 'data from local API',
secretValue: 'data from Local API',
},
data: {
value: 'wrongvalue',
@@ -289,28 +289,28 @@ describe('Hooks', () => {
collection: contextHooksSlug,
})
expect(retrievedDoc.value).toEqual('data from local API')
expect(retrievedDoc.value).toEqual('data from Local API')
})
it('should pass context from local API to global hooks', async () => {
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')
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',
field_beforeChange_GlobalAndField_override: 'data from Local API context',
},
})
expect(globalDocumentWithContext.field_globalAndField).toEqual('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 () => {
it('should pass context from REST API to hooks', async () => {
const params = new URLSearchParams({
context_secretValue: 'data from rest API',
context_secretValue: 'data from REST API',
})
// send context as query params. It will be parsed by the beforeOperation hook
const { doc } = await restClient
@@ -326,7 +326,7 @@ describe('Hooks', () => {
id: doc.id,
})
expect(retrievedDoc.value).toEqual('data from rest API')
expect(retrievedDoc.value).toEqual('data from REST API')
})
})
@@ -426,15 +426,19 @@ describe('Hooks', () => {
expect(JSON.parse(doc.collection_beforeOperation_collection)).toStrictEqual(
sanitizedHooksCollection,
)
expect(JSON.parse(doc.collection_beforeChange_collection)).toStrictEqual(
sanitizedHooksCollection,
)
expect(JSON.parse(doc.collection_afterChange_collection)).toStrictEqual(
sanitizedHooksCollection,
)
expect(JSON.parse(doc.collection_afterRead_collection)).toStrictEqual(
sanitizedHooksCollection,
)
expect(JSON.parse(doc.collection_afterOperation_collection)).toStrictEqual(
sanitizedHooksCollection,
)