@klnap/payload-redirect 0.1.0 → 0.3.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 +7 -0
- package/README.md +179 -158
- package/dist/core/redirects/auto/create-redirect.d.ts +8 -3
- package/dist/core/redirects/auto/create-redirect.js +20 -45
- package/dist/core/redirects/auto/match-existing-rule.js +1 -1
- package/dist/core/redirects/auto/persist-auto-rule.d.ts +3 -2
- package/dist/core/redirects/auto/persist-auto-rule.js +5 -4
- package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +2 -2
- package/dist/core/redirects/auto/publication/get-publication-transition.js +20 -36
- package/dist/core/redirects/auto/publication/locales.d.ts +7 -5
- package/dist/core/redirects/auto/publication/locales.js +32 -28
- package/dist/core/redirects/auto/publication/publish-intent.d.ts +1 -3
- package/dist/core/redirects/auto/publication/publish-intent.js +2 -26
- package/dist/core/redirects/auto/publication/versions/document-locale.d.ts +4 -6
- package/dist/core/redirects/auto/publication/versions/document-locale.js +9 -18
- package/dist/core/redirects/auto/publication/versions/version-pages.d.ts +2 -10
- package/dist/core/redirects/auto/publication/versions/version-pages.js +4 -30
- package/dist/core/redirects/auto/publication/versions/version-rows.d.ts +1 -7
- package/dist/core/redirects/auto/publication/versions/version-rows.js +4 -28
- package/dist/core/redirects/auto/types.d.ts +27 -6
- 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 +20 -23
- package/dist/core/redirects/collection/duplicate-guard.d.ts +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +6 -9
- package/dist/core/redirects/collection/fields.d.ts +1 -0
- package/dist/core/redirects/collection/fields.js +77 -77
- 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 +5 -8
- package/dist/core/redirects/queries.d.ts +4 -18
- package/dist/core/redirects/queries.js +6 -10
- package/dist/core/url/published-path.d.ts +5 -16
- package/dist/core/url/published-path.js +42 -95
- package/dist/core/url/route-params.d.ts +3 -11
- package/dist/core/url/route-params.js +31 -99
- package/dist/exports/index.d.ts +1 -7
- package/dist/exports/index.js +0 -4
- package/dist/plugin/payload-plugin.d.ts +3 -3
- package/dist/plugin/payload-plugin.js +142 -43
- package/dist/plugin/types.d.ts +17 -0
- package/dist/plugin/types.js +1 -0
- package/docs/FAILURE_SEMANTICS.md +50 -94
- package/package.json +11 -9
- 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 -163
- package/dist/core/redirects/auto/publication/transitions-to-pairs.d.ts +0 -7
- package/dist/core/redirects/auto/publication/transitions-to-pairs.js +0 -16
- package/dist/core/redirects/auto/published-link-config.d.ts +0 -6
- package/dist/core/redirects/auto/published-link-config.js +0 -49
- package/dist/core/redirects/auto/published-path.d.ts +0 -6
- package/dist/core/redirects/auto/published-path.js +0 -11
- 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/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/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/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
- package/docs/RELEASING.md +0 -41
- package/docs/adr/0002-database-indexes.md +0 -45
|
@@ -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
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { CollectionConfig, PayloadRequest } from 'payload';
|
|
2
|
-
import type { PublicationTransition } from '../types.js';
|
|
3
|
-
export declare const transitionForLocale: ({ req, collection, docId, locale, identifier, localizedField, hookDoc, publishIntent, }: {
|
|
4
|
-
req: PayloadRequest;
|
|
5
|
-
collection: CollectionConfig;
|
|
6
|
-
docId: string | number;
|
|
7
|
-
locale: string;
|
|
8
|
-
identifier: string;
|
|
9
|
-
localizedField: boolean;
|
|
10
|
-
hookDoc: Record<string, unknown>;
|
|
11
|
-
publishIntent: boolean;
|
|
12
|
-
}) => Promise<PublicationTransition>;
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { identifierValueToString } from './identifier-transitions.js';
|
|
2
|
-
import { fetchLocalePublishedDoc } from './versions/document-locale.js';
|
|
3
|
-
import { fetchPublishedVersionPages, fetchVersionHistoryPages, } from './versions/version-pages.js';
|
|
4
|
-
import { extractIdentifierFromVersionSnapshot, normalizeIdentifierValue, } from './identifier-snapshot.js';
|
|
5
|
-
import { hasLocalizedDraftStatus, resolveDefaultLocale } from './locales.js';
|
|
6
|
-
import { isLocalePublished } from './publish-intent.js';
|
|
7
|
-
export const transitionForLocale = async ({ req, collection, docId, locale, identifier, localizedField, hookDoc, publishIntent, }) => {
|
|
8
|
-
const localizeStatus = hasLocalizedDraftStatus(collection);
|
|
9
|
-
const publishedDoc = await fetchLocalePublishedDoc({
|
|
10
|
-
req,
|
|
11
|
-
collection,
|
|
12
|
-
docId,
|
|
13
|
-
locale,
|
|
14
|
-
});
|
|
15
|
-
const hookPublished = isLocalePublished(hookDoc._status, locale, localizeStatus);
|
|
16
|
-
const docPublished = publishedDoc != null && isLocalePublished(publishedDoc._status, locale, localizeStatus);
|
|
17
|
-
const identifierSource = publishedDoc ?? hookDoc;
|
|
18
|
-
const defaultLocale = resolveDefaultLocale(req);
|
|
19
|
-
const useAllLocaleVersions = localizedField;
|
|
20
|
-
let publishedVersions = await fetchPublishedVersionPages({
|
|
21
|
-
req,
|
|
22
|
-
collectionSlug: collection.slug,
|
|
23
|
-
docId,
|
|
24
|
-
locale,
|
|
25
|
-
versionLocale: useAllLocaleVersions ? undefined : locale,
|
|
26
|
-
versionFilter: localizeStatus ? { localizeStatus: true, defaultLocale, identifier } : undefined,
|
|
27
|
-
});
|
|
28
|
-
if (localizedField && defaultLocale && locale !== defaultLocale && !publishIntent) {
|
|
29
|
-
publishedVersions = publishedVersions.filter((row) => {
|
|
30
|
-
const version = row.version;
|
|
31
|
-
if (!version)
|
|
32
|
-
return false;
|
|
33
|
-
const rawField = version[identifier];
|
|
34
|
-
if (typeof rawField === 'string') {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
const slug = extractIdentifierFromVersionSnapshot(version, identifier, locale, true, locale);
|
|
38
|
-
if (!slug)
|
|
39
|
-
return false;
|
|
40
|
-
const defaultSlug = extractIdentifierFromVersionSnapshot(version, identifier, defaultLocale, true, locale);
|
|
41
|
-
return slug !== defaultSlug;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
const seenIdentifiers = new Set();
|
|
45
|
-
publishedVersions = publishedVersions.filter((row) => {
|
|
46
|
-
const version = row.version;
|
|
47
|
-
if (!version)
|
|
48
|
-
return false;
|
|
49
|
-
const value = extractIdentifierFromVersionSnapshot(version, identifier, locale, localizedField, locale);
|
|
50
|
-
if (!value)
|
|
51
|
-
return false;
|
|
52
|
-
if (seenIdentifiers.has(value))
|
|
53
|
-
return false;
|
|
54
|
-
seenIdentifiers.add(value);
|
|
55
|
-
return true;
|
|
56
|
-
});
|
|
57
|
-
if (publishedVersions.length === 0 && !publishIntent && !hookPublished && !docPublished) {
|
|
58
|
-
return { published: false };
|
|
59
|
-
}
|
|
60
|
-
if (!publishIntent && !hookPublished && !docPublished && publishedVersions.length < 2) {
|
|
61
|
-
return { published: false };
|
|
62
|
-
}
|
|
63
|
-
const draftDoc = publishIntent && localizedField
|
|
64
|
-
? await fetchLocalePublishedDoc({
|
|
65
|
-
req,
|
|
66
|
-
collection,
|
|
67
|
-
docId,
|
|
68
|
-
locale,
|
|
69
|
-
draft: true,
|
|
70
|
-
})
|
|
71
|
-
: undefined;
|
|
72
|
-
const currentVersion = publishedVersions[0]?.version;
|
|
73
|
-
const current = (draftDoc
|
|
74
|
-
? extractIdentifierFromVersionSnapshot(draftDoc, identifier, locale, localizedField, locale)
|
|
75
|
-
: undefined) ??
|
|
76
|
-
extractIdentifierFromVersionSnapshot(hookDoc, identifier, locale, localizedField, locale) ??
|
|
77
|
-
(currentVersion
|
|
78
|
-
? extractIdentifierFromVersionSnapshot(currentVersion, identifier, locale, localizedField, locale)
|
|
79
|
-
: undefined) ??
|
|
80
|
-
extractIdentifierFromVersionSnapshot(identifierSource, identifier, locale, localizedField, locale);
|
|
81
|
-
if (!current) {
|
|
82
|
-
return { published: true };
|
|
83
|
-
}
|
|
84
|
-
let defaultSlug;
|
|
85
|
-
if (localizedField && defaultLocale) {
|
|
86
|
-
defaultSlug = extractIdentifierFromVersionSnapshot(hookDoc, identifier, defaultLocale, true, locale);
|
|
87
|
-
if (!defaultSlug) {
|
|
88
|
-
const defaultDoc = await fetchLocalePublishedDoc({
|
|
89
|
-
req,
|
|
90
|
-
collection,
|
|
91
|
-
docId,
|
|
92
|
-
locale: defaultLocale,
|
|
93
|
-
});
|
|
94
|
-
if (defaultDoc) {
|
|
95
|
-
defaultSlug = extractIdentifierFromVersionSnapshot(defaultDoc, identifier, defaultLocale, true, defaultLocale);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
let versionsForPrevious = [...publishedVersions];
|
|
100
|
-
if (publishIntent && localizedField) {
|
|
101
|
-
versionsForPrevious = await fetchVersionHistoryPages({
|
|
102
|
-
req,
|
|
103
|
-
collectionSlug: collection.slug,
|
|
104
|
-
docId,
|
|
105
|
-
versionLocale: locale,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
if (current) {
|
|
109
|
-
const hasCurrentHead = versionsForPrevious.some((row) => {
|
|
110
|
-
const version = row.version;
|
|
111
|
-
if (!version)
|
|
112
|
-
return false;
|
|
113
|
-
return (extractIdentifierFromVersionSnapshot(version, identifier, locale, localizedField, locale) === current);
|
|
114
|
-
});
|
|
115
|
-
if (!hasCurrentHead) {
|
|
116
|
-
versionsForPrevious.unshift({ version: hookDoc });
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
let previous;
|
|
120
|
-
for (const row of versionsForPrevious) {
|
|
121
|
-
const version = row.version;
|
|
122
|
-
if (!version)
|
|
123
|
-
continue;
|
|
124
|
-
if (localizedField) {
|
|
125
|
-
const rawField = version[identifier];
|
|
126
|
-
if (rawField && typeof rawField === 'object' && !Array.isArray(rawField)) {
|
|
127
|
-
if (!(locale in rawField))
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
const candidate = extractIdentifierFromVersionSnapshot(version, identifier, locale, localizedField, locale);
|
|
132
|
-
if (!candidate || candidate === current)
|
|
133
|
-
continue;
|
|
134
|
-
if (localizedField &&
|
|
135
|
-
defaultLocale &&
|
|
136
|
-
locale !== defaultLocale &&
|
|
137
|
-
defaultSlug &&
|
|
138
|
-
candidate === defaultSlug) {
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
previous = candidate;
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
if (!previous) {
|
|
145
|
-
return { published: true };
|
|
146
|
-
}
|
|
147
|
-
if (localizedField && defaultLocale) {
|
|
148
|
-
if (locale === defaultLocale) {
|
|
149
|
-
const slugField = hookDoc[identifier];
|
|
150
|
-
if (slugField && typeof slugField === 'object' && !Array.isArray(slugField)) {
|
|
151
|
-
for (const [code, value] of Object.entries(slugField)) {
|
|
152
|
-
if (code === locale)
|
|
153
|
-
continue;
|
|
154
|
-
const other = identifierValueToString(value);
|
|
155
|
-
if (other && normalizeIdentifierValue(other) === previous) {
|
|
156
|
-
return { published: true };
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return { published: true, locale, previous, current };
|
|
163
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { PublicationTransition } from '../types.js';
|
|
2
|
-
export type PublicationTransitionPair = {
|
|
3
|
-
previous: string;
|
|
4
|
-
current: string;
|
|
5
|
-
locale?: string;
|
|
6
|
-
};
|
|
7
|
-
export declare const publicationTransitionsToPairs: (transitions: PublicationTransition[]) => PublicationTransitionPair[];
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { collectScalarIdentifierTransition } from './identifier-transitions.js';
|
|
2
|
-
export const publicationTransitionsToPairs = (transitions) => {
|
|
3
|
-
const pairs = [];
|
|
4
|
-
for (const t of transitions) {
|
|
5
|
-
if (!t.published)
|
|
6
|
-
continue;
|
|
7
|
-
const scalar = collectScalarIdentifierTransition({
|
|
8
|
-
previous: t.previous,
|
|
9
|
-
current: t.current,
|
|
10
|
-
});
|
|
11
|
-
if (!scalar)
|
|
12
|
-
continue;
|
|
13
|
-
pairs.push({ previous: scalar.previous, current: scalar.current, locale: t.locale });
|
|
14
|
-
}
|
|
15
|
-
return pairs;
|
|
16
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { LocalePrefix, PublishedLinkPathTemplate } from '../../url/published-path.js';
|
|
2
|
-
export type PublishedLinkWire = PublishedLinkPathTemplate & {
|
|
3
|
-
localePrefix?: LocalePrefix;
|
|
4
|
-
};
|
|
5
|
-
/** Reads `custom.redirect.publishedLink`, then `custom.draft.publishedLink` (same wire shape). */
|
|
6
|
-
export declare function readPublishedLinkWire(custom: unknown): PublishedLinkWire | null;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
const REDIRECT_CUSTOM_KEY = 'redirect';
|
|
2
|
-
const DRAFT_CUSTOM_KEY = 'draft';
|
|
3
|
-
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
4
|
-
const parsePublishedLinkWire = (value) => {
|
|
5
|
-
if (!isRecord(value))
|
|
6
|
-
return null;
|
|
7
|
-
const wire = {};
|
|
8
|
-
if (typeof value.path === 'string' && value.path.length > 0) {
|
|
9
|
-
wire.path = value.path;
|
|
10
|
-
}
|
|
11
|
-
if (isRecord(value.paths) && Object.keys(value.paths).length > 0) {
|
|
12
|
-
wire.paths = value.paths;
|
|
13
|
-
}
|
|
14
|
-
if (isRecord(value.routes) && Object.keys(value.routes).length > 0) {
|
|
15
|
-
wire.routes = value.routes;
|
|
16
|
-
}
|
|
17
|
-
if (isRecord(value.params) && Object.keys(value.params).length > 0) {
|
|
18
|
-
wire.params = value.params;
|
|
19
|
-
}
|
|
20
|
-
if (isRecord(value.localePrefix) && Object.keys(value.localePrefix).length > 0) {
|
|
21
|
-
wire.localePrefix = value.localePrefix;
|
|
22
|
-
}
|
|
23
|
-
const hasTemplate = Boolean(wire.path) ||
|
|
24
|
-
Boolean(wire.paths && Object.keys(wire.paths).length > 0) ||
|
|
25
|
-
Boolean(wire.routes && Object.keys(wire.routes).length > 0);
|
|
26
|
-
return hasTemplate ? wire : null;
|
|
27
|
-
};
|
|
28
|
-
const readDraftPublishedLink = (custom) => {
|
|
29
|
-
if (!isRecord(custom))
|
|
30
|
-
return null;
|
|
31
|
-
const entry = custom[DRAFT_CUSTOM_KEY];
|
|
32
|
-
if (!isRecord(entry))
|
|
33
|
-
return null;
|
|
34
|
-
if (entry.enabled === false)
|
|
35
|
-
return null;
|
|
36
|
-
return parsePublishedLinkWire(entry.publishedLink);
|
|
37
|
-
};
|
|
38
|
-
const readRedirectPublishedLink = (custom) => {
|
|
39
|
-
if (!isRecord(custom))
|
|
40
|
-
return null;
|
|
41
|
-
const entry = custom[REDIRECT_CUSTOM_KEY];
|
|
42
|
-
if (!isRecord(entry))
|
|
43
|
-
return null;
|
|
44
|
-
return parsePublishedLinkWire(entry.publishedLink);
|
|
45
|
-
};
|
|
46
|
-
/** Reads `custom.redirect.publishedLink`, then `custom.draft.publishedLink` (same wire shape). */
|
|
47
|
-
export function readPublishedLinkWire(custom) {
|
|
48
|
-
return readRedirectPublishedLink(custom) ?? readDraftPublishedLink(custom);
|
|
49
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { CollectionConfig } from 'payload';
|
|
2
|
-
import { type LocalePrefix } from '../../url/published-path.js';
|
|
3
|
-
export declare function resolveCollectionPublishedPathConfig(collection: CollectionConfig, pluginLocalePrefix?: LocalePrefix): import("../../url/published-path.js").ResolvedPublishedPathConfig | null;
|
|
4
|
-
export declare function identifierValueForPath(identifier: string, value: string): {
|
|
5
|
-
[x: string]: string;
|
|
6
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { readPublishedLinkWire } from './published-link-config.js';
|
|
2
|
-
import { paramKeyFromIdentifierPath } from '../identifier-field-path.js';
|
|
3
|
-
import { resolvePublishedPathConfig } from '../../url/published-path.js';
|
|
4
|
-
export function resolveCollectionPublishedPathConfig(collection, pluginLocalePrefix) {
|
|
5
|
-
const link = readPublishedLinkWire(collection.custom);
|
|
6
|
-
return resolvePublishedPathConfig(pluginLocalePrefix, link ?? undefined);
|
|
7
|
-
}
|
|
8
|
-
export function identifierValueForPath(identifier, value) {
|
|
9
|
-
const key = paramKeyFromIdentifierPath(identifier);
|
|
10
|
-
return { [key]: value };
|
|
11
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type FieldLike = {
|
|
2
|
-
name?: string;
|
|
3
|
-
type?: string;
|
|
4
|
-
localized?: boolean;
|
|
5
|
-
fields?: FieldLike[];
|
|
6
|
-
tabs?: Array<{
|
|
7
|
-
fields?: FieldLike[];
|
|
8
|
-
}>;
|
|
9
|
-
};
|
|
10
|
-
export declare function isIdentifierPathLocalized(fields: FieldLike[], path: string): boolean;
|
|
11
|
-
/** Read identifier string from doc; honors localized maps on the leaf (or intermediate) value. */
|
|
12
|
-
export declare function readIdentifierAtPath(doc: Record<string, unknown>, path: string, locale: string, localizedField: boolean): string | undefined;
|
|
13
|
-
/** Last segment of a field path — used for publishedLink param keys. */
|
|
14
|
-
export declare function paramKeyFromIdentifierPath(path: string): string;
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
const containerTypes = new Set(['group', 'row', 'collapsible', 'array']);
|
|
2
|
-
const identifierValueToString = (value) => {
|
|
3
|
-
if (value == null)
|
|
4
|
-
return undefined;
|
|
5
|
-
if (typeof value === 'string') {
|
|
6
|
-
const t = value.trim();
|
|
7
|
-
return t.length > 0 ? t : undefined;
|
|
8
|
-
}
|
|
9
|
-
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
10
|
-
return String(value);
|
|
11
|
-
}
|
|
12
|
-
return undefined;
|
|
13
|
-
};
|
|
14
|
-
function flattenFields(fields) {
|
|
15
|
-
const out = [];
|
|
16
|
-
for (const field of fields) {
|
|
17
|
-
if (field.type === 'tabs' && field.tabs) {
|
|
18
|
-
for (const tab of field.tabs) {
|
|
19
|
-
if (tab.fields)
|
|
20
|
-
out.push(...flattenFields(tab.fields));
|
|
21
|
-
}
|
|
22
|
-
continue;
|
|
23
|
-
}
|
|
24
|
-
if (field.type === 'row' && field.fields) {
|
|
25
|
-
out.push(...flattenFields(field.fields));
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
out.push(field);
|
|
29
|
-
if (field.type && containerTypes.has(field.type) && field.fields) {
|
|
30
|
-
out.push(...flattenFields(field.fields));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return out;
|
|
34
|
-
}
|
|
35
|
-
function resolveFieldAtPath(fields, path) {
|
|
36
|
-
const segments = path.split('.').filter(Boolean);
|
|
37
|
-
if (segments.length === 0)
|
|
38
|
-
return undefined;
|
|
39
|
-
let currentFields = flattenFields(fields);
|
|
40
|
-
let resolved;
|
|
41
|
-
for (let i = 0; i < segments.length; i++) {
|
|
42
|
-
const segment = segments[i];
|
|
43
|
-
resolved = currentFields.find((f) => f.name === segment);
|
|
44
|
-
if (!resolved)
|
|
45
|
-
return undefined;
|
|
46
|
-
if (i < segments.length - 1) {
|
|
47
|
-
if (!resolved.fields?.length)
|
|
48
|
-
return undefined;
|
|
49
|
-
currentFields = flattenFields(resolved.fields);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return resolved;
|
|
53
|
-
}
|
|
54
|
-
export function isIdentifierPathLocalized(fields, path) {
|
|
55
|
-
const field = resolveFieldAtPath(fields, path);
|
|
56
|
-
return Boolean(field?.localized);
|
|
57
|
-
}
|
|
58
|
-
function readValueAtPath(doc, path) {
|
|
59
|
-
const segments = path.split('.').filter(Boolean);
|
|
60
|
-
let current = doc;
|
|
61
|
-
for (const segment of segments) {
|
|
62
|
-
if (!current || typeof current !== 'object' || Array.isArray(current))
|
|
63
|
-
return undefined;
|
|
64
|
-
current = current[segment];
|
|
65
|
-
}
|
|
66
|
-
return current;
|
|
67
|
-
}
|
|
68
|
-
/** Read identifier string from doc; honors localized maps on the leaf (or intermediate) value. */
|
|
69
|
-
export function readIdentifierAtPath(doc, path, locale, localizedField) {
|
|
70
|
-
if (!localizedField) {
|
|
71
|
-
return identifierValueToString(readValueAtPath(doc, path));
|
|
72
|
-
}
|
|
73
|
-
const segments = path.split('.').filter(Boolean);
|
|
74
|
-
if (segments.length === 0)
|
|
75
|
-
return undefined;
|
|
76
|
-
let current = doc;
|
|
77
|
-
for (let i = 0; i < segments.length; i++) {
|
|
78
|
-
if (!current || typeof current !== 'object' || Array.isArray(current))
|
|
79
|
-
return undefined;
|
|
80
|
-
const segment = segments[i];
|
|
81
|
-
if (segment === undefined)
|
|
82
|
-
return undefined;
|
|
83
|
-
current = current[segment];
|
|
84
|
-
}
|
|
85
|
-
if (current && typeof current === 'object' && !Array.isArray(current)) {
|
|
86
|
-
return identifierValueToString(current[locale]);
|
|
87
|
-
}
|
|
88
|
-
return identifierValueToString(current);
|
|
89
|
-
}
|
|
90
|
-
/** Last segment of a field path — used for publishedLink param keys. */
|
|
91
|
-
export function paramKeyFromIdentifierPath(path) {
|
|
92
|
-
const segments = path.split('.').filter(Boolean);
|
|
93
|
-
return segments[segments.length - 1] ?? path;
|
|
94
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Payload } from 'payload';
|
|
2
|
-
import type { ResolvedRedirect } from './resolved.js';
|
|
3
|
-
export type ResolveRedirectArgs = {
|
|
4
|
-
payload: Payload;
|
|
5
|
-
from: string;
|
|
6
|
-
};
|
|
7
|
-
export declare const resolveRedirect: ({ payload, from, }: ResolveRedirectArgs) => Promise<ResolvedRedirect>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { RedirectAmbiguousRuleError } from '../model/errors.js';
|
|
2
|
-
import { normalizeRedirectSourcePath } from '../normalize-path.js';
|
|
3
|
-
import { findRedirectByFrom, redirectResolveSelect } from '../queries.js';
|
|
4
|
-
import { toResolvedRedirect } from './resolved.js';
|
|
5
|
-
import { getRedirectRuntime } from '../runtime.js';
|
|
6
|
-
export const resolveRedirect = async ({ payload, from, }) => {
|
|
7
|
-
const { collectionSlug } = getRedirectRuntime(payload.config);
|
|
8
|
-
const normalizedFrom = normalizeRedirectSourcePath(from);
|
|
9
|
-
const result = await findRedirectByFrom(payload, {
|
|
10
|
-
collectionSlug,
|
|
11
|
-
from: normalizedFrom,
|
|
12
|
-
overrideAccess: false,
|
|
13
|
-
select: redirectResolveSelect,
|
|
14
|
-
});
|
|
15
|
-
if (result.totalDocs > 1) {
|
|
16
|
-
throw new RedirectAmbiguousRuleError(`Multiple redirect rules exist for "${normalizedFrom}" (unique constraint should prevent this)`);
|
|
17
|
-
}
|
|
18
|
-
const doc = result.docs[0];
|
|
19
|
-
if (!doc)
|
|
20
|
-
return null;
|
|
21
|
-
return toResolvedRedirect({
|
|
22
|
-
from: String(doc.from),
|
|
23
|
-
to: doc.to,
|
|
24
|
-
StatusCode: doc.StatusCode,
|
|
25
|
-
enabled: doc.enabled !== false,
|
|
26
|
-
});
|
|
27
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { RedirectStatusCode } from '../model/status-code.js';
|
|
2
|
-
export type ResolvedRedirect = {
|
|
3
|
-
statusCode: 301 | 302;
|
|
4
|
-
to: string;
|
|
5
|
-
} | null;
|
|
6
|
-
export type RedirectRecord = {
|
|
7
|
-
from: string;
|
|
8
|
-
to?: string | null;
|
|
9
|
-
StatusCode: RedirectStatusCode;
|
|
10
|
-
enabled: boolean;
|
|
11
|
-
};
|
|
12
|
-
export declare const toResolvedRedirect: (record: RedirectRecord) => ResolvedRedirect | null;
|
|
13
|
-
export declare const redirectLogicalIdentity: (record: {
|
|
14
|
-
from: string;
|
|
15
|
-
to?: string | null;
|
|
16
|
-
StatusCode: RedirectStatusCode;
|
|
17
|
-
}) => string;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { assertRedirectStatusCode } from '../model/status-code.js';
|
|
2
|
-
export const toResolvedRedirect = (record) => {
|
|
3
|
-
if (!record.enabled)
|
|
4
|
-
return null;
|
|
5
|
-
let code;
|
|
6
|
-
try {
|
|
7
|
-
code = assertRedirectStatusCode(record.StatusCode);
|
|
8
|
-
}
|
|
9
|
-
catch {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
const to = record.to;
|
|
13
|
-
if (typeof to !== 'string' || to.trim().length === 0) {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
return { statusCode: code, to: to.trim() };
|
|
17
|
-
};
|
|
18
|
-
export const redirectLogicalIdentity = (record) => `${record.from}\0${record.to ?? ''}\0${record.StatusCode}`;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Config } from 'payload';
|
|
2
|
-
import type { LocalePrefix } from '../url/published-path.js';
|
|
3
|
-
export declare const PAYLOAD_REDIRECT_CUSTOM_KEY = "payloadRedirect";
|
|
4
|
-
export type PayloadRedirectRuntime = {
|
|
5
|
-
collectionSlug: string;
|
|
6
|
-
localePrefix?: LocalePrefix;
|
|
7
|
-
};
|
|
8
|
-
export declare const registerRedirectRuntime: (config: Config, runtime: PayloadRedirectRuntime) => Config['custom'];
|
|
9
|
-
export declare const getRedirectRuntime: (config: Pick<Config, 'custom'>) => PayloadRedirectRuntime;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export const PAYLOAD_REDIRECT_CUSTOM_KEY = 'payloadRedirect';
|
|
2
|
-
export const registerRedirectRuntime = (config, runtime) => ({
|
|
3
|
-
...(config.custom ?? {}),
|
|
4
|
-
[PAYLOAD_REDIRECT_CUSTOM_KEY]: runtime,
|
|
5
|
-
});
|
|
6
|
-
export const getRedirectRuntime = (config) => {
|
|
7
|
-
const custom = config.custom;
|
|
8
|
-
const runtime = custom?.[PAYLOAD_REDIRECT_CUSTOM_KEY];
|
|
9
|
-
if (!runtime?.collectionSlug) {
|
|
10
|
-
throw new Error('payload-redirect: plugin runtime missing — add redirects() to payload config');
|
|
11
|
-
}
|
|
12
|
-
return runtime;
|
|
13
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function localeCode(locale: unknown): string | null;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export function localeCode(locale) {
|
|
2
|
-
if (typeof locale === 'string' && locale.length > 0)
|
|
3
|
-
return locale;
|
|
4
|
-
if (locale && typeof locale === 'object' && 'code' in locale) {
|
|
5
|
-
const code = String(locale.code);
|
|
6
|
-
return code.length > 0 ? code : null;
|
|
7
|
-
}
|
|
8
|
-
return null;
|
|
9
|
-
}
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { type RouteParamsConfig } from './route-params.js';
|
|
2
|
-
export type PreviewRoutes = Record<string, string | Record<string, string>>;
|
|
3
|
-
export declare function buildPreviewPath(previewRoutes: PreviewRoutes, data: Record<string, unknown>, locale: unknown, routeParams?: RouteParamsConfig): string | null;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { localeCode } from './preview-path.js';
|
|
2
|
-
import { applyDynamicSegments } from './route-params.js';
|
|
3
|
-
function patternForLocale(previewRoutes, locale) {
|
|
4
|
-
const entries = Object.entries(previewRoutes);
|
|
5
|
-
if (entries.length === 0)
|
|
6
|
-
return null;
|
|
7
|
-
const first = entries[0];
|
|
8
|
-
if (!first)
|
|
9
|
-
return null;
|
|
10
|
-
const [, template] = first;
|
|
11
|
-
const code = localeCode(locale);
|
|
12
|
-
if (!code)
|
|
13
|
-
return null;
|
|
14
|
-
if (typeof template === 'string')
|
|
15
|
-
return template;
|
|
16
|
-
return template[code] ?? null;
|
|
17
|
-
}
|
|
18
|
-
export function buildPreviewPath(previewRoutes, data, locale, routeParams) {
|
|
19
|
-
const code = localeCode(locale);
|
|
20
|
-
if (!code)
|
|
21
|
-
return null;
|
|
22
|
-
const pattern = patternForLocale(previewRoutes, locale);
|
|
23
|
-
if (!pattern)
|
|
24
|
-
return null;
|
|
25
|
-
const resolved = applyDynamicSegments(pattern, data, locale, routeParams);
|
|
26
|
-
if (resolved === null)
|
|
27
|
-
return null;
|
|
28
|
-
if (resolved === '/' || resolved === '')
|
|
29
|
-
return `/${code}`;
|
|
30
|
-
return `/${code}${resolved.startsWith('/') ? resolved : `/${resolved}`}`;
|
|
31
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { PublishedLinkWire } from '../core/redirects/auto/published-link-config.js';
|
|
2
|
-
/** Shape for `CollectionConfig.custom.redirect` — auto redirect on identifier change. */
|
|
3
|
-
export type RedirectIdentifierField = string;
|
|
4
|
-
export type PayloadRedirectCustom = {
|
|
5
|
-
enabled?: boolean;
|
|
6
|
-
identifier: RedirectIdentifierField;
|
|
7
|
-
publishedLink?: PublishedLinkWire;
|
|
8
|
-
};
|
|
9
|
-
export declare function readRedirectCustom(custom: unknown): PayloadRedirectCustom | null;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const REDIRECT_CUSTOM_KEY = 'redirect';
|
|
2
|
-
export function readRedirectCustom(custom) {
|
|
3
|
-
if (!custom || typeof custom !== 'object')
|
|
4
|
-
return null;
|
|
5
|
-
const entry = custom[REDIRECT_CUSTOM_KEY];
|
|
6
|
-
if (!entry || typeof entry !== 'object')
|
|
7
|
-
return null;
|
|
8
|
-
const record = entry;
|
|
9
|
-
if (record.enabled === false)
|
|
10
|
-
return null;
|
|
11
|
-
const identifier = record.identifier;
|
|
12
|
-
if (typeof identifier !== 'string' || !identifier.trim())
|
|
13
|
-
return null;
|
|
14
|
-
return {
|
|
15
|
-
enabled: record.enabled !== false,
|
|
16
|
-
identifier: identifier.trim(),
|
|
17
|
-
};
|
|
18
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { CollectionConfig } from 'payload';
|
|
2
|
-
import type { RedirectsCollectionAccess } from '../core/redirects/collection/access.js';
|
|
3
|
-
import type { LocalePrefix } from '../core/url/published-path.js';
|
|
4
|
-
export type RedirectsPluginConfig = {
|
|
5
|
-
localePrefix?: LocalePrefix;
|
|
6
|
-
/** Enable Payload trash (soft delete). Default true. */
|
|
7
|
-
trash?: boolean;
|
|
8
|
-
/** Host-owned collection (register in `config.collections` and pass the same config here). */
|
|
9
|
-
collection: CollectionConfig;
|
|
10
|
-
/** Access overrides when the host collection omits handlers (base collection wins). */
|
|
11
|
-
access?: RedirectsCollectionAccess;
|
|
12
|
-
};
|
|
13
|
-
export type ResolvedRedirectsPluginConfig = {
|
|
14
|
-
localePrefix?: LocalePrefix;
|
|
15
|
-
collection: CollectionConfig;
|
|
16
|
-
};
|
|
17
|
-
export declare const resolveRedirectsPluginConfig: (config?: RedirectsPluginConfig) => ResolvedRedirectsPluginConfig;
|