@klnap/payload-redirect 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -192
- package/dist/core/redirects/auto/create-redirect.js +26 -10
- package/dist/core/redirects/auto/persist-auto-rule.js +3 -2
- package/dist/core/redirects/auto/publication/transition-for-locale.js +5 -1
- package/dist/core/redirects/auto/publication/transitions-to-pairs.d.ts +2 -0
- package/dist/core/redirects/auto/publication/transitions-to-pairs.js +7 -1
- package/dist/core/redirects/auto/published-link-config.js +5 -1
- package/dist/core/redirects/auto/published-path.js +18 -1
- package/dist/core/redirects/auto/redirect-custom.d.ts +7 -0
- package/dist/core/redirects/auto/redirect-custom.js +19 -0
- package/dist/core/redirects/auto/types.d.ts +2 -0
- package/dist/core/redirects/collection/access.d.ts +6 -11
- package/dist/core/redirects/collection/access.js +7 -24
- package/dist/core/redirects/collection/collection.d.ts +2 -10
- package/dist/core/redirects/collection/collection.js +19 -22
- package/dist/core/redirects/collection/duplicate-guard.d.ts +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +5 -8
- package/dist/core/redirects/collection/fields.d.ts +1 -0
- package/dist/core/redirects/collection/fields.js +76 -76
- package/dist/core/redirects/collection/origin-hook.js +7 -14
- package/dist/core/redirects/collection/types.d.ts +7 -0
- package/dist/core/redirects/collection/types.js +1 -0
- package/dist/core/redirects/model/errors.d.ts +0 -6
- package/dist/core/redirects/model/errors.js +0 -6
- package/dist/core/redirects/model/status-code.js +4 -7
- package/dist/core/redirects/queries.d.ts +4 -9
- package/dist/core/redirects/queries.js +5 -9
- package/dist/core/url/published-path.d.ts +1 -0
- package/dist/core/url/published-path.js +31 -2
- package/dist/exports/index.d.ts +1 -7
- package/dist/exports/index.js +0 -4
- package/dist/plugin/payload-plugin.d.ts +4 -3
- package/dist/plugin/payload-plugin.js +140 -43
- package/dist/plugin/types.d.ts +43 -0
- package/dist/plugin/types.js +1 -0
- package/docs/FAILURE_SEMANTICS.md +44 -92
- package/docs/adr/0002-database-indexes.md +3 -4
- package/package.json +4 -3
- package/dist/core/redirects/resolution/resolve.d.ts +0 -7
- package/dist/core/redirects/resolution/resolve.js +0 -27
- package/dist/core/redirects/resolution/resolved.d.ts +0 -17
- package/dist/core/redirects/resolution/resolved.js +0 -18
- package/dist/plugin/redirect-custom.d.ts +0 -9
- package/dist/plugin/redirect-custom.js +0 -18
- package/dist/plugin/resolve-config.d.ts +0 -17
- package/dist/plugin/resolve-config.js +0 -20
- package/dist/plugin/validate-config.d.ts +0 -3
- package/dist/plugin/validate-config.js +0 -40
package/README.md
CHANGED
|
@@ -1,250 +1,139 @@
|
|
|
1
1
|
# @klnap/payload-redirect
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Rules use HTTP `301` or `302`. The resolver returns `ResolvedRedirect | null`; it does **not** ship HTTP middleware, caching, or framework integrations. Your application applies the HTTP redirect after lookup.
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { redirects } from '@klnap/payload-redirect'
|
|
9
|
-
|
|
10
|
-
// Redirects is your host application's collection configuration.
|
|
11
|
-
// Add to your Payload config:
|
|
12
|
-
const plugins = [redirects({ collection: Redirects })]
|
|
13
|
-
```
|
|
3
|
+
Payload plugin for manual and publication-generated redirects.
|
|
14
4
|
|
|
15
5
|
## Install
|
|
16
6
|
|
|
17
|
-
Install the package in your Payload application:
|
|
18
|
-
|
|
19
7
|
```sh
|
|
20
8
|
npm install @klnap/payload-redirect
|
|
21
|
-
# or
|
|
22
|
-
bun add @klnap/payload-redirect
|
|
23
9
|
```
|
|
24
10
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
## Requirements
|
|
28
|
-
|
|
29
|
-
| Requirement | Version |
|
|
30
|
-
| ----------- | ------------------------------------------------------------- |
|
|
31
|
-
| **Node.js** | `^24.22.3 or >=26.13.1` |
|
|
32
|
-
| **Payload** | `>=3.94.1 <4` (peer), `@payloadcms/db-postgres` `>=3.94.1 <4` |
|
|
33
|
-
|
|
34
|
-
Auto redirects require **drafts/versioning** on content collections.
|
|
11
|
+
Requires Payload `>=3.94.1 <4`. PostgreSQL is the supported adapter for transaction-safe
|
|
12
|
+
duplicate and redirect-chain checks.
|
|
35
13
|
|
|
36
|
-
##
|
|
14
|
+
## Configure
|
|
37
15
|
|
|
38
16
|
```ts
|
|
39
|
-
import type { CollectionConfig } from 'payload'
|
|
40
17
|
import { buildConfig } from 'payload'
|
|
41
18
|
import { redirects } from '@klnap/payload-redirect'
|
|
42
|
-
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
43
|
-
|
|
44
|
-
export const Redirects: CollectionConfig = {
|
|
45
|
-
slug: 'redirects',
|
|
46
|
-
fields: [], // host stub — see note below on merge, types, and migrations
|
|
47
|
-
}
|
|
48
19
|
|
|
49
20
|
export default buildConfig({
|
|
50
|
-
collections: [
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
collections: [
|
|
22
|
+
{
|
|
23
|
+
slug: 'articles',
|
|
24
|
+
versions: { drafts: true },
|
|
25
|
+
fields: [{ name: 'slug', type: 'text', required: true }],
|
|
26
|
+
custom: {
|
|
27
|
+
redirect: {
|
|
28
|
+
identifier: 'slug',
|
|
29
|
+
publishedLink: { path: '/articles/[slug]' },
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
plugins: [
|
|
35
|
+
redirects({
|
|
36
|
+
localePrefix: { en: '', pl: '/pl' },
|
|
37
|
+
}),
|
|
38
|
+
],
|
|
55
39
|
})
|
|
56
40
|
```
|
|
57
41
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
**PostgreSQL.** The package adds no migrations and does not wrap the adapter. The host application owns its migrations. Schema changes go through Payload's migration flow (`payload migrate:create` and `migrate`, or dev push, per project).
|
|
63
|
-
|
|
64
|
-
## Redirect collection
|
|
65
|
-
|
|
66
|
-
The host defines a redirects collection (slug and optional extra fields). The plugin merges required fields, hooks, and default access into that entry before Payload initializes. Incompatible host declarations of plugin-owned fields, including incorrect field types or required index settings, throw **`RedirectConfigError`** at startup.
|
|
67
|
-
|
|
68
|
-
Merged plugin fields: `from`, `to`, `StatusCode`, `enabled`, `origin`.
|
|
42
|
+
`redirects()` adds the plugin-owned `redirects` collection and wires publication hooks for
|
|
43
|
+
collections with `custom.redirect.identifier`. It preserves existing hooks. Do not register a
|
|
44
|
+
second redirects collection or add manual publication hooks.
|
|
69
45
|
|
|
70
|
-
|
|
46
|
+
The plugin creates a rule when a published identifier changes. Draft saves, first publications,
|
|
47
|
+
unpublishes, and unchanged identifiers do not create rules. A manual rule on the same source path
|
|
48
|
+
always wins over an automatic rule.
|
|
71
49
|
|
|
72
|
-
|
|
50
|
+
## Published paths
|
|
73
51
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
| `collection` | **Yes** | — | Host redirects `CollectionConfig` (also in `config.collections`). |
|
|
77
|
-
| `localePrefix` | No | — | Per-locale URL prefix for Auto path building (`Record<string, string>`). |
|
|
78
|
-
| `trash` | No | `true` | Soft delete on merged collection unless host `collection.trash` is set. |
|
|
79
|
-
| `access` | No | — | Overrides for `read` / `create` / `update` / `delete`. Host collection access wins when set. |
|
|
52
|
+
Put collection-specific settings in `custom.redirect.publishedLink`. `custom.draft.publishedLink`
|
|
53
|
+
is also read when `custom.redirect.publishedLink` is absent.
|
|
80
54
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
Use **one** wiring style per collection — not both. Each style registers the same `createRedirect` `afterChange` logic; combining them would run Auto twice on every publish.
|
|
86
|
-
|
|
87
|
-
| Style | When to use |
|
|
88
|
-
| --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
89
|
-
| **Declarative** (`custom.redirect`) | Default. The `redirects()` plugin appends `afterChange` when it sees `custom.redirect` on the collection. |
|
|
90
|
-
| **Manual hook** (`createRedirect(...)`) | When you cannot use `custom.redirect` (generated configs, shared hook arrays) or you must control exact hook order relative to other hooks. Do **not** set `custom.redirect` on the same collection. |
|
|
91
|
-
|
|
92
|
-
**Declarative** — identifier on the collection; plugin wires the hook:
|
|
55
|
+
Use `path` for a shared template, `paths` for locale-specific templates, or `getPath` for custom
|
|
56
|
+
logic. Template parameters match top-level collection fields; localized fields resolve for the
|
|
57
|
+
published locale. Dynamic values are URL-encoded as single path segments.
|
|
93
58
|
|
|
94
59
|
```ts
|
|
95
|
-
{
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
60
|
+
custom: {
|
|
61
|
+
redirect: {
|
|
62
|
+
identifier: 'slug',
|
|
63
|
+
publishedLink: {
|
|
64
|
+
paths: {
|
|
65
|
+
en: '/articles/[slug]',
|
|
66
|
+
pl: '/artykuly/[slug]',
|
|
67
|
+
},
|
|
102
68
|
},
|
|
103
69
|
},
|
|
104
70
|
}
|
|
105
71
|
```
|
|
106
72
|
|
|
107
|
-
|
|
73
|
+
`getPath` receives the published document snapshot and locale. Return a site path or `null` to skip
|
|
74
|
+
that transition:
|
|
108
75
|
|
|
109
76
|
```ts
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
afterChange: [createRedirect({ identifier: 'slug' })],
|
|
77
|
+
publishedLink: {
|
|
78
|
+
getPath: ({ doc, locale }) =>
|
|
79
|
+
`/articles/${String(doc.category)}/${String(doc.slug)}-${locale}`,
|
|
114
80
|
}
|
|
115
81
|
```
|
|
116
82
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
On publish, Auto compares the **previous** and **current** published identifier for each locale. The redirect rule is always **`from` → old public path**, **`to` → new public path** (both normalized). If the two paths are equal after normalization, no rule is created.
|
|
120
|
-
|
|
121
|
-
**Without** a resolvable `publishedLink` template (or when path building returns null for a side), each side falls back to the **normalized identifier value** alone (for example the slug string), not a full URL path — configure `publishedLink` when `from`/`to` must be site paths.
|
|
122
|
-
|
|
123
|
-
Templates live on the content collection under **`custom.redirect.publishedLink`**, or under **`custom.draft.publishedLink`** (same wire shape — useful when that block already exists on the collection):
|
|
83
|
+
Centralized path rules can be set in `redirects()`; collection `publishedLink` settings take precedence:
|
|
124
84
|
|
|
125
85
|
```ts
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
86
|
+
redirects({
|
|
87
|
+
localePrefix: { en: '', pl: '/pl' },
|
|
88
|
+
collections: {
|
|
89
|
+
articles: {
|
|
90
|
+
identifier: 'slug',
|
|
130
91
|
path: '/articles/[slug]',
|
|
131
|
-
paths: { pl: '/artykuly/[slug]' },
|
|
132
|
-
params: { slug: { field: 'slug', localized: true } },
|
|
133
92
|
},
|
|
134
93
|
},
|
|
135
|
-
},
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
Set plugin **`localePrefix`** when paths include locale segments (merged with link config):
|
|
139
|
-
|
|
140
|
-
```ts
|
|
141
|
-
redirects({
|
|
142
|
-
collection: Redirects,
|
|
143
|
-
localePrefix: { en: '', pl: '/pl' },
|
|
144
|
-
})
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
Redirect **documents** have no locale field; each rule is one path identity. Identifier transitions are evaluated **per locale** on publish.
|
|
148
|
-
|
|
149
|
-
### Request context
|
|
150
|
-
|
|
151
|
-
Export **`REDIRECT_CONTEXT_KEYS`** from the main entry:
|
|
152
|
-
|
|
153
|
-
| Key | Value | Effect |
|
|
154
|
-
| ---------- | ------------------------- | ------------------------------------- |
|
|
155
|
-
| `skipAuto` | `payloadRedirectSkipAuto` | Skips Auto redirect creation |
|
|
156
|
-
| `internal` | `payloadRedirectInternal` | Internal writes; sets origin **Auto** |
|
|
157
|
-
|
|
158
|
-
Set these flags only in **trusted server code** (Local API, background jobs) on `req.context`. Never copy values from request bodies, query strings, or other client-controlled input — a user could otherwise bypass Auto redirect creation or forge internal writes.
|
|
159
|
-
|
|
160
|
-
Use **`skipAuto`** when an import or job must not create Auto rules. Use **`internal`** together with server-side redirect writes so origin is recorded as **Auto**.
|
|
161
|
-
|
|
162
|
-
## Admin usage
|
|
163
|
-
|
|
164
|
-
| Field | Description |
|
|
165
|
-
| -------------- | ------------------------------------------------------ |
|
|
166
|
-
| **from** | Site-relative source path; unique. |
|
|
167
|
-
| **to** | Site-relative destination; required for `301` / `302`. |
|
|
168
|
-
| **StatusCode** | `301` (default) or `302`; both require **to**. |
|
|
169
|
-
| **enabled** | Disabled rules are ignored by `resolveRedirect`. |
|
|
170
|
-
| **origin** | Read-only **Manual** (`manual`) or **Auto** (`auto`). |
|
|
171
|
-
|
|
172
|
-
Rules created in the admin UI are **Manual**. Duplicate `from`: _A redirect rule already exists for this source path_.
|
|
173
|
-
|
|
174
|
-
## Auto redirects
|
|
175
|
-
|
|
176
|
-
Created on publish when a published identifier transition is detected (`origin` **Auto**, `301`, enabled). See [Published paths and localization](#published-paths-and-localization) for how `from`/`to` are computed.
|
|
177
|
-
|
|
178
|
-
**Not created:** draft saves, first publication, `payloadRedirectSkipAuto`, unpublished docs, unchanged identifier, invalid/empty paths, document delete, or when **Manual** rule already owns `from`.
|
|
179
|
-
|
|
180
|
-
**Updated:** existing **Auto** rule on same `from` when `to` changes.
|
|
181
|
-
|
|
182
|
-
## Redirect resolution (host responsibility)
|
|
183
|
-
|
|
184
|
-
```ts
|
|
185
|
-
import { resolveRedirect } from '@klnap/payload-redirect'
|
|
186
|
-
|
|
187
|
-
const result = await resolveRedirect({
|
|
188
|
-
payload,
|
|
189
|
-
from: requestPathname,
|
|
190
94
|
})
|
|
191
|
-
// { statusCode: 301 | 302; to: string } | null
|
|
192
95
|
```
|
|
193
96
|
|
|
194
|
-
|
|
97
|
+
Payload localization defines the available locales. If you set a prefix map, include every locale.
|
|
98
|
+
An empty prefix means no locale segment. Without a prefix map, the builder uses the locale code.
|
|
195
99
|
|
|
196
|
-
|
|
197
|
-
- Matching is case-sensitive and does not decode percent escapes; pass the same path representation used when saving the rule.
|
|
198
|
-
- Query strings are matched exactly. Source fragments are rejected because HTTP requests do not send them.
|
|
199
|
-
- Literal and percent-encoded dot segments normalize to their browser-equivalent path (for example, `/a/../b` becomes `/b`). Other percent escapes and Unicode remain literal.
|
|
200
|
-
- Saving an enabled rule rejects destinations whose existing redirect chain loops or exceeds 1,000 hops.
|
|
201
|
-
- **`null`:** no enabled rule — serve normal content or 404.
|
|
202
|
-
- **`RedirectAmbiguousRuleError`:** more than one match (data issue).
|
|
203
|
-
- Does not follow redirect chains; the returned **`to`** is not resolved recursively (your app may call `resolveRedirect` again if you implement chaining yourself).
|
|
100
|
+
## Redirect collection
|
|
204
101
|
|
|
205
|
-
|
|
102
|
+
The plugin owns the collection and its required fields: `from`, `to`, `StatusCode`, `enabled`, and
|
|
103
|
+
`origin`. `from` is unique. Redirect writes normalize paths and reject duplicate sources and
|
|
104
|
+
enabled loops. Manual rules are not overwritten by publication hooks.
|
|
206
105
|
|
|
207
|
-
|
|
208
|
-
- Auto writes run with `overrideAccess: false` and the internal context (`payloadRedirectInternal`). They pass create and update access only through that context. Delete access still needs a user. See [docs/FAILURE_SEMANTICS.md](docs/FAILURE_SEMANTICS.md).
|
|
209
|
-
- Default **create**, **update** and **delete** of manual rules accept only a signed-in human (`req.user` with a type other than `service`). Service principals are refused by default, so automation cannot write manual rules. Package-internal automatic writes are unaffected. A host that deliberately lets a service principal manage manual rules grants it explicitly through the plugin's `access` option, one operation at a time. The override applies only to the operations it names:
|
|
106
|
+
Pass native Payload collection settings through `overrides`:
|
|
210
107
|
|
|
211
108
|
```ts
|
|
212
109
|
redirects({
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
110
|
+
overrides: {
|
|
111
|
+
slug: 'url-rules',
|
|
112
|
+
labels: { singular: 'URL rule', plural: 'URL rules' },
|
|
113
|
+
admin: { group: 'Content' },
|
|
216
114
|
},
|
|
217
115
|
})
|
|
218
116
|
```
|
|
219
117
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
## Errors
|
|
223
|
-
|
|
224
|
-
See [docs/FAILURE_SEMANTICS.md](docs/FAILURE_SEMANTICS.md).
|
|
225
|
-
|
|
226
|
-
## Public API
|
|
227
|
-
|
|
228
|
-
### `@klnap/payload-redirect`
|
|
118
|
+
Use `overrides.fields` to customize the plugin fields. It receives `defaultFields`; required fields,
|
|
119
|
+
validators, uniqueness, and indexes remain enforced.
|
|
229
120
|
|
|
230
|
-
`
|
|
121
|
+
`enabled: false` disables automatic generation only. The manual redirects collection remains
|
|
122
|
+
available.
|
|
231
123
|
|
|
232
|
-
##
|
|
124
|
+
## Checks and limits
|
|
233
125
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
| No Auto rule | Draft/first publish/skip context/no transition |
|
|
239
|
-
| Auto skipped | Manual rule on same `from` |
|
|
240
|
-
| Auto runs twice on publish | Both `custom.redirect` and `createRedirect` hook on the same collection — pick one wiring style |
|
|
241
|
-
| Auto paths are raw slugs | No published-link template or path build returned null — add `publishedLink` or check `params` |
|
|
126
|
+
- PostgreSQL is required for transaction-scoped advisory locking and race-safe graph validation.
|
|
127
|
+
- Other database adapters are not claimed to provide equivalent concurrency guarantees.
|
|
128
|
+
- The plugin stores redirect rules; the host application serves them from its routing layer.
|
|
129
|
+
- The package does not expose a redirect resolver or HTTP middleware.
|
|
242
130
|
|
|
243
|
-
|
|
131
|
+
Run package checks:
|
|
244
132
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
133
|
+
```sh
|
|
134
|
+
bun run typecheck
|
|
135
|
+
bun run typecheck:consumer
|
|
136
|
+
bun test src tests/unit
|
|
137
|
+
bun run test:int
|
|
138
|
+
bun run check:release
|
|
139
|
+
```
|
|
@@ -7,6 +7,18 @@ import { paramKeyFromIdentifierPath } from '../identifier-field-path.js';
|
|
|
7
7
|
import { normalizePathOrUrl } from '../normalize-path.js';
|
|
8
8
|
import { getRedirectRuntime } from '../runtime.js';
|
|
9
9
|
import { buildPublishedPath } from '../../url/published-path.js';
|
|
10
|
+
const publishedDataForLocale = (doc, collection, locale) => {
|
|
11
|
+
const data = { ...doc };
|
|
12
|
+
for (const field of collection.fields) {
|
|
13
|
+
if (!('name' in field) || typeof field.name !== 'string' || !('localized' in field))
|
|
14
|
+
continue;
|
|
15
|
+
const value = data[field.name];
|
|
16
|
+
if (field.localized === true && value && typeof value === 'object' && !Array.isArray(value)) {
|
|
17
|
+
data[field.name] = value[locale];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return data;
|
|
21
|
+
};
|
|
10
22
|
export const createRedirect = (options) => async (args) => {
|
|
11
23
|
const { req, collection } = args;
|
|
12
24
|
if (req.context?.[REDIRECT_CONTEXT_KEYS.skipAuto]) {
|
|
@@ -19,23 +31,27 @@ export const createRedirect = (options) => async (args) => {
|
|
|
19
31
|
}
|
|
20
32
|
const { collectionSlug, localePrefix } = getRedirectRuntime(req.payload.config);
|
|
21
33
|
const pathConfig = resolveCollectionPublishedPathConfig(collection, localePrefix);
|
|
22
|
-
for (const { previous, current, locale } of pairs) {
|
|
23
|
-
const resolvePath = (identifierValue) => {
|
|
34
|
+
for (const { previous, current, locale, previousDoc, currentDoc } of pairs) {
|
|
35
|
+
const resolvePath = (identifierValue, publishedDoc) => {
|
|
24
36
|
if (!pathConfig || !locale)
|
|
25
37
|
return identifierValue;
|
|
26
38
|
const paramKey = paramKeyFromIdentifierPath(options.identifier);
|
|
27
39
|
const mapping = pathConfig.params?.[paramKey];
|
|
28
|
-
const data =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
const data = publishedDataForLocale(publishedDoc, collection, locale);
|
|
41
|
+
if (data[paramKey] === undefined) {
|
|
42
|
+
data[paramKey] =
|
|
43
|
+
mapping &&
|
|
44
|
+
typeof mapping === 'object' &&
|
|
45
|
+
'localized' in mapping &&
|
|
46
|
+
mapping.localized === true
|
|
47
|
+
? { [locale]: identifierValue }
|
|
48
|
+
: identifierValueForPath(options.identifier, identifierValue)[paramKey];
|
|
49
|
+
}
|
|
34
50
|
const built = buildPublishedPath(pathConfig, data, locale);
|
|
35
51
|
return built;
|
|
36
52
|
};
|
|
37
|
-
const fromRaw = resolvePath(previous);
|
|
38
|
-
const toRaw = resolvePath(current);
|
|
53
|
+
const fromRaw = resolvePath(previous, previousDoc);
|
|
54
|
+
const toRaw = resolvePath(current, currentDoc);
|
|
39
55
|
if (!fromRaw || !toRaw) {
|
|
40
56
|
continue;
|
|
41
57
|
}
|
|
@@ -17,6 +17,7 @@ export const ensureAutoRedirect = async ({ req, collectionSlug, rule, context, }
|
|
|
17
17
|
enabled: true,
|
|
18
18
|
origin: 'auto',
|
|
19
19
|
};
|
|
20
|
+
const payload = req.payload;
|
|
20
21
|
const existing = await findRedirectByFrom(req.payload, {
|
|
21
22
|
collectionSlug,
|
|
22
23
|
from: rule.from,
|
|
@@ -30,7 +31,7 @@ export const ensureAutoRedirect = async ({ req, collectionSlug, rule, context, }
|
|
|
30
31
|
return 'skipped';
|
|
31
32
|
}
|
|
32
33
|
if (doc.origin === 'auto') {
|
|
33
|
-
await
|
|
34
|
+
await payload.update({
|
|
34
35
|
collection: collectionSlug,
|
|
35
36
|
id: doc.id,
|
|
36
37
|
data: {
|
|
@@ -46,7 +47,7 @@ export const ensureAutoRedirect = async ({ req, collectionSlug, rule, context, }
|
|
|
46
47
|
}
|
|
47
48
|
return 'skipped';
|
|
48
49
|
}
|
|
49
|
-
await
|
|
50
|
+
await payload.create({
|
|
50
51
|
collection: collectionSlug,
|
|
51
52
|
data,
|
|
52
53
|
context: internalContext,
|
|
@@ -81,6 +81,8 @@ export const transitionForLocale = async ({ req, collection, docId, locale, iden
|
|
|
81
81
|
if (!current) {
|
|
82
82
|
return { published: true };
|
|
83
83
|
}
|
|
84
|
+
const currentDoc = [draftDoc, hookDoc, currentVersion, identifierSource].find((candidate) => candidate !== undefined &&
|
|
85
|
+
extractIdentifierFromVersionSnapshot(candidate, identifier, locale, localizedField, locale) === current);
|
|
84
86
|
let defaultSlug;
|
|
85
87
|
if (localizedField && defaultLocale) {
|
|
86
88
|
defaultSlug = extractIdentifierFromVersionSnapshot(hookDoc, identifier, defaultLocale, true, locale);
|
|
@@ -117,6 +119,7 @@ export const transitionForLocale = async ({ req, collection, docId, locale, iden
|
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
121
|
let previous;
|
|
122
|
+
let previousDoc;
|
|
120
123
|
for (const row of versionsForPrevious) {
|
|
121
124
|
const version = row.version;
|
|
122
125
|
if (!version)
|
|
@@ -139,6 +142,7 @@ export const transitionForLocale = async ({ req, collection, docId, locale, iden
|
|
|
139
142
|
continue;
|
|
140
143
|
}
|
|
141
144
|
previous = candidate;
|
|
145
|
+
previousDoc = version;
|
|
142
146
|
break;
|
|
143
147
|
}
|
|
144
148
|
if (!previous) {
|
|
@@ -159,5 +163,5 @@ export const transitionForLocale = async ({ req, collection, docId, locale, iden
|
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
}
|
|
162
|
-
return { published: true, locale, previous, current };
|
|
166
|
+
return { published: true, locale, previous, current, previousDoc, currentDoc };
|
|
163
167
|
};
|
|
@@ -3,5 +3,7 @@ export type PublicationTransitionPair = {
|
|
|
3
3
|
previous: string;
|
|
4
4
|
current: string;
|
|
5
5
|
locale?: string;
|
|
6
|
+
previousDoc?: Record<string, unknown>;
|
|
7
|
+
currentDoc?: Record<string, unknown>;
|
|
6
8
|
};
|
|
7
9
|
export declare const publicationTransitionsToPairs: (transitions: PublicationTransition[]) => PublicationTransitionPair[];
|
|
@@ -10,7 +10,13 @@ export const publicationTransitionsToPairs = (transitions) => {
|
|
|
10
10
|
});
|
|
11
11
|
if (!scalar)
|
|
12
12
|
continue;
|
|
13
|
-
pairs.push({
|
|
13
|
+
pairs.push({
|
|
14
|
+
previous: scalar.previous,
|
|
15
|
+
current: scalar.current,
|
|
16
|
+
locale: t.locale,
|
|
17
|
+
previousDoc: t.previousDoc,
|
|
18
|
+
currentDoc: t.currentDoc,
|
|
19
|
+
});
|
|
14
20
|
}
|
|
15
21
|
return pairs;
|
|
16
22
|
};
|
|
@@ -17,12 +17,16 @@ const parsePublishedLinkWire = (value) => {
|
|
|
17
17
|
if (isRecord(value.params) && Object.keys(value.params).length > 0) {
|
|
18
18
|
wire.params = value.params;
|
|
19
19
|
}
|
|
20
|
+
if (typeof value.getPath === 'function') {
|
|
21
|
+
wire.getPath = value.getPath;
|
|
22
|
+
}
|
|
20
23
|
if (isRecord(value.localePrefix) && Object.keys(value.localePrefix).length > 0) {
|
|
21
24
|
wire.localePrefix = value.localePrefix;
|
|
22
25
|
}
|
|
23
26
|
const hasTemplate = Boolean(wire.path) ||
|
|
24
27
|
Boolean(wire.paths && Object.keys(wire.paths).length > 0) ||
|
|
25
|
-
Boolean(wire.routes && Object.keys(wire.routes).length > 0)
|
|
28
|
+
Boolean(wire.routes && Object.keys(wire.routes).length > 0) ||
|
|
29
|
+
typeof wire.getPath === 'function';
|
|
26
30
|
return hasTemplate ? wire : null;
|
|
27
31
|
};
|
|
28
32
|
const readDraftPublishedLink = (custom) => {
|
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import { readPublishedLinkWire } from './published-link-config.js';
|
|
2
2
|
import { paramKeyFromIdentifierPath } from '../identifier-field-path.js';
|
|
3
3
|
import { resolvePublishedPathConfig } from '../../url/published-path.js';
|
|
4
|
+
import { extractDynamicSegments } from '../../url/route-params.js';
|
|
4
5
|
export function resolveCollectionPublishedPathConfig(collection, pluginLocalePrefix) {
|
|
5
6
|
const link = readPublishedLinkWire(collection.custom);
|
|
6
|
-
|
|
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
|
+
});
|
|
7
24
|
}
|
|
8
25
|
export function identifierValueForPath(identifier, value) {
|
|
9
26
|
const key = paramKeyFromIdentifierPath(identifier);
|
|
@@ -0,0 +1,7 @@
|
|
|
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;
|
|
@@ -0,0 +1,19 @@
|
|
|
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,13 +1,8 @@
|
|
|
1
1
|
import type { Access, PayloadRequest } from 'payload';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export declare const isInternalRedirectWrite: (req: PayloadRequest) => boolean;
|
|
3
|
+
export declare const defaultRedirectAccess: {
|
|
4
|
+
read: Access;
|
|
5
|
+
create: Access;
|
|
6
|
+
update: Access;
|
|
7
|
+
delete: Access;
|
|
7
8
|
};
|
|
8
|
-
export declare const isRedirectInternalRequest: (req: PayloadRequest) => boolean;
|
|
9
|
-
export declare const redirectReadAccess: Access;
|
|
10
|
-
export declare const redirectCreateAccess: Access;
|
|
11
|
-
export declare const redirectUpdateAccess: Access;
|
|
12
|
-
export declare const redirectDeleteAccess: Access;
|
|
13
|
-
export declare const mergeRedirectsAccess: (base: RedirectsCollectionAccess | undefined, overrides: RedirectsCollectionAccess | undefined) => RedirectsCollectionAccess;
|
|
@@ -1,26 +1,9 @@
|
|
|
1
1
|
import { REDIRECT_CONTEXT_KEYS } from '../context-keys.js';
|
|
2
|
-
export const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const isHumanSignedIn = (req) => Boolean(req.user) && req.user.type !== 'service';
|
|
10
|
-
export const redirectCreateAccess = ({ req }) => {
|
|
11
|
-
if (isRedirectInternalRequest(req))
|
|
12
|
-
return true;
|
|
13
|
-
return isHumanSignedIn(req);
|
|
2
|
+
export const isInternalRedirectWrite = (req) => Boolean(req.context?.[REDIRECT_CONTEXT_KEYS.internal]);
|
|
3
|
+
const isHumanUser = (req) => Boolean(req.user) && req.user.type !== 'service';
|
|
4
|
+
export const defaultRedirectAccess = {
|
|
5
|
+
read: (() => true),
|
|
6
|
+
create: (({ req }) => isInternalRedirectWrite(req) || isHumanUser(req)),
|
|
7
|
+
update: (({ req }) => isInternalRedirectWrite(req) || isHumanUser(req)),
|
|
8
|
+
delete: (({ req }) => isHumanUser(req)),
|
|
14
9
|
};
|
|
15
|
-
export const redirectUpdateAccess = ({ req }) => {
|
|
16
|
-
if (isRedirectInternalRequest(req))
|
|
17
|
-
return true;
|
|
18
|
-
return isHumanSignedIn(req);
|
|
19
|
-
};
|
|
20
|
-
export const redirectDeleteAccess = ({ req }) => isHumanSignedIn(req);
|
|
21
|
-
export const mergeRedirectsAccess = (base, overrides) => ({
|
|
22
|
-
read: base?.read ?? overrides?.read ?? redirectReadAccess,
|
|
23
|
-
create: base?.create ?? overrides?.create ?? redirectCreateAccess,
|
|
24
|
-
update: base?.update ?? overrides?.update ?? redirectUpdateAccess,
|
|
25
|
-
delete: base?.delete ?? overrides?.delete ?? redirectDeleteAccess,
|
|
26
|
-
});
|
|
@@ -1,11 +1,3 @@
|
|
|
1
1
|
import type { CollectionConfig } from 'payload';
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
4
|
-
base?: CollectionConfig;
|
|
5
|
-
/** Soft-delete (trash). Default true; set false to disable. Base collection `trash` wins when set. */
|
|
6
|
-
trash?: boolean;
|
|
7
|
-
/** Plugin-level access overrides (applied when base collection omits a handler). */
|
|
8
|
-
access?: RedirectsCollectionAccess;
|
|
9
|
-
};
|
|
10
|
-
export declare const createRedirectsCollection: (options: CreateRedirectsCollectionOptions) => CollectionConfig;
|
|
11
|
-
export type { RedirectsCollectionAccess } from './access.js';
|
|
2
|
+
import type { RedirectCollectionOverrides } from './types.js';
|
|
3
|
+
export declare const createRedirectsCollection: (overrides?: RedirectCollectionOverrides) => CollectionConfig;
|