feat(plugin-redirects)!: update fields overrides to use a function (#6675)

## Description

Updates the `fields` override in plugin redirects to allow for
overriding

```ts
// before
overrides: {
  fields: [
    {
      type: 'text',
      name: 'customField',
    },
  ],
},

// current
overrides: {
  fields: ({ defaultFields }) => {
    return [
      ...defaultFields,
      {
        type: 'text',
        name: 'customField',
      },
    ]
  },
},
```


## Type of change

- [x] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
This commit is contained in:
Paul
2024-06-07 10:41:09 -04:00
committed by GitHub
parent 7c8d562f03
commit e4a90294ea
4 changed files with 92 additions and 108 deletions

View File

@@ -1,34 +0,0 @@
// @ts-nocheck
/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item: unknown): boolean {
return item && typeof item === 'object' && !Array.isArray(item)
}
/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
export default function deepMerge<T, R>(target: T, source: R): T {
const output = { ...target }
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] })
} else {
output[key] = deepMerge(target[key], source[key])
}
} else {
Object.assign(output, { [key]: source[key] })
}
})
}
return output
}

View File

@@ -1,80 +1,85 @@
import type { Config } from 'payload/config' import type { Config } from 'payload/config'
import type { CollectionConfig, Field } from 'payload/types'
import type { RedirectsPluginConfig } from './types.js' import type { RedirectsPluginConfig } from './types.js'
import deepMerge from './deepMerge.js'
export const redirectsPlugin = export const redirectsPlugin =
(pluginConfig: RedirectsPluginConfig) => (pluginConfig: RedirectsPluginConfig) =>
(incomingConfig: Config): Config => ({ (incomingConfig: Config): Config => {
...incomingConfig, const defaultFields: Field[] = [
collections: [ {
...(incomingConfig?.collections || []), name: 'from',
deepMerge( type: 'text',
{ index: true,
slug: 'redirects', label: 'From URL',
access: { required: true,
read: (): boolean => true, },
}, {
admin: { name: 'to',
defaultColumns: ['from', 'to.type', 'createdAt'], type: 'group',
}, fields: [
fields: [ {
{ name: 'type',
name: 'from', type: 'radio',
type: 'text', admin: {
index: true, layout: 'horizontal',
label: 'From URL',
required: true,
}, },
{ defaultValue: 'reference',
name: 'to', label: 'To URL Type',
type: 'group', options: [
fields: [ {
{ label: 'Internal link',
name: 'type', value: 'reference',
type: 'radio', },
admin: { {
layout: 'horizontal', label: 'Custom URL',
}, value: 'custom',
defaultValue: 'reference', },
label: 'To URL Type', ],
options: [ },
{ {
label: 'Internal link', name: 'reference',
value: 'reference', type: 'relationship',
}, admin: {
{ condition: (_, siblingData) => siblingData?.type === 'reference',
label: 'Custom URL',
value: 'custom',
},
],
},
{
name: 'reference',
type: 'relationship',
admin: {
condition: (_, siblingData) => siblingData?.type === 'reference',
},
label: 'Document to redirect to',
relationTo: pluginConfig?.collections || [],
required: true,
},
{
name: 'url',
type: 'text',
admin: {
condition: (_, siblingData) => siblingData?.type === 'custom',
},
label: 'Custom URL',
required: true,
},
],
label: false,
}, },
], label: 'Document to redirect to',
}, relationTo: pluginConfig?.collections || [],
pluginConfig?.overrides || {}, required: true,
), },
], {
}) name: 'url',
type: 'text',
admin: {
condition: (_, siblingData) => siblingData?.type === 'custom',
},
label: 'Custom URL',
required: true,
},
],
label: false,
},
]
const redirectsCollection: CollectionConfig = {
...(pluginConfig?.overrides || {}),
slug: pluginConfig?.overrides?.slug || 'redirects',
access: {
read: () => true,
...(pluginConfig?.overrides?.access || {}),
},
admin: {
defaultColumns: ['from', 'to.type', 'createdAt'],
...(pluginConfig?.overrides?.admin || {}),
},
fields:
pluginConfig?.overrides?.fields && typeof pluginConfig?.overrides?.fields === 'function'
? pluginConfig?.overrides.fields({ defaultFields })
: defaultFields,
}
return {
...incomingConfig,
collections: [...(incomingConfig?.collections || []), redirectsCollection],
}
}

View File

@@ -1,6 +1,8 @@
import type { CollectionConfig } from 'payload/types' import type { CollectionConfig, Field } from 'payload/types'
export type FieldsOverride = (args: { defaultFields: Field[] }) => Field[]
export type RedirectsPluginConfig = { export type RedirectsPluginConfig = {
collections?: string[] collections?: string[]
overrides?: Partial<CollectionConfig> overrides?: Partial<Omit<CollectionConfig, 'fields'>> & { fields: FieldsOverride }
} }

View File

@@ -27,6 +27,17 @@ export default buildConfigWithDefaults({
plugins: [ plugins: [
redirectsPlugin({ redirectsPlugin({
collections: ['pages'], collections: ['pages'],
overrides: {
fields: ({ defaultFields }) => {
return [
...defaultFields,
{
type: 'text',
name: 'customField',
},
]
},
},
}), }),
], ],
}) })