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:
@@ -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
|
|
||||||
}
|
|
||||||
@@ -1,25 +1,12 @@
|
|||||||
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 || []),
|
|
||||||
deepMerge(
|
|
||||||
{
|
|
||||||
slug: 'redirects',
|
|
||||||
access: {
|
|
||||||
read: (): boolean => true,
|
|
||||||
},
|
|
||||||
admin: {
|
|
||||||
defaultColumns: ['from', 'to.type', 'createdAt'],
|
|
||||||
},
|
|
||||||
fields: [
|
|
||||||
{
|
{
|
||||||
name: 'from',
|
name: 'from',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
@@ -72,9 +59,27 @@ export const redirectsPlugin =
|
|||||||
],
|
],
|
||||||
label: false,
|
label: false,
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
|
|
||||||
|
const redirectsCollection: CollectionConfig = {
|
||||||
|
...(pluginConfig?.overrides || {}),
|
||||||
|
slug: pluginConfig?.overrides?.slug || 'redirects',
|
||||||
|
access: {
|
||||||
|
read: () => true,
|
||||||
|
...(pluginConfig?.overrides?.access || {}),
|
||||||
},
|
},
|
||||||
pluginConfig?.overrides || {},
|
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],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,17 @@ export default buildConfigWithDefaults({
|
|||||||
plugins: [
|
plugins: [
|
||||||
redirectsPlugin({
|
redirectsPlugin({
|
||||||
collections: ['pages'],
|
collections: ['pages'],
|
||||||
|
overrides: {
|
||||||
|
fields: ({ defaultFields }) => {
|
||||||
|
return [
|
||||||
|
...defaultFields,
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'customField',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user