@decocms/blocks 7.77.10 → 7.77.12
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 +1 -1
- package/src/cms/blockSource.ts +11 -2
- package/src/cms/schema.test.ts +52 -0
- package/src/cms/schema.ts +86 -5
- package/src/sdk/index.ts +1 -0
- package/src/sdk/redirects.legacy.test.ts +164 -0
- package/src/sdk/redirects.test.ts +328 -0
- package/src/sdk/redirects.ts +384 -79
- package/src/sdk/splitExactRedirects.test.ts +9 -7
package/package.json
CHANGED
package/src/cms/blockSource.ts
CHANGED
|
@@ -175,10 +175,19 @@ export function redirectKey(id: string, path: string): string {
|
|
|
175
175
|
return `${redirectPrefix(id)}${path}`;
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
/**
|
|
178
|
+
/**
|
|
179
|
+
* One rule stored at a `redirect:<id>:<path>` key. The value is one rule, or an
|
|
180
|
+
* array of them when several sources fold to the same `path` (see
|
|
181
|
+
* `ExactRedirect.shadowed` in `@decocms/blocks/sdk/redirects`).
|
|
182
|
+
*/
|
|
179
183
|
export interface StoredRedirect {
|
|
184
|
+
/** Source as written. Absent on values written before it existed. */
|
|
185
|
+
from?: string;
|
|
180
186
|
to: string;
|
|
181
|
-
|
|
187
|
+
/** Temporary is always written as 307; `"legacy"` semantics answer it as 302. */
|
|
188
|
+
status: 301 | 307;
|
|
189
|
+
/** Answer with `to` as written instead of appending the request's query. */
|
|
190
|
+
discardQueryParameters?: boolean;
|
|
182
191
|
}
|
|
183
192
|
|
|
184
193
|
// ---------------------------------------------------------------------------
|
package/src/cms/schema.test.ts
CHANGED
|
@@ -354,3 +354,55 @@ describe("composeMeta idempotency", () => {
|
|
|
354
354
|
expect(recomposed.schema.root.sections.anyOf).toHaveLength(sectionsAnyOf.length);
|
|
355
355
|
});
|
|
356
356
|
});
|
|
357
|
+
|
|
358
|
+
describe("composeMeta: requestToParam function block", () => {
|
|
359
|
+
const fnKey = b64("website/functions/requestToParam.ts");
|
|
360
|
+
|
|
361
|
+
it("bakes the function block, its props and the RequestURLParam type", () => {
|
|
362
|
+
const meta = composeMeta(emptySiteMeta());
|
|
363
|
+
expect(meta.manifest.blocks.functions?.["website/functions/requestToParam.ts"]).toEqual({
|
|
364
|
+
$ref: `#/definitions/${fnKey}`,
|
|
365
|
+
namespace: "website",
|
|
366
|
+
});
|
|
367
|
+
expect(meta.schema.definitions[`${fnKey}@Props`].properties.param).toMatchObject({
|
|
368
|
+
type: "string",
|
|
369
|
+
default: "slug",
|
|
370
|
+
});
|
|
371
|
+
const paramType = meta.schema.definitions[`${fnKey}@RequestURLParam`];
|
|
372
|
+
expect(paramType.anyOf[0]).toEqual({ type: "string", title: "Force param" });
|
|
373
|
+
expect(paramType.anyOf[1].properties.__resolveType.enum).toEqual([
|
|
374
|
+
"website/functions/requestToParam.ts",
|
|
375
|
+
]);
|
|
376
|
+
expect(meta.schema.root.functions.anyOf).toContainEqual({
|
|
377
|
+
$ref: `#/definitions/${fnKey}`,
|
|
378
|
+
inputSchema: `#/definitions/${fnKey}@Props`,
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
describe("composeMeta: Resolvable wrapping of array props", () => {
|
|
384
|
+
it("leaves a plain array of objects inline (no saved-block picker, as in deco-cx/deco)", () => {
|
|
385
|
+
const site = emptySiteMeta();
|
|
386
|
+
const images = {
|
|
387
|
+
type: "array",
|
|
388
|
+
items: {
|
|
389
|
+
type: "object",
|
|
390
|
+
properties: {
|
|
391
|
+
imageSrc: { type: "string", format: "image-uri" },
|
|
392
|
+
width: { type: "number" },
|
|
393
|
+
height: { type: "number" },
|
|
394
|
+
widthMobile: { type: "number" },
|
|
395
|
+
heightMobile: { type: "number" },
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
title: "Images",
|
|
399
|
+
};
|
|
400
|
+
site.schema.definitions["slider.tsx@Props"] = {
|
|
401
|
+
type: "object",
|
|
402
|
+
properties: { images },
|
|
403
|
+
required: ["images"],
|
|
404
|
+
};
|
|
405
|
+
const meta = composeMeta(site);
|
|
406
|
+
expect(meta.schema.definitions["slider.tsx@Props"].properties.images).toEqual(images);
|
|
407
|
+
});
|
|
408
|
+
});
|
package/src/cms/schema.ts
CHANGED
|
@@ -978,6 +978,72 @@ function buildFrameworkSections(sectionAnyOf: any[], loaderUnion: any[]) {
|
|
|
978
978
|
return { definitions, manifestBlocks, extraAnyOf };
|
|
979
979
|
}
|
|
980
980
|
|
|
981
|
+
// ---------------------------------------------------------------------------
|
|
982
|
+
// Functions: website/functions/requestToParam.ts
|
|
983
|
+
// ---------------------------------------------------------------------------
|
|
984
|
+
|
|
985
|
+
const REQUEST_TO_PARAM_TYPE = "website/functions/requestToParam.ts";
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* The `requestToParam` function block and the `RequestURLParam` prop type the
|
|
989
|
+
* schema generator points at, shaped as deco-cx/deco emitted them. The runtime
|
|
990
|
+
* already resolves saved `requestToParam` blocks (WELL_KNOWN_TYPES); without
|
|
991
|
+
* these definitions the admin could not offer "Get params from request
|
|
992
|
+
* parameters" for a `RequestURLParam` prop.
|
|
993
|
+
*
|
|
994
|
+
* deco-cx/deco gave `param` a `dynamic-options` picker backed by
|
|
995
|
+
* `website/loaders/options/urlParams.ts`, which has no port here — a plain
|
|
996
|
+
* string (default `slug`) keeps the field editable instead of an empty picker.
|
|
997
|
+
*/
|
|
998
|
+
function buildFunctionDefinitions() {
|
|
999
|
+
const fnKey = toBase64(REQUEST_TO_PARAM_TYPE);
|
|
1000
|
+
const propsKey = `${fnKey}@Props`;
|
|
1001
|
+
const paramTypeKey = `${fnKey}@RequestURLParam`;
|
|
1002
|
+
|
|
1003
|
+
const fnDef = {
|
|
1004
|
+
title: "Get params from request parameters",
|
|
1005
|
+
description: "Set param to slug for routes of type /:slug",
|
|
1006
|
+
type: "object",
|
|
1007
|
+
allOf: [{ $ref: `#/definitions/${propsKey}` }],
|
|
1008
|
+
required: ["__resolveType"],
|
|
1009
|
+
properties: {
|
|
1010
|
+
__resolveType: {
|
|
1011
|
+
type: "string",
|
|
1012
|
+
enum: [REQUEST_TO_PARAM_TYPE],
|
|
1013
|
+
default: REQUEST_TO_PARAM_TYPE,
|
|
1014
|
+
},
|
|
1015
|
+
},
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
return {
|
|
1019
|
+
definitions: {
|
|
1020
|
+
[fnKey]: fnDef,
|
|
1021
|
+
[propsKey]: {
|
|
1022
|
+
type: "object",
|
|
1023
|
+
properties: {
|
|
1024
|
+
param: {
|
|
1025
|
+
type: "string",
|
|
1026
|
+
default: "slug",
|
|
1027
|
+
description: "Param name to extract from the Request URL",
|
|
1028
|
+
title: "Param",
|
|
1029
|
+
},
|
|
1030
|
+
},
|
|
1031
|
+
required: ["param"],
|
|
1032
|
+
},
|
|
1033
|
+
[paramTypeKey]: {
|
|
1034
|
+
anyOf: [{ type: "string", title: "Force param" }, fnDef],
|
|
1035
|
+
},
|
|
1036
|
+
} as Record<string, any>,
|
|
1037
|
+
manifestBlocks: {
|
|
1038
|
+
[REQUEST_TO_PARAM_TYPE]: { $ref: `#/definitions/${fnKey}`, namespace: "website" },
|
|
1039
|
+
} as Record<string, any>,
|
|
1040
|
+
functionAnyOf: [
|
|
1041
|
+
{ $ref: `#/definitions/${RESOLVABLE_LITERAL_KEY}` },
|
|
1042
|
+
{ $ref: `#/definitions/${fnKey}`, inputSchema: `#/definitions/${propsKey}` },
|
|
1043
|
+
],
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
|
|
981
1047
|
// ---------------------------------------------------------------------------
|
|
982
1048
|
// Post-processing: wrap complex properties with Resolvable anyOf
|
|
983
1049
|
// ---------------------------------------------------------------------------
|
|
@@ -1012,10 +1078,14 @@ function wrapResolvableProperties(
|
|
|
1012
1078
|
const shouldWrap = isLoaderCompatibleProperty(propSchema);
|
|
1013
1079
|
if (!shouldWrap) continue;
|
|
1014
1080
|
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
//
|
|
1081
|
+
// Only where a loader can actually fill the property (product arrays).
|
|
1082
|
+
// deco-cx/deco offered no saved-block picker for a plain array of
|
|
1083
|
+
// objects, and wrapping every one added a "Select from saved" mode the
|
|
1084
|
+
// CMS user never had.
|
|
1018
1085
|
const loaderRefs = isProductArrayProperty(propSchema) ? productLoaderRefs : [];
|
|
1086
|
+
if (loaderRefs.length === 0) continue;
|
|
1087
|
+
|
|
1088
|
+
const { nullable, title, hide, ...rest } = propSchema;
|
|
1019
1089
|
|
|
1020
1090
|
const wrapped: any = {
|
|
1021
1091
|
anyOf: [resolvableRef, { ...rest, title: title || "Inline data" }, ...loaderRefs],
|
|
@@ -1066,9 +1136,13 @@ function wrapNestedProperties(
|
|
|
1066
1136
|
if (!propSchema || typeof propSchema !== "object") continue;
|
|
1067
1137
|
if (propSchema.anyOf || propSchema.$ref) continue;
|
|
1068
1138
|
|
|
1069
|
-
|
|
1139
|
+
const nestedLoaderRefs =
|
|
1140
|
+
isLoaderCompatibleProperty(propSchema) && isProductArrayProperty(propSchema)
|
|
1141
|
+
? productLoaderRefs
|
|
1142
|
+
: [];
|
|
1143
|
+
if (nestedLoaderRefs.length > 0) {
|
|
1070
1144
|
const { nullable, title, hide, ...rest } = propSchema;
|
|
1071
|
-
const loaderRefs =
|
|
1145
|
+
const loaderRefs = nestedLoaderRefs;
|
|
1072
1146
|
const wrapped: any = {
|
|
1073
1147
|
anyOf: [resolvableRef, { ...rest, title: title || "Inline data" }, ...loaderRefs],
|
|
1074
1148
|
};
|
|
@@ -1153,6 +1227,7 @@ export function composeMeta(siteMeta: MetaResponse, options?: ComposeMetaOptions
|
|
|
1153
1227
|
const page = buildPageSchema(fullSectionAnyOf);
|
|
1154
1228
|
const actions = buildActionDefinitions();
|
|
1155
1229
|
const matchers = buildMatcherDefinitions();
|
|
1230
|
+
const functions = buildFunctionDefinitions();
|
|
1156
1231
|
|
|
1157
1232
|
const sectionRefDef = { title: "Section", anyOf: fullSectionAnyOf };
|
|
1158
1233
|
|
|
@@ -1166,6 +1241,7 @@ export function composeMeta(siteMeta: MetaResponse, options?: ComposeMetaOptions
|
|
|
1166
1241
|
...loaders.definitions,
|
|
1167
1242
|
...actions.definitions,
|
|
1168
1243
|
...matchers.definitions,
|
|
1244
|
+
...functions.definitions,
|
|
1169
1245
|
...fwSections.definitions,
|
|
1170
1246
|
...page.definitions,
|
|
1171
1247
|
...(siteMeta.schema?.definitions || {}),
|
|
@@ -1203,6 +1279,10 @@ export function composeMeta(siteMeta: MetaResponse, options?: ComposeMetaOptions
|
|
|
1203
1279
|
...(siteMeta.manifest?.blocks?.matchers || {}),
|
|
1204
1280
|
...matchers.manifestBlocks,
|
|
1205
1281
|
},
|
|
1282
|
+
functions: {
|
|
1283
|
+
...(siteMeta.manifest?.blocks?.functions || {}),
|
|
1284
|
+
...functions.manifestBlocks,
|
|
1285
|
+
},
|
|
1206
1286
|
},
|
|
1207
1287
|
},
|
|
1208
1288
|
schema: {
|
|
@@ -1213,6 +1293,7 @@ export function composeMeta(siteMeta: MetaResponse, options?: ComposeMetaOptions
|
|
|
1213
1293
|
pages: { anyOf: page.rootAnyOf },
|
|
1214
1294
|
loaders: { anyOf: loaderUnion },
|
|
1215
1295
|
matchers: { anyOf: matchers.matcherAnyOf },
|
|
1296
|
+
functions: { title: "functions", anyOf: functions.functionAnyOf },
|
|
1216
1297
|
},
|
|
1217
1298
|
},
|
|
1218
1299
|
};
|
package/src/sdk/index.ts
CHANGED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `semantics: "legacy"` — the default. Pins what sites already on `@decocms/*`
|
|
3
|
+
* answer (case- and trailing-slash-insensitive match, 302 for temporary, query
|
|
4
|
+
* dropped), so turning on `"fresh"` for one site changes nothing for the
|
|
5
|
+
* others. The rows mirror real rules of two such storefronts.
|
|
6
|
+
*
|
|
7
|
+
* The only differences from before are rules that could never be served (a
|
|
8
|
+
* self-redirect loops forever) and a `Location` that `encodeURI` double-encoded.
|
|
9
|
+
*/
|
|
10
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
11
|
+
import {
|
|
12
|
+
addRedirects,
|
|
13
|
+
getRedirectSemantics,
|
|
14
|
+
loadRedirects,
|
|
15
|
+
matchRedirect,
|
|
16
|
+
normalizePath,
|
|
17
|
+
parseRedirectsCsv,
|
|
18
|
+
redirectLocation,
|
|
19
|
+
selectExactRedirect,
|
|
20
|
+
setRedirectSemantics,
|
|
21
|
+
splitExactRedirects,
|
|
22
|
+
} from "./redirects";
|
|
23
|
+
|
|
24
|
+
afterEach(() => setRedirectSemantics("legacy"));
|
|
25
|
+
|
|
26
|
+
const block = (redirects: Array<Record<string, unknown>>) =>
|
|
27
|
+
loadRedirects({ r: { __resolveType: "website/loaders/redirects.ts", redirects } });
|
|
28
|
+
|
|
29
|
+
describe("default", () => {
|
|
30
|
+
it("is legacy", () => {
|
|
31
|
+
expect(getRedirectSemantics()).toBe("legacy");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("matching folds case and a trailing slash", () => {
|
|
36
|
+
const map = block([
|
|
37
|
+
{ from: "/baixeoapp", to: "/baixe-o-app", type: "permanent" },
|
|
38
|
+
{ from: "/Loja/Produto/123", to: "/kit-123", type: "permanent" },
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
it("matches any case and with or without a trailing slash", () => {
|
|
42
|
+
for (const path of ["/baixeoapp", "/BaixeoApp", "/baixeoapp/", "/BAIXEOAPP/"]) {
|
|
43
|
+
expect(matchRedirect(path, map)).toMatchObject({ to: "/baixe-o-app", status: 301 });
|
|
44
|
+
}
|
|
45
|
+
expect(matchRedirect("/loja/produto/123", map)?.to).toBe("/kit-123");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("scans globs case-insensitively", () => {
|
|
49
|
+
const globs = block([{ from: "/PharolGranado/*", to: "/blog/*", type: "permanent" }]);
|
|
50
|
+
expect(matchRedirect("/pharolgranado/post-1", globs)?.to).toBe("/blog/post-1");
|
|
51
|
+
expect(matchRedirect("/PharolGranado/post-1", globs)?.to).toBe("/blog/post-1");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("keys an absolute source by its pathname alone", () => {
|
|
55
|
+
expect(normalizePath("https://www.example.com/Old/?x=1")).toBe("/old");
|
|
56
|
+
const abs = block([{ from: "https://www.example.com/Old/?x=1", to: "/new" }]);
|
|
57
|
+
expect(matchRedirect("/old", abs)?.to).toBe("/new");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("adds the leading slash a source was written without", () => {
|
|
61
|
+
const map = block([{ from: "camisa-roma", to: "/roupas/camisas", type: "permanent" }]);
|
|
62
|
+
expect(matchRedirect("/camisa-roma", map)?.to).toBe("/roupas/camisas");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("status", () => {
|
|
67
|
+
it("is 301 for permanent and 302 for anything else", () => {
|
|
68
|
+
const map = block([
|
|
69
|
+
{ from: "/a", to: "/b", type: "permanent" },
|
|
70
|
+
{ from: "/exemplo", to: "https://www.google.com", type: "temporary" },
|
|
71
|
+
{ from: "/c", to: "/d" },
|
|
72
|
+
]);
|
|
73
|
+
expect(matchRedirect("/a", map)?.status).toBe(301);
|
|
74
|
+
expect(matchRedirect("/exemplo", map)?.status).toBe(302);
|
|
75
|
+
expect(matchRedirect("/c", map)?.status).toBe(302);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("is 302 for a CSV row that is not `permanent`/`301`", () => {
|
|
79
|
+
const map = loadRedirects({});
|
|
80
|
+
addRedirects(map, parseRedirectsCsv("from,to,type\n/a,/b,PERMANENT\n/c,/d,301\n/e,/f\n"));
|
|
81
|
+
expect(matchRedirect("/a", map)?.status).toBe(302);
|
|
82
|
+
expect(matchRedirect("/c", map)?.status).toBe(301);
|
|
83
|
+
expect(matchRedirect("/e", map)?.status).toBe(302);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("Location", () => {
|
|
88
|
+
it("drops the request's query string", () => {
|
|
89
|
+
expect(redirectLocation({ to: "/baixe-o-app" }, "?utm_source=a&gclid=x")).toBe("/baixe-o-app");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("encodes what a header cannot carry, without double-encoding an escape", () => {
|
|
93
|
+
expect(redirectLocation({ to: "/promoção" })).toBe("/promo%C3%A7%C3%A3o");
|
|
94
|
+
expect(redirectLocation({ to: "/Regulamento%20Oficina.pdf" })).toBe(
|
|
95
|
+
"/Regulamento%20Oficina.pdf",
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("rules that can never be served are dropped", () => {
|
|
101
|
+
it("drops a rule whose source equals its target as written", () => {
|
|
102
|
+
const map = block([{ from: "kit-de-sabonetes", to: "kit-de-sabonetes", type: "permanent" }]);
|
|
103
|
+
expect(matchRedirect("/kit-de-sabonetes", map)).toBeNull();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("drops a rule that folding turns into a self-redirect", () => {
|
|
107
|
+
const map = block([
|
|
108
|
+
{ from: "/Calca/p", to: "/calca/p", type: "permanent" },
|
|
109
|
+
{ from: "/x/", to: "/x?utm_source=a", type: "permanent" },
|
|
110
|
+
]);
|
|
111
|
+
expect(matchRedirect("/Calca/p", map)).toBeNull();
|
|
112
|
+
expect(matchRedirect("/calca/p", map)).toBeNull();
|
|
113
|
+
expect(matchRedirect("/x", map)).toBeNull();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("keeps a rule to another host with the same path", () => {
|
|
117
|
+
const map = block([{ from: "/exemplo", to: "https://www.google.com/exemplo" }]);
|
|
118
|
+
expect(matchRedirect("/exemplo", map)?.to).toBe("https://www.google.com/exemplo");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("KV-keyed exact rules", () => {
|
|
123
|
+
it("serve the last rule written for a key, temporary as 302", () => {
|
|
124
|
+
const { exact } = splitExactRedirects({
|
|
125
|
+
r: {
|
|
126
|
+
__resolveType: "website/loaders/redirects.ts",
|
|
127
|
+
redirects: [
|
|
128
|
+
{ from: "/meia/p", to: "/a" },
|
|
129
|
+
{ from: "/Meia/p/", to: "/b" },
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
expect(exact).toHaveLength(1);
|
|
134
|
+
const value = [...exact[0].shadowed!, exact[0]];
|
|
135
|
+
for (const path of ["/meia/p", "/MEIA/p", "/meia/p/"]) {
|
|
136
|
+
expect(selectExactRedirect(value, path)).toMatchObject({ to: "/b", status: 302 });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("read a value written before `from` was stored", () => {
|
|
141
|
+
expect(selectExactRedirect({ to: "/new", status: 301 }, "/Old")).toMatchObject({
|
|
142
|
+
to: "/new",
|
|
143
|
+
status: 301,
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("never serve a folded self-redirect", () => {
|
|
148
|
+
expect(
|
|
149
|
+
selectExactRedirect({ from: "/Calca/p", to: "/calca/p", status: 301 }, "/calca/p"),
|
|
150
|
+
).toBeNull();
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("switching to fresh", () => {
|
|
155
|
+
it("changes only maps built afterwards", () => {
|
|
156
|
+
const legacy = block([{ from: "/Old", to: "/new" }]);
|
|
157
|
+
setRedirectSemantics("fresh");
|
|
158
|
+
const fresh = block([{ from: "/Old", to: "/new" }]);
|
|
159
|
+
expect(matchRedirect("/old", fresh)).toBeNull();
|
|
160
|
+
expect(matchRedirect("/Old", fresh)?.status).toBe(307);
|
|
161
|
+
setRedirectSemantics("legacy");
|
|
162
|
+
expect(matchRedirect("/old", legacy)?.status).toBe(302);
|
|
163
|
+
});
|
|
164
|
+
});
|