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

@@ -18,6 +18,61 @@ export default buildConfigWithDefaults({
cors: ['http://localhost:3000', 'http://localhost:3001'],
globals: [
MenuGlobal,
{
slug: 'custom-ts',
fields: [
{
name: 'custom',
type: 'text',
typescriptSchema: [
() => ({
enum: ['hello', 'world'],
}),
],
},
{
name: 'withDefinitionsUsage',
type: 'text',
typescriptSchema: [
() => ({
type: 'array',
items: {
$ref: `#/definitions/objectWithNumber`,
},
}),
],
},
{
name: 'json',
type: 'json',
required: true,
jsonSchema: {
uri: 'a://b/foo.json',
fileMatch: ['a://b/foo.json'],
schema: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
id: {
type: 'string',
},
name: {
type: 'string',
},
age: {
type: 'integer',
},
// Add other properties here
},
required: ['id', 'name'], // Specify which properties are required
},
},
},
},
],
},
// ...add more globals here
],
onInit: async (payload) => {
@@ -48,5 +103,21 @@ export default buildConfigWithDefaults({
},
typescript: {
outputFile: path.resolve(dirname, 'payload-types.ts'),
schema: [
({ jsonSchema }) => {
jsonSchema.definitions.objectWithNumber = {
type: 'object',
properties: {
id: {
type: 'number',
required: true,
},
},
required: true,
additionalProperties: false,
}
return jsonSchema
},
],
},
})

View File

@@ -18,6 +18,7 @@ export interface Config {
};
globals: {
menu: Menu;
'custom-ts': CustomT;
};
locale: null;
user: User & {
@@ -149,6 +150,6 @@ export interface Auth {
declare module 'payload' {
// @ts-ignore
// @ts-ignore
export interface GeneratedTypes extends Config {}
}
}