feat: allow users/plugins to modify and extend generated types for fields & config, add generated types for json field (#6984)

- Improves type for `jsonSchema` property of JSON field
- Adds type generation of JSON field with `jsonSchema`
- Adds `typescriptSchema` property to fields that allows you override
default field type generation by providing a JSON schema.
- Adds `typescript.schema` property in payload config, to allow for any
modifications of the type schemas

---------

Co-authored-by: Alessio Gravili <alessio@gravili.de>
This commit is contained in:
Ritsu
2024-07-02 19:48:21 +03:00
committed by GitHub
parent 955b845725
commit eb2f7631f7
27 changed files with 528 additions and 309 deletions

View File

@@ -61,16 +61,55 @@ You can specify where you want your types to be generated by adding a property t
The above example places your types next to your Payload config itself as the file `generated-types.ts`.
## Custom generated types
Payload generates your types based on a JSON schema. You can extend that JSON schema, and thus the generated types, by passing a function to `typescript.schema`:
```ts
// payload.config.ts
{
// ...
typescript: {
schema: [
({ jsonSchema }) => {
// Modify the JSON schema here
jsonSchema.definitions.Test = {
type: 'object',
properties: {
title: { type: 'string' },
content: { type: 'string' },
},
required: ['title', 'content'],
}
return jsonSchema
},
]
}
}
// This will generate the following type in your payload-types.ts:
export interface Test {
title: string;
content: string;
[k: string]: unknown;
}
```
This function takes the existing JSON schema as an argument and returns the modified JSON schema. It can be useful for plugins that wish to generate their own types.
## Example Usage
For example, let's look at the following simple Payload config:
```ts
import type { Config } from 'payload'
const config: Config = {
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
admin: {
user: 'users',
}
},
collections: [
{
slug: 'users',