@crombolo/simple-redirects 0.36.2 → 0.38.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -2
- package/src/index.ts +77 -76
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/simple-redirects",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.38.1",
|
|
4
4
|
"description": "Manage redirects in the Crombolo backoffice: a Settings screen over the redirect engine in core.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -39,6 +39,12 @@
|
|
|
39
39
|
"id": "redirects",
|
|
40
40
|
"name": "Redirects",
|
|
41
41
|
"server": "redirects",
|
|
42
|
+
"schemaVersion": 3,
|
|
43
|
+
"permissions": [
|
|
44
|
+
"redirects.read",
|
|
45
|
+
"redirects.write",
|
|
46
|
+
"documents.read"
|
|
47
|
+
],
|
|
42
48
|
"extensions": [
|
|
43
49
|
{
|
|
44
50
|
"type": "menuItem",
|
|
@@ -66,6 +72,6 @@
|
|
|
66
72
|
]
|
|
67
73
|
},
|
|
68
74
|
"peerDependencies": {
|
|
69
|
-
"@crombolo/crombolo": ">=0.
|
|
75
|
+
"@crombolo/crombolo": ">=0.38.1"
|
|
70
76
|
}
|
|
71
77
|
}
|
package/src/index.ts
CHANGED
|
@@ -13,11 +13,12 @@
|
|
|
13
13
|
* because a site imported `redirects()` into its `crombolo.config.ts` — see
|
|
14
14
|
* `docs/bundles.md` for why that asymmetry is the whole security story.
|
|
15
15
|
*/
|
|
16
|
-
import
|
|
17
|
-
BundleRedirectInput,
|
|
18
|
-
|
|
19
|
-
BundleSection,
|
|
20
|
-
|
|
16
|
+
import {
|
|
17
|
+
type BundleRedirectInput,
|
|
18
|
+
type BundleRouteContext,
|
|
19
|
+
type BundleSection,
|
|
20
|
+
route,
|
|
21
|
+
type ServerBundle,
|
|
21
22
|
} from '@crombolo/crombolo/bundle'
|
|
22
23
|
import manifest from '../package.json' with { type: 'json' }
|
|
23
24
|
|
|
@@ -26,6 +27,8 @@ const CAPABILITIES = ['redirects', 'documents', 'log'] as const
|
|
|
26
27
|
|
|
27
28
|
type Capability = (typeof CAPABILITIES)[number]
|
|
28
29
|
|
|
30
|
+
type Host = BundleRouteContext<Capability>['bundle']
|
|
31
|
+
|
|
29
32
|
/** The URL segment the endpoints answer below, and the name in the log: the package's `crombolo.id`. */
|
|
30
33
|
export const REDIRECTS_BUNDLE_ID = manifest.crombolo.id
|
|
31
34
|
|
|
@@ -67,8 +70,8 @@ const parseBody = async (request: Request): Promise<Record<string, unknown>> =>
|
|
|
67
70
|
* own redirect listing does.
|
|
68
71
|
*/
|
|
69
72
|
async function present(
|
|
70
|
-
rule: Awaited<ReturnType<
|
|
71
|
-
documents:
|
|
73
|
+
rule: Awaited<ReturnType<Host['redirects']['byKey']>>,
|
|
74
|
+
documents: Host['documents'],
|
|
72
75
|
) {
|
|
73
76
|
if (!rule) return undefined
|
|
74
77
|
return {
|
|
@@ -102,79 +105,77 @@ export function redirects(options: RedirectsBundleOptions = {}): ServerBundle<Ca
|
|
|
102
105
|
id: REDIRECTS_BUNDLE_ID,
|
|
103
106
|
section,
|
|
104
107
|
capabilities: CAPABILITIES,
|
|
108
|
+
// Back-office routes (story 211): the screen calls them below the bundle's
|
|
109
|
+
// namespace, and the host has checked the user and the section first.
|
|
105
110
|
routes: [
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
target: outcome.rule.target,
|
|
133
|
-
})
|
|
134
|
-
return json({ ok: true, rule: await present(outcome.rule, host.documents) })
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
{
|
|
138
|
-
method: 'PUT',
|
|
139
|
-
path: 'rules/:key',
|
|
140
|
-
async handler({ request, params, principal, host }) {
|
|
141
|
-
const outcome = await host.redirects.replace(
|
|
142
|
-
params.key as string,
|
|
143
|
-
readInput(await parseBody(request)),
|
|
144
|
-
)
|
|
145
|
-
if (!outcome.ok)
|
|
146
|
-
return json(
|
|
147
|
-
{ ok: false, message: outcome.message },
|
|
148
|
-
outcome.reason === 'notFound' ? 404 : outcome.reason === 'notEditable' ? 409 : 400,
|
|
149
|
-
)
|
|
150
|
-
host.log.info('{user} changed the redirect {pattern} -> {target}', {
|
|
151
|
-
user: principal.email,
|
|
152
|
-
pattern: outcome.rule.pattern,
|
|
153
|
-
target: outcome.rule.target,
|
|
154
|
-
})
|
|
155
|
-
return json({ ok: true, rule: await present(outcome.rule, host.documents) })
|
|
111
|
+
route<unknown, unknown, BundleRouteContext<Capability>>(
|
|
112
|
+
'rules',
|
|
113
|
+
{
|
|
114
|
+
async GET(_request, { url, bundle }) {
|
|
115
|
+
const take = Math.min(Math.max(Number(url.searchParams.get('take') ?? 100), 1), 500)
|
|
116
|
+
const skip = Math.max(Number(url.searchParams.get('skip') ?? 0), 0)
|
|
117
|
+
const result = await bundle.redirects.list({
|
|
118
|
+
filter: url.searchParams.get('filter') ?? undefined,
|
|
119
|
+
skip,
|
|
120
|
+
take,
|
|
121
|
+
})
|
|
122
|
+
return json({
|
|
123
|
+
total: result.total,
|
|
124
|
+
items: await Promise.all(result.items.map((rule) => present(rule, bundle.documents))),
|
|
125
|
+
})
|
|
126
|
+
},
|
|
127
|
+
async POST(request, { principal, bundle }) {
|
|
128
|
+
const outcome = await bundle.redirects.save(readInput(await parseBody(request)))
|
|
129
|
+
if (!outcome.ok) return json({ ok: false, message: outcome.message }, 400)
|
|
130
|
+
bundle.log.info('{user} added the redirect {pattern} -> {target}', {
|
|
131
|
+
user: principal?.email,
|
|
132
|
+
pattern: outcome.rule.pattern,
|
|
133
|
+
target: outcome.rule.target,
|
|
134
|
+
})
|
|
135
|
+
return json({ ok: true, rule: await present(outcome.rule, bundle.documents) })
|
|
136
|
+
},
|
|
156
137
|
},
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return json(
|
|
167
|
-
{
|
|
168
|
-
ok: false,
|
|
169
|
-
message:
|
|
170
|
-
'Only a redirect added here can be deleted here. A configured rule belongs to the site, and a tracked one to the page that was renamed.',
|
|
171
|
-
},
|
|
172
|
-
409,
|
|
138
|
+
{ audience: 'backoffice' },
|
|
139
|
+
),
|
|
140
|
+
route<unknown, unknown, BundleRouteContext<Capability>>(
|
|
141
|
+
'rules/:key',
|
|
142
|
+
{
|
|
143
|
+
async PUT(request, { params, principal, bundle }) {
|
|
144
|
+
const outcome = await bundle.redirects.replace(
|
|
145
|
+
params.key as string,
|
|
146
|
+
readInput(await parseBody(request)),
|
|
173
147
|
)
|
|
174
|
-
|
|
175
|
-
|
|
148
|
+
if (!outcome.ok)
|
|
149
|
+
return json(
|
|
150
|
+
{ ok: false, message: outcome.message },
|
|
151
|
+
outcome.reason === 'notFound' ? 404 : outcome.reason === 'notEditable' ? 409 : 400,
|
|
152
|
+
)
|
|
153
|
+
bundle.log.info('{user} changed the redirect {pattern} -> {target}', {
|
|
154
|
+
user: principal?.email,
|
|
155
|
+
pattern: outcome.rule.pattern,
|
|
156
|
+
target: outcome.rule.target,
|
|
157
|
+
})
|
|
158
|
+
return json({ ok: true, rule: await present(outcome.rule, bundle.documents) })
|
|
159
|
+
},
|
|
160
|
+
async DELETE(_request, { params, principal, bundle }) {
|
|
161
|
+
const outcome = await bundle.redirects.remove(params.key as string)
|
|
162
|
+
if (outcome === 'notFound')
|
|
163
|
+
return json({ ok: false, message: 'That redirect no longer exists.' }, 404)
|
|
164
|
+
if (outcome === 'notEditable')
|
|
165
|
+
return json(
|
|
166
|
+
{
|
|
167
|
+
ok: false,
|
|
168
|
+
message:
|
|
169
|
+
'Only a redirect added here can be deleted here. A configured rule belongs to the site, and a tracked one to the page that was renamed.',
|
|
170
|
+
},
|
|
171
|
+
409,
|
|
172
|
+
)
|
|
173
|
+
bundle.log.info('{user} deleted a redirect', { user: principal?.email })
|
|
174
|
+
return json({ ok: true })
|
|
175
|
+
},
|
|
176
176
|
},
|
|
177
|
-
|
|
177
|
+
{ audience: 'backoffice' },
|
|
178
|
+
),
|
|
178
179
|
],
|
|
179
180
|
}
|
|
180
181
|
}
|