@klnap/payload-redirect 0.2.0 → 0.3.1
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 +13 -0
- package/README.md +212 -79
- package/dist/core/redirects/auto/create-redirect.d.ts +9 -4
- package/dist/core/redirects/auto/create-redirect.js +26 -66
- 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 +4 -3
- package/dist/core/redirects/auto/match-existing-rule.js.map +1 -0
- package/dist/core/redirects/auto/persist-auto-rule.d.ts +5 -4
- package/dist/core/redirects/auto/persist-auto-rule.js +12 -11
- package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -0
- package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +3 -3
- package/dist/core/redirects/auto/publication/get-publication-transition.js +22 -37
- package/dist/core/redirects/auto/publication/get-publication-transition.js.map +1 -0
- package/dist/core/redirects/auto/publication/locales.d.ts +7 -5
- package/dist/core/redirects/auto/publication/locales.js +33 -28
- package/dist/core/redirects/auto/publication/locales.js.map +1 -0
- package/dist/core/redirects/auto/publication/publish-intent.d.ts +2 -4
- package/dist/core/redirects/auto/publication/publish-intent.js +6 -29
- package/dist/core/redirects/auto/publication/publish-intent.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/document-locale.d.ts +4 -6
- package/dist/core/redirects/auto/publication/versions/document-locale.js +10 -18
- package/dist/core/redirects/auto/publication/versions/document-locale.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/version-pages.d.ts +4 -12
- package/dist/core/redirects/auto/publication/versions/version-pages.js +7 -32
- package/dist/core/redirects/auto/publication/versions/version-pages.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.d.ts +1 -7
- package/dist/core/redirects/auto/publication/versions/version-rows.js +7 -30
- package/dist/core/redirects/auto/publication/versions/version-rows.js.map +1 -0
- package/dist/core/redirects/auto/types.d.ts +27 -8
- package/dist/core/redirects/auto/types.js +1 -0
- package/dist/core/redirects/auto/types.js.map +1 -0
- package/dist/core/redirects/collection/access.d.ts +1 -1
- package/dist/core/redirects/collection/access.js +3 -2
- package/dist/core/redirects/collection/access.js.map +1 -0
- package/dist/core/redirects/collection/collection.d.ts +2 -2
- package/dist/core/redirects/collection/collection.js +10 -9
- 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 +35 -34
- 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 +52 -51
- 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 +6 -5
- 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 +3 -12
- package/dist/core/redirects/queries.js +3 -2
- package/dist/core/redirects/queries.js.map +1 -0
- package/dist/core/url/published-path.d.ts +4 -16
- package/dist/core/url/published-path.js +40 -121
- package/dist/core/url/published-path.js.map +1 -0
- package/dist/core/url/route-params.d.ts +3 -11
- package/dist/core/url/route-params.js +32 -99
- 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 -3
- package/dist/plugin/payload-plugin.js +94 -91
- package/dist/plugin/payload-plugin.js.map +1 -0
- package/dist/plugin/types.d.ts +9 -35
- package/dist/plugin/types.js +1 -0
- package/dist/plugin/types.js.map +1 -0
- package/docs/FAILURE_SEMANTICS.md +31 -27
- package/package.json +29 -20
- package/dist/core/redirects/auto/publication/identifier-snapshot.d.ts +0 -2
- package/dist/core/redirects/auto/publication/identifier-snapshot.js +0 -27
- package/dist/core/redirects/auto/publication/identifier-transitions.d.ts +0 -18
- package/dist/core/redirects/auto/publication/identifier-transitions.js +0 -36
- package/dist/core/redirects/auto/publication/transition-for-locale.d.ts +0 -12
- package/dist/core/redirects/auto/publication/transition-for-locale.js +0 -167
- package/dist/core/redirects/auto/publication/transitions-to-pairs.d.ts +0 -9
- package/dist/core/redirects/auto/publication/transitions-to-pairs.js +0 -22
- package/dist/core/redirects/auto/published-link-config.d.ts +0 -6
- package/dist/core/redirects/auto/published-link-config.js +0 -53
- package/dist/core/redirects/auto/published-path.d.ts +0 -6
- package/dist/core/redirects/auto/published-path.js +0 -28
- package/dist/core/redirects/auto/redirect-custom.d.ts +0 -7
- package/dist/core/redirects/auto/redirect-custom.js +0 -19
- package/dist/core/redirects/identifier-field-path.d.ts +0 -14
- package/dist/core/redirects/identifier-field-path.js +0 -94
- package/dist/core/redirects/runtime.d.ts +0 -9
- package/dist/core/redirects/runtime.js +0 -13
- package/dist/core/url/preview-path.d.ts +0 -1
- package/dist/core/url/preview-path.js +0 -9
- package/dist/core/url/preview-routes.d.ts +0 -3
- package/dist/core/url/preview-routes.js +0 -31
- package/docs/RELEASING.md +0 -41
- package/docs/adr/0002-database-indexes.md +0 -44
|
@@ -1,121 +1,131 @@
|
|
|
1
|
-
import { definePlugin } from
|
|
2
|
-
import { createRedirect } from
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
const PLUGIN_SLUG =
|
|
7
|
-
const DEFAULT_COLLECTION_SLUG =
|
|
8
|
-
const
|
|
1
|
+
import { definePlugin } from "payload";
|
|
2
|
+
import { createRedirect } from "../core/redirects/auto/create-redirect.js";
|
|
3
|
+
import { createRedirectsCollection } from "../core/redirects/collection/collection.js";
|
|
4
|
+
import { isValidLocalePrefix } from "../core/url/published-path.js";
|
|
5
|
+
import { isValidPathTemplate } from "../core/url/route-params.js";
|
|
6
|
+
const PLUGIN_SLUG = "@klnap/payload-redirect";
|
|
7
|
+
const DEFAULT_COLLECTION_SLUG = "redirects";
|
|
8
|
+
const PATH_OPTIONS = new Set(["enabled", "path", "paths", "getPath"]);
|
|
9
|
+
const asRecord = (value) => value && typeof value === "object" && !Array.isArray(value)
|
|
9
10
|
? value
|
|
10
11
|
: undefined;
|
|
11
12
|
const localeCodes = (config) => {
|
|
12
13
|
const locales = config.localization === false ? undefined : config.localization?.locales;
|
|
13
14
|
return Array.isArray(locales)
|
|
14
|
-
? locales.map((locale) => typeof locale ===
|
|
15
|
+
? locales.map((locale) => typeof locale === "string" ? locale : locale.code)
|
|
15
16
|
: [];
|
|
16
17
|
};
|
|
17
18
|
const assertLocaleMap = (value, locales, name) => {
|
|
18
19
|
const map = asRecord(value);
|
|
19
20
|
if (!map)
|
|
20
21
|
throw new Error(`payload-redirect: ${name} must be a locale map`);
|
|
21
|
-
const missing = locales.filter((locale) => !(locale
|
|
22
|
-
const unknown = locales.
|
|
23
|
-
? Object.keys(map).filter((locale) => !locales.includes(locale))
|
|
24
|
-
: [];
|
|
22
|
+
const missing = locales.filter((locale) => !Object.hasOwn(map, locale));
|
|
23
|
+
const unknown = Object.keys(map).filter((locale) => !locales.includes(locale));
|
|
25
24
|
if (missing.length || unknown.length) {
|
|
26
|
-
throw new Error(`payload-redirect: ${name} has invalid locales (missing: ${missing.join(
|
|
25
|
+
throw new Error(`payload-redirect: ${name} has invalid locales (missing: ${missing.join(", ") || "none"}; unknown: ${unknown.join(", ") || "none"})`);
|
|
27
26
|
}
|
|
28
27
|
for (const [locale, path] of Object.entries(map)) {
|
|
29
|
-
if (typeof path !==
|
|
28
|
+
if (typeof path !== "string") {
|
|
30
29
|
throw new Error(`payload-redirect: ${name}.${locale} must be a string`);
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
return map;
|
|
34
33
|
};
|
|
34
|
+
const validatePathOptions = (value, slug, locales, collection) => {
|
|
35
|
+
const options = asRecord(value);
|
|
36
|
+
if (!options)
|
|
37
|
+
throw new Error(`payload-redirect: redirect config for "${slug}" must be an object`);
|
|
38
|
+
const unsupported = Object.keys(options).filter((key) => !PATH_OPTIONS.has(key));
|
|
39
|
+
if (unsupported.length) {
|
|
40
|
+
throw new Error(`payload-redirect: unsupported redirect option for "${slug}": ${unsupported.join(", ")}`);
|
|
41
|
+
}
|
|
42
|
+
const modes = ["path", "paths", "getPath"].filter((key) => Object.hasOwn(options, key));
|
|
43
|
+
if (options.enabled === false) {
|
|
44
|
+
if (modes.length) {
|
|
45
|
+
throw new Error(`payload-redirect: disabled redirect config for "${slug}" cannot define a path`);
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (options.enabled !== undefined && options.enabled !== true) {
|
|
50
|
+
throw new Error(`payload-redirect: redirect config for "${slug}" enabled must be a boolean`);
|
|
51
|
+
}
|
|
52
|
+
if (modes.length !== 1) {
|
|
53
|
+
throw new Error(`payload-redirect: collection "${slug}" must configure exactly one of path, paths, or getPath`);
|
|
54
|
+
}
|
|
55
|
+
if (!collection.versions ||
|
|
56
|
+
typeof collection.versions !== "object" ||
|
|
57
|
+
!collection.versions.drafts) {
|
|
58
|
+
throw new Error(`payload-redirect: automatic redirects for "${slug}" require versions.drafts`);
|
|
59
|
+
}
|
|
60
|
+
if (Object.hasOwn(options, "path")) {
|
|
61
|
+
if (typeof options.path !== "string" || !isValidPathTemplate(options.path)) {
|
|
62
|
+
throw new Error(`payload-redirect: collections.${slug}.path must be a valid site-relative template`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (Object.hasOwn(options, "paths")) {
|
|
66
|
+
if (locales.length === 0) {
|
|
67
|
+
throw new Error(`payload-redirect: collections.${slug}.paths requires Payload localization`);
|
|
68
|
+
}
|
|
69
|
+
const paths = assertLocaleMap(options.paths, locales, `collections.${slug}.paths`);
|
|
70
|
+
for (const [locale, path] of Object.entries(paths)) {
|
|
71
|
+
if (!isValidPathTemplate(path)) {
|
|
72
|
+
throw new Error(`payload-redirect: collections.${slug}.paths.${locale} must be a valid site-relative template`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (typeof options.getPath !== "function") {
|
|
77
|
+
throw new Error(`payload-redirect: collections.${slug}.getPath must be a function`);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const selectedCollectionOptions = (collection, options) => {
|
|
81
|
+
const central = asRecord(options.collections);
|
|
82
|
+
if (central && Object.hasOwn(central, collection.slug)) {
|
|
83
|
+
return central[collection.slug];
|
|
84
|
+
}
|
|
85
|
+
return asRecord(collection.custom)?.redirect;
|
|
86
|
+
};
|
|
35
87
|
const validateOptions = (config, options) => {
|
|
36
88
|
const locales = localeCodes(config);
|
|
37
|
-
if (options.
|
|
38
|
-
const prefixes = assertLocaleMap(options.
|
|
89
|
+
if (options.localePrefixes !== undefined) {
|
|
90
|
+
const prefixes = assertLocaleMap(options.localePrefixes, locales, "localePrefixes");
|
|
39
91
|
for (const [locale, prefix] of Object.entries(prefixes)) {
|
|
40
|
-
if (prefix
|
|
41
|
-
(
|
|
42
|
-
prefix.startsWith('//') ||
|
|
43
|
-
prefix.includes('?') ||
|
|
44
|
-
prefix.includes('#') ||
|
|
45
|
-
prefix.includes('\\') ||
|
|
46
|
-
[...prefix].some((character) => {
|
|
47
|
-
const code = character.charCodeAt(0);
|
|
48
|
-
return code < 32 || code === 127;
|
|
49
|
-
}))) {
|
|
50
|
-
throw new Error(`payload-redirect: localePrefix.${locale} must be a site-relative path`);
|
|
92
|
+
if (!isValidLocalePrefix(prefix)) {
|
|
93
|
+
throw new Error(`payload-redirect: localePrefixes.${locale} must be a site-relative path prefix`);
|
|
51
94
|
}
|
|
52
95
|
}
|
|
53
96
|
}
|
|
54
|
-
for (const
|
|
55
|
-
if (!collectionOptions)
|
|
56
|
-
continue;
|
|
97
|
+
for (const slug of Object.keys(asRecord(options.collections) ?? {})) {
|
|
57
98
|
if (!config.collections?.some((collection) => collection.slug === slug)) {
|
|
58
99
|
throw new Error(`payload-redirect: configured collection "${slug}" is not registered`);
|
|
59
100
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (collectionOptions.enabled === false)
|
|
101
|
+
}
|
|
102
|
+
for (const collection of config.collections ?? []) {
|
|
103
|
+
const selected = selectedCollectionOptions(collection, options);
|
|
104
|
+
if (selected === undefined)
|
|
65
105
|
continue;
|
|
66
|
-
|
|
67
|
-
if (typeof collectionOptions.path !== 'string' || !collectionOptions.path.startsWith('/')) {
|
|
68
|
-
throw new Error(`payload-redirect: collection "${slug}" path must start with "/"`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
if ('paths' in collectionOptions) {
|
|
72
|
-
if (locales.length === 0) {
|
|
73
|
-
throw new Error(`payload-redirect: collection "${slug}" paths requires Payload localization`);
|
|
74
|
-
}
|
|
75
|
-
const paths = assertLocaleMap(collectionOptions.paths, locales, `collections.${slug}.paths`);
|
|
76
|
-
for (const [locale, path] of Object.entries(paths)) {
|
|
77
|
-
if (!path.startsWith('/')) {
|
|
78
|
-
throw new Error(`payload-redirect: collections.${slug}.paths.${locale} must start with "/"`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if ('getPath' in collectionOptions && typeof collectionOptions.getPath !== 'function') {
|
|
83
|
-
throw new Error(`payload-redirect: collection "${slug}" getPath must be a function`);
|
|
84
|
-
}
|
|
106
|
+
validatePathOptions(selected, collection.slug, locales, collection);
|
|
85
107
|
}
|
|
86
108
|
};
|
|
87
|
-
const wireCollectionRedirect = (collection, options) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const redirect = asRecord(custom?.redirect);
|
|
92
|
-
const central = options.collections?.[collection.slug];
|
|
93
|
-
const redirectOptions = readRedirectCustom(custom);
|
|
94
|
-
const identifier = central?.identifier ?? redirectOptions?.identifier;
|
|
95
|
-
if (typeof identifier !== 'string' || !identifier.trim())
|
|
96
|
-
return collection;
|
|
97
|
-
if (central?.enabled === false || redirect?.enabled === false)
|
|
109
|
+
const wireCollectionRedirect = (collection, options, collectionSlug) => {
|
|
110
|
+
const selected = selectedCollectionOptions(collection, options);
|
|
111
|
+
const record = asRecord(selected);
|
|
112
|
+
if (!record || record.enabled === false)
|
|
98
113
|
return collection;
|
|
99
|
-
const publishedLink = {
|
|
100
|
-
...(central?.path !== undefined ? { path: central.path } : {}),
|
|
101
|
-
...(central?.paths !== undefined ? { paths: central.paths } : {}),
|
|
102
|
-
...(central?.getPath !== undefined ? { getPath: central.getPath } : {}),
|
|
103
|
-
...asRecord(redirect?.publishedLink),
|
|
104
|
-
};
|
|
105
|
-
const redirectConfig = {
|
|
106
|
-
...redirect,
|
|
107
|
-
identifier: identifier.trim(),
|
|
108
|
-
publishedLink: Object.keys(publishedLink).length > 0 ? publishedLink : redirect?.publishedLink,
|
|
109
|
-
};
|
|
110
114
|
const afterChange = collection.hooks?.afterChange;
|
|
111
115
|
const hooks = Array.isArray(afterChange) ? afterChange : afterChange ? [afterChange] : [];
|
|
112
|
-
const
|
|
116
|
+
const redirectOptions = selected;
|
|
113
117
|
return {
|
|
114
118
|
...collection,
|
|
115
|
-
custom: { ...custom, redirect: redirectConfig },
|
|
116
119
|
hooks: {
|
|
117
120
|
...collection.hooks,
|
|
118
|
-
afterChange: [
|
|
121
|
+
afterChange: [
|
|
122
|
+
...hooks,
|
|
123
|
+
createRedirect({
|
|
124
|
+
collectionSlug,
|
|
125
|
+
pathOptions: redirectOptions,
|
|
126
|
+
localePrefixes: options.localePrefixes,
|
|
127
|
+
}),
|
|
128
|
+
],
|
|
119
129
|
},
|
|
120
130
|
};
|
|
121
131
|
};
|
|
@@ -125,28 +135,21 @@ const definedRedirectsPlugin = definePlugin({
|
|
|
125
135
|
validateOptions(config, options);
|
|
126
136
|
const registeredCount = config.plugins?.filter((plugin) => plugin.slug === PLUGIN_SLUG).length ?? 0;
|
|
127
137
|
if (registeredCount > 1) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
}
|
|
130
|
-
if (Object.hasOwn(asRecord(config.custom) ?? {}, 'payloadRedirect')) {
|
|
131
|
-
throw new Error('payload-redirect: config.custom.payloadRedirect is reserved');
|
|
138
|
+
throw new Error("payload-redirect: register redirects() only once in config.plugins");
|
|
132
139
|
}
|
|
133
140
|
const redirectsCollection = createRedirectsCollection(options.overrides);
|
|
134
141
|
const collectionSlug = redirectsCollection.slug || DEFAULT_COLLECTION_SLUG;
|
|
135
|
-
|
|
136
|
-
if (existing) {
|
|
142
|
+
if (config.collections?.some(({ slug }) => slug === collectionSlug)) {
|
|
137
143
|
throw new Error(`payload-redirect: collection slug "${collectionSlug}" is already registered; configure it with redirects({ overrides: { slug } }) instead of defining a host collection`);
|
|
138
144
|
}
|
|
139
145
|
return {
|
|
140
146
|
...config,
|
|
141
|
-
custom: registerRedirectRuntime(config, {
|
|
142
|
-
collectionSlug,
|
|
143
|
-
localePrefix: options.localePrefix,
|
|
144
|
-
}),
|
|
145
147
|
collections: [
|
|
146
|
-
...(config.collections ?? []).map((collection) => wireCollectionRedirect(collection, options)),
|
|
148
|
+
...(config.collections ?? []).map((collection) => wireCollectionRedirect(collection, options, collectionSlug)),
|
|
147
149
|
redirectsCollection,
|
|
148
150
|
],
|
|
149
151
|
};
|
|
150
152
|
},
|
|
151
153
|
});
|
|
152
154
|
export const redirects = (options = {}) => definedRedirectsPlugin(options);
|
|
155
|
+
//# 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,EAAsC,YAAY,EAAe,MAAM,SAAS,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,MAAM,+CAA+C,CAAC;AAC/E,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAC;AAC3F,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAGtE,MAAM,WAAW,GAAG,yBAAyB,CAAC;AAC9C,MAAM,uBAAuB,GAAG,WAAW,CAAC;AAC5C,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC;AAEtE,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAuC,EAAE,CACvE,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;IACzD,CAAC,CAAE,KAAiC;IACpC,CAAC,CAAC,SAAS,CAAC;AAEhB,MAAM,WAAW,GAAG,CAAC,MAAc,EAAY,EAAE;IAC/C,MAAM,OAAO,GAAG,MAAM,CAAC,YAAY,KAAK,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC;IACzF,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QAC3B,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,C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AAO,EAAE,EAAE,EAAE;QACjC,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACjC,MAAM,eAAe,GACnB,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,WAAW,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC;QAC9E,IAAI,eAAe,GAAG,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;QACxF,CAAC;QAED,MAAM,mBAAmB,GAAG,yBAAyB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACzE,MAAM,cAAc,GAAG,mBAAmB,CAAC,IAAI,IAAI,uBAAuB,CAAC;QAC3E,IAAI,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,cAAc,CAAC,EAAE,CAAC;YACpE,MAAM,IAAI,KAAK,CACb,sCAAsC,cAAc,qHAAqH,CAC1K,CAAC;QACJ,CAAC;QAED,OAAO;YACL,GAAG,MAAM;YACT,WAAW,EAAE;gBACX,GAAG,CAAC,MAAM,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAC/C,sBAAsB,CAAC,UAAU,EAAE,OAAO,EAAE,cAAc,CAAC,CAC5D;gBACD,mBAAmB;aACpB;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,OAAO,GAAqB,EAAE,EAAU,EAAE,CAClE,sBAAsB,CAAC,OAAO,CAAC,CAAC","sourcesContent":["import { type CollectionConfig, type Config, definePlugin, type Plugin } from \"payload\";\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 { RedirectCollectionOptions, RedirectsOptions } 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 slug: string,\n locales: string[],\n collection: CollectionConfig,\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: collection \"${slug}\" must configure exactly one of path, paths, or getPath`,\n );\n }\n if (\n !collection.versions ||\n typeof collection.versions !== \"object\" ||\n !collection.versions.drafts\n ) {\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: collections.${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(`payload-redirect: collections.${slug}.paths requires Payload localization`);\n }\n const paths = assertLocaleMap(options.paths, locales, `collections.${slug}.paths`);\n for (const [locale, path] of Object.entries(paths)) {\n if (!isValidPathTemplate(path)) {\n throw new Error(\n `payload-redirect: collections.${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 selectedCollectionOptions = (\n collection: CollectionConfig,\n options: RedirectsOptions,\n): unknown => {\n const central = asRecord(options.collections);\n if (central && Object.hasOwn(central, collection.slug)) {\n return central[collection.slug];\n }\n return asRecord(collection.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 collection of config.collections ?? []) {\n const selected = selectedCollectionOptions(collection, options);\n if (selected === undefined) continue;\n validatePathOptions(selected, collection.slug, locales, collection);\n }\n};\n\nconst wireCollectionRedirect = (\n collection: CollectionConfig,\n options: RedirectsOptions,\n collectionSlug: string,\n): CollectionConfig => {\n const selected = selectedCollectionOptions(collection, options);\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 collectionSlug,\n pathOptions: redirectOptions,\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 };\n },\n});\n\nexport const redirects = (options: RedirectsOptions = {}): Plugin =>\n definedRedirectsPlugin(options);\n"]}
|
package/dist/plugin/types.d.ts
CHANGED
|
@@ -1,39 +1,13 @@
|
|
|
1
|
-
import type { CollectionSlug
|
|
2
|
-
import type {
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type PathModes<TSlug extends CollectionSlug> = {
|
|
8
|
-
path: string;
|
|
9
|
-
paths?: never;
|
|
10
|
-
getPath?: never;
|
|
11
|
-
} | {
|
|
12
|
-
path?: never;
|
|
13
|
-
paths: RedirectLocaleMap;
|
|
14
|
-
getPath?: never;
|
|
15
|
-
} | {
|
|
16
|
-
path?: never;
|
|
17
|
-
paths?: never;
|
|
18
|
-
getPath: (args: {
|
|
19
|
-
doc: DataFromCollectionSlug<TSlug>;
|
|
20
|
-
locale: TypedLocale;
|
|
21
|
-
}) => string | null;
|
|
22
|
-
};
|
|
23
|
-
type DisabledPathModes<TSlug extends CollectionSlug> = Partial<PathModes<TSlug>>;
|
|
24
|
-
export type RedirectCollectionOptions<TSlug extends CollectionSlug> = ({
|
|
25
|
-
identifier: string;
|
|
26
|
-
enabled?: true;
|
|
27
|
-
} & PathModes<TSlug>) | ({
|
|
28
|
-
identifier?: string;
|
|
29
|
-
enabled: false;
|
|
30
|
-
} & DisabledPathModes<TSlug>);
|
|
1
|
+
import type { CollectionSlug } 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
|
+
type LocaleMap = string extends LocaleCode ? Record<string, string> : Record<LocaleCode, string>;
|
|
6
|
+
export type RedirectCollectionOptions<TSlug extends CollectionSlug = CollectionSlug> = RedirectPathOptions<TSlug>;
|
|
31
7
|
export type RedirectsOptions = {
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
localePrefix?: RedirectLocaleMap;
|
|
36
|
-
/** Centralized per-collection automatic redirect configuration. */
|
|
8
|
+
/** Payload locale codes map to explicit site-relative prefixes; omission means no prefix. */
|
|
9
|
+
localePrefixes?: LocaleMap;
|
|
10
|
+
/** Each entry opts that collection into automatic redirects. Central entries replace local config. */
|
|
37
11
|
collections?: {
|
|
38
12
|
[TSlug in CollectionSlug]?: RedirectCollectionOptions<TSlug>;
|
|
39
13
|
};
|
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 { CollectionSlug } 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<TSlug>;\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 /** Native collection overrides for the plugin-owned redirects collection. */\n overrides?: RedirectCollectionOverrides;\n};\n"]}
|
|
@@ -4,27 +4,31 @@ 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
|
-
|
|
|
14
|
-
|
|
|
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 has no redirect configuration | The collection is left unchanged; no publication hook is added. |
|
|
14
|
+
| Central collection options name an unregistered collection | Plugin configuration throws and names the collection. |
|
|
15
|
+
| A configured collection has no `versions.drafts` | Plugin configuration throws and names the collection. |
|
|
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. |
|
|
15
19
|
|
|
16
20
|
## Redirect collection writes
|
|
17
21
|
|
|
18
22
|
`from` and `to` must be non-empty site-relative paths. The plugin normalizes them before checking
|
|
19
23
|
duplicates and loops.
|
|
20
24
|
|
|
21
|
-
| Condition
|
|
22
|
-
|
|
23
|
-
| Duplicate normalized `from`
|
|
24
|
-
| Enabled redirect chain loops or exceeds 1,000 hops
|
|
25
|
-
| `
|
|
26
|
-
| `to` is missing or invalid
|
|
27
|
-
| `origin` is invalid
|
|
25
|
+
| Condition | Result |
|
|
26
|
+
| --------------------------------------------------- | -------------------------------------------------------------- |
|
|
27
|
+
| Duplicate normalized `from` | Payload validation error on `from`. |
|
|
28
|
+
| Enabled redirect chain loops or exceeds 1,000 hops | Payload validation error on `to`. |
|
|
29
|
+
| `statusCode` is not `301` or `302` | Payload field validation error. |
|
|
30
|
+
| `to` is missing or invalid | Payload field validation error. |
|
|
31
|
+
| `origin` is invalid | Payload field validation error. |
|
|
28
32
|
| A manual rule already owns an automatic source path | Automatic write is skipped; the manual rule remains unchanged. |
|
|
29
33
|
|
|
30
34
|
Internal writes carry server-side request context and run inside the Payload transaction. PostgreSQL
|
|
@@ -33,20 +37,20 @@ same concurrent-write guarantee.
|
|
|
33
37
|
|
|
34
38
|
## Published paths
|
|
35
39
|
|
|
36
|
-
Automatic rules use `
|
|
37
|
-
|
|
40
|
+
Automatic rules use one of `path`, `paths`, or `getPath`, configured centrally or in
|
|
41
|
+
`custom.redirect`. Central configuration replaces local configuration.
|
|
38
42
|
|
|
39
|
-
| Condition
|
|
40
|
-
|
|
41
|
-
| No matching path configuration
|
|
42
|
-
| Template value is missing or unsafe
|
|
43
|
-
| `getPath` returns `null` | That
|
|
44
|
-
| `getPath` returns a non-string value
|
|
45
|
-
| Locale is absent
|
|
46
|
-
| Locale is missing from a configured prefix map
|
|
47
|
-
| Generated source and destination normalize to the same path | No redirect is written.
|
|
43
|
+
| Condition | Result |
|
|
44
|
+
| ----------------------------------------------------------- | ----------------------------------------------------------------------------- |
|
|
45
|
+
| No matching path configuration | Automatic generation is not registered for that collection. |
|
|
46
|
+
| Template value is missing or unsafe | That locale's publication transition is skipped. |
|
|
47
|
+
| `getPath` returns `null` or a non-site-relative path | That locale's transition is skipped. |
|
|
48
|
+
| `getPath` returns a non-string value | That locale's transition is skipped. |
|
|
49
|
+
| Locale is absent | A shared `path` works without a prefix map; localized `paths` cannot resolve. |
|
|
50
|
+
| Locale is missing from a configured prefix map | Plugin configuration fails before Payload builds. |
|
|
51
|
+
| Generated source and destination normalize to the same path | No redirect is written. |
|
|
48
52
|
|
|
49
|
-
Draft saves, first publications, unpublishes, deletions, and unchanged
|
|
53
|
+
Draft saves, first publications, unpublishes, deletions, and unchanged complete paths do not create
|
|
50
54
|
automatic rules.
|
|
51
55
|
|
|
52
56
|
## Adapter boundary
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klnap/payload-redirect",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Redirect management for Payload CMS.",
|
|
5
5
|
"homepage": "https://github.com/klnap/payload-redirect#readme",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/klnap/payload-redirect/issues"
|
|
@@ -10,43 +10,49 @@
|
|
|
10
10
|
"payload",
|
|
11
11
|
"payloadcms",
|
|
12
12
|
"payload-plugin",
|
|
13
|
+
"cms",
|
|
14
|
+
"redirect",
|
|
13
15
|
"redirects",
|
|
14
|
-
"
|
|
16
|
+
"redirect-management"
|
|
15
17
|
],
|
|
16
18
|
"license": "MIT",
|
|
17
19
|
"author": "klnap",
|
|
18
20
|
"repository": {
|
|
19
21
|
"type": "git",
|
|
20
|
-
"url": "https://github.com/klnap/payload-redirect"
|
|
22
|
+
"url": "git+https://github.com/klnap/payload-redirect.git"
|
|
21
23
|
},
|
|
22
24
|
"type": "module",
|
|
23
25
|
"sideEffects": false,
|
|
26
|
+
"packageManager": "bun@1.4.5",
|
|
24
27
|
"exports": {
|
|
25
28
|
".": {
|
|
26
29
|
"types": "./dist/exports/index.d.ts",
|
|
27
|
-
"import": "./dist/exports/index.js"
|
|
28
|
-
"default": "./dist/exports/index.js"
|
|
30
|
+
"import": "./dist/exports/index.js"
|
|
29
31
|
}
|
|
30
32
|
},
|
|
31
33
|
"files": [
|
|
32
34
|
"dist",
|
|
33
35
|
"README.md",
|
|
34
36
|
"LICENSE",
|
|
35
|
-
"docs"
|
|
37
|
+
"docs/FAILURE_SEMANTICS.md",
|
|
38
|
+
"CHANGELOG.md"
|
|
36
39
|
],
|
|
37
40
|
"scripts": {
|
|
38
41
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
39
|
-
"typecheck:consumer": "
|
|
42
|
+
"typecheck:consumer": "tsc -p tsconfig.type-fixture.json && tsc -p tsconfig.type-fixture.no-generated.json",
|
|
40
43
|
"test": "bun test src tests/unit",
|
|
41
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",
|
|
42
45
|
"check:boundaries": "depcruise --config dependency-cruiser.config.mjs --output-type err-long src",
|
|
43
|
-
"lint": "biome
|
|
44
|
-
"format": "biome format
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
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",
|
|
48
54
|
"check:exports": "bun scripts/check-exports.mjs",
|
|
49
|
-
"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"
|
|
50
56
|
},
|
|
51
57
|
"peerDependencies": {
|
|
52
58
|
"@payloadcms/db-postgres": ">=3.94.1 <4",
|
|
@@ -58,14 +64,17 @@
|
|
|
58
64
|
}
|
|
59
65
|
},
|
|
60
66
|
"devDependencies": {
|
|
67
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
68
|
+
"@biomejs/biome": "2.6.8",
|
|
69
|
+
"@changesets/cli": "3.0.3",
|
|
61
70
|
"@swc/core": "1.16.22",
|
|
62
71
|
"@payloadcms/db-postgres": "3.94.1",
|
|
63
72
|
"@payloadcms/richtext-lexical": "3.94.1",
|
|
64
|
-
"@types/bun": "
|
|
73
|
+
"@types/bun": "1.4.5",
|
|
65
74
|
"@types/pg": "^8.23.1",
|
|
66
|
-
"@biomejs/biome": "2.6.8",
|
|
67
|
-
"oxlint": "1.94.0",
|
|
68
75
|
"dependency-cruiser": "^18.8.0",
|
|
76
|
+
"npm": "12.2.3",
|
|
77
|
+
"publint": "0.3.25",
|
|
69
78
|
"payload": "3.94.1",
|
|
70
79
|
"pg": "^8.23.1",
|
|
71
80
|
"tsc-alias": "^1.9.10",
|
|
@@ -76,11 +85,11 @@
|
|
|
76
85
|
"happy-dom": "21.0.4"
|
|
77
86
|
},
|
|
78
87
|
"engines": {
|
|
79
|
-
"node": "^24.22.3 || >=26.13.1"
|
|
88
|
+
"node": "^22.23.5 || ^24.22.3 || >=26.13.1"
|
|
80
89
|
},
|
|
81
90
|
"publishConfig": {
|
|
82
|
-
"access": "public"
|
|
91
|
+
"access": "public",
|
|
92
|
+
"registry": "https://registry.npmjs.org/"
|
|
83
93
|
},
|
|
84
|
-
"main": "./dist/exports/index.js",
|
|
85
94
|
"types": "./dist/exports/index.d.ts"
|
|
86
95
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { identifierValueToString } from './identifier-transitions.js';
|
|
2
|
-
import { readIdentifierAtPath } from '../../identifier-field-path.js';
|
|
3
|
-
import { normalizePathOrUrl } from '../../normalize-path.js';
|
|
4
|
-
export const normalizeIdentifierValue = (value) => {
|
|
5
|
-
if (value.startsWith('/') || /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value)) {
|
|
6
|
-
return normalizePathOrUrl(value);
|
|
7
|
-
}
|
|
8
|
-
return value;
|
|
9
|
-
};
|
|
10
|
-
export const extractIdentifierFromVersionSnapshot = (doc, identifier, locale, localized, contextLocale) => {
|
|
11
|
-
if (localized && !identifier.includes('.')) {
|
|
12
|
-
const field = doc[identifier];
|
|
13
|
-
if (typeof field === 'string') {
|
|
14
|
-
if (contextLocale !== undefined && contextLocale !== locale) {
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
17
|
-
const raw = identifierValueToString(field);
|
|
18
|
-
if (!raw)
|
|
19
|
-
return undefined;
|
|
20
|
-
return normalizeIdentifierValue(raw);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
const raw = readIdentifierAtPath(doc, identifier, locale, localized);
|
|
24
|
-
if (!raw)
|
|
25
|
-
return undefined;
|
|
26
|
-
return normalizeIdentifierValue(raw);
|
|
27
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type IdentifierTransition = {
|
|
2
|
-
locale?: string;
|
|
3
|
-
previous: string;
|
|
4
|
-
current: string;
|
|
5
|
-
};
|
|
6
|
-
export declare const identifierValueToString: (value: unknown) => string | undefined;
|
|
7
|
-
/**
|
|
8
|
-
* Compare previous/current per locale without cross-locale fallback.
|
|
9
|
-
*/
|
|
10
|
-
export declare const collectLocalizedIdentifierTransitions: (args: {
|
|
11
|
-
locales: string[];
|
|
12
|
-
previousByLocale: Record<string, string | undefined>;
|
|
13
|
-
currentByLocale: Record<string, string | undefined>;
|
|
14
|
-
}) => IdentifierTransition[];
|
|
15
|
-
export declare const collectScalarIdentifierTransition: (args: {
|
|
16
|
-
previous?: string;
|
|
17
|
-
current?: string;
|
|
18
|
-
}) => IdentifierTransition | undefined;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export const identifierValueToString = (value) => {
|
|
2
|
-
if (value == null)
|
|
3
|
-
return undefined;
|
|
4
|
-
if (typeof value === 'string') {
|
|
5
|
-
const t = value.trim();
|
|
6
|
-
return t.length > 0 ? t : undefined;
|
|
7
|
-
}
|
|
8
|
-
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
9
|
-
return String(value);
|
|
10
|
-
}
|
|
11
|
-
return undefined;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Compare previous/current per locale without cross-locale fallback.
|
|
15
|
-
*/
|
|
16
|
-
export const collectLocalizedIdentifierTransitions = (args) => {
|
|
17
|
-
const out = [];
|
|
18
|
-
for (const locale of args.locales) {
|
|
19
|
-
const previous = args.previousByLocale[locale];
|
|
20
|
-
const current = args.currentByLocale[locale];
|
|
21
|
-
if (!previous || !current)
|
|
22
|
-
continue;
|
|
23
|
-
if (previous === current)
|
|
24
|
-
continue;
|
|
25
|
-
out.push({ locale, previous, current });
|
|
26
|
-
}
|
|
27
|
-
return out;
|
|
28
|
-
};
|
|
29
|
-
export const collectScalarIdentifierTransition = (args) => {
|
|
30
|
-
const { previous, current } = args;
|
|
31
|
-
if (!previous || !current)
|
|
32
|
-
return undefined;
|
|
33
|
-
if (previous === current)
|
|
34
|
-
return undefined;
|
|
35
|
-
return { previous, current };
|
|
36
|
-
};
|