@kanorio/sdk 1.0.0 → 1.1.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 +2 -2
- package/dist/customer-oauth.d.ts +21 -21
- package/dist/customer-oauth.js +12 -12
- package/dist/customer.d.ts +22 -22
- package/dist/customer.js +20 -11
- package/dist/delivery.d.ts +9 -9
- package/dist/delivery.js +6 -6
- package/dist/edge.d.ts +25 -21
- package/dist/edge.js +19 -19
- package/dist/forms.d.ts +13 -13
- package/dist/forms.js +13 -13
- package/dist/generated/delivery.d.ts +35 -7
- package/dist/generated/management.d.ts +111 -26
- package/dist/http.d.ts +13 -13
- package/dist/http.js +8 -8
- package/dist/index.js +2 -2
- package/dist/locales.d.ts +22 -22
- package/dist/locales.js +12 -12
- package/dist/management.d.ts +5 -5
- package/dist/management.js +3 -3
- package/dist/paginate.d.ts +3 -3
- package/dist/paginate.js +3 -3
- package/dist/preview.d.ts +13 -13
- package/dist/preview.js +11 -11
- package/dist/protection.d.ts +18 -18
- package/dist/protection.js +11 -11
- package/dist/revalidate.d.ts +22 -22
- package/dist/revalidate.js +14 -14
- package/dist/seo.d.ts +20 -20
- package/dist/seo.js +20 -20
- package/dist/server-key.d.ts +2 -2
- package/dist/server-key.js +2 -2
- package/dist/shared/api/delivery/cache-tags.d.ts +39 -39
- package/dist/shared/api/delivery/cache-tags.js +34 -34
- package/dist/shared/api/server-key.d.ts +32 -28
- package/dist/shared/api/server-key.js +41 -33
- package/dist/shared/business-registry/attribution.d.ts +5 -5
- package/dist/shared/business-registry/attribution.js +4 -4
- package/dist/shared/business-registry/define.d.ts +1 -1
- package/dist/shared/business-registry/define.js +1 -1
- package/dist/shared/business-registry/formats/eu-vat.js +4 -4
- package/dist/shared/business-registry/formats/hk-br.d.ts +2 -2
- package/dist/shared/business-registry/formats/hk-br.js +4 -4
- package/dist/shared/business-registry/formats/jp-trn.d.ts +3 -3
- package/dist/shared/business-registry/formats/jp-trn.js +4 -4
- package/dist/shared/business-registry/formats/kr-brn.d.ts +2 -2
- package/dist/shared/business-registry/formats/kr-brn.js +3 -3
- package/dist/shared/business-registry/formats/my.d.ts +3 -3
- package/dist/shared/business-registry/formats/my.js +4 -4
- package/dist/shared/business-registry/formats/sg-uen.js +4 -4
- package/dist/shared/business-registry/formats/tw-vat.d.ts +5 -5
- package/dist/shared/business-registry/formats/tw-vat.js +6 -6
- package/dist/shared/business-registry/index.js +1 -1
- package/dist/shared/business-registry/lookup-wire.d.ts +9 -9
- package/dist/shared/business-registry/lookup-wire.js +4 -4
- package/dist/shared/business-registry/normalize.d.ts +3 -3
- package/dist/shared/business-registry/normalize.js +4 -4
- package/dist/shared/business-registry/registry.d.ts +9 -9
- package/dist/shared/business-registry/registry.js +13 -13
- package/dist/shared/business-registry/types.d.ts +20 -20
- package/dist/shared/business-registry/types.js +6 -6
- package/dist/shared/localization/locale-path.d.ts +18 -18
- package/dist/shared/localization/locale-path.js +18 -18
- package/dist/shared/localization/request-locale.d.ts +16 -16
- package/dist/shared/localization/request-locale.js +13 -13
- package/dist/shared/seo/alternates.d.ts +26 -26
- package/dist/shared/seo/alternates.js +18 -18
- package/dist/shared/types/forms-logic.d.ts +28 -28
- package/dist/shared/types/forms-logic.js +27 -27
- package/dist/shared/types/number-input.d.ts +2 -2
- package/dist/shared/types/number-input.js +3 -3
- package/dist/shared/utils/redirect-matching.d.ts +33 -24
- package/dist/shared/utils/redirect-matching.js +73 -40
- package/dist/typed.d.ts +5 -5
- package/dist/typed.js +3 -3
- package/package.json +10 -10
|
@@ -1,40 +1,73 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Copied from Kanorio's shared code by `pnpm --filter @kanorio/sdk gen`. Do not edit by hand; a contract test keeps the two copies identical.
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* The single source of truth for URL redirect matching and loop detection (a contract across apps).
|
|
4
4
|
*
|
|
5
|
-
* -
|
|
6
|
-
* -
|
|
7
|
-
*
|
|
5
|
+
* - The hosted-site middleware uses `matchRedirect` at runtime to perform redirects.
|
|
6
|
+
* - Before saving, the server uses `isSelfLoop`, `findRedirectLoopKeys` and `hasRedirectChainLoop`
|
|
7
|
+
* to simulate the same matching and detect loops.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Both use this module, so the matching rules (exact first, then the longest wildcard prefix, then resolving relative and wildcard targets)
|
|
10
|
+
* never drift apart. Pure string functions with no React or Node dependency, safe to bundle for an edge runtime.
|
|
11
11
|
*/
|
|
12
|
-
/**
|
|
12
|
+
/** Representative sub-path of a wildcard source during loop simulation (internal only, chosen not to collide with real paths). */
|
|
13
13
|
const LOOP_PROBE = "__kanorio_loop_probe__";
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* Global step budget for loop detection: normal input stays far below it.
|
|
16
|
+
* It only triggers on pathological input (very many wildcard rules), to keep a save from taking too long;
|
|
17
|
+
* past it detection stops, and the rest is covered by the middleware's runtime guard and the browser's ERR_TOO_MANY_REDIRECTS.
|
|
18
18
|
*/
|
|
19
19
|
const MAX_TOTAL_STEPS = 200_000;
|
|
20
|
+
/** Paths that need no encoding: unreserved characters and the separators allowed in a path (most rules take this route and skip parsing) */
|
|
21
|
+
const PLAIN_PATH_PATTERN = /^[A-Za-z0-9\-._~/*!$&'()+,;=:@]*$/;
|
|
22
|
+
/** Cache of canonical forms: within one isolate the rule strings are the same on every request; cleared entirely past the limit */
|
|
23
|
+
const _canonicalPathCache = new Map();
|
|
24
|
+
const CANONICAL_PATH_CACHE_MAX = 5000;
|
|
20
25
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
26
|
+
* Paths are matched in canonical form: the percent-encoding browsers actually send, with escape codes in uppercase; query and fragment are not part of the path.
|
|
27
|
+
*
|
|
28
|
+
* A visitor's pathname arrives encoded (`/%E7%94%A2%E5%93%81.pdf`), while the source an owner typed may be raw characters
|
|
29
|
+
* or an encoded string pasted from the address bar (in either case). Compared as plain strings, a rule written in raw characters would never match.
|
|
30
|
+
* Both sides go through this function before comparing; it parses with the WHATWG URL, so the result agrees with browsers and `NextRequest.nextUrl.pathname`.
|
|
31
|
+
*/
|
|
32
|
+
export function canonicalRedirectPath(path) {
|
|
33
|
+
if (!path.startsWith("/") || PLAIN_PATH_PATTERN.test(path))
|
|
34
|
+
return path;
|
|
35
|
+
const cached = _canonicalPathCache.get(path);
|
|
36
|
+
if (cached !== undefined)
|
|
37
|
+
return cached;
|
|
38
|
+
let encoded = path;
|
|
39
|
+
try {
|
|
40
|
+
encoded = new URL(`http://x${path}`).pathname;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// Cannot be parsed (an unpaired surrogate, for example): left as is, which means it never matches
|
|
44
|
+
}
|
|
45
|
+
const canonical = encoded.replace(/%[0-9a-f]{2}/gi, (escape) => escape.toUpperCase());
|
|
46
|
+
if (_canonicalPathCache.size >= CANONICAL_PATH_CACHE_MAX)
|
|
47
|
+
_canonicalPathCache.clear();
|
|
48
|
+
_canonicalPathCache.set(path, canonical);
|
|
49
|
+
return canonical;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolves one path by the matching rules: exact match first, otherwise the longest wildcard prefix (a `sourcePath` ending in `/*`).
|
|
53
|
+
* A target ending in `/*` keeps the remaining sub-path; otherwise everything goes to the same target.
|
|
54
|
+
* Returns the matched rule, the resolved target (an in-site relative path or a full external URL) and the status code; null without a match.
|
|
55
|
+
* Path and source are both compared in the canonical form of `canonicalRedirectPath` (raw and percent-encoded are the same path).
|
|
24
56
|
*/
|
|
25
57
|
export function matchRedirect(pathname, rules) {
|
|
26
|
-
|
|
27
|
-
|
|
58
|
+
const requested = canonicalRedirectPath(pathname);
|
|
59
|
+
// 1. Exact pathname match (case-sensitive, per the URL standard)
|
|
60
|
+
const exact = rules.find((rule) => canonicalRedirectPath(rule.sourcePath) === requested);
|
|
28
61
|
if (exact) {
|
|
29
62
|
return { rule: exact, target: exact.targetUrl, statusCode: exact.statusCode ?? 301 };
|
|
30
63
|
}
|
|
31
|
-
// 2.
|
|
64
|
+
// 2. Wildcard prefix match (the longest prefix wins)
|
|
32
65
|
let best = null;
|
|
33
66
|
for (const rule of rules) {
|
|
34
67
|
if (!rule.sourcePath.endsWith("/*"))
|
|
35
68
|
continue;
|
|
36
|
-
const prefix = rule.sourcePath.slice(0, -1); // "/blog/*" → "/blog/"
|
|
37
|
-
if (
|
|
69
|
+
const prefix = canonicalRedirectPath(rule.sourcePath).slice(0, -1); // "/blog/*" → "/blog/"
|
|
70
|
+
if (requested.startsWith(prefix)) {
|
|
38
71
|
if (!best || prefix.length > best.prefix.length) {
|
|
39
72
|
best = { prefix, rule };
|
|
40
73
|
}
|
|
@@ -42,15 +75,15 @@ export function matchRedirect(pathname, rules) {
|
|
|
42
75
|
}
|
|
43
76
|
if (!best)
|
|
44
77
|
return null;
|
|
45
|
-
const suffix =
|
|
78
|
+
const suffix = requested.slice(best.prefix.length); // the remaining sub-path (already encoded, still a valid URL once appended to the target)
|
|
46
79
|
const target = best.rule.targetUrl.endsWith("/*")
|
|
47
80
|
? best.rule.targetUrl.slice(0, -1) + suffix
|
|
48
81
|
: best.rule.targetUrl;
|
|
49
82
|
return { rule: best.rule, target, statusCode: best.rule.statusCode ?? 301 };
|
|
50
83
|
}
|
|
51
84
|
/**
|
|
52
|
-
*
|
|
53
|
-
* `scoped`
|
|
85
|
+
* Redirect matching on multilingual sites: locale-specific rules first (full path, target as is), then shared rules (unprefixed path).
|
|
86
|
+
* When `scoped` is true the target already includes the locale prefix; when false the caller adds the request locale's prefix to in-site targets (the existing behavior).
|
|
54
87
|
*/
|
|
55
88
|
export function matchLocalizedRedirect(fullPathname, localePathname, rules) {
|
|
56
89
|
const scopedRules = rules.filter((rule) => rule.locale);
|
|
@@ -63,17 +96,17 @@ export function matchLocalizedRedirect(fullPathname, localePathname, rules) {
|
|
|
63
96
|
const hit = matchRedirect(localePathname, shared);
|
|
64
97
|
return hit ? { ...hit, scoped: false } : null;
|
|
65
98
|
}
|
|
66
|
-
/**
|
|
99
|
+
/** Strips the trailing slash (keeping the root "/"), for self-redirect comparison. */
|
|
67
100
|
export function stripTrailingSlash(value) {
|
|
68
101
|
return value.length > 1 && value.endsWith("/") ? value.slice(0, -1) : value;
|
|
69
102
|
}
|
|
70
|
-
/**
|
|
103
|
+
/** Exact self-redirect (/foo and /foo/ are the same). */
|
|
71
104
|
export function isExactSelf(source, target) {
|
|
72
105
|
return stripTrailingSlash(source) === stripTrailingSlash(target);
|
|
73
106
|
}
|
|
74
107
|
/**
|
|
75
|
-
* wildcard
|
|
76
|
-
*
|
|
108
|
+
* A wildcard source whose in-site relative target falls inside what the source covers ("/*" to "/", "/blog/*" to "/blog/x").
|
|
109
|
+
* Targets outside it ("/blog/*" to "/about") and external URLs do not count.
|
|
77
110
|
*/
|
|
78
111
|
export function isScopeLoop(source, target) {
|
|
79
112
|
if (!source.endsWith("/*") || !target.startsWith("/"))
|
|
@@ -82,18 +115,18 @@ export function isScopeLoop(source, target) {
|
|
|
82
115
|
const targetBase = target.endsWith("/*") ? target.slice(0, -1) : target;
|
|
83
116
|
return targetBase.startsWith(srcPrefix);
|
|
84
117
|
}
|
|
85
|
-
/**
|
|
118
|
+
/** Whether a single rule loops on itself (an exact self-redirect or a wildcard covering its own target). */
|
|
86
119
|
export function isSelfLoop(source, target) {
|
|
87
120
|
return isExactSelf(source, target) || isScopeLoop(source, target);
|
|
88
121
|
}
|
|
89
122
|
/**
|
|
90
|
-
*
|
|
123
|
+
* Finds the keys of rules that take part in a redirect loop across rules (for example /a to /b and /b to /a).
|
|
91
124
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
125
|
+
* Starting from each rule's representative path, it applies `matchRedirect` hop by hop and detects a cycle with a visited set.
|
|
126
|
+
* Single-rule self-loops should be handled first by the caller with `isSelfLoop`; this focuses on rules pointing at each other.
|
|
94
127
|
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
128
|
+
* Precision: `maxHops = number of rules + 1`. In this functional structure an acyclic walk ends after visiting at most as many
|
|
129
|
+
* distinct states as there are rules, so `number of rules + 1` is an exact bound with no false positives; a growing chain (never ending) is judged a loop once it exceeds the bound.
|
|
97
130
|
*/
|
|
98
131
|
export function findRedirectLoopKeys(rules) {
|
|
99
132
|
const loopKeys = new Set();
|
|
@@ -101,7 +134,7 @@ export function findRedirectLoopKeys(rules) {
|
|
|
101
134
|
let budget = MAX_TOTAL_STEPS;
|
|
102
135
|
for (const start of rules) {
|
|
103
136
|
if (budget <= 0)
|
|
104
|
-
break; //
|
|
137
|
+
break; // pathological input: the budget is spent, stop detecting (the runtime guard covers the rest)
|
|
105
138
|
const sample = start.sourcePath.endsWith("/*")
|
|
106
139
|
? start.sourcePath.slice(0, -1) + LOOP_PROBE
|
|
107
140
|
: start.sourcePath;
|
|
@@ -110,20 +143,20 @@ export function findRedirectLoopKeys(rules) {
|
|
|
110
143
|
let cur = sample;
|
|
111
144
|
for (let i = 0; i < maxHops; i++) {
|
|
112
145
|
if (budget-- <= 0 || cur === null) {
|
|
113
|
-
cur = null; //
|
|
146
|
+
cur = null; // budget spent: conservatively treated as ended (not misjudged as a loop)
|
|
114
147
|
break;
|
|
115
148
|
}
|
|
116
|
-
//
|
|
117
|
-
//
|
|
149
|
+
// The type is annotated explicitly: cur is assigned matched.target at the end of the loop, and letting matched be inferred
|
|
150
|
+
// would form a circular inference with cur (TS7022). The annotation breaks the cycle.
|
|
118
151
|
const matched = matchRedirect(cur, rules);
|
|
119
|
-
//
|
|
152
|
+
// No match or an external URL: the redirect chain ends here, no loop
|
|
120
153
|
if (!matched || !matched.target.startsWith("/")) {
|
|
121
154
|
cur = null;
|
|
122
155
|
break;
|
|
123
156
|
}
|
|
124
157
|
usedKeys.push(matched.rule.key);
|
|
125
158
|
if (seen.has(matched.target)) {
|
|
126
|
-
//
|
|
159
|
+
// Back on a path already visited: a loop; mark every rule on the way
|
|
127
160
|
usedKeys.forEach((k) => loopKeys.add(k));
|
|
128
161
|
cur = null;
|
|
129
162
|
break;
|
|
@@ -131,13 +164,13 @@ export function findRedirectLoopKeys(rules) {
|
|
|
131
164
|
seen.add(matched.target);
|
|
132
165
|
cur = matched.target;
|
|
133
166
|
}
|
|
134
|
-
//
|
|
167
|
+
// Still not ended after maxHops: a growing endless chain, conservatively treated as a loop
|
|
135
168
|
if (cur !== null)
|
|
136
169
|
usedKeys.forEach((k) => loopKeys.add(k));
|
|
137
170
|
}
|
|
138
171
|
return loopKeys;
|
|
139
172
|
}
|
|
140
|
-
/**
|
|
173
|
+
/** Whether a redirect loop across rules exists. Check single-rule self-loops separately with `isSelfLoop`. */
|
|
141
174
|
export function hasRedirectChainLoop(rules) {
|
|
142
175
|
const keyed = rules.map((r, i) => ({
|
|
143
176
|
key: String(i),
|
package/dist/typed.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* `client.get("/sites/{siteId}", { params: { siteId }, query })`
|
|
4
|
-
*
|
|
2
|
+
* A minimal client typed from the `paths` interface that openapi-typescript generates:
|
|
3
|
+
* `client.get("/sites/{siteId}", { params: { siteId }, query })` checks the path, path parameters, query, body and the success response type.
|
|
4
|
+
* No runtime validation; whatever the server answers is passed through.
|
|
5
5
|
*/
|
|
6
6
|
import type { HttpClient } from "./http.js";
|
|
7
7
|
type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
@@ -58,7 +58,7 @@ type JsonOf<R> = R extends {
|
|
|
58
58
|
export type SuccessResponseOf<O> = {
|
|
59
59
|
[S in SuccessStatus & keyof ResponsesOf<O>]: JsonOf<ResponsesOf<O>[S]>;
|
|
60
60
|
}[SuccessStatus & keyof ResponsesOf<O>];
|
|
61
|
-
/**
|
|
61
|
+
/** Not accepted when there is no such parameter; optional (`undefined`) or all-optional (`{}` is valid too, for a query with only `locale?`) parameters need not be passed */
|
|
62
62
|
type OptionalIfNever<K extends string, T> = [T] extends [never] ? {
|
|
63
63
|
[k in K]?: undefined;
|
|
64
64
|
} : undefined extends T ? {
|
|
@@ -78,7 +78,7 @@ export interface TypedClient<P> {
|
|
|
78
78
|
put<K extends PathsWithMethod<P, "put">>(path: K, req?: TypedRequest<OperationOf<P, K, "put">>): Promise<SuccessResponseOf<OperationOf<P, K, "put">>>;
|
|
79
79
|
patch<K extends PathsWithMethod<P, "patch">>(path: K, req?: TypedRequest<OperationOf<P, K, "patch">>): Promise<SuccessResponseOf<OperationOf<P, K, "patch">>>;
|
|
80
80
|
delete<K extends PathsWithMethod<P, "delete">>(path: K, req?: TypedRequest<OperationOf<P, K, "delete">>): Promise<SuccessResponseOf<OperationOf<P, K, "delete">>>;
|
|
81
|
-
/**
|
|
81
|
+
/** The underlying HTTP client (untyped) */
|
|
82
82
|
http: HttpClient;
|
|
83
83
|
}
|
|
84
84
|
export declare function createTypedClient<P>(http: HttpClient): TypedClient<P>;
|
package/dist/typed.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* `client.get("/sites/{siteId}", { params: { siteId }, query })`
|
|
4
|
-
*
|
|
2
|
+
* A minimal client typed from the `paths` interface that openapi-typescript generates:
|
|
3
|
+
* `client.get("/sites/{siteId}", { params: { siteId }, query })` checks the path, path parameters, query, body and the success response type.
|
|
4
|
+
* No runtime validation; whatever the server answers is passed through.
|
|
5
5
|
*/
|
|
6
6
|
function fillPath(template, params) {
|
|
7
7
|
return template.replace(/\{([^}]+)\}/g, (_, name) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kanorio/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Typed fetch clients and headless helpers (SEO rewrites, JSON-LD, webhook revalidateTag) for the Kanorio Management API, Content Delivery API and Customer API.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -30,39 +30,39 @@
|
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
32
|
"types": "./dist/index.d.ts",
|
|
33
|
-
"
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
34
|
},
|
|
35
35
|
"./management": {
|
|
36
36
|
"types": "./dist/management.d.ts",
|
|
37
|
-
"
|
|
37
|
+
"default": "./dist/management.js"
|
|
38
38
|
},
|
|
39
39
|
"./delivery": {
|
|
40
40
|
"types": "./dist/delivery.d.ts",
|
|
41
|
-
"
|
|
41
|
+
"default": "./dist/delivery.js"
|
|
42
42
|
},
|
|
43
43
|
"./customer": {
|
|
44
44
|
"types": "./dist/customer.d.ts",
|
|
45
|
-
"
|
|
45
|
+
"default": "./dist/customer.js"
|
|
46
46
|
},
|
|
47
47
|
"./customer-oauth": {
|
|
48
48
|
"types": "./dist/customer-oauth.d.ts",
|
|
49
|
-
"
|
|
49
|
+
"default": "./dist/customer-oauth.js"
|
|
50
50
|
},
|
|
51
51
|
"./edge": {
|
|
52
52
|
"types": "./dist/edge.d.ts",
|
|
53
|
-
"
|
|
53
|
+
"default": "./dist/edge.js"
|
|
54
54
|
},
|
|
55
55
|
"./forms": {
|
|
56
56
|
"types": "./dist/forms.d.ts",
|
|
57
|
-
"
|
|
57
|
+
"default": "./dist/forms.js"
|
|
58
58
|
},
|
|
59
59
|
"./tax-id": {
|
|
60
60
|
"types": "./dist/tax-id.d.ts",
|
|
61
|
-
"
|
|
61
|
+
"default": "./dist/tax-id.js"
|
|
62
62
|
},
|
|
63
63
|
"./preview": {
|
|
64
64
|
"types": "./dist/preview.d.ts",
|
|
65
|
-
"
|
|
65
|
+
"default": "./dist/preview.js"
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
68
|
"files": [
|