@klnap/payload-redirect 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -192
- package/dist/core/redirects/auto/create-redirect.js +26 -10
- package/dist/core/redirects/auto/persist-auto-rule.js +3 -2
- package/dist/core/redirects/auto/publication/transition-for-locale.js +5 -1
- package/dist/core/redirects/auto/publication/transitions-to-pairs.d.ts +2 -0
- package/dist/core/redirects/auto/publication/transitions-to-pairs.js +7 -1
- package/dist/core/redirects/auto/published-link-config.js +5 -1
- package/dist/core/redirects/auto/published-path.js +18 -1
- package/dist/core/redirects/auto/redirect-custom.d.ts +7 -0
- package/dist/core/redirects/auto/redirect-custom.js +19 -0
- package/dist/core/redirects/auto/types.d.ts +2 -0
- package/dist/core/redirects/collection/access.d.ts +6 -11
- package/dist/core/redirects/collection/access.js +7 -24
- package/dist/core/redirects/collection/collection.d.ts +2 -10
- package/dist/core/redirects/collection/collection.js +19 -22
- package/dist/core/redirects/collection/duplicate-guard.d.ts +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +5 -8
- package/dist/core/redirects/collection/fields.d.ts +1 -0
- package/dist/core/redirects/collection/fields.js +76 -76
- package/dist/core/redirects/collection/origin-hook.js +7 -14
- package/dist/core/redirects/collection/types.d.ts +7 -0
- package/dist/core/redirects/collection/types.js +1 -0
- package/dist/core/redirects/model/errors.d.ts +0 -6
- package/dist/core/redirects/model/errors.js +0 -6
- package/dist/core/redirects/model/status-code.js +4 -7
- package/dist/core/redirects/queries.d.ts +4 -9
- package/dist/core/redirects/queries.js +5 -9
- package/dist/core/url/published-path.d.ts +1 -0
- package/dist/core/url/published-path.js +31 -2
- package/dist/exports/index.d.ts +1 -7
- package/dist/exports/index.js +0 -4
- package/dist/plugin/payload-plugin.d.ts +4 -3
- package/dist/plugin/payload-plugin.js +140 -43
- package/dist/plugin/types.d.ts +43 -0
- package/dist/plugin/types.js +1 -0
- package/docs/FAILURE_SEMANTICS.md +44 -92
- package/docs/adr/0002-database-indexes.md +3 -4
- package/package.json +4 -3
- package/dist/core/redirects/resolution/resolve.d.ts +0 -7
- package/dist/core/redirects/resolution/resolve.js +0 -27
- package/dist/core/redirects/resolution/resolved.d.ts +0 -17
- package/dist/core/redirects/resolution/resolved.js +0 -18
- package/dist/plugin/redirect-custom.d.ts +0 -9
- package/dist/plugin/redirect-custom.js +0 -18
- package/dist/plugin/resolve-config.d.ts +0 -17
- package/dist/plugin/resolve-config.js +0 -20
- package/dist/plugin/validate-config.d.ts +0 -3
- package/dist/plugin/validate-config.js +0 -40
|
@@ -1,41 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defaultRedirectAccess } from './access.js';
|
|
2
2
|
import { createDuplicateBeforeChange } from './duplicate-guard.js';
|
|
3
|
-
import { buildRedirectFields, mergeRedirectFields } from './fields.js';
|
|
3
|
+
import { assertRedirectFields, buildRedirectFields, mergeRedirectFields } from './fields.js';
|
|
4
4
|
import { createRedirectOriginBeforeChange } from './origin-hook.js';
|
|
5
5
|
const DEFAULT_SLUG = 'redirects';
|
|
6
|
-
export const createRedirectsCollection = (
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
6
|
+
export const createRedirectsCollection = (overrides = {}) => {
|
|
7
|
+
const defaultFields = buildRedirectFields();
|
|
8
|
+
const fields = overrides.fields
|
|
9
|
+
? mergeRedirectFields(overrides.fields({ defaultFields }))
|
|
10
|
+
: defaultFields;
|
|
11
|
+
assertRedirectFields(fields);
|
|
12
|
+
const slug = overrides.slug ?? DEFAULT_SLUG;
|
|
13
|
+
const existingHooks = overrides.hooks ?? {};
|
|
14
|
+
const access = { ...defaultRedirectAccess, ...overrides.access };
|
|
13
15
|
return {
|
|
14
|
-
...
|
|
16
|
+
...overrides,
|
|
15
17
|
slug,
|
|
16
|
-
trash,
|
|
17
|
-
versions:
|
|
18
|
-
timestamps:
|
|
18
|
+
trash: overrides.trash ?? true,
|
|
19
|
+
versions: overrides.versions ?? false,
|
|
20
|
+
timestamps: overrides.timestamps ?? true,
|
|
19
21
|
admin: {
|
|
20
22
|
useAsTitle: 'from',
|
|
21
23
|
defaultColumns: ['from', 'to', 'StatusCode', 'origin', 'enabled', 'updatedAt'],
|
|
22
24
|
listSearchableFields: ['from', 'to'],
|
|
23
|
-
...
|
|
24
|
-
},
|
|
25
|
-
defaultSort: 'from',
|
|
26
|
-
access: {
|
|
27
|
-
read: access.read ?? redirectReadAccess,
|
|
28
|
-
create: access.create ?? redirectCreateAccess,
|
|
29
|
-
update: access.update ?? redirectUpdateAccess,
|
|
30
|
-
delete: access.delete ?? redirectDeleteAccess,
|
|
25
|
+
...overrides.admin,
|
|
31
26
|
},
|
|
27
|
+
defaultSort: overrides.defaultSort ?? 'from',
|
|
28
|
+
access,
|
|
32
29
|
fields,
|
|
33
30
|
hooks: {
|
|
34
31
|
...existingHooks,
|
|
35
32
|
beforeChange: [
|
|
36
33
|
...(existingHooks.beforeChange ?? []),
|
|
37
34
|
createRedirectOriginBeforeChange(),
|
|
38
|
-
createDuplicateBeforeChange(),
|
|
35
|
+
createDuplicateBeforeChange(slug),
|
|
39
36
|
],
|
|
40
37
|
},
|
|
41
38
|
};
|
|
@@ -18,4 +18,4 @@ export declare const resolveDuplicateGuardExcludeId: (operation: string, origina
|
|
|
18
18
|
* loop walk for any update that omits `from` or `to`.
|
|
19
19
|
*/
|
|
20
20
|
export declare const effectiveRuleData: (operation: string, data: Record<string, unknown> | undefined, originalDoc: Record<string, unknown> | null | undefined) => Record<string, unknown> | undefined;
|
|
21
|
-
export declare const createDuplicateBeforeChange: () => CollectionBeforeChangeHook;
|
|
21
|
+
export declare const createDuplicateBeforeChange: (collectionSlug?: string) => CollectionBeforeChangeHook;
|
|
@@ -3,7 +3,6 @@ import { lockRedirectRuleGraph } from './graph-lock.js';
|
|
|
3
3
|
import { assertRedirectStatusCode, DEFAULT_REDIRECT_STATUS_CODE, validateDestinationForStatus, } from '../model/status-code.js';
|
|
4
4
|
import { normalizeRedirectSourcePath, normalizeRelativeRedirectPath, } from '../normalize-path.js';
|
|
5
5
|
import { findRedirectByFrom } from '../queries.js';
|
|
6
|
-
import { getRedirectRuntime } from '../runtime.js';
|
|
7
6
|
/** Enabled rules followed from `to` before a write is refused as unverifiable. Bounds the lookups per write. */
|
|
8
7
|
export const MAX_REDIRECT_CHAIN_HOPS = 1000;
|
|
9
8
|
const DUPLICATE_MESSAGE = 'A redirect rule already exists for this source path';
|
|
@@ -57,7 +56,7 @@ const resolveExcludeId = (args) => resolveDuplicateGuardExcludeId(args.operation
|
|
|
57
56
|
* repeats a hop without reaching `from` predates this write, so it is accepted. Runs past
|
|
58
57
|
* MAX_REDIRECT_CHAIN_HOPS are refused as unverifiable. One indexed lookup on `from` per hop.
|
|
59
58
|
*/
|
|
60
|
-
const assertNoLoop = async (args, normalizedFrom) => {
|
|
59
|
+
const assertNoLoop = async (args, normalizedFrom, collectionSlug) => {
|
|
61
60
|
const { data, req } = args;
|
|
62
61
|
if (!data || data.enabled === false)
|
|
63
62
|
return;
|
|
@@ -67,7 +66,6 @@ const assertNoLoop = async (args, normalizedFrom) => {
|
|
|
67
66
|
if (normalizedTo === normalizedFrom) {
|
|
68
67
|
throw new ValidationError({ errors: [{ message: LOOP_MESSAGE, path: 'to' }] });
|
|
69
68
|
}
|
|
70
|
-
const { collectionSlug } = getRedirectRuntime(req.payload.config);
|
|
71
69
|
const excludeId = resolveExcludeId(args);
|
|
72
70
|
const visited = new Set();
|
|
73
71
|
let current = normalizedTo;
|
|
@@ -97,14 +95,13 @@ const assertNoLoop = async (args, normalizedFrom) => {
|
|
|
97
95
|
}
|
|
98
96
|
throw new ValidationError({ errors: [{ message: CHAIN_TOO_LONG_MESSAGE, path: 'to' }] });
|
|
99
97
|
};
|
|
100
|
-
const assertNoDuplicateFrom = async (args) => {
|
|
98
|
+
const assertNoDuplicateFrom = async (args, collectionSlug) => {
|
|
101
99
|
const { data, req } = args;
|
|
102
100
|
if (!data)
|
|
103
101
|
return undefined;
|
|
104
102
|
const normalizedFrom = resolveNormalizedFrom(data);
|
|
105
103
|
if (!normalizedFrom)
|
|
106
104
|
return undefined;
|
|
107
|
-
const { collectionSlug } = getRedirectRuntime(req.payload.config);
|
|
108
105
|
const duplicate = await findDuplicateFrom({
|
|
109
106
|
req,
|
|
110
107
|
collectionSlug,
|
|
@@ -114,7 +111,7 @@ const assertNoDuplicateFrom = async (args) => {
|
|
|
114
111
|
if (duplicate) {
|
|
115
112
|
throw new ValidationError({ errors: [{ message: DUPLICATE_MESSAGE, path: 'from' }] });
|
|
116
113
|
}
|
|
117
|
-
await assertNoLoop(args, normalizedFrom);
|
|
114
|
+
await assertNoLoop(args, normalizedFrom, collectionSlug);
|
|
118
115
|
return normalizedFrom;
|
|
119
116
|
};
|
|
120
117
|
const normalizeRulePaths = (data) => {
|
|
@@ -162,7 +159,7 @@ export const effectiveRuleData = (operation, data, originalDoc) => {
|
|
|
162
159
|
return data;
|
|
163
160
|
return { ...originalDoc, ...data };
|
|
164
161
|
};
|
|
165
|
-
export const createDuplicateBeforeChange = () => {
|
|
162
|
+
export const createDuplicateBeforeChange = (collectionSlug = 'redirects') => {
|
|
166
163
|
return async (args) => {
|
|
167
164
|
const { data, req, operation, originalDoc } = args;
|
|
168
165
|
if (operation === 'create' || operation === 'update') {
|
|
@@ -183,7 +180,7 @@ export const createDuplicateBeforeChange = () => {
|
|
|
183
180
|
req,
|
|
184
181
|
operation,
|
|
185
182
|
originalDoc: args.originalDoc,
|
|
186
|
-
});
|
|
183
|
+
}, collectionSlug);
|
|
187
184
|
if (data && normalizedFrom && typeof data.from === 'string') {
|
|
188
185
|
data.from = normalizedFrom;
|
|
189
186
|
}
|
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import { RedirectValidationError } from '../model/errors.js';
|
|
2
1
|
import { DEFAULT_REDIRECT_ORIGIN, isRedirectOrigin, REDIRECT_ORIGIN_VALUES, } from '../model/origin.js';
|
|
3
2
|
import { assertRedirectStatusCode, DEFAULT_REDIRECT_STATUS_CODE, validateDestinationForStatus, } from '../model/status-code.js';
|
|
4
|
-
import { normalizeRedirectSourcePath, normalizeRelativeRedirectPath
|
|
5
|
-
/**
|
|
6
|
-
* Matches collection `read: () => true`. Required under hosts that wrap field
|
|
7
|
-
* access fail-closed (e.g. host IAM plugins): Payload validates sort/where
|
|
8
|
-
* against field read, and `defaultSort: 'from'` always queries these paths.
|
|
9
|
-
*/
|
|
3
|
+
import { normalizeRedirectSourcePath, normalizeRelativeRedirectPath } from '../normalize-path.js';
|
|
10
4
|
const publicFieldRead = { read: () => true };
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (typeof from !== 'string' || from.trim().length === 0)
|
|
5
|
+
const normalizeSiblingFrom = (value) => {
|
|
6
|
+
if (typeof value !== 'string')
|
|
14
7
|
return undefined;
|
|
15
8
|
try {
|
|
16
|
-
return
|
|
9
|
+
return normalizeRedirectSourcePath(value);
|
|
17
10
|
}
|
|
18
11
|
catch {
|
|
19
12
|
return undefined;
|
|
@@ -27,36 +20,36 @@ export const buildRedirectFields = () => [
|
|
|
27
20
|
unique: true,
|
|
28
21
|
access: publicFieldRead,
|
|
29
22
|
validate: (value) => {
|
|
30
|
-
if (typeof value !== 'string'
|
|
23
|
+
if (typeof value !== 'string')
|
|
24
|
+
return 'from is required';
|
|
25
|
+
if (!value.trim())
|
|
31
26
|
return 'from is required';
|
|
32
|
-
}
|
|
33
27
|
try {
|
|
34
28
|
normalizeRedirectSourcePath(value);
|
|
35
29
|
return true;
|
|
36
30
|
}
|
|
37
31
|
catch (error) {
|
|
38
|
-
return error instanceof
|
|
32
|
+
return error instanceof Error ? error.message : 'Invalid from path';
|
|
39
33
|
}
|
|
40
34
|
},
|
|
41
35
|
},
|
|
42
36
|
{
|
|
43
37
|
name: 'to',
|
|
44
38
|
type: 'text',
|
|
39
|
+
required: true,
|
|
45
40
|
access: publicFieldRead,
|
|
46
41
|
validate: (value, { siblingData }) => {
|
|
47
42
|
try {
|
|
48
|
-
const
|
|
49
|
-
validateDestinationForStatus(
|
|
50
|
-
if (typeof value === 'string' &&
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return 'to must differ from from';
|
|
54
|
-
}
|
|
43
|
+
const statusCode = assertRedirectStatusCode(siblingData?.StatusCode ?? DEFAULT_REDIRECT_STATUS_CODE);
|
|
44
|
+
validateDestinationForStatus(statusCode, value);
|
|
45
|
+
if (typeof value === 'string' &&
|
|
46
|
+
normalizeRelativeRedirectPath(value) === normalizeSiblingFrom(siblingData?.from)) {
|
|
47
|
+
return 'to must differ from from';
|
|
55
48
|
}
|
|
56
49
|
return true;
|
|
57
50
|
}
|
|
58
51
|
catch (error) {
|
|
59
|
-
return error instanceof
|
|
52
|
+
return error instanceof Error ? error.message : 'Invalid to path';
|
|
60
53
|
}
|
|
61
54
|
},
|
|
62
55
|
},
|
|
@@ -64,7 +57,7 @@ export const buildRedirectFields = () => [
|
|
|
64
57
|
name: 'StatusCode',
|
|
65
58
|
type: 'select',
|
|
66
59
|
label: 'Type',
|
|
67
|
-
defaultValue:
|
|
60
|
+
defaultValue: String(DEFAULT_REDIRECT_STATUS_CODE),
|
|
68
61
|
required: true,
|
|
69
62
|
access: publicFieldRead,
|
|
70
63
|
options: [
|
|
@@ -77,12 +70,10 @@ export const buildRedirectFields = () => [
|
|
|
77
70
|
return true;
|
|
78
71
|
}
|
|
79
72
|
catch (error) {
|
|
80
|
-
return error instanceof
|
|
73
|
+
return error instanceof Error ? error.message : 'Invalid StatusCode';
|
|
81
74
|
}
|
|
82
75
|
},
|
|
83
|
-
admin: {
|
|
84
|
-
position: 'sidebar',
|
|
85
|
-
},
|
|
76
|
+
admin: { position: 'sidebar' },
|
|
86
77
|
},
|
|
87
78
|
{
|
|
88
79
|
name: 'enabled',
|
|
@@ -90,9 +81,7 @@ export const buildRedirectFields = () => [
|
|
|
90
81
|
defaultValue: true,
|
|
91
82
|
index: true,
|
|
92
83
|
access: publicFieldRead,
|
|
93
|
-
admin: {
|
|
94
|
-
position: 'sidebar',
|
|
95
|
-
},
|
|
84
|
+
admin: { position: 'sidebar' },
|
|
96
85
|
},
|
|
97
86
|
{
|
|
98
87
|
name: 'origin',
|
|
@@ -104,59 +93,70 @@ export const buildRedirectFields = () => [
|
|
|
104
93
|
{ label: 'Manual', value: 'manual' },
|
|
105
94
|
{ label: 'Auto', value: 'auto' },
|
|
106
95
|
],
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
readOnly: true,
|
|
110
|
-
},
|
|
111
|
-
validate: (value) => {
|
|
112
|
-
if (!isRedirectOrigin(value)) {
|
|
113
|
-
return `origin must be ${REDIRECT_ORIGIN_VALUES.join(' or ')}`;
|
|
114
|
-
}
|
|
115
|
-
return true;
|
|
116
|
-
},
|
|
96
|
+
validate: (value) => isRedirectOrigin(value) ? true : `origin must be ${REDIRECT_ORIGIN_VALUES.join(' or ')}`,
|
|
97
|
+
admin: { position: 'sidebar', readOnly: true },
|
|
117
98
|
},
|
|
118
99
|
];
|
|
119
100
|
export const mergeRedirectFields = (existingFields) => {
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
.filter((
|
|
101
|
+
const defaults = buildRedirectFields();
|
|
102
|
+
const existingByName = new Map(existingFields
|
|
103
|
+
.filter((field) => 'name' in field && typeof field.name === 'string')
|
|
123
104
|
.map((field) => [field.name, field]));
|
|
124
|
-
const
|
|
125
|
-
.filter((
|
|
105
|
+
const defaultNames = new Set(defaults
|
|
106
|
+
.filter((field) => 'name' in field && typeof field.name === 'string')
|
|
126
107
|
.map((field) => field.name));
|
|
127
|
-
const merged =
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
const pluginValidate = 'validate' in field && typeof field.validate === 'function' ? field.validate : undefined;
|
|
138
|
-
const hostValidate = 'validate' in hostField && typeof hostField.validate === 'function'
|
|
139
|
-
? hostField.validate
|
|
140
|
-
: undefined;
|
|
141
|
-
const validate = pluginValidate
|
|
142
|
-
? async (value, options) => {
|
|
143
|
-
const result = await pluginValidate(value, options);
|
|
144
|
-
if (result !== true || !hostValidate)
|
|
145
|
-
return result;
|
|
146
|
-
return hostValidate(value, options);
|
|
147
|
-
}
|
|
108
|
+
const merged = defaults.map((field) => {
|
|
109
|
+
if (!('name' in field) || typeof field.name !== 'string')
|
|
110
|
+
return field;
|
|
111
|
+
const override = existingByName.get(field.name);
|
|
112
|
+
if (!override)
|
|
113
|
+
return field;
|
|
114
|
+
const validate = 'validate' in field && typeof field.validate === 'function' ? field.validate : undefined;
|
|
115
|
+
const overrideValidate = 'validate' in override && typeof override.validate === 'function'
|
|
116
|
+
? override.validate
|
|
148
117
|
: undefined;
|
|
149
|
-
|
|
118
|
+
return {
|
|
150
119
|
...field,
|
|
151
|
-
...
|
|
152
|
-
...(validate
|
|
153
|
-
|
|
120
|
+
...override,
|
|
121
|
+
...(validate
|
|
122
|
+
? {
|
|
123
|
+
validate: async (value, options) => {
|
|
124
|
+
const run = validate;
|
|
125
|
+
const result = await run(value, options);
|
|
126
|
+
return result === true && overrideValidate
|
|
127
|
+
? overrideValidate(value, options)
|
|
128
|
+
: result;
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
: {}),
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
return [
|
|
135
|
+
...merged,
|
|
136
|
+
...existingFields.filter((field) => !('name' in field) || typeof field.name !== 'string' || !defaultNames.has(field.name)),
|
|
137
|
+
];
|
|
138
|
+
};
|
|
139
|
+
export const assertRedirectFields = (fields) => {
|
|
140
|
+
const expected = new Map([
|
|
141
|
+
['from', 'text'],
|
|
142
|
+
['to', 'text'],
|
|
143
|
+
['StatusCode', 'select'],
|
|
144
|
+
['enabled', 'checkbox'],
|
|
145
|
+
['origin', 'select'],
|
|
146
|
+
]);
|
|
147
|
+
const named = fields.filter((field) => 'name' in field && typeof field.name === 'string');
|
|
148
|
+
for (const [name, type] of expected) {
|
|
149
|
+
const matching = named.filter((field) => field.name === name);
|
|
150
|
+
if (matching.length !== 1 || !('type' in matching[0]) || matching[0].type !== type) {
|
|
151
|
+
throw new Error(`payload-redirect: overrides.fields must retain exactly one ${type} field named "${name}"`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const from = named.find((field) => field.name === 'from');
|
|
155
|
+
const enabled = named.find((field) => field.name === 'enabled');
|
|
156
|
+
if (!from || !('unique' in from) || from.unique !== true) {
|
|
157
|
+
throw new Error('payload-redirect: overrides.fields must keep from.unique: true');
|
|
154
158
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (name && pluginNames.has(name))
|
|
158
|
-
continue;
|
|
159
|
-
merged.push(field);
|
|
159
|
+
if (!enabled || !('index' in enabled) || enabled.index !== true) {
|
|
160
|
+
throw new Error('payload-redirect: overrides.fields must keep enabled.index: true');
|
|
160
161
|
}
|
|
161
|
-
return merged;
|
|
162
162
|
};
|
|
@@ -1,22 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DEFAULT_REDIRECT_ORIGIN } from '../model/origin.js';
|
|
1
|
+
import { isInternalRedirectWrite } from './access.js';
|
|
3
2
|
export const createRedirectOriginBeforeChange = () => async (args) => {
|
|
4
3
|
const { data, operation, req } = args;
|
|
5
|
-
if (req
|
|
6
|
-
|
|
7
|
-
data.origin = 'auto';
|
|
8
|
-
}
|
|
9
|
-
return data;
|
|
4
|
+
if (isInternalRedirectWrite(req)) {
|
|
5
|
+
data.origin = 'auto';
|
|
10
6
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
data.origin = DEFAULT_REDIRECT_ORIGIN;
|
|
7
|
+
else if (operation === 'create') {
|
|
8
|
+
data.origin = 'manual';
|
|
14
9
|
}
|
|
15
|
-
|
|
10
|
+
else {
|
|
16
11
|
data.origin =
|
|
17
|
-
args.originalDoc?.origin === 'auto'
|
|
18
|
-
? 'manual'
|
|
19
|
-
: (args.originalDoc?.origin ?? DEFAULT_REDIRECT_ORIGIN);
|
|
12
|
+
args.originalDoc?.origin === 'auto' ? 'manual' : (args.originalDoc?.origin ?? 'manual');
|
|
20
13
|
}
|
|
21
14
|
return data;
|
|
22
15
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
export declare class RedirectConfigError extends Error {
|
|
2
|
-
readonly name = "RedirectConfigError";
|
|
3
|
-
}
|
|
4
1
|
export declare class RedirectValidationError extends Error {
|
|
5
2
|
readonly name = "RedirectValidationError";
|
|
6
3
|
}
|
|
7
|
-
export declare class RedirectAmbiguousRuleError extends Error {
|
|
8
|
-
readonly name = "RedirectAmbiguousRuleError";
|
|
9
|
-
}
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
export class RedirectConfigError extends Error {
|
|
2
|
-
name = 'RedirectConfigError';
|
|
3
|
-
}
|
|
4
1
|
export class RedirectValidationError extends Error {
|
|
5
2
|
name = 'RedirectValidationError';
|
|
6
3
|
}
|
|
7
|
-
export class RedirectAmbiguousRuleError extends Error {
|
|
8
|
-
name = 'RedirectAmbiguousRuleError';
|
|
9
|
-
}
|
|
@@ -2,12 +2,9 @@ import { RedirectValidationError } from './errors.js';
|
|
|
2
2
|
const SUPPORTED = [301, 302];
|
|
3
3
|
export const DEFAULT_REDIRECT_STATUS_CODE = 301;
|
|
4
4
|
const normalizeStatusCode = (value) => {
|
|
5
|
-
const
|
|
6
|
-
if (
|
|
7
|
-
return
|
|
8
|
-
if (SUPPORTED.includes(n)) {
|
|
9
|
-
return n;
|
|
10
|
-
}
|
|
5
|
+
const code = typeof value === 'string' ? Number(value) : value;
|
|
6
|
+
if (code === 301 || code === 302)
|
|
7
|
+
return code;
|
|
11
8
|
return undefined;
|
|
12
9
|
};
|
|
13
10
|
export const isRedirectStatusCode = (value) => normalizeStatusCode(value) !== undefined;
|
|
@@ -22,6 +19,6 @@ export const requiresDestination = (_statusCode) => true;
|
|
|
22
19
|
export const validateDestinationForStatus = (statusCode, to) => {
|
|
23
20
|
const hasTo = typeof to === 'string' && to.trim().length > 0;
|
|
24
21
|
if (!hasTo) {
|
|
25
|
-
throw new RedirectValidationError(`
|
|
22
|
+
throw new RedirectValidationError(`statusCode ${statusCode} requires a non-empty "to" field`);
|
|
26
23
|
}
|
|
27
24
|
};
|
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
import type { Payload, PayloadRequest, SelectType } from 'payload';
|
|
2
2
|
import type { RedirectStatusCode } from './model/status-code.js';
|
|
3
|
-
/** Fields loaded by `resolveRedirect` (omit description). */
|
|
4
|
-
export declare const redirectResolveSelect: {
|
|
5
|
-
readonly from: true;
|
|
6
|
-
readonly to: true;
|
|
7
|
-
readonly StatusCode: true;
|
|
8
|
-
readonly enabled: true;
|
|
9
|
-
};
|
|
10
3
|
/** Lookup hooks / idempotent writes (includes `id`). */
|
|
11
4
|
export declare const redirectLookupSelect: {
|
|
5
|
+
readonly id: true;
|
|
12
6
|
readonly from: true;
|
|
13
7
|
readonly to: true;
|
|
14
8
|
readonly StatusCode: true;
|
|
15
9
|
readonly enabled: true;
|
|
16
|
-
readonly id: true;
|
|
17
10
|
readonly origin: true;
|
|
18
11
|
};
|
|
19
12
|
export type RedirectLookupDoc = {
|
|
@@ -34,5 +27,7 @@ type FindByFromBase = {
|
|
|
34
27
|
};
|
|
35
28
|
export declare const findRedirectByFrom: (payload: Payload, { collectionSlug, from, req, overrideAccess, context, select, excludeId, }: FindByFromBase & {
|
|
36
29
|
excludeId?: string | number;
|
|
37
|
-
}) => Promise<
|
|
30
|
+
}) => Promise<{
|
|
31
|
+
docs: RedirectLookupDoc[];
|
|
32
|
+
}>;
|
|
38
33
|
export {};
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
export const
|
|
1
|
+
/** Lookup hooks / idempotent writes (includes `id`). */
|
|
2
|
+
export const redirectLookupSelect = {
|
|
3
|
+
id: true,
|
|
3
4
|
from: true,
|
|
4
5
|
to: true,
|
|
5
6
|
StatusCode: true,
|
|
6
7
|
enabled: true,
|
|
7
|
-
};
|
|
8
|
-
/** Lookup hooks / idempotent writes (includes `id`). */
|
|
9
|
-
export const redirectLookupSelect = {
|
|
10
|
-
id: true,
|
|
11
|
-
...redirectResolveSelect,
|
|
12
8
|
origin: true,
|
|
13
9
|
};
|
|
14
10
|
export const findRedirectByFrom = async (payload, { collectionSlug, from, req, overrideAccess = false, context, select = redirectLookupSelect, excludeId, }) => {
|
|
@@ -17,7 +13,7 @@ export const findRedirectByFrom = async (payload, { collectionSlug, from, req, o
|
|
|
17
13
|
and: [{ from: { equals: from } }, { id: { not_equals: excludeId } }],
|
|
18
14
|
}
|
|
19
15
|
: { from: { equals: from } };
|
|
20
|
-
return payload.find({
|
|
16
|
+
return (await payload.find({
|
|
21
17
|
collection: collectionSlug,
|
|
22
18
|
where,
|
|
23
19
|
limit: 1,
|
|
@@ -26,5 +22,5 @@ export const findRedirectByFrom = async (payload, { collectionSlug, from, req, o
|
|
|
26
22
|
overrideAccess,
|
|
27
23
|
context,
|
|
28
24
|
req,
|
|
29
|
-
});
|
|
25
|
+
}));
|
|
30
26
|
};
|
|
@@ -56,6 +56,17 @@ function joinPrefixAndPath(prefix, pathname) {
|
|
|
56
56
|
const normalized = pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
57
57
|
return `${prefix}${normalized}`;
|
|
58
58
|
}
|
|
59
|
+
function isSiteRelativePath(path) {
|
|
60
|
+
return (path.startsWith('/') &&
|
|
61
|
+
!path.startsWith('//') &&
|
|
62
|
+
!path.includes('?') &&
|
|
63
|
+
!path.includes('#') &&
|
|
64
|
+
!path.includes('\\') &&
|
|
65
|
+
![...path].some((character) => {
|
|
66
|
+
const code = character.charCodeAt(0);
|
|
67
|
+
return code < 32 || code === 127;
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
59
70
|
function usesLegacyPreviewPathBuilder(config) {
|
|
60
71
|
const hasNewTemplate = Boolean(config.path) || Boolean(config.paths && Object.keys(config.paths).length > 0);
|
|
61
72
|
if (hasNewTemplate)
|
|
@@ -65,6 +76,22 @@ function usesLegacyPreviewPathBuilder(config) {
|
|
|
65
76
|
return Object.keys(config.localePrefix).length === 0;
|
|
66
77
|
}
|
|
67
78
|
export function buildPublishedPath(config, data, locale) {
|
|
79
|
+
if (typeof config.getPath === 'function') {
|
|
80
|
+
const path = config.getPath({ doc: data, locale });
|
|
81
|
+
if (path === null)
|
|
82
|
+
return null;
|
|
83
|
+
if (typeof path !== 'string') {
|
|
84
|
+
throw new Error('getPath must return a path or null');
|
|
85
|
+
}
|
|
86
|
+
const code = localeCode(locale);
|
|
87
|
+
if (!code)
|
|
88
|
+
return null;
|
|
89
|
+
const prefix = resolveLocaleRoutePrefix(config.localePrefix, code);
|
|
90
|
+
if (prefix === null)
|
|
91
|
+
return null;
|
|
92
|
+
const result = joinPrefixAndPath(prefix, path);
|
|
93
|
+
return isSiteRelativePath(result) ? result : null;
|
|
94
|
+
}
|
|
68
95
|
if (usesLegacyPreviewPathBuilder(config)) {
|
|
69
96
|
if (!config.routes)
|
|
70
97
|
throw new Error('Legacy preview path requires locale routes');
|
|
@@ -82,7 +109,8 @@ export function buildPublishedPath(config, data, locale) {
|
|
|
82
109
|
const prefix = resolveLocaleRoutePrefix(config.localePrefix, code);
|
|
83
110
|
if (prefix === null)
|
|
84
111
|
return null;
|
|
85
|
-
|
|
112
|
+
const result = joinPrefixAndPath(prefix, resolved);
|
|
113
|
+
return isSiteRelativePath(result) ? result : null;
|
|
86
114
|
}
|
|
87
115
|
export function resolvePublishedPathConfig(pluginLocalePrefix, link) {
|
|
88
116
|
if (!link)
|
|
@@ -90,7 +118,7 @@ export function resolvePublishedPathConfig(pluginLocalePrefix, link) {
|
|
|
90
118
|
const hasRoutes = link.routes && typeof link.routes === 'object' && Object.keys(link.routes).length > 0;
|
|
91
119
|
const hasPath = typeof link.path === 'string' && link.path.length > 0;
|
|
92
120
|
const hasPaths = link.paths && Object.keys(link.paths).length > 0;
|
|
93
|
-
if (!hasRoutes && !hasPath && !hasPaths)
|
|
121
|
+
if (!hasRoutes && !hasPath && !hasPaths && typeof link.getPath !== 'function')
|
|
94
122
|
return null;
|
|
95
123
|
return {
|
|
96
124
|
localePrefix: mergeLocalePrefix(pluginLocalePrefix, link.localePrefix),
|
|
@@ -98,5 +126,6 @@ export function resolvePublishedPathConfig(pluginLocalePrefix, link) {
|
|
|
98
126
|
paths: link.paths,
|
|
99
127
|
routes: hasRoutes ? link.routes : undefined,
|
|
100
128
|
params: link.params,
|
|
129
|
+
getPath: link.getPath,
|
|
101
130
|
};
|
|
102
131
|
}
|
package/dist/exports/index.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
export { type CreateRedirectOptions, createRedirect, } from '../core/redirects/auto/create-redirect.js';
|
|
2
|
-
export type { RedirectsCollectionAccess } from '../core/redirects/collection/access.js';
|
|
3
|
-
export { REDIRECT_CONTEXT_KEYS } from '../core/redirects/context-keys.js';
|
|
4
|
-
export { RedirectAmbiguousRuleError, RedirectConfigError, RedirectValidationError, } from '../core/redirects/model/errors.js';
|
|
5
1
|
export type { RedirectOrigin } from '../core/redirects/model/origin.js';
|
|
6
2
|
export type { RedirectStatusCode } from '../core/redirects/model/status-code.js';
|
|
7
|
-
export { type ResolveRedirectArgs, resolveRedirect, } from '../core/redirects/resolution/resolve.js';
|
|
8
|
-
export type { ResolvedRedirect } from '../core/redirects/resolution/resolved.js';
|
|
9
3
|
export { redirects } from '../plugin/payload-plugin.js';
|
|
10
|
-
export type {
|
|
4
|
+
export type { RedirectsOptions } from '../plugin/types.js';
|
package/dist/exports/index.js
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
export { createRedirect, } from '../core/redirects/auto/create-redirect.js';
|
|
2
|
-
export { REDIRECT_CONTEXT_KEYS } from '../core/redirects/context-keys.js';
|
|
3
|
-
export { RedirectAmbiguousRuleError, RedirectConfigError, RedirectValidationError, } from '../core/redirects/model/errors.js';
|
|
4
|
-
export { resolveRedirect, } from '../core/redirects/resolution/resolve.js';
|
|
5
1
|
export { redirects } from '../plugin/payload-plugin.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
3
|
-
export declare const redirects: (
|
|
1
|
+
import { type Plugin } from 'payload';
|
|
2
|
+
import type { RedirectsOptions } from './types.js';
|
|
3
|
+
export declare const redirects: (options?: RedirectsOptions) => Plugin;
|
|
4
|
+
export type { RedirectsOptions } from './types.js';
|