Chore/overview docs (#6906)

## Description

More progress to docs.
This commit is contained in:
James Mikrut
2024-06-24 13:17:50 -04:00
committed by GitHub
parent 7f753fb3b5
commit effba3e45b
14 changed files with 307 additions and 201 deletions

View File

@@ -34,7 +34,7 @@ All collection Hook properties accept arrays of synchronous or asynchronous func
`collections/exampleHooks.js`
```ts
import { CollectionConfig } from 'payload/types';
import type { CollectionConfig } from 'payload';
export const ExampleHooks: CollectionConfig = {
slug: 'example-hooks',
@@ -70,7 +70,7 @@ The `beforeOperation` hook can be used to modify the arguments that operations a
Available Collection operations include `create`, `read`, `update`, `delete`, `login`, `refresh`, and `forgotPassword`.
```ts
import { CollectionBeforeOperationHook } from 'payload/types'
import type { CollectionBeforeOperationHook } from 'payload'
const beforeOperationHook: CollectionBeforeOperationHook = async ({
args, // original arguments passed into the operation
@@ -92,7 +92,7 @@ Please do note that this does not run before the client-side validation. If you
3. `validate` runs on the server
```ts
import { CollectionBeforeValidateHook } from 'payload/types'
import type { CollectionBeforeValidateHook } from 'payload'
const beforeValidateHook: CollectionBeforeValidateHook = async ({
data, // incoming data to update or create with
@@ -109,7 +109,7 @@ const beforeValidateHook: CollectionBeforeValidateHook = async ({
Immediately following validation, `beforeChange` hooks will run within `create` and `update` operations. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.
```ts
import { CollectionBeforeChangeHook } from 'payload/types'
import type { CollectionBeforeChangeHook } from 'payload'
const beforeChangeHook: CollectionBeforeChangeHook = async ({
data, // incoming data to update or create with
@@ -126,7 +126,7 @@ const beforeChangeHook: CollectionBeforeChangeHook = async ({
After a document is created or updated, the `afterChange` hook runs. This hook is helpful to recalculate statistics such as total sales within a global, syncing user profile changes to a CRM, and more.
```ts
import { CollectionAfterChangeHook } from 'payload/types'
import type { CollectionAfterChangeHook } from 'payload'
const afterChangeHook: CollectionAfterChangeHook = async ({
doc, // full document data
@@ -143,7 +143,7 @@ const afterChangeHook: CollectionAfterChangeHook = async ({
Runs before `find` and `findByID` operations are transformed for output by `afterRead`. This hook fires before hidden fields are removed and before localized fields are flattened into the requested locale. Using this Hook will provide you with all locales and all hidden fields via the `doc` argument.
```ts
import { CollectionBeforeReadHook } from 'payload/types'
import type { CollectionBeforeReadHook } from 'payload'
const beforeReadHook: CollectionBeforeReadHook = async ({
doc, // full document data
@@ -159,7 +159,7 @@ const beforeReadHook: CollectionBeforeReadHook = async ({
Runs as the last step before documents are returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.
```ts
import { CollectionAfterReadHook } from 'payload/types'
import type { CollectionAfterReadHook } from 'payload'
const afterReadHook: CollectionAfterReadHook = async ({
doc, // full document data
@@ -176,7 +176,7 @@ const afterReadHook: CollectionAfterReadHook = async ({
Runs before the `delete` operation. Returned values are discarded.
```ts
import { CollectionBeforeDeleteHook } from 'payload/types';
import type { CollectionBeforeDeleteHook } from 'payload';
const beforeDeleteHook: CollectionBeforeDeleteHook = async ({
req, // full Request object
@@ -189,7 +189,7 @@ const beforeDeleteHook: CollectionBeforeDeleteHook = async ({
Runs immediately after the `delete` operation removes records from the database. Returned values are discarded.
```ts
import { CollectionAfterDeleteHook } from 'payload/types';
import type { CollectionAfterDeleteHook } from 'payload';
const afterDeleteHook: CollectionAfterDeleteHook = async ({
req, // full Request object
@@ -205,7 +205,7 @@ The `afterOperation` hook can be used to modify the result of operations or exec
Available Collection operations include `create`, `find`, `findByID`, `update`, `updateByID`, `delete`, `deleteByID`, `login`, `refresh`, and `forgotPassword`.
```ts
import { CollectionAfterOperationHook } from 'payload/types'
import type { CollectionAfterOperationHook } from 'payload'
const afterOperationHook: CollectionAfterOperationHook = async ({
args, // arguments passed into the operation
@@ -222,7 +222,7 @@ const afterOperationHook: CollectionAfterOperationHook = async ({
For auth-enabled Collections, this hook runs during `login` operations where a user with the provided credentials exist, but before a token is generated and added to the response. You can optionally modify the user that is returned, or throw an error in order to deny the login operation.
```ts
import { CollectionBeforeLoginHook } from 'payload/types'
import type { CollectionBeforeLoginHook } from 'payload'
const beforeLoginHook: CollectionBeforeLoginHook = async ({
req, // full Request object
@@ -237,7 +237,7 @@ const beforeLoginHook: CollectionBeforeLoginHook = async ({
For auth-enabled Collections, this hook runs after successful `login` operations. You can optionally modify the user that is returned.
```ts
import { CollectionAfterLoginHook } from 'payload/types';
import type { CollectionAfterLoginHook } from 'payload';
const afterLoginHook: CollectionAfterLoginHook = async ({
req, // full Request object
@@ -251,7 +251,7 @@ const afterLoginHook: CollectionAfterLoginHook = async ({
For auth-enabled Collections, this hook runs after `logout` operations.
```ts
import { CollectionAfterLogoutHook } from 'payload/types';
import type { CollectionAfterLogoutHook } from 'payload';
const afterLogoutHook: CollectionAfterLogoutHook = async ({
req, // full Request object
@@ -263,7 +263,7 @@ const afterLogoutHook: CollectionAfterLogoutHook = async ({
For auth-enabled Collections, this hook runs after `refresh` operations.
```ts
import { CollectionAfterRefreshHook } from 'payload/types';
import type { CollectionAfterRefreshHook } from 'payload';
const afterRefreshHook: CollectionAfterRefreshHook = async ({
req, // full Request object
@@ -277,7 +277,7 @@ const afterRefreshHook: CollectionAfterRefreshHook = async ({
For auth-enabled Collections, this hook runs after `me` operations.
```ts
import { CollectionAfterMeHook } from 'payload/types';
import type { CollectionAfterMeHook } from 'payload';
const afterMeHook: CollectionAfterMeHook = async ({
req, // full Request object
@@ -290,7 +290,7 @@ const afterMeHook: CollectionAfterMeHook = async ({
For auth-enabled Collections, this hook runs after successful `forgotPassword` operations. Returned values are discarded.
```ts
import { CollectionAfterForgotPasswordHook } from 'payload/types'
import type { CollectionAfterForgotPasswordHook } from 'payload'
const afterForgotPasswordHook: CollectionAfterForgotPasswordHook = async ({
args, // arguments passed into the operation
@@ -319,5 +319,5 @@ import type {
CollectionAfterRefreshHook,
CollectionAfterMeHook,
CollectionAfterForgotPasswordHook,
} from 'payload/types'
} from 'payload'
```

View File

@@ -30,7 +30,7 @@ functionalities to be easily reusable across your projects.
Example field configuration:
```ts
import { Field } from 'payload/types';
import type { Field } from 'payload';
const ExampleField: Field = {
name: 'name',
@@ -107,7 +107,7 @@ Runs before the `update` operation. This hook allows you to pre-process or forma
validation.
```ts
import { Field } from 'payload/types'
import type { Field } from 'payload'
const usernameField: Field = {
name: 'username',
@@ -134,7 +134,7 @@ you can be confident that the field data that will be saved to the document is v
validations.
```ts
import { Field } from 'payload/types'
import type { Field } from 'payload'
const emailField: Field = {
name: 'email',
@@ -162,7 +162,7 @@ The `afterChange` hook is executed after a field's value has been changed and sa
for post-processing or triggering side effects based on the new value of the field.
```ts
import { Field } from 'payload/types'
import type { Field } from 'payload'
const membershipStatusField: Field = {
name: 'membershipStatus',
@@ -199,7 +199,7 @@ The `afterRead` hook is invoked after a field value is read from the database. T
transforming the field data for output.
```ts
import { Field } from 'payload/types'
import type { Field } from 'payload'
const dateField: Field = {
name: 'createdAt',
@@ -230,7 +230,7 @@ By Default, unique and required text fields Payload will append "- Copy" to the
Here is an example of a number field with a hook that increments the number to avoid unique constraint errors when duplicating a document:
```ts
import { Field } from 'payload/types'
import type { Field } from 'payload'
const numberField: Field = {
name: 'number',
@@ -249,7 +249,7 @@ const numberField: Field = {
Payload exports a type for field hooks which can be accessed and used as follows:
```ts
import type { FieldHook } from 'payload/types'
import type { FieldHook } from 'payload'
// Field hook type is a generic that takes three arguments:
// 1: The document type

View File

@@ -21,7 +21,7 @@ All Global Hook properties accept arrays of synchronous or asynchronous function
`globals/example-hooks.js`
```ts
import { GlobalConfig } from 'payload/types';
import type { GlobalConfig } from 'payload';
const ExampleHooks: GlobalConfig = {
slug: 'header',
@@ -43,7 +43,7 @@ const ExampleHooks: GlobalConfig = {
Runs before the `update` operation. This hook allows you to add or format data before the incoming data is validated.
```ts
import { GlobalBeforeValidateHook } from 'payload/types'
import type { GlobalBeforeValidateHook } from 'payload'
const beforeValidateHook: GlobalBeforeValidateHook = async ({
data, // incoming data to update or create with
@@ -59,7 +59,7 @@ const beforeValidateHook: GlobalBeforeValidateHook = async ({
Immediately following validation, `beforeChange` hooks will run within the `update` operation. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.
```ts
import { GlobalBeforeChangeHook } from 'payload/types'
import type { GlobalBeforeChangeHook } from 'payload'
const beforeChangeHook: GlobalBeforeChangeHook = async ({
data, // incoming data to update or create with
@@ -75,7 +75,7 @@ const beforeChangeHook: GlobalBeforeChangeHook = async ({
After a global is updated, the `afterChange` hook runs. Use this hook to purge caches of your applications, sync site data to CRMs, and more.
```ts
import { GlobalAfterChangeHook } from 'payload/types'
import type { GlobalAfterChangeHook } from 'payload'
const afterChangeHook: GlobalAfterChangeHook = async ({
doc, // full document data
@@ -91,7 +91,7 @@ const afterChangeHook: GlobalAfterChangeHook = async ({
Runs before `findOne` global operation is transformed for output by `afterRead`. This hook fires before hidden fields are removed and before localized fields are flattened into the requested locale. Using this Hook will provide you with all locales and all hidden fields via the `doc` argument.
```ts
import { GlobalBeforeReadHook } from 'payload/types'
import type { GlobalBeforeReadHook } from 'payload'
const beforeReadHook: GlobalBeforeReadHook = async ({
doc, // full document data
@@ -104,7 +104,7 @@ const beforeReadHook: GlobalBeforeReadHook = async ({
Runs as the last step before a global is returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.
```ts
import { GlobalAfterReadHook } from 'payload/types'
import type { GlobalAfterReadHook } from 'payload'
const afterReadHook: GlobalAfterReadHook = async ({
doc, // full document data
@@ -124,5 +124,5 @@ import type {
GlobalAfterChangeHook,
GlobalBeforeReadHook,
GlobalAfterReadHook,
} from 'payload/types'
} from 'payload'
```