@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.
Files changed (109) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +212 -79
  3. package/dist/core/redirects/auto/create-redirect.d.ts +9 -4
  4. package/dist/core/redirects/auto/create-redirect.js +26 -66
  5. package/dist/core/redirects/auto/create-redirect.js.map +1 -0
  6. package/dist/core/redirects/auto/match-existing-rule.d.ts +1 -1
  7. package/dist/core/redirects/auto/match-existing-rule.js +4 -3
  8. package/dist/core/redirects/auto/match-existing-rule.js.map +1 -0
  9. package/dist/core/redirects/auto/persist-auto-rule.d.ts +5 -4
  10. package/dist/core/redirects/auto/persist-auto-rule.js +12 -11
  11. package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -0
  12. package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +3 -3
  13. package/dist/core/redirects/auto/publication/get-publication-transition.js +22 -37
  14. package/dist/core/redirects/auto/publication/get-publication-transition.js.map +1 -0
  15. package/dist/core/redirects/auto/publication/locales.d.ts +7 -5
  16. package/dist/core/redirects/auto/publication/locales.js +33 -28
  17. package/dist/core/redirects/auto/publication/locales.js.map +1 -0
  18. package/dist/core/redirects/auto/publication/publish-intent.d.ts +2 -4
  19. package/dist/core/redirects/auto/publication/publish-intent.js +6 -29
  20. package/dist/core/redirects/auto/publication/publish-intent.js.map +1 -0
  21. package/dist/core/redirects/auto/publication/versions/document-locale.d.ts +4 -6
  22. package/dist/core/redirects/auto/publication/versions/document-locale.js +10 -18
  23. package/dist/core/redirects/auto/publication/versions/document-locale.js.map +1 -0
  24. package/dist/core/redirects/auto/publication/versions/version-pages.d.ts +4 -12
  25. package/dist/core/redirects/auto/publication/versions/version-pages.js +7 -32
  26. package/dist/core/redirects/auto/publication/versions/version-pages.js.map +1 -0
  27. package/dist/core/redirects/auto/publication/versions/version-rows.d.ts +1 -7
  28. package/dist/core/redirects/auto/publication/versions/version-rows.js +7 -30
  29. package/dist/core/redirects/auto/publication/versions/version-rows.js.map +1 -0
  30. package/dist/core/redirects/auto/types.d.ts +27 -8
  31. package/dist/core/redirects/auto/types.js +1 -0
  32. package/dist/core/redirects/auto/types.js.map +1 -0
  33. package/dist/core/redirects/collection/access.d.ts +1 -1
  34. package/dist/core/redirects/collection/access.js +3 -2
  35. package/dist/core/redirects/collection/access.js.map +1 -0
  36. package/dist/core/redirects/collection/collection.d.ts +2 -2
  37. package/dist/core/redirects/collection/collection.js +10 -9
  38. package/dist/core/redirects/collection/collection.js.map +1 -0
  39. package/dist/core/redirects/collection/duplicate-guard.d.ts +1 -1
  40. package/dist/core/redirects/collection/duplicate-guard.js +35 -34
  41. package/dist/core/redirects/collection/duplicate-guard.js.map +1 -0
  42. package/dist/core/redirects/collection/fields.d.ts +1 -1
  43. package/dist/core/redirects/collection/fields.js +52 -51
  44. package/dist/core/redirects/collection/fields.js.map +1 -0
  45. package/dist/core/redirects/collection/graph-lock.d.ts +1 -1
  46. package/dist/core/redirects/collection/graph-lock.js +8 -7
  47. package/dist/core/redirects/collection/graph-lock.js.map +1 -0
  48. package/dist/core/redirects/collection/origin-hook.d.ts +1 -1
  49. package/dist/core/redirects/collection/origin-hook.js +6 -5
  50. package/dist/core/redirects/collection/origin-hook.js.map +1 -0
  51. package/dist/core/redirects/collection/types.d.ts +2 -2
  52. package/dist/core/redirects/collection/types.js +1 -0
  53. package/dist/core/redirects/collection/types.js.map +1 -0
  54. package/dist/core/redirects/context-keys.d.ts +2 -2
  55. package/dist/core/redirects/context-keys.js +3 -2
  56. package/dist/core/redirects/context-keys.js.map +1 -0
  57. package/dist/core/redirects/model/errors.js +2 -1
  58. package/dist/core/redirects/model/errors.js.map +1 -0
  59. package/dist/core/redirects/model/origin.d.ts +1 -1
  60. package/dist/core/redirects/model/origin.js +4 -3
  61. package/dist/core/redirects/model/origin.js.map +1 -0
  62. package/dist/core/redirects/model/status-code.js +5 -4
  63. package/dist/core/redirects/model/status-code.js.map +1 -0
  64. package/dist/core/redirects/normalize-path.js +23 -22
  65. package/dist/core/redirects/normalize-path.js.map +1 -0
  66. package/dist/core/redirects/queries.d.ts +3 -12
  67. package/dist/core/redirects/queries.js +3 -2
  68. package/dist/core/redirects/queries.js.map +1 -0
  69. package/dist/core/url/published-path.d.ts +4 -16
  70. package/dist/core/url/published-path.js +40 -121
  71. package/dist/core/url/published-path.js.map +1 -0
  72. package/dist/core/url/route-params.d.ts +3 -11
  73. package/dist/core/url/route-params.js +32 -99
  74. package/dist/core/url/route-params.js.map +1 -0
  75. package/dist/exports/index.d.ts +4 -4
  76. package/dist/exports/index.js +2 -1
  77. package/dist/exports/index.js.map +1 -0
  78. package/dist/plugin/payload-plugin.d.ts +2 -3
  79. package/dist/plugin/payload-plugin.js +94 -91
  80. package/dist/plugin/payload-plugin.js.map +1 -0
  81. package/dist/plugin/types.d.ts +9 -35
  82. package/dist/plugin/types.js +1 -0
  83. package/dist/plugin/types.js.map +1 -0
  84. package/docs/FAILURE_SEMANTICS.md +31 -27
  85. package/package.json +29 -20
  86. package/dist/core/redirects/auto/publication/identifier-snapshot.d.ts +0 -2
  87. package/dist/core/redirects/auto/publication/identifier-snapshot.js +0 -27
  88. package/dist/core/redirects/auto/publication/identifier-transitions.d.ts +0 -18
  89. package/dist/core/redirects/auto/publication/identifier-transitions.js +0 -36
  90. package/dist/core/redirects/auto/publication/transition-for-locale.d.ts +0 -12
  91. package/dist/core/redirects/auto/publication/transition-for-locale.js +0 -167
  92. package/dist/core/redirects/auto/publication/transitions-to-pairs.d.ts +0 -9
  93. package/dist/core/redirects/auto/publication/transitions-to-pairs.js +0 -22
  94. package/dist/core/redirects/auto/published-link-config.d.ts +0 -6
  95. package/dist/core/redirects/auto/published-link-config.js +0 -53
  96. package/dist/core/redirects/auto/published-path.d.ts +0 -6
  97. package/dist/core/redirects/auto/published-path.js +0 -28
  98. package/dist/core/redirects/auto/redirect-custom.d.ts +0 -7
  99. package/dist/core/redirects/auto/redirect-custom.js +0 -19
  100. package/dist/core/redirects/identifier-field-path.d.ts +0 -14
  101. package/dist/core/redirects/identifier-field-path.js +0 -94
  102. package/dist/core/redirects/runtime.d.ts +0 -9
  103. package/dist/core/redirects/runtime.js +0 -13
  104. package/dist/core/url/preview-path.d.ts +0 -1
  105. package/dist/core/url/preview-path.js +0 -9
  106. package/dist/core/url/preview-routes.d.ts +0 -3
  107. package/dist/core/url/preview-routes.js +0 -31
  108. package/docs/RELEASING.md +0 -41
  109. package/docs/adr/0002-database-indexes.md +0 -44
@@ -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,167 +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
- const currentDoc = [draftDoc, hookDoc, currentVersion, identifierSource].find((candidate) => candidate !== undefined &&
85
- extractIdentifierFromVersionSnapshot(candidate, identifier, locale, localizedField, locale) === current);
86
- let defaultSlug;
87
- if (localizedField && defaultLocale) {
88
- defaultSlug = extractIdentifierFromVersionSnapshot(hookDoc, identifier, defaultLocale, true, locale);
89
- if (!defaultSlug) {
90
- const defaultDoc = await fetchLocalePublishedDoc({
91
- req,
92
- collection,
93
- docId,
94
- locale: defaultLocale,
95
- });
96
- if (defaultDoc) {
97
- defaultSlug = extractIdentifierFromVersionSnapshot(defaultDoc, identifier, defaultLocale, true, defaultLocale);
98
- }
99
- }
100
- }
101
- let versionsForPrevious = [...publishedVersions];
102
- if (publishIntent && localizedField) {
103
- versionsForPrevious = await fetchVersionHistoryPages({
104
- req,
105
- collectionSlug: collection.slug,
106
- docId,
107
- versionLocale: locale,
108
- });
109
- }
110
- if (current) {
111
- const hasCurrentHead = versionsForPrevious.some((row) => {
112
- const version = row.version;
113
- if (!version)
114
- return false;
115
- return (extractIdentifierFromVersionSnapshot(version, identifier, locale, localizedField, locale) === current);
116
- });
117
- if (!hasCurrentHead) {
118
- versionsForPrevious.unshift({ version: hookDoc });
119
- }
120
- }
121
- let previous;
122
- let previousDoc;
123
- for (const row of versionsForPrevious) {
124
- const version = row.version;
125
- if (!version)
126
- continue;
127
- if (localizedField) {
128
- const rawField = version[identifier];
129
- if (rawField && typeof rawField === 'object' && !Array.isArray(rawField)) {
130
- if (!(locale in rawField))
131
- continue;
132
- }
133
- }
134
- const candidate = extractIdentifierFromVersionSnapshot(version, identifier, locale, localizedField, locale);
135
- if (!candidate || candidate === current)
136
- continue;
137
- if (localizedField &&
138
- defaultLocale &&
139
- locale !== defaultLocale &&
140
- defaultSlug &&
141
- candidate === defaultSlug) {
142
- continue;
143
- }
144
- previous = candidate;
145
- previousDoc = version;
146
- break;
147
- }
148
- if (!previous) {
149
- return { published: true };
150
- }
151
- if (localizedField && defaultLocale) {
152
- if (locale === defaultLocale) {
153
- const slugField = hookDoc[identifier];
154
- if (slugField && typeof slugField === 'object' && !Array.isArray(slugField)) {
155
- for (const [code, value] of Object.entries(slugField)) {
156
- if (code === locale)
157
- continue;
158
- const other = identifierValueToString(value);
159
- if (other && normalizeIdentifierValue(other) === previous) {
160
- return { published: true };
161
- }
162
- }
163
- }
164
- }
165
- }
166
- return { published: true, locale, previous, current, previousDoc, currentDoc };
167
- };
@@ -1,9 +0,0 @@
1
- import type { PublicationTransition } from '../types.js';
2
- export type PublicationTransitionPair = {
3
- previous: string;
4
- current: string;
5
- locale?: string;
6
- previousDoc?: Record<string, unknown>;
7
- currentDoc?: Record<string, unknown>;
8
- };
9
- export declare const publicationTransitionsToPairs: (transitions: PublicationTransition[]) => PublicationTransitionPair[];
@@ -1,22 +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({
14
- previous: scalar.previous,
15
- current: scalar.current,
16
- locale: t.locale,
17
- previousDoc: t.previousDoc,
18
- currentDoc: t.currentDoc,
19
- });
20
- }
21
- return pairs;
22
- };
@@ -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,53 +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 (typeof value.getPath === 'function') {
21
- wire.getPath = value.getPath;
22
- }
23
- if (isRecord(value.localePrefix) && Object.keys(value.localePrefix).length > 0) {
24
- wire.localePrefix = value.localePrefix;
25
- }
26
- const hasTemplate = Boolean(wire.path) ||
27
- Boolean(wire.paths && Object.keys(wire.paths).length > 0) ||
28
- Boolean(wire.routes && Object.keys(wire.routes).length > 0) ||
29
- typeof wire.getPath === 'function';
30
- return hasTemplate ? wire : null;
31
- };
32
- const readDraftPublishedLink = (custom) => {
33
- if (!isRecord(custom))
34
- return null;
35
- const entry = custom[DRAFT_CUSTOM_KEY];
36
- if (!isRecord(entry))
37
- return null;
38
- if (entry.enabled === false)
39
- return null;
40
- return parsePublishedLinkWire(entry.publishedLink);
41
- };
42
- const readRedirectPublishedLink = (custom) => {
43
- if (!isRecord(custom))
44
- return null;
45
- const entry = custom[REDIRECT_CUSTOM_KEY];
46
- if (!isRecord(entry))
47
- return null;
48
- return parsePublishedLinkWire(entry.publishedLink);
49
- };
50
- /** Reads `custom.redirect.publishedLink`, then `custom.draft.publishedLink` (same wire shape). */
51
- export function readPublishedLinkWire(custom) {
52
- return readRedirectPublishedLink(custom) ?? readDraftPublishedLink(custom);
53
- }
@@ -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,28 +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
- import { extractDynamicSegments } from '../../url/route-params.js';
5
- export function resolveCollectionPublishedPathConfig(collection, pluginLocalePrefix) {
6
- const link = readPublishedLinkWire(collection.custom);
7
- if (!link)
8
- return null;
9
- const templates = [
10
- ...(typeof link.path === 'string' ? [link.path] : []),
11
- ...Object.values(link.paths ?? {}),
12
- ...Object.values(link.routes ?? {}).flatMap((route) => typeof route === 'string' ? [route] : Object.values(route)),
13
- ];
14
- const params = Object.fromEntries([...new Set(templates.flatMap(extractDynamicSegments))].flatMap((name) => {
15
- const field = collection.fields.find((candidate) => 'name' in candidate && candidate.name === name);
16
- return field && 'type' in field
17
- ? [[name, { field: name, localized: 'localized' in field && field.localized === true }]]
18
- : [];
19
- }));
20
- return resolvePublishedPathConfig(pluginLocalePrefix, {
21
- ...link,
22
- params: { ...params, ...link.params },
23
- });
24
- }
25
- export function identifierValueForPath(identifier, value) {
26
- const key = paramKeyFromIdentifierPath(identifier);
27
- return { [key]: value };
28
- }
@@ -1,7 +0,0 @@
1
- import { type PublishedLinkWire } from './published-link-config.js';
2
- export type PayloadRedirectCustom = {
3
- enabled?: boolean;
4
- identifier: string;
5
- publishedLink?: PublishedLinkWire;
6
- };
7
- export declare function readRedirectCustom(custom: unknown): PayloadRedirectCustom | null;
@@ -1,19 +0,0 @@
1
- import { readPublishedLinkWire } from './published-link-config.js';
2
- export function readRedirectCustom(custom) {
3
- if (!custom || typeof custom !== 'object' || Array.isArray(custom))
4
- return null;
5
- const entry = custom.redirect;
6
- if (!entry || typeof entry !== 'object' || Array.isArray(entry))
7
- return null;
8
- const record = entry;
9
- if (record.enabled === false)
10
- return null;
11
- if (typeof record.identifier !== 'string' || !record.identifier.trim())
12
- return null;
13
- const publishedLink = readPublishedLinkWire({ redirect: record }) ?? undefined;
14
- return {
15
- enabled: record.enabled !== false,
16
- identifier: record.identifier.trim(),
17
- ...(publishedLink ? { publishedLink } : {}),
18
- };
19
- }
@@ -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,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
- }
package/docs/RELEASING.md DELETED
@@ -1,41 +0,0 @@
1
- # Releasing
2
-
3
- The first staged release uses package version `0.1.0`. npm creates the public
4
- `0.0.0-stage` placeholder when the first package is staged; the package contents
5
- stay private until a maintainer approves the staged release with 2FA.
6
-
7
- ## One-time setup
8
-
9
- 1. Create the public GitHub repository `klnap/payload-redirect` and push `main`.
10
- 2. From the package root, run `npm stage publish` while logged in to npm. This
11
- bootstraps the package entry and stages the `0.1.0` contents.
12
- 3. Configure the npm trusted publisher:
13
- - Provider: GitHub Actions
14
- - Organization/user: `klnap`
15
- - Repository: `payload-redirect`
16
- - Workflow filename: `release.yml`
17
- - Environment: `npm`
18
- - Allow `npm publish`; do not enable dist-tag changes.
19
- 4. Create the GitHub environment named `npm`.
20
-
21
- The release workflow needs no npm token. npm grants it short-lived OIDC
22
- publishing access and generates provenance. Stage approval remains a separate
23
- maintainer action and requires 2FA.
24
-
25
- ## Publish the initial release
26
-
27
- Review and approve the staged `0.1.0` package on npmjs.com or with
28
- `npm stage approve <stage-id>`. npm exposes a `0.0.0-stage` placeholder while
29
- the package is staged. After approval, push the matching release tag:
30
-
31
- ```sh
32
- git tag v0.1.0
33
- git push origin v0.1.0
34
- ```
35
-
36
- The workflow validates the tagged package and skips publishing if `0.1.0` is
37
- already on npm. Later versions are validated and published by the same OIDC
38
- workflow.
39
-
40
- For later releases, update `package.json`, validate the change, and push a matching
41
- `v<version>` tag from `main`.
@@ -1,44 +0,0 @@
1
- # ADR-0002: Redirects collection indexes and queries
2
-
3
- ## Status
4
-
5
- Accepted
6
-
7
- ## Context
8
-
9
- Hooks look up by normalized `from`. At most one row per `from` is a domain invariant.
10
-
11
- ## Schema (Payload field flags)
12
-
13
- | Field | Flags | Role |
14
- | :--- | :--- | :--- |
15
- | `from` | `unique: true` | Point lookups; duplicate guard (Postgres unique btree via Payload) |
16
- | `enabled` | `index: true` | Filters on active rules |
17
-
18
- Collection defaults: `versions: false`, `defaultSort: 'from'`, `timestamps: true`.
19
-
20
- The plugin owns this collection and enforces compatible `from` / `enabled` flags.
21
-
22
- ## Postgres (host migration, optional but recommended)
23
-
24
- Partial index for active rules:
25
-
26
- ```sql
27
- CREATE INDEX IF NOT EXISTS "redirects_enabled_from_active_idx"
28
- ON "redirects" USING btree ("from")
29
- WHERE "enabled" = true AND "deleted_at" IS NULL;
30
- ```
31
-
32
- See host database performance docs when adding indexes for large redirect tables.
33
-
34
- ## Query patterns (package)
35
-
36
- | Path | DB round-trips | Notes |
37
- | :--- | :--- | :--- |
38
- | `ensureAutoRedirect` | 1 or 2 | Single `find` by `from`; create if absent |
39
- | Duplicate guard | 1 per save | `beforeChange`; `id not_equals` on update |
40
- | Redirect-chain guard | 1 per hop | `beforeChange`; bounded at 1,000 hops |
41
-
42
- ## Non-goals
43
-
44
- - Composite `(enabled, from)` btree without partial predicate — redundant with unique `from` for point lookups