@klnap/payload-redirect 0.3.0 → 0.4.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/CHANGELOG.md +19 -0
- package/README.md +355 -172
- package/dist/core/redirects/auto/create-global-redirect.d.ts +11 -0
- package/dist/core/redirects/auto/create-global-redirect.js +18 -0
- package/dist/core/redirects/auto/create-global-redirect.js.map +1 -0
- package/dist/core/redirects/auto/create-redirect.d.ts +11 -7
- package/dist/core/redirects/auto/create-redirect.js +24 -14
- package/dist/core/redirects/auto/create-redirect.js.map +1 -0
- package/dist/core/redirects/auto/match-existing-rule.d.ts +1 -1
- package/dist/core/redirects/auto/match-existing-rule.js +3 -2
- package/dist/core/redirects/auto/match-existing-rule.js.map +1 -0
- package/dist/core/redirects/auto/persist-auto-rule.d.ts +3 -3
- package/dist/core/redirects/auto/persist-auto-rule.js +17 -16
- package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -0
- package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +2 -2
- package/dist/core/redirects/auto/publication/get-publication-transition.js +7 -6
- package/dist/core/redirects/auto/publication/get-publication-transition.js.map +1 -0
- package/dist/core/redirects/auto/publication/global-transition.d.ts +6 -0
- package/dist/core/redirects/auto/publication/global-transition.js +63 -0
- package/dist/core/redirects/auto/publication/global-transition.js.map +1 -0
- package/dist/core/redirects/auto/publication/locales.d.ts +2 -1
- package/dist/core/redirects/auto/publication/locales.js +18 -13
- package/dist/core/redirects/auto/publication/locales.js.map +1 -0
- package/dist/core/redirects/auto/publication/publish-intent.d.ts +1 -1
- package/dist/core/redirects/auto/publication/publish-intent.js +5 -4
- package/dist/core/redirects/auto/publication/publish-intent.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/document-locale.d.ts +1 -1
- package/dist/core/redirects/auto/publication/versions/document-locale.js +1 -0
- package/dist/core/redirects/auto/publication/versions/document-locale.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/version-pages.d.ts +2 -2
- package/dist/core/redirects/auto/publication/versions/version-pages.js +3 -2
- package/dist/core/redirects/auto/publication/versions/version-pages.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.js +5 -4
- package/dist/core/redirects/auto/publication/versions/version-rows.js.map +1 -0
- package/dist/core/redirects/auto/types.d.ts +3 -3
- package/dist/core/redirects/auto/types.js +1 -0
- package/dist/core/redirects/auto/types.js.map +1 -0
- package/dist/core/redirects/collection/collection.d.ts +2 -2
- package/dist/core/redirects/collection/collection.js +9 -11
- package/dist/core/redirects/collection/collection.js.map +1 -0
- package/dist/core/redirects/collection/duplicate-guard.d.ts +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +34 -33
- package/dist/core/redirects/collection/duplicate-guard.js.map +1 -0
- package/dist/core/redirects/collection/fields.d.ts +1 -1
- package/dist/core/redirects/collection/fields.js +51 -56
- package/dist/core/redirects/collection/fields.js.map +1 -0
- package/dist/core/redirects/collection/graph-lock.d.ts +1 -1
- package/dist/core/redirects/collection/graph-lock.js +8 -7
- package/dist/core/redirects/collection/graph-lock.js.map +1 -0
- package/dist/core/redirects/collection/origin-hook.d.ts +1 -1
- package/dist/core/redirects/collection/origin-hook.js +8 -6
- package/dist/core/redirects/collection/origin-hook.js.map +1 -0
- package/dist/core/redirects/collection/types.d.ts +2 -2
- package/dist/core/redirects/collection/types.js +1 -0
- package/dist/core/redirects/collection/types.js.map +1 -0
- package/dist/core/redirects/context-keys.d.ts +2 -2
- package/dist/core/redirects/context-keys.js +3 -2
- package/dist/core/redirects/context-keys.js.map +1 -0
- package/dist/core/redirects/model/errors.js +2 -1
- package/dist/core/redirects/model/errors.js.map +1 -0
- package/dist/core/redirects/model/origin.d.ts +1 -1
- package/dist/core/redirects/model/origin.js +4 -3
- package/dist/core/redirects/model/origin.js.map +1 -0
- package/dist/core/redirects/model/status-code.js +5 -4
- package/dist/core/redirects/model/status-code.js.map +1 -0
- package/dist/core/redirects/normalize-path.js +23 -22
- package/dist/core/redirects/normalize-path.js.map +1 -0
- package/dist/core/redirects/queries.d.ts +2 -2
- package/dist/core/redirects/queries.js +1 -0
- package/dist/core/redirects/queries.js.map +1 -0
- package/dist/core/url/published-path.js +20 -19
- package/dist/core/url/published-path.js.map +1 -0
- package/dist/core/url/route-params.js +9 -8
- package/dist/core/url/route-params.js.map +1 -0
- package/dist/exports/index.d.ts +4 -4
- package/dist/exports/index.js +2 -1
- package/dist/exports/index.js.map +1 -0
- package/dist/plugin/payload-plugin.d.ts +2 -2
- package/dist/plugin/payload-plugin.js +74 -38
- package/dist/plugin/payload-plugin.js.map +1 -0
- package/dist/plugin/types.d.ts +10 -5
- package/dist/plugin/types.js +1 -0
- package/dist/plugin/types.js.map +1 -0
- package/docs/FAILURE_SEMANTICS.md +50 -31
- package/package.json +21 -13
- package/dist/core/redirects/collection/access.d.ts +0 -8
- package/dist/core/redirects/collection/access.js +0 -9
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { definePlugin } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
1
|
+
import { definePlugin, } from "payload";
|
|
2
|
+
import { createGlobalRedirect } from "../core/redirects/auto/create-global-redirect.js";
|
|
3
|
+
import { createRedirect } from "../core/redirects/auto/create-redirect.js";
|
|
4
|
+
import { createRedirectsCollection } from "../core/redirects/collection/collection.js";
|
|
5
|
+
import { isValidLocalePrefix } from "../core/url/published-path.js";
|
|
6
|
+
import { isValidPathTemplate } from "../core/url/route-params.js";
|
|
7
|
+
const PLUGIN_SLUG = "@klnap/payload-redirect";
|
|
8
|
+
const DEFAULT_COLLECTION_SLUG = "redirects";
|
|
9
|
+
const PATH_OPTIONS = new Set(["enabled", "path", "paths", "getPath"]);
|
|
10
|
+
const asRecord = (value) => value && typeof value === "object" && !Array.isArray(value)
|
|
10
11
|
? value
|
|
11
12
|
: undefined;
|
|
12
13
|
const localeCodes = (config) => {
|
|
13
14
|
const locales = config.localization === false ? undefined : config.localization?.locales;
|
|
14
15
|
return Array.isArray(locales)
|
|
15
|
-
? locales.map((locale) => typeof locale ===
|
|
16
|
+
? locales.map((locale) => typeof locale === "string" ? locale : locale.code)
|
|
16
17
|
: [];
|
|
17
18
|
};
|
|
18
19
|
const assertLocaleMap = (value, locales, name) => {
|
|
@@ -22,24 +23,24 @@ const assertLocaleMap = (value, locales, name) => {
|
|
|
22
23
|
const missing = locales.filter((locale) => !Object.hasOwn(map, locale));
|
|
23
24
|
const unknown = Object.keys(map).filter((locale) => !locales.includes(locale));
|
|
24
25
|
if (missing.length || unknown.length) {
|
|
25
|
-
throw new Error(`payload-redirect: ${name} has invalid locales (missing: ${missing.join(
|
|
26
|
+
throw new Error(`payload-redirect: ${name} has invalid locales (missing: ${missing.join(", ") || "none"}; unknown: ${unknown.join(", ") || "none"})`);
|
|
26
27
|
}
|
|
27
28
|
for (const [locale, path] of Object.entries(map)) {
|
|
28
|
-
if (typeof path !==
|
|
29
|
+
if (typeof path !== "string") {
|
|
29
30
|
throw new Error(`payload-redirect: ${name}.${locale} must be a string`);
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
return map;
|
|
33
34
|
};
|
|
34
|
-
const validatePathOptions = (value, slug, locales,
|
|
35
|
+
const validatePathOptions = (value, entityType, slug, locales, versions) => {
|
|
35
36
|
const options = asRecord(value);
|
|
36
37
|
if (!options)
|
|
37
38
|
throw new Error(`payload-redirect: redirect config for "${slug}" must be an object`);
|
|
38
39
|
const unsupported = Object.keys(options).filter((key) => !PATH_OPTIONS.has(key));
|
|
39
40
|
if (unsupported.length) {
|
|
40
|
-
throw new Error(`payload-redirect: unsupported redirect option for "${slug}": ${unsupported.join(
|
|
41
|
+
throw new Error(`payload-redirect: unsupported redirect option for "${slug}": ${unsupported.join(", ")}`);
|
|
41
42
|
}
|
|
42
|
-
const modes = [
|
|
43
|
+
const modes = ["path", "paths", "getPath"].filter((key) => Object.hasOwn(options, key));
|
|
43
44
|
if (options.enabled === false) {
|
|
44
45
|
if (modes.length) {
|
|
45
46
|
throw new Error(`payload-redirect: disabled redirect config for "${slug}" cannot define a path`);
|
|
@@ -50,44 +51,42 @@ const validatePathOptions = (value, slug, locales, collection) => {
|
|
|
50
51
|
throw new Error(`payload-redirect: redirect config for "${slug}" enabled must be a boolean`);
|
|
51
52
|
}
|
|
52
53
|
if (modes.length !== 1) {
|
|
53
|
-
throw new Error(`payload-redirect:
|
|
54
|
+
throw new Error(`payload-redirect: ${entityType} "${slug}" must configure exactly one of path, paths, or getPath`);
|
|
54
55
|
}
|
|
55
|
-
if (!
|
|
56
|
-
typeof collection.versions !== 'object' ||
|
|
57
|
-
!collection.versions.drafts) {
|
|
56
|
+
if (!asRecord(versions)?.drafts) {
|
|
58
57
|
throw new Error(`payload-redirect: automatic redirects for "${slug}" require versions.drafts`);
|
|
59
58
|
}
|
|
60
|
-
if (Object.hasOwn(options,
|
|
61
|
-
if (typeof options.path !==
|
|
62
|
-
throw new Error(`payload-redirect:
|
|
59
|
+
if (Object.hasOwn(options, "path")) {
|
|
60
|
+
if (typeof options.path !== "string" || !isValidPathTemplate(options.path)) {
|
|
61
|
+
throw new Error(`payload-redirect: ${entityType}s.${slug}.path must be a valid site-relative template`);
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
|
-
else if (Object.hasOwn(options,
|
|
64
|
+
else if (Object.hasOwn(options, "paths")) {
|
|
66
65
|
if (locales.length === 0) {
|
|
67
|
-
throw new Error(`payload-redirect:
|
|
66
|
+
throw new Error(`payload-redirect: ${entityType}s.${slug}.paths requires Payload localization`);
|
|
68
67
|
}
|
|
69
|
-
const paths = assertLocaleMap(options.paths, locales,
|
|
68
|
+
const paths = assertLocaleMap(options.paths, locales, `${entityType}s.${slug}.paths`);
|
|
70
69
|
for (const [locale, path] of Object.entries(paths)) {
|
|
71
70
|
if (!isValidPathTemplate(path)) {
|
|
72
|
-
throw new Error(`payload-redirect:
|
|
71
|
+
throw new Error(`payload-redirect: ${entityType}s.${slug}.paths.${locale} must be a valid site-relative template`);
|
|
73
72
|
}
|
|
74
73
|
}
|
|
75
74
|
}
|
|
76
|
-
else if (typeof options.getPath !==
|
|
75
|
+
else if (typeof options.getPath !== "function") {
|
|
77
76
|
throw new Error(`payload-redirect: collections.${slug}.getPath must be a function`);
|
|
78
77
|
}
|
|
79
78
|
};
|
|
80
|
-
const
|
|
81
|
-
const central = asRecord(
|
|
82
|
-
if (central && Object.hasOwn(central,
|
|
83
|
-
return central[
|
|
79
|
+
const selectedOptions = (entity, centralOptions) => {
|
|
80
|
+
const central = asRecord(centralOptions);
|
|
81
|
+
if (central && Object.hasOwn(central, entity.slug)) {
|
|
82
|
+
return central[entity.slug];
|
|
84
83
|
}
|
|
85
|
-
return asRecord(
|
|
84
|
+
return asRecord(entity.custom)?.redirect;
|
|
86
85
|
};
|
|
87
86
|
const validateOptions = (config, options) => {
|
|
88
87
|
const locales = localeCodes(config);
|
|
89
88
|
if (options.localePrefixes !== undefined) {
|
|
90
|
-
const prefixes = assertLocaleMap(options.localePrefixes, locales,
|
|
89
|
+
const prefixes = assertLocaleMap(options.localePrefixes, locales, "localePrefixes");
|
|
91
90
|
for (const [locale, prefix] of Object.entries(prefixes)) {
|
|
92
91
|
if (!isValidLocalePrefix(prefix)) {
|
|
93
92
|
throw new Error(`payload-redirect: localePrefixes.${locale} must be a site-relative path prefix`);
|
|
@@ -99,15 +98,26 @@ const validateOptions = (config, options) => {
|
|
|
99
98
|
throw new Error(`payload-redirect: configured collection "${slug}" is not registered`);
|
|
100
99
|
}
|
|
101
100
|
}
|
|
101
|
+
for (const slug of Object.keys(asRecord(options.globals) ?? {})) {
|
|
102
|
+
if (!config.globals?.some((global) => global.slug === slug)) {
|
|
103
|
+
throw new Error(`payload-redirect: configured global "${slug}" is not registered`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
102
106
|
for (const collection of config.collections ?? []) {
|
|
103
|
-
const selected =
|
|
107
|
+
const selected = selectedOptions(collection, options.collections);
|
|
108
|
+
if (selected === undefined)
|
|
109
|
+
continue;
|
|
110
|
+
validatePathOptions(selected, "collection", collection.slug, locales, collection.versions);
|
|
111
|
+
}
|
|
112
|
+
for (const global of config.globals ?? []) {
|
|
113
|
+
const selected = selectedOptions(global, options.globals);
|
|
104
114
|
if (selected === undefined)
|
|
105
115
|
continue;
|
|
106
|
-
validatePathOptions(selected,
|
|
116
|
+
validatePathOptions(selected, "global", global.slug, locales, global.versions);
|
|
107
117
|
}
|
|
108
118
|
};
|
|
109
119
|
const wireCollectionRedirect = (collection, options, collectionSlug) => {
|
|
110
|
-
const selected =
|
|
120
|
+
const selected = selectedOptions(collection, options.collections);
|
|
111
121
|
const record = asRecord(selected);
|
|
112
122
|
if (!record || record.enabled === false)
|
|
113
123
|
return collection;
|
|
@@ -121,7 +131,7 @@ const wireCollectionRedirect = (collection, options, collectionSlug) => {
|
|
|
121
131
|
afterChange: [
|
|
122
132
|
...hooks,
|
|
123
133
|
createRedirect({
|
|
124
|
-
collectionSlug,
|
|
134
|
+
redirectCollectionSlug: collectionSlug,
|
|
125
135
|
pathOptions: redirectOptions,
|
|
126
136
|
localePrefixes: options.localePrefixes,
|
|
127
137
|
}),
|
|
@@ -129,13 +139,37 @@ const wireCollectionRedirect = (collection, options, collectionSlug) => {
|
|
|
129
139
|
},
|
|
130
140
|
};
|
|
131
141
|
};
|
|
142
|
+
const wireGlobalRedirect = (global, options, redirectCollectionSlug) => {
|
|
143
|
+
const selected = selectedOptions(global, options.globals);
|
|
144
|
+
const record = asRecord(selected);
|
|
145
|
+
if (!record || record.enabled === false)
|
|
146
|
+
return global;
|
|
147
|
+
const afterChange = global.hooks?.afterChange;
|
|
148
|
+
const hooks = Array.isArray(afterChange) ? afterChange : afterChange ? [afterChange] : [];
|
|
149
|
+
const pathOptions = selected;
|
|
150
|
+
return {
|
|
151
|
+
...global,
|
|
152
|
+
hooks: {
|
|
153
|
+
...global.hooks,
|
|
154
|
+
afterChange: [
|
|
155
|
+
...hooks,
|
|
156
|
+
createGlobalRedirect({
|
|
157
|
+
globalSlug: global.slug,
|
|
158
|
+
redirectCollectionSlug,
|
|
159
|
+
pathOptions,
|
|
160
|
+
localePrefixes: options.localePrefixes,
|
|
161
|
+
}),
|
|
162
|
+
],
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
};
|
|
132
166
|
const definedRedirectsPlugin = definePlugin({
|
|
133
167
|
slug: PLUGIN_SLUG,
|
|
134
168
|
plugin: ({ config, ...options }) => {
|
|
135
169
|
validateOptions(config, options);
|
|
136
170
|
const registeredCount = config.plugins?.filter((plugin) => plugin.slug === PLUGIN_SLUG).length ?? 0;
|
|
137
171
|
if (registeredCount > 1) {
|
|
138
|
-
throw new Error(
|
|
172
|
+
throw new Error("payload-redirect: register redirects() only once in config.plugins");
|
|
139
173
|
}
|
|
140
174
|
const redirectsCollection = createRedirectsCollection(options.overrides);
|
|
141
175
|
const collectionSlug = redirectsCollection.slug || DEFAULT_COLLECTION_SLUG;
|
|
@@ -148,7 +182,9 @@ const definedRedirectsPlugin = definePlugin({
|
|
|
148
182
|
...(config.collections ?? []).map((collection) => wireCollectionRedirect(collection, options, collectionSlug)),
|
|
149
183
|
redirectsCollection,
|
|
150
184
|
],
|
|
185
|
+
globals: (config.globals ?? []).map((global) => wireGlobalRedirect(global, options, collectionSlug)),
|
|
151
186
|
};
|
|
152
187
|
},
|
|
153
188
|
});
|
|
154
189
|
export const redirects = (options = {}) => definedRedirectsPlugin(options);
|
|
190
|
+
//# sourceMappingURL=payload-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payload-plugin.js","sourceRoot":"","sources":["../../src/plugin/payload-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,YAAY,GAEb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,oBAAoB,EAAE,MAAM,sDAAsD,CAAC;AAC5F,OAAO,EAAE,cAAc,EAAE,MAAM,+CAA+C,C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import { createGlobalRedirect } from \"#redirect/core/redirects/auto/create-global-redirect\";\nimport { createRedirect } from \"#redirect/core/redirects/auto/create-redirect\";\nimport { createRedirectsCollection } from \"#redirect/core/redirects/collection/collection\";\nimport { isValidLocalePrefix } from \"#redirect/core/url/published-path\";\nimport { isValidPathTemplate } from \"#redirect/core/url/route-params\";\nimport type {\n RedirectCollectionOptions,\n RedirectGlobalOptions,\n RedirectsOptions,\n} from \"./types.js\";\n\nconst PLUGIN_SLUG = \"@klnap/payload-redirect\";\nconst DEFAULT_COLLECTION_SLUG = \"redirects\";\nconst PATH_OPTIONS = new Set([\"enabled\", \"path\", \"paths\", \"getPath\"]);\n\nconst asRecord = (value: unknown): Record<string, unknown> | undefined =>\n value && typeof value === \"object\" && !Array.isArray(value)\n ? (value as Record<string, unknown>)\n : undefined;\n\nconst localeCodes = (config: Config): string[] => {\n const locales = config.localization === false ? undefined : config.localization?.locales;\n return Array.isArray(locales)\n ? locales.map((locale) =>\n typeof locale === \"string\" ? locale : (locale as { code: string }).code,\n )\n : [];\n};\n\nconst assertLocaleMap = (\n value: unknown,\n locales: string[],\n name: string,\n): Record<string, string> => {\n const map = asRecord(value);\n if (!map) throw new Error(`payload-redirect: ${name} must be a locale map`);\n const missing = locales.filter((locale) => !Object.hasOwn(map, locale));\n const unknown = Object.keys(map).filter((locale) => !locales.includes(locale));\n if (missing.length || unknown.length) {\n throw new Error(\n `payload-redirect: ${name} has invalid locales (missing: ${missing.join(\", \") || \"none\"}; unknown: ${unknown.join(\", \") || \"none\"})`,\n );\n }\n for (const [locale, path] of Object.entries(map)) {\n if (typeof path !== \"string\") {\n throw new Error(`payload-redirect: ${name}.${locale} must be a string`);\n }\n }\n return map as Record<string, string>;\n};\n\nconst validatePathOptions = (\n value: unknown,\n entityType: \"collection\" | \"global\",\n slug: string,\n locales: string[],\n versions: unknown,\n): void => {\n const options = asRecord(value);\n if (!options)\n throw new Error(`payload-redirect: redirect config for \"${slug}\" must be an object`);\n\n const unsupported = Object.keys(options).filter((key) => !PATH_OPTIONS.has(key));\n if (unsupported.length) {\n throw new Error(\n `payload-redirect: unsupported redirect option for \"${slug}\": ${unsupported.join(\", \")}`,\n );\n }\n\n const modes = [\"path\", \"paths\", \"getPath\"].filter((key) => Object.hasOwn(options, key));\n if (options.enabled === false) {\n if (modes.length) {\n throw new Error(\n `payload-redirect: disabled redirect config for \"${slug}\" cannot define a path`,\n );\n }\n return;\n }\n if (options.enabled !== undefined && options.enabled !== true) {\n throw new Error(`payload-redirect: redirect config for \"${slug}\" enabled must be a boolean`);\n }\n if (modes.length !== 1) {\n throw new Error(\n `payload-redirect: ${entityType} \"${slug}\" must configure exactly one of path, paths, or getPath`,\n );\n }\n if (!asRecord(versions)?.drafts) {\n throw new Error(`payload-redirect: automatic redirects for \"${slug}\" require versions.drafts`);\n }\n\n if (Object.hasOwn(options, \"path\")) {\n if (typeof options.path !== \"string\" || !isValidPathTemplate(options.path)) {\n throw new Error(\n `payload-redirect: ${entityType}s.${slug}.path must be a valid site-relative template`,\n );\n }\n } else if (Object.hasOwn(options, \"paths\")) {\n if (locales.length === 0) {\n throw new Error(\n `payload-redirect: ${entityType}s.${slug}.paths requires Payload localization`,\n );\n }\n const paths = assertLocaleMap(options.paths, locales, `${entityType}s.${slug}.paths`);\n for (const [locale, path] of Object.entries(paths)) {\n if (!isValidPathTemplate(path)) {\n throw new Error(\n `payload-redirect: ${entityType}s.${slug}.paths.${locale} must be a valid site-relative template`,\n );\n }\n }\n } else if (typeof options.getPath !== \"function\") {\n throw new Error(`payload-redirect: collections.${slug}.getPath must be a function`);\n }\n};\n\nconst selectedOptions = (\n entity: { slug: string; custom?: unknown },\n centralOptions: unknown,\n): unknown => {\n const central = asRecord(centralOptions);\n if (central && Object.hasOwn(central, entity.slug)) {\n return central[entity.slug];\n }\n return asRecord(entity.custom)?.redirect;\n};\n\nconst validateOptions = (config: Config, options: RedirectsOptions): void => {\n const locales = localeCodes(config);\n if (options.localePrefixes !== undefined) {\n const prefixes = assertLocaleMap(options.localePrefixes, locales, \"localePrefixes\");\n for (const [locale, prefix] of Object.entries(prefixes)) {\n if (!isValidLocalePrefix(prefix)) {\n throw new Error(\n `payload-redirect: localePrefixes.${locale} must be a site-relative path prefix`,\n );\n }\n }\n }\n\n for (const slug of Object.keys(asRecord(options.collections) ?? {})) {\n if (!config.collections?.some((collection) => collection.slug === slug)) {\n throw new Error(`payload-redirect: configured collection \"${slug}\" is not registered`);\n }\n }\n\n for (const slug of Object.keys(asRecord(options.globals) ?? {})) {\n if (!config.globals?.some((global) => global.slug === slug)) {\n throw new Error(`payload-redirect: configured global \"${slug}\" is not registered`);\n }\n }\n\n for (const collection of config.collections ?? []) {\n const selected = selectedOptions(collection, options.collections);\n if (selected === undefined) continue;\n validatePathOptions(selected, \"collection\", collection.slug, locales, collection.versions);\n }\n\n for (const global of config.globals ?? []) {\n const selected = selectedOptions(global, options.globals);\n if (selected === undefined) continue;\n validatePathOptions(selected, \"global\", global.slug, locales, global.versions);\n }\n};\n\nconst wireCollectionRedirect = (\n collection: CollectionConfig,\n options: RedirectsOptions,\n collectionSlug: string,\n): CollectionConfig => {\n const selected = selectedOptions(collection, options.collections);\n const record = asRecord(selected);\n if (!record || record.enabled === false) return collection;\n\n const afterChange = collection.hooks?.afterChange;\n const hooks = Array.isArray(afterChange) ? afterChange : afterChange ? [afterChange] : [];\n const redirectOptions = selected as RedirectCollectionOptions;\n\n return {\n ...collection,\n hooks: {\n ...collection.hooks,\n afterChange: [\n ...hooks,\n createRedirect({\n redirectCollectionSlug: collectionSlug,\n pathOptions: redirectOptions,\n localePrefixes: options.localePrefixes,\n }),\n ],\n },\n };\n};\n\nconst wireGlobalRedirect = (\n global: GlobalConfig,\n options: RedirectsOptions,\n redirectCollectionSlug: string,\n): GlobalConfig => {\n const selected = selectedOptions(global, options.globals);\n const record = asRecord(selected);\n if (!record || record.enabled === false) return global;\n\n const afterChange = global.hooks?.afterChange;\n const hooks = Array.isArray(afterChange) ? afterChange : afterChange ? [afterChange] : [];\n const pathOptions = selected as RedirectGlobalOptions;\n\n return {\n ...global,\n hooks: {\n ...global.hooks,\n afterChange: [\n ...hooks,\n createGlobalRedirect({\n globalSlug: global.slug,\n redirectCollectionSlug,\n pathOptions,\n localePrefixes: options.localePrefixes,\n }),\n ],\n },\n };\n};\n\nconst definedRedirectsPlugin = definePlugin<RedirectsOptions>({\n slug: PLUGIN_SLUG,\n plugin: ({ config, ...options }) => {\n validateOptions(config, options);\n const registeredCount =\n config.plugins?.filter((plugin) => plugin.slug === PLUGIN_SLUG).length ?? 0;\n if (registeredCount > 1) {\n throw new Error(\"payload-redirect: register redirects() only once in config.plugins\");\n }\n\n const redirectsCollection = createRedirectsCollection(options.overrides);\n const collectionSlug = redirectsCollection.slug || DEFAULT_COLLECTION_SLUG;\n if (config.collections?.some(({ slug }) => slug === collectionSlug)) {\n throw new Error(\n `payload-redirect: collection slug \"${collectionSlug}\" is already registered; configure it with redirects({ overrides: { slug } }) instead of defining a host collection`,\n );\n }\n\n return {\n ...config,\n collections: [\n ...(config.collections ?? []).map((collection) =>\n wireCollectionRedirect(collection, options, collectionSlug),\n ),\n redirectsCollection,\n ],\n globals: (config.globals ?? []).map((global) =>\n wireGlobalRedirect(global, options, collectionSlug),\n ),\n };\n },\n});\n\nexport const redirects = (options: RedirectsOptions = {}): Plugin =>\n definedRedirectsPlugin(options);\n"]}
|
package/dist/plugin/types.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { CollectionSlug } from
|
|
2
|
-
import type { RedirectPathOptions } from
|
|
3
|
-
import type { RedirectCollectionOverrides } from
|
|
4
|
-
type LocaleCode = Extract<import(
|
|
1
|
+
import type { CollectionSlug, DataFromCollectionSlug, DataFromGlobalSlug, GlobalSlug } from "payload";
|
|
2
|
+
import type { RedirectPathOptions } from "../core/redirects/auto/types.js";
|
|
3
|
+
import type { RedirectCollectionOverrides } from "../core/redirects/collection/types.js";
|
|
4
|
+
type LocaleCode = Extract<import("payload").TypedLocale, string>;
|
|
5
5
|
type LocaleMap = string extends LocaleCode ? Record<string, string> : Record<LocaleCode, string>;
|
|
6
|
-
export type RedirectCollectionOptions<TSlug extends CollectionSlug = CollectionSlug> = RedirectPathOptions<TSlug
|
|
6
|
+
export type RedirectCollectionOptions<TSlug extends CollectionSlug = CollectionSlug> = RedirectPathOptions<DataFromCollectionSlug<TSlug>>;
|
|
7
|
+
export type RedirectGlobalOptions<TSlug extends GlobalSlug = GlobalSlug> = RedirectPathOptions<DataFromGlobalSlug<TSlug>>;
|
|
7
8
|
export type RedirectsOptions = {
|
|
8
9
|
/** Payload locale codes map to explicit site-relative prefixes; omission means no prefix. */
|
|
9
10
|
localePrefixes?: LocaleMap;
|
|
@@ -11,6 +12,10 @@ export type RedirectsOptions = {
|
|
|
11
12
|
collections?: {
|
|
12
13
|
[TSlug in CollectionSlug]?: RedirectCollectionOptions<TSlug>;
|
|
13
14
|
};
|
|
15
|
+
/** Each entry opts that global into automatic redirects. Central entries replace local config. */
|
|
16
|
+
globals?: {
|
|
17
|
+
[TSlug in GlobalSlug]?: RedirectGlobalOptions<TSlug>;
|
|
18
|
+
};
|
|
14
19
|
/** Native collection overrides for the plugin-owned redirects collection. */
|
|
15
20
|
overrides?: RedirectCollectionOverrides;
|
|
16
21
|
};
|
package/dist/plugin/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/plugin/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n CollectionSlug,\n DataFromCollectionSlug,\n DataFromGlobalSlug,\n GlobalSlug,\n} from \"payload\";\nimport type { RedirectPathOptions } from \"#redirect/core/redirects/auto/types\";\nimport type { RedirectCollectionOverrides } from \"#redirect/core/redirects/collection/types\";\n\ntype LocaleCode = Extract<import(\"payload\").TypedLocale, string>;\ntype LocaleMap = string extends LocaleCode ? Record<string, string> : Record<LocaleCode, string>;\n\nexport type RedirectCollectionOptions<TSlug extends CollectionSlug = CollectionSlug> =\n RedirectPathOptions<DataFromCollectionSlug<TSlug>>;\n\nexport type RedirectGlobalOptions<TSlug extends GlobalSlug = GlobalSlug> = RedirectPathOptions<\n DataFromGlobalSlug<TSlug>\n>;\n\nexport type RedirectsOptions = {\n /** Payload locale codes map to explicit site-relative prefixes; omission means no prefix. */\n localePrefixes?: LocaleMap;\n /** Each entry opts that collection into automatic redirects. Central entries replace local config. */\n collections?: { [TSlug in CollectionSlug]?: RedirectCollectionOptions<TSlug> };\n /** Each entry opts that global into automatic redirects. Central entries replace local config. */\n globals?: { [TSlug in GlobalSlug]?: RedirectGlobalOptions<TSlug> };\n /** Native collection overrides for the plugin-owned redirects collection. */\n overrides?: RedirectCollectionOverrides;\n};\n"]}
|
|
@@ -4,31 +4,37 @@ This document records observable configuration, validation, and publication beha
|
|
|
4
4
|
|
|
5
5
|
## Plugin configuration
|
|
6
6
|
|
|
7
|
-
| Condition
|
|
8
|
-
|
|
9
|
-
| `redirects()` is not registered
|
|
10
|
-
| The redirects collection slug already exists
|
|
11
|
-
| `redirects()` is registered more than once
|
|
12
|
-
| `overrides.fields` changes a required field type
|
|
13
|
-
| A
|
|
14
|
-
| Central
|
|
15
|
-
| A configured
|
|
16
|
-
| Path config has unsupported options or multiple strategies | Plugin configuration throws before Payload builds.
|
|
17
|
-
| Locale map omits or adds a Payload locale
|
|
18
|
-
| Automatic generation is disabled
|
|
7
|
+
| Condition | Result |
|
|
8
|
+
| ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `redirects()` is not registered | Payload config has no redirects collection or automatic publication hooks. |
|
|
10
|
+
| The redirects collection slug already exists | Plugin config throws and names the colliding slug. |
|
|
11
|
+
| `redirects()` is registered more than once | Plugin config throws; register it once. |
|
|
12
|
+
| `overrides.fields` changes a required field type | Plugin config throws before Payload builds. Missing required fields are restored from defaults. |
|
|
13
|
+
| A Collection or Global has no redirect configuration | The entity is left unchanged; no publication hook is added. |
|
|
14
|
+
| Central options name an unregistered Collection or Global | Plugin configuration throws and names the entity. |
|
|
15
|
+
| A configured Collection or Global has no `versions.drafts` | Plugin configuration throws and names the entity. |
|
|
16
|
+
| Path config has unsupported options or multiple strategies | Plugin configuration throws before Payload builds. |
|
|
17
|
+
| Locale map omits or adds a Payload locale | Plugin configuration throws and names invalid locales. |
|
|
18
|
+
| Automatic generation is disabled | No automatic hook is added; manual rules remain available. |
|
|
19
|
+
| A Global defines `custom.redirect` | The plugin appends its Global publication hook and preserves existing hooks. |
|
|
19
20
|
|
|
20
21
|
## Redirect collection writes
|
|
21
22
|
|
|
22
23
|
`from` and `to` must be non-empty site-relative paths. The plugin normalizes them before checking
|
|
23
24
|
duplicates and loops.
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
|
31
|
-
|
|
|
26
|
+
Collection and field authorization use Payload's native defaults and consumer overrides. Trusted
|
|
27
|
+
automatic redirect operations use Local API `overrideAccess: true`; their request-context marker
|
|
28
|
+
tracks rule origin and recursion only. Ordinary REST, GraphQL, and user-scoped Local API operations
|
|
29
|
+
remain subject to their configured access rules.
|
|
30
|
+
|
|
31
|
+
| Condition | Result |
|
|
32
|
+
| --------------------------------------------------- | -------------------------------------------------------------- |
|
|
33
|
+
| Duplicate normalized `from` | Payload validation error on `from`. |
|
|
34
|
+
| Enabled redirect chain loops or exceeds 1,000 hops | Payload validation error on `to`. |
|
|
35
|
+
| `statusCode` is not `301` or `302` | Payload field validation error. |
|
|
36
|
+
| `to` is missing or invalid | Payload field validation error. |
|
|
37
|
+
| `origin` is invalid | Payload field validation error. |
|
|
32
38
|
| A manual rule already owns an automatic source path | Automatic write is skipped; the manual rule remains unchanged. |
|
|
33
39
|
|
|
34
40
|
Internal writes carry server-side request context and run inside the Payload transaction. PostgreSQL
|
|
@@ -40,18 +46,31 @@ same concurrent-write guarantee.
|
|
|
40
46
|
Automatic rules use one of `path`, `paths`, or `getPath`, configured centrally or in
|
|
41
47
|
`custom.redirect`. Central configuration replaces local configuration.
|
|
42
48
|
|
|
43
|
-
| Condition
|
|
44
|
-
|
|
45
|
-
| No matching path configuration
|
|
46
|
-
| Template value is missing or unsafe
|
|
47
|
-
| `getPath` returns `null` or a non-site-relative path
|
|
48
|
-
| `getPath` returns a non-string value
|
|
49
|
-
| Locale is absent
|
|
50
|
-
| Locale is missing from a configured prefix map
|
|
51
|
-
| Generated source and destination normalize to the same path | No redirect is written.
|
|
52
|
-
|
|
53
|
-
Draft saves, first publications, unpublishes,
|
|
54
|
-
|
|
49
|
+
| Condition | Result |
|
|
50
|
+
| ----------------------------------------------------------- | ----------------------------------------------------------------------------- |
|
|
51
|
+
| No matching path configuration | Automatic generation is not registered for that Collection or Global. |
|
|
52
|
+
| Template value is missing or unsafe | That locale's publication transition is skipped. |
|
|
53
|
+
| `getPath` returns `null` or a non-site-relative path | That locale's transition is skipped. |
|
|
54
|
+
| `getPath` returns a non-string value | That locale's transition is skipped. |
|
|
55
|
+
| Locale is absent | A shared `path` works without a prefix map; localized `paths` cannot resolve. |
|
|
56
|
+
| Locale is missing from a configured prefix map | Plugin configuration fails before Payload builds. |
|
|
57
|
+
| Generated source and destination normalize to the same path | No redirect is written. |
|
|
58
|
+
|
|
59
|
+
Draft saves, first publications, unpublishes, and deletions create no automatic rules. Existing
|
|
60
|
+
redirects remain when a document is unpublished or deleted.
|
|
61
|
+
|
|
62
|
+
On an unchanged republish, an enabled identical automatic rule is left alone. A missing rule is
|
|
63
|
+
recreated, and a disabled automatic rule is re-enabled, when an earlier published version resolves
|
|
64
|
+
to a different path. The source is the latest earlier published path that differs from the current
|
|
65
|
+
path.
|
|
66
|
+
|
|
67
|
+
An automatic write skips a source path owned by a manual rule. Editing an automatic rule changes
|
|
68
|
+
its origin to `manual`, so later publications do not update it. A path-template change does not
|
|
69
|
+
migrate historical rules.
|
|
70
|
+
|
|
71
|
+
If a generated rule would close a loop, validation rejects the redirect write and the publication
|
|
72
|
+
fails. The collection has one globally unique `from` field and no locale or hostname scope; two
|
|
73
|
+
localized routes cannot own the same source path with different destinations.
|
|
55
74
|
|
|
56
75
|
## Adapter boundary
|
|
57
76
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klnap/payload-redirect",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Redirect management for Payload CMS.",
|
|
5
5
|
"homepage": "https://github.com/klnap/payload-redirect#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"type": "module",
|
|
25
25
|
"sideEffects": false,
|
|
26
|
+
"packageManager": "bun@1.4.5",
|
|
26
27
|
"exports": {
|
|
27
28
|
".": {
|
|
28
29
|
"types": "./dist/exports/index.d.ts",
|
|
@@ -38,17 +39,20 @@
|
|
|
38
39
|
],
|
|
39
40
|
"scripts": {
|
|
40
41
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
41
|
-
"typecheck:consumer": "
|
|
42
|
+
"typecheck:consumer": "tsc -p tsconfig.type-fixture.json && tsc -p tsconfig.type-fixture.no-generated.json",
|
|
42
43
|
"test": "bun test src tests/unit",
|
|
43
44
|
"test:int": "bun scripts/with-ephemeral-postgres.mjs scripts/ephemeral-postgres.config.mjs -- bun test tests/integration tests/security --max-concurrency 1 --timeout=15000",
|
|
44
45
|
"check:boundaries": "depcruise --config dependency-cruiser.config.mjs --output-type err-long src",
|
|
45
|
-
"lint": "biome
|
|
46
|
-
"format": "biome format
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
46
|
+
"lint": "biome lint .",
|
|
47
|
+
"format": "biome format .",
|
|
48
|
+
"format:write": "biome format --write .",
|
|
49
|
+
"verify:package": "publint --strict && attw --profile esm-only --pack .",
|
|
50
|
+
"check": "bun run format && bun run lint && bun run check:boundaries && bun run typecheck && bun run test && bun run build && bun run typecheck:consumer && bun run verify:package && bun run check:exports",
|
|
51
|
+
"changeset": "changeset",
|
|
52
|
+
"changeset:version": "changeset version",
|
|
53
|
+
"changeset:version-check": "bun run changeset:version && bun run check",
|
|
50
54
|
"check:exports": "bun scripts/check-exports.mjs",
|
|
51
|
-
"build": "
|
|
55
|
+
"build": "bun -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json"
|
|
52
56
|
},
|
|
53
57
|
"peerDependencies": {
|
|
54
58
|
"@payloadcms/db-postgres": ">=3.94.1 <4",
|
|
@@ -60,14 +64,17 @@
|
|
|
60
64
|
}
|
|
61
65
|
},
|
|
62
66
|
"devDependencies": {
|
|
67
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
68
|
+
"@biomejs/biome": "2.6.8",
|
|
69
|
+
"@changesets/cli": "3.0.3",
|
|
63
70
|
"@swc/core": "1.16.22",
|
|
64
71
|
"@payloadcms/db-postgres": "3.94.1",
|
|
65
72
|
"@payloadcms/richtext-lexical": "3.94.1",
|
|
66
|
-
"@types/bun": "
|
|
73
|
+
"@types/bun": "1.4.5",
|
|
67
74
|
"@types/pg": "^8.23.1",
|
|
68
|
-
"@biomejs/biome": "2.6.8",
|
|
69
|
-
"oxlint": "1.94.0",
|
|
70
75
|
"dependency-cruiser": "^18.8.0",
|
|
76
|
+
"npm": "12.2.3",
|
|
77
|
+
"publint": "0.3.25",
|
|
71
78
|
"payload": "3.94.1",
|
|
72
79
|
"pg": "^8.23.1",
|
|
73
80
|
"tsc-alias": "^1.9.10",
|
|
@@ -78,10 +85,11 @@
|
|
|
78
85
|
"happy-dom": "21.0.4"
|
|
79
86
|
},
|
|
80
87
|
"engines": {
|
|
81
|
-
"node": "^24.22.3 || >=26.13.1"
|
|
88
|
+
"node": "^22.23.5 || ^24.22.3 || >=26.13.1"
|
|
82
89
|
},
|
|
83
90
|
"publishConfig": {
|
|
84
|
-
"access": "public"
|
|
91
|
+
"access": "public",
|
|
92
|
+
"registry": "https://registry.npmjs.org/"
|
|
85
93
|
},
|
|
86
94
|
"types": "./dist/exports/index.d.ts"
|
|
87
95
|
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { REDIRECT_CONTEXT_KEYS } from '../context-keys.js';
|
|
2
|
-
export const isInternalRedirectWrite = (req) => Boolean(req.context?.[REDIRECT_CONTEXT_KEYS.internal]);
|
|
3
|
-
const isHumanUser = (req) => Boolean(req.user) && req.user.type !== 'service';
|
|
4
|
-
export const defaultRedirectAccess = {
|
|
5
|
-
read: (() => true),
|
|
6
|
-
create: (({ req }) => isInternalRedirectWrite(req) || isHumanUser(req)),
|
|
7
|
-
update: (({ req }) => isInternalRedirectWrite(req) || isHumanUser(req)),
|
|
8
|
-
delete: (({ req }) => isHumanUser(req)),
|
|
9
|
-
};
|