|
1 | 1 | import type { Config } from 'payload/config'
|
| 2 | +import type { CollectionConfig, Field } from 'payload/types' |
2 | 3 |
|
3 | 4 | import type { RedirectsPluginConfig } from './types.js'
|
4 | 5 |
|
5 |
| -import deepMerge from './deepMerge.js' |
6 |
| - |
7 | 6 | export const redirectsPlugin =
|
8 | 7 | (pluginConfig: RedirectsPluginConfig) =>
|
9 |
| - (incomingConfig: Config): Config => ({ |
10 |
| - ...incomingConfig, |
11 |
| - collections: [ |
12 |
| - ...(incomingConfig?.collections || []), |
13 |
| - deepMerge( |
14 |
| - { |
15 |
| - slug: 'redirects', |
16 |
| - access: { |
17 |
| - read: (): boolean => true, |
18 |
| - }, |
19 |
| - admin: { |
20 |
| - defaultColumns: ['from', 'to.type', 'createdAt'], |
| 8 | + (incomingConfig: Config): Config => { |
| 9 | + const defaultFields: Field[] = [ |
| 10 | + { |
| 11 | + name: 'from', |
| 12 | + type: 'text', |
| 13 | + index: true, |
| 14 | + label: 'From URL', |
| 15 | + required: true, |
| 16 | + }, |
| 17 | + { |
| 18 | + name: 'to', |
| 19 | + type: 'group', |
| 20 | + fields: [ |
| 21 | + { |
| 22 | + name: 'type', |
| 23 | + type: 'radio', |
| 24 | + admin: { |
| 25 | + layout: 'horizontal', |
| 26 | + }, |
| 27 | + defaultValue: 'reference', |
| 28 | + label: 'To URL Type', |
| 29 | + options: [ |
| 30 | + { |
| 31 | + label: 'Internal link', |
| 32 | + value: 'reference', |
| 33 | + }, |
| 34 | + { |
| 35 | + label: 'Custom URL', |
| 36 | + value: 'custom', |
| 37 | + }, |
| 38 | + ], |
21 | 39 | },
|
22 |
| - fields: [ |
23 |
| - { |
24 |
| - name: 'from', |
25 |
| - type: 'text', |
26 |
| - index: true, |
27 |
| - label: 'From URL', |
28 |
| - required: true, |
| 40 | + { |
| 41 | + name: 'reference', |
| 42 | + type: 'relationship', |
| 43 | + admin: { |
| 44 | + condition: (_, siblingData) => siblingData?.type === 'reference', |
29 | 45 | },
|
30 |
| - { |
31 |
| - name: 'to', |
32 |
| - type: 'group', |
33 |
| - fields: [ |
34 |
| - { |
35 |
| - name: 'type', |
36 |
| - type: 'radio', |
37 |
| - admin: { |
38 |
| - layout: 'horizontal', |
39 |
| - }, |
40 |
| - defaultValue: 'reference', |
41 |
| - label: 'To URL Type', |
42 |
| - options: [ |
43 |
| - { |
44 |
| - label: 'Internal link', |
45 |
| - value: 'reference', |
46 |
| - }, |
47 |
| - { |
48 |
| - label: 'Custom URL', |
49 |
| - value: 'custom', |
50 |
| - }, |
51 |
| - ], |
52 |
| - }, |
53 |
| - { |
54 |
| - name: 'reference', |
55 |
| - type: 'relationship', |
56 |
| - admin: { |
57 |
| - condition: (_, siblingData) => siblingData?.type === 'reference', |
58 |
| - }, |
59 |
| - label: 'Document to redirect to', |
60 |
| - relationTo: pluginConfig?.collections || [], |
61 |
| - required: true, |
62 |
| - }, |
63 |
| - { |
64 |
| - name: 'url', |
65 |
| - type: 'text', |
66 |
| - admin: { |
67 |
| - condition: (_, siblingData) => siblingData?.type === 'custom', |
68 |
| - }, |
69 |
| - label: 'Custom URL', |
70 |
| - required: true, |
71 |
| - }, |
72 |
| - ], |
73 |
| - label: false, |
| 46 | + label: 'Document to redirect to', |
| 47 | + relationTo: pluginConfig?.collections || [], |
| 48 | + required: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'url', |
| 52 | + type: 'text', |
| 53 | + admin: { |
| 54 | + condition: (_, siblingData) => siblingData?.type === 'custom', |
74 | 55 | },
|
75 |
| - ], |
76 |
| - }, |
77 |
| - pluginConfig?.overrides || {}, |
78 |
| - ), |
79 |
| - ], |
80 |
| - }) |
| 56 | + label: 'Custom URL', |
| 57 | + required: true, |
| 58 | + }, |
| 59 | + ], |
| 60 | + label: false, |
| 61 | + }, |
| 62 | + ] |
| 63 | + |
| 64 | + const redirectsCollection: CollectionConfig = { |
| 65 | + ...(pluginConfig?.overrides || {}), |
| 66 | + slug: pluginConfig?.overrides?.slug || 'redirects', |
| 67 | + access: { |
| 68 | + read: () => true, |
| 69 | + ...(pluginConfig?.overrides?.access || {}), |
| 70 | + }, |
| 71 | + admin: { |
| 72 | + defaultColumns: ['from', 'to.type', 'createdAt'], |
| 73 | + ...(pluginConfig?.overrides?.admin || {}), |
| 74 | + }, |
| 75 | + fields: |
| 76 | + pluginConfig?.overrides?.fields && typeof pluginConfig?.overrides?.fields === 'function' |
| 77 | + ? pluginConfig?.overrides.fields({ defaultFields }) |
| 78 | + : defaultFields, |
| 79 | + } |
| 80 | + |
| 81 | + return { |
| 82 | + ...incomingConfig, |
| 83 | + collections: [...(incomingConfig?.collections || []), redirectsCollection], |
| 84 | + } |
| 85 | + } |
0 commit comments