@kanorio/sdk 1.0.0 → 1.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 +41 -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 +36 -20
- package/dist/forms.d.ts +33 -14
- package/dist/forms.js +20 -14
- package/dist/generated/delivery.d.ts +564 -13
- package/dist/generated/management.d.ts +790 -81
- package/dist/http.d.ts +13 -13
- package/dist/http.js +8 -8
- package/dist/image.d.ts +77 -0
- package/dist/image.js +78 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -3
- 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/next.d.ts +66 -0
- package/dist/next.js +58 -0
- 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 +137 -21
- package/dist/seo.js +113 -21
- package/dist/server-key.d.ts +2 -2
- package/dist/server-key.js +2 -2
- package/dist/shared/api/delivery/cache-tags.d.ts +41 -39
- package/dist/shared/api/delivery/cache-tags.js +36 -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 +15 -10
|
@@ -1,11 +1,11 @@
|
|
|
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
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* Request locale resolution: recognizes the locale prefix in the first path segment.
|
|
4
|
+
* Shared by Kanorio's hosted-site middleware and the SDK's middleware; runs at the edge (pure functions, no Node dependency).
|
|
5
5
|
*
|
|
6
|
-
* -
|
|
7
|
-
* -
|
|
8
|
-
* -
|
|
6
|
+
* - Secondary locales are served under a `/{prefix}/` subdirectory; the prefix is the locale's saved `pathPrefix` (default `/en`, see `locale-path.ts`), case-insensitive.
|
|
7
|
+
* - The primary locale never has a prefix: `/zh-hant/about` (the primary locale code used as a prefix) must redirect 301 to `/about` (`redirectTo`).
|
|
8
|
+
* - Anything that is not a live locale's prefix is an ordinary path.
|
|
9
9
|
*/
|
|
10
10
|
import { localeOfPrefix, localePrefixOf } from "./locale-path.js";
|
|
11
11
|
export function resolveRequestLocale(input) {
|
|
@@ -18,12 +18,12 @@ export function resolveRequestLocale(input) {
|
|
|
18
18
|
const locale = localeOfPrefix(first, input.locales, primaryLocale);
|
|
19
19
|
if (locale)
|
|
20
20
|
return { locale, pathname: rest, prefixed: true, redirectTo: null };
|
|
21
|
-
//
|
|
21
|
+
// The primary locale code as a prefix (`/zh-hant/about`): the primary locale has no prefix, so redirect 301 to the unprefixed URL
|
|
22
22
|
if (first === primaryLocale.toLowerCase())
|
|
23
23
|
return { locale: primaryLocale, pathname: rest, prefixed: false, redirectTo: rest };
|
|
24
24
|
return { locale: primaryLocale, pathname, prefixed: false, redirectTo: null };
|
|
25
25
|
}
|
|
26
|
-
/**
|
|
26
|
+
/** Turns a path in the primary locale's space into a locale's URL path (no prefix for the primary; the saved prefix for a secondary locale) */
|
|
27
27
|
export function withLocalePrefix(pathname, locale, primaryLocale, routes) {
|
|
28
28
|
const clean = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
29
29
|
const prefix = localePrefixOf(locale, routes, primaryLocale);
|
|
@@ -32,9 +32,9 @@ export function withLocalePrefix(pathname, locale, primaryLocale, routes) {
|
|
|
32
32
|
return `/${prefix}${clean === "/" ? "" : clean}`;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* Route parameter of hosted sites: visitor pages are statically regenerated, so the request locale cannot come from a header and the middleware
|
|
36
|
+
* encodes it into the `[siteId]` segment: `/en-us/about` becomes `/public/{siteId}__en-us/about`. The primary locale is not encoded (the parameter is just the siteId).
|
|
37
|
+
* The separator is `__`: site ids contain only lowercase letters and digits, and locale codes only letters, digits and hyphens, so they cannot clash.
|
|
38
38
|
*/
|
|
39
39
|
export const SITE_PARAM_LOCALE_SEPARATOR = "__";
|
|
40
40
|
export function composeSiteParam(siteId, locale, primaryLocale) {
|
|
@@ -42,7 +42,7 @@ export function composeSiteParam(siteId, locale, primaryLocale) {
|
|
|
42
42
|
return siteId;
|
|
43
43
|
return `${siteId}${SITE_PARAM_LOCALE_SEPARATOR}${locale.toLowerCase()}`;
|
|
44
44
|
}
|
|
45
|
-
/**
|
|
45
|
+
/** Splits the route parameter back into siteId and request locale (lowercase code; null without a prefix, meaning the primary locale) */
|
|
46
46
|
export function parseSiteParam(param) {
|
|
47
47
|
const index = param.indexOf(SITE_PARAM_LOCALE_SEPARATOR);
|
|
48
48
|
if (index <= 0)
|
|
@@ -52,7 +52,7 @@ export function parseSiteParam(param) {
|
|
|
52
52
|
return { siteId: param, locale: null };
|
|
53
53
|
return { siteId: param.slice(0, index), locale: locale.toLowerCase() };
|
|
54
54
|
}
|
|
55
|
-
/**
|
|
55
|
+
/** A locale's root path in URLs: empty for the primary locale, `/{prefix}` for a secondary one; path builders put it first */
|
|
56
56
|
export function localeRootPath(locale, primaryLocale, routes) {
|
|
57
57
|
if (!locale)
|
|
58
58
|
return "";
|
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Canonical and hreflang for a customer site. `<head>` and the sitemap both call this one function, so they always agree.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* -
|
|
6
|
-
* -
|
|
7
|
-
* - fallback
|
|
8
|
-
* - hreflang
|
|
9
|
-
* -
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* -
|
|
4
|
+
* Rules:
|
|
5
|
+
* - The primary locale has no prefix (`/about`) and secondary locales use a subdirectory (`/en/about`); the prefix is the locale's saved `pathPrefix` (`locale-path.ts`).
|
|
6
|
+
* - Each locale version is self-canonical; only locales that are live and have a translation of this page are listed, plus the primary locale; x-default points to the primary locale.
|
|
7
|
+
* - A fallback page (a secondary locale was requested but this page has no translation, so primary-locale content is served): canonical points to the primary locale URL and no hreflang is emitted.
|
|
8
|
+
* - hreflang codes: `zh-Hant` becomes `zh-TW`, everything else is the locale code; Google accepts BCP 47 such as `en-US` and `pt-BR`.
|
|
9
|
+
* - Language catch-all (as Nuxt i18n does): when a language has a single regional version in this set (`en-US`),
|
|
10
|
+
* a bare-language hreflang (`en`) is added for the same URL so users of that language elsewhere match it too; it is not added when the language has two or more versions
|
|
11
|
+
* (there is no basis for choosing one, so it is left to Google) and never for Chinese (Traditional, Simplified and Hong Kong are different writing systems).
|
|
12
|
+
* - Never redirects by Accept-Language or IP (this only produces markup).
|
|
13
13
|
*/
|
|
14
|
-
/**
|
|
14
|
+
/** Site locale code to hreflang */
|
|
15
15
|
export declare function hreflangOf(locale: string): string;
|
|
16
|
-
/**
|
|
16
|
+
/** Locale to URL prefix (secondary locales only; built by `localePrefixMap`). A locale not listed falls back to its lowercase code */
|
|
17
17
|
export type LocalePrefixes = Readonly<Record<string, string>>;
|
|
18
18
|
export interface SiteAlternatesInput {
|
|
19
|
-
/**
|
|
19
|
+
/** The site's public URL, without a trailing slash (`https://www.example.com`) */
|
|
20
20
|
siteUrl: string;
|
|
21
|
-
/**
|
|
21
|
+
/** The unprefixed primary-locale path (home page `/`, otherwise `/about`, `/blog/x`) */
|
|
22
22
|
path: string;
|
|
23
23
|
primaryLocale: string;
|
|
24
|
-
/**
|
|
24
|
+
/** The requested locale (the one in the URL) */
|
|
25
25
|
requestLocale: string;
|
|
26
|
-
/**
|
|
26
|
+
/** The locale actually served: the primary locale when there is no translation */
|
|
27
27
|
resolvedLocale: string;
|
|
28
|
-
/**
|
|
28
|
+
/** Secondary locales that are live and have a translation of this page (excluding the primary); independent of `requestLocale` */
|
|
29
29
|
availableLocales: readonly string[];
|
|
30
|
-
/**
|
|
30
|
+
/** URL prefixes of the secondary locales */
|
|
31
31
|
localePrefixes: LocalePrefixes;
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* This page's path in each locale (no prefix; translated slugs give each locale its own path). `localePaths` of the Delivery response;
|
|
34
|
+
* `path` is used when it is not given or a locale is missing
|
|
35
35
|
*/
|
|
36
36
|
localizedPaths?: LocalizedPaths | null;
|
|
37
37
|
}
|
|
38
|
-
/**
|
|
38
|
+
/** Locale to this page's path in that locale (no prefix) */
|
|
39
39
|
export type LocalizedPaths = Readonly<Record<string, string>>;
|
|
40
40
|
export interface SiteAlternates {
|
|
41
41
|
canonical: string;
|
|
42
|
-
/** hreflang
|
|
42
|
+
/** hreflang to full URL; empty for a fallback page */
|
|
43
43
|
languages: Record<string, string>;
|
|
44
44
|
}
|
|
45
|
-
/**
|
|
45
|
+
/** This page's URL in a locale */
|
|
46
46
|
export declare function localizedSiteUrl(siteUrl: string, primaryLocale: string, locale: string, path: string, localePrefixes: LocalePrefixes): string;
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
49
|
-
* `<head>`
|
|
48
|
+
* hreflang to URL for a set of locale versions (primary first, secondary locales sorted by code, language catch-alls, and x-default last pointing to the primary).
|
|
49
|
+
* Both the alternates in `<head>` and the sitemap's `<xhtml:link>` are produced here.
|
|
50
50
|
*/
|
|
51
51
|
export declare function hreflangEntries(siteUrl: string, path: string, primaryLocale: string, secondaryLocales: readonly string[], localePrefixes: LocalePrefixes, localizedPaths?: LocalizedPaths | null): Array<{
|
|
52
52
|
hreflang: string;
|
|
@@ -54,7 +54,7 @@ export declare function hreflangEntries(siteUrl: string, path: string, primaryLo
|
|
|
54
54
|
locale: string | null;
|
|
55
55
|
}>;
|
|
56
56
|
export declare function buildSiteAlternates(input: SiteAlternatesInput): SiteAlternates;
|
|
57
|
-
/** sitemap `<xhtml:link rel="alternate"
|
|
57
|
+
/** For the sitemap's `<xhtml:link rel="alternate">`: every version of the same page (primary, language catch-alls and x-default) */
|
|
58
58
|
export declare function sitemapAlternates(siteUrl: string, path: string, primaryLocale: string, availableLocales: readonly string[], localePrefixes: LocalePrefixes, localizedPaths?: LocalizedPaths | null): {
|
|
59
59
|
hreflang: string;
|
|
60
60
|
href: string;
|
|
@@ -1,19 +1,19 @@
|
|
|
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
|
+
* Canonical and hreflang for a customer site. `<head>` and the sitemap both call this one function, so they always agree.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* -
|
|
7
|
-
* -
|
|
8
|
-
* - fallback
|
|
9
|
-
* - hreflang
|
|
10
|
-
* -
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* -
|
|
5
|
+
* Rules:
|
|
6
|
+
* - The primary locale has no prefix (`/about`) and secondary locales use a subdirectory (`/en/about`); the prefix is the locale's saved `pathPrefix` (`locale-path.ts`).
|
|
7
|
+
* - Each locale version is self-canonical; only locales that are live and have a translation of this page are listed, plus the primary locale; x-default points to the primary locale.
|
|
8
|
+
* - A fallback page (a secondary locale was requested but this page has no translation, so primary-locale content is served): canonical points to the primary locale URL and no hreflang is emitted.
|
|
9
|
+
* - hreflang codes: `zh-Hant` becomes `zh-TW`, everything else is the locale code; Google accepts BCP 47 such as `en-US` and `pt-BR`.
|
|
10
|
+
* - Language catch-all (as Nuxt i18n does): when a language has a single regional version in this set (`en-US`),
|
|
11
|
+
* a bare-language hreflang (`en`) is added for the same URL so users of that language elsewhere match it too; it is not added when the language has two or more versions
|
|
12
|
+
* (there is no basis for choosing one, so it is left to Google) and never for Chinese (Traditional, Simplified and Hong Kong are different writing systems).
|
|
13
|
+
* - Never redirects by Accept-Language or IP (this only produces markup).
|
|
14
14
|
*/
|
|
15
15
|
import { languageOfLocale } from "../localization/locale-path.js";
|
|
16
|
-
/**
|
|
16
|
+
/** Site locale code to hreflang */
|
|
17
17
|
export function hreflangOf(locale) {
|
|
18
18
|
if (locale === "zh-Hant")
|
|
19
19
|
return "zh-TW";
|
|
@@ -22,18 +22,18 @@ export function hreflangOf(locale) {
|
|
|
22
22
|
function pathIn(locale, path, localizedPaths) {
|
|
23
23
|
return localizedPaths?.[locale] ?? path;
|
|
24
24
|
}
|
|
25
|
-
/**
|
|
25
|
+
/** This page's URL in a locale */
|
|
26
26
|
export function localizedSiteUrl(siteUrl, primaryLocale, locale, path, localePrefixes) {
|
|
27
27
|
const base = siteUrl.replace(/\/+$/, "");
|
|
28
28
|
const cleanPath = path === "/" || path === "" ? "" : path.startsWith("/") ? path : `/${path}`;
|
|
29
|
-
//
|
|
29
|
+
// The home page follows the site's convention: the primary home page is `https://host` (no trailing slash), a secondary one is `https://host/en`
|
|
30
30
|
if (locale === primaryLocale)
|
|
31
31
|
return `${base}${cleanPath}`;
|
|
32
32
|
return `${base}/${localePrefixes[locale] ?? locale.toLowerCase()}${cleanPath}`;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
* `<head>`
|
|
35
|
+
* hreflang to URL for a set of locale versions (primary first, secondary locales sorted by code, language catch-alls, and x-default last pointing to the primary).
|
|
36
|
+
* Both the alternates in `<head>` and the sitemap's `<xhtml:link>` are produced here.
|
|
37
37
|
*/
|
|
38
38
|
export function hreflangEntries(siteUrl, path, primaryLocale, secondaryLocales, localePrefixes, localizedPaths) {
|
|
39
39
|
const versions = [primaryLocale, ...[...new Set(secondaryLocales.filter((l) => l !== primaryLocale))].sort()];
|
|
@@ -57,7 +57,7 @@ export function hreflangEntries(siteUrl, path, primaryLocale, secondaryLocales,
|
|
|
57
57
|
export function buildSiteAlternates(input) {
|
|
58
58
|
const { siteUrl, path, primaryLocale, requestLocale, resolvedLocale, localePrefixes, localizedPaths } = input;
|
|
59
59
|
const canonical = localizedSiteUrl(siteUrl, primaryLocale, resolvedLocale, pathIn(resolvedLocale, path, localizedPaths), localePrefixes);
|
|
60
|
-
//
|
|
60
|
+
// Fallback: a secondary locale was requested but primary-locale content is served, so canonical points to the primary and no hreflang is emitted
|
|
61
61
|
if (resolvedLocale !== requestLocale)
|
|
62
62
|
return { canonical, languages: {} };
|
|
63
63
|
const languages = {};
|
|
@@ -65,7 +65,7 @@ export function buildSiteAlternates(input) {
|
|
|
65
65
|
languages[entry.hreflang] = entry.href;
|
|
66
66
|
return { canonical, languages };
|
|
67
67
|
}
|
|
68
|
-
/** sitemap `<xhtml:link rel="alternate"
|
|
68
|
+
/** For the sitemap's `<xhtml:link rel="alternate">`: every version of the same page (primary, language catch-alls and x-default) */
|
|
69
69
|
export function sitemapAlternates(siteUrl, path, primaryLocale, availableLocales, localePrefixes, localizedPaths) {
|
|
70
70
|
return hreflangEntries(siteUrl, path, primaryLocale, availableLocales, localePrefixes, localizedPaths).map(({ hreflang, href }) => ({ hreflang, href }));
|
|
71
71
|
}
|
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Conditional display for online forms: "this question appears only when A is chosen".
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* A question can carry `visibleWhen`: a set of conditions (at most 5) that shows it when all (`all`) or any (`any`) of them match.
|
|
5
|
+
* The storefront (deciding which questions to draw) and the server (deciding which to validate and which answers to store) both call `resolveVisibleFieldIds`, so the two always agree:
|
|
6
|
+
* a hidden question is not shown, not validated (required does not block) and its answer is stored as null.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
11
|
-
* - Email
|
|
12
|
-
* -
|
|
8
|
+
* Rules:
|
|
9
|
+
* - A condition can only refer to an earlier question (`kind: "field"`) or one of the form's hidden fields (`kind: "hidden"`)
|
|
10
|
+
* - When the source question is itself hidden, it counts as unanswered
|
|
11
|
+
* - Email and phone questions cannot be hidden (they identify the contact, and the form's other rules rely on them)
|
|
12
|
+
* - When a rule cannot be evaluated (missing source, a later question, an operator that does not apply), the question is shown: better to ask one question too many than to hide a required one forever
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* Pure functions with no dependencies: `@kanorio/sdk` carries a copy, so a self-hosted front end evaluates the same way. Only relative imports inside this folder.
|
|
15
|
+
* Scoring, if added later, only needs one more source in `kind` (a variable), with no data migration.
|
|
16
16
|
*/
|
|
17
17
|
export declare const FORM_CONDITION_OPS: readonly ["answered", "not_answered", "includes", "not_includes", "equals", "not_equals", "contains", "gt", "gte", "lt", "lte"];
|
|
18
18
|
export type FormConditionOp = (typeof FORM_CONDITION_OPS)[number];
|
|
19
19
|
export declare const FORM_LOGIC_LIMITS: {
|
|
20
|
-
/**
|
|
20
|
+
/** Maximum conditions per question */
|
|
21
21
|
readonly MAX_CONDITIONS: 5;
|
|
22
|
-
/**
|
|
22
|
+
/** Maximum length of a condition's comparison text */
|
|
23
23
|
readonly VALUE_MAX: 200;
|
|
24
24
|
};
|
|
25
25
|
export interface FormCondition {
|
|
26
|
-
/**
|
|
26
|
+
/** Whether the source is a question (`ref` is the question id) or a hidden field (`ref` is the hidden field id) */
|
|
27
27
|
kind: "field" | "hidden";
|
|
28
28
|
ref: string;
|
|
29
29
|
op: FormConditionOp;
|
|
30
|
-
/**
|
|
30
|
+
/** An option id (includes), comparison text (equals, contains) or a number (numeric comparison); answered and unanswered take no value */
|
|
31
31
|
value?: string | number;
|
|
32
32
|
}
|
|
33
33
|
export interface FormVisibleWhen {
|
|
@@ -35,8 +35,8 @@ export interface FormVisibleWhen {
|
|
|
35
35
|
conditions: FormCondition[];
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
38
|
+
* Which operators each source supports. Phone, date, time and email can only be tested for answered or not; file, description text, address and tax ID cannot be sources (empty arrays).
|
|
39
|
+
* A new question type needs an entry here (forms.ts has a compile-time check).
|
|
40
40
|
*/
|
|
41
41
|
export declare const CONDITION_OPS_BY_TYPE: {
|
|
42
42
|
readonly short_text: readonly ["answered", "not_answered", "equals", "not_equals", "contains"];
|
|
@@ -53,15 +53,15 @@ export declare const CONDITION_OPS_BY_TYPE: {
|
|
|
53
53
|
readonly statement: readonly [];
|
|
54
54
|
readonly address: readonly [];
|
|
55
55
|
readonly tax_id: readonly [];
|
|
56
|
-
/**
|
|
56
|
+
/** A hidden field (a string taken from a URL parameter) */
|
|
57
57
|
readonly hidden: readonly ["answered", "not_answered", "equals", "not_equals", "contains"];
|
|
58
58
|
};
|
|
59
59
|
export type FormConditionSourceType = keyof typeof CONDITION_OPS_BY_TYPE;
|
|
60
60
|
export declare function conditionOpsFor(sourceType: string): readonly FormConditionOp[];
|
|
61
|
-
/**
|
|
61
|
+
/** Which kind of value this operator takes */
|
|
62
62
|
export type FormConditionValueKind = "none" | "option" | "text" | "number";
|
|
63
63
|
export declare function conditionValueKind(sourceType: string, op: FormConditionOp): FormConditionValueKind;
|
|
64
|
-
/**
|
|
64
|
+
/** The question details conditional logic needs (a subset of the form definition) */
|
|
65
65
|
export interface LogicField {
|
|
66
66
|
id: string;
|
|
67
67
|
type: string;
|
|
@@ -71,13 +71,13 @@ export interface LogicField {
|
|
|
71
71
|
}>;
|
|
72
72
|
}
|
|
73
73
|
export type FormConditionIssue =
|
|
74
|
-
/**
|
|
74
|
+
/** The source does not exist, or is not an earlier question */
|
|
75
75
|
"missing_source" | "op_not_allowed" | "value_required" | "value_unexpected"
|
|
76
|
-
/**
|
|
76
|
+
/** The value has the wrong type (text where a number is needed, or the reverse) */
|
|
77
77
|
| "value_type" | "unknown_option";
|
|
78
78
|
/**
|
|
79
|
-
*
|
|
80
|
-
* schema
|
|
79
|
+
* Checks whether a condition is valid for its source. `source` is undefined when the source cannot be found.
|
|
80
|
+
* Shared by the schema (blocking on save) and the editor (marking as you type).
|
|
81
81
|
*/
|
|
82
82
|
export declare function findConditionIssue(condition: FormCondition, source: {
|
|
83
83
|
type: string;
|
|
@@ -85,12 +85,12 @@ export declare function findConditionIssue(condition: FormCondition, source: {
|
|
|
85
85
|
id: string;
|
|
86
86
|
}>;
|
|
87
87
|
} | undefined): FormConditionIssue | null;
|
|
88
|
-
/**
|
|
88
|
+
/** Whether a single condition holds for an answer; null when the operator does not apply to this source (cannot be evaluated) */
|
|
89
89
|
export declare function evaluateCondition(condition: FormCondition, sourceType: string, raw: unknown): boolean | null;
|
|
90
90
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
91
|
+
* Works out which questions to show from the current answers. `answers` maps a question id or hidden field id to its value (the raw value the visitor typed is fine).
|
|
92
|
+
* Evaluated in question order: when an earlier question is hidden, a condition referring to it treats it as unanswered.
|
|
93
93
|
*/
|
|
94
94
|
export declare function resolveVisibleFieldIds(fields: readonly LogicField[], answers: Readonly<Record<string, unknown>>): Set<string>;
|
|
95
|
-
/**
|
|
95
|
+
/** Whether this form has any conditional display (without it, the storefront need not recompute on every input) */
|
|
96
96
|
export declare function hasVisibilityRules(fields: readonly LogicField[]): boolean;
|
|
@@ -1,19 +1,19 @@
|
|
|
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
|
+
* Conditional display for online forms: "this question appears only when A is chosen".
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* A question can carry `visibleWhen`: a set of conditions (at most 5) that shows it when all (`all`) or any (`any`) of them match.
|
|
6
|
+
* The storefront (deciding which questions to draw) and the server (deciding which to validate and which answers to store) both call `resolveVisibleFieldIds`, so the two always agree:
|
|
7
|
+
* a hidden question is not shown, not validated (required does not block) and its answer is stored as null.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
11
|
-
* -
|
|
12
|
-
* - Email
|
|
13
|
-
* -
|
|
9
|
+
* Rules:
|
|
10
|
+
* - A condition can only refer to an earlier question (`kind: "field"`) or one of the form's hidden fields (`kind: "hidden"`)
|
|
11
|
+
* - When the source question is itself hidden, it counts as unanswered
|
|
12
|
+
* - Email and phone questions cannot be hidden (they identify the contact, and the form's other rules rely on them)
|
|
13
|
+
* - When a rule cannot be evaluated (missing source, a later question, an operator that does not apply), the question is shown: better to ask one question too many than to hide a required one forever
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* Pure functions with no dependencies: `@kanorio/sdk` carries a copy, so a self-hosted front end evaluates the same way. Only relative imports inside this folder.
|
|
16
|
+
* Scoring, if added later, only needs one more source in `kind` (a variable), with no data migration.
|
|
17
17
|
*/
|
|
18
18
|
import { parseNumberInput } from "./number-input.js";
|
|
19
19
|
export const FORM_CONDITION_OPS = [
|
|
@@ -30,9 +30,9 @@ export const FORM_CONDITION_OPS = [
|
|
|
30
30
|
"lte",
|
|
31
31
|
];
|
|
32
32
|
export const FORM_LOGIC_LIMITS = {
|
|
33
|
-
/**
|
|
33
|
+
/** Maximum conditions per question */
|
|
34
34
|
MAX_CONDITIONS: 5,
|
|
35
|
-
/**
|
|
35
|
+
/** Maximum length of a condition's comparison text */
|
|
36
36
|
VALUE_MAX: 200,
|
|
37
37
|
};
|
|
38
38
|
const PRESENCE_OPS = ["answered", "not_answered"];
|
|
@@ -40,8 +40,8 @@ const TEXT_OPS = ["answered", "not_answered", "equals", "not_equals", "contains"
|
|
|
40
40
|
const CHOICE_OPS = ["answered", "not_answered", "includes", "not_includes"];
|
|
41
41
|
const NUMBER_OPS = ["answered", "not_answered", "equals", "not_equals", "gt", "gte", "lt", "lte"];
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
43
|
+
* Which operators each source supports. Phone, date, time and email can only be tested for answered or not; file, description text, address and tax ID cannot be sources (empty arrays).
|
|
44
|
+
* A new question type needs an entry here (forms.ts has a compile-time check).
|
|
45
45
|
*/
|
|
46
46
|
export const CONDITION_OPS_BY_TYPE = {
|
|
47
47
|
short_text: TEXT_OPS,
|
|
@@ -58,7 +58,7 @@ export const CONDITION_OPS_BY_TYPE = {
|
|
|
58
58
|
statement: [],
|
|
59
59
|
address: [],
|
|
60
60
|
tax_id: [],
|
|
61
|
-
/**
|
|
61
|
+
/** A hidden field (a string taken from a URL parameter) */
|
|
62
62
|
hidden: TEXT_OPS,
|
|
63
63
|
};
|
|
64
64
|
export function conditionOpsFor(sourceType) {
|
|
@@ -85,8 +85,8 @@ export function conditionValueKind(sourceType, op) {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
*
|
|
89
|
-
* schema
|
|
88
|
+
* Checks whether a condition is valid for its source. `source` is undefined when the source cannot be found.
|
|
89
|
+
* Shared by the schema (blocking on save) and the editor (marking as you type).
|
|
90
90
|
*/
|
|
91
91
|
export function findConditionIssue(condition, source) {
|
|
92
92
|
if (!source)
|
|
@@ -116,11 +116,11 @@ function isAnswered(raw) {
|
|
|
116
116
|
return raw.length > 0;
|
|
117
117
|
return true;
|
|
118
118
|
}
|
|
119
|
-
/**
|
|
119
|
+
/** Text comparison ignores case and surrounding whitespace, and treats full-width and half-width as the same */
|
|
120
120
|
function foldText(value) {
|
|
121
121
|
return typeof value === "string" ? value.normalize("NFKC").trim().toLowerCase() : typeof value === "number" ? String(value) : "";
|
|
122
122
|
}
|
|
123
|
-
/**
|
|
123
|
+
/** Whether a single condition holds for an answer; null when the operator does not apply to this source (cannot be evaluated) */
|
|
124
124
|
export function evaluateCondition(condition, sourceType, raw) {
|
|
125
125
|
if (!conditionOpsFor(sourceType).includes(condition.op))
|
|
126
126
|
return null;
|
|
@@ -154,7 +154,7 @@ export function evaluateCondition(condition, sourceType, raw) {
|
|
|
154
154
|
case "lte": {
|
|
155
155
|
const number = parseNumberInput(raw);
|
|
156
156
|
const target = typeof condition.value === "number" ? condition.value : null;
|
|
157
|
-
//
|
|
157
|
+
// Empty or not a number: the comparison does not hold
|
|
158
158
|
if (number === null || target === null)
|
|
159
159
|
return false;
|
|
160
160
|
if (condition.op === "gt")
|
|
@@ -168,8 +168,8 @@ export function evaluateCondition(condition, sourceType, raw) {
|
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
/**
|
|
171
|
-
*
|
|
172
|
-
*
|
|
171
|
+
* Works out which questions to show from the current answers. `answers` maps a question id or hidden field id to its value (the raw value the visitor typed is fine).
|
|
172
|
+
* Evaluated in question order: when an earlier question is hidden, a condition referring to it treats it as unanswered.
|
|
173
173
|
*/
|
|
174
174
|
export function resolveVisibleFieldIds(fields, answers) {
|
|
175
175
|
const visible = new Set();
|
|
@@ -177,7 +177,7 @@ export function resolveVisibleFieldIds(fields, answers) {
|
|
|
177
177
|
for (const field of fields) {
|
|
178
178
|
const rule = field.visibleWhen;
|
|
179
179
|
const conditions = rule && Array.isArray(rule.conditions) ? rule.conditions : [];
|
|
180
|
-
// Email
|
|
180
|
+
// Email and phone identify the contact and are not affected by conditions
|
|
181
181
|
if (conditions.length === 0 || field.type === "email" || field.type === "phone") {
|
|
182
182
|
visible.add(field.id);
|
|
183
183
|
seen.set(field.id, field.type);
|
|
@@ -190,7 +190,7 @@ export function resolveVisibleFieldIds(fields, answers) {
|
|
|
190
190
|
undecidable = true;
|
|
191
191
|
return false;
|
|
192
192
|
}
|
|
193
|
-
//
|
|
193
|
+
// The source question is itself hidden: treated as unanswered
|
|
194
194
|
const raw = condition.kind === "hidden" || visible.has(condition.ref) ? answers[condition.ref] : null;
|
|
195
195
|
const result = evaluateCondition(condition, sourceType, raw);
|
|
196
196
|
if (result === null)
|
|
@@ -203,7 +203,7 @@ export function resolveVisibleFieldIds(fields, answers) {
|
|
|
203
203
|
}
|
|
204
204
|
return visible;
|
|
205
205
|
}
|
|
206
|
-
/**
|
|
206
|
+
/** Whether this form has any conditional display (without it, the storefront need not recompute on every input) */
|
|
207
207
|
export function hasVisibilityRules(fields) {
|
|
208
208
|
return fields.some((field) => (field.visibleWhen?.conditions?.length ?? 0) > 0);
|
|
209
209
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* A visitor's typed number to a number. Accepts full-width digits (common with Chinese, Japanese and Korean input methods) and thousands separators; null when it is not a finite number.
|
|
3
|
+
* No dependencies: form validation, conditional logic and `@kanorio/sdk` use the same code.
|
|
4
4
|
*/
|
|
5
5
|
export declare function parseNumberInput(raw: unknown): number | null;
|
|
@@ -1,7 +1,7 @@
|
|
|
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
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* A visitor's typed number to a number. Accepts full-width digits (common with Chinese, Japanese and Korean input methods) and thousands separators; null when it is not a finite number.
|
|
4
|
+
* No dependencies: form validation, conditional logic and `@kanorio/sdk` use the same code.
|
|
5
5
|
*/
|
|
6
6
|
export function parseNumberInput(raw) {
|
|
7
7
|
if (typeof raw === "number")
|
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The single source of truth for URL redirect matching and loop detection (a contract across apps).
|
|
3
3
|
*
|
|
4
|
-
* -
|
|
5
|
-
* -
|
|
6
|
-
*
|
|
4
|
+
* - The hosted-site middleware uses `matchRedirect` at runtime to perform redirects.
|
|
5
|
+
* - Before saving, the server uses `isSelfLoop`, `findRedirectLoopKeys` and `hasRedirectChainLoop`
|
|
6
|
+
* to simulate the same matching and detect loops.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Both use this module, so the matching rules (exact first, then the longest wildcard prefix, then resolving relative and wildcard targets)
|
|
9
|
+
* never drift apart. Pure string functions with no React or Node dependency, safe to bundle for an edge runtime.
|
|
10
10
|
*/
|
|
11
11
|
export type RedirectRuleLite = {
|
|
12
12
|
sourcePath: string;
|
|
13
13
|
targetUrl: string;
|
|
14
14
|
statusCode?: number;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
16
|
+
* The locale a rule belongs to. null or unset: shared by every locale, matched on the path without the locale prefix, with in-site targets given the prefix back.
|
|
17
|
+
* A value: belongs to that locale only (a renamed translated slug, a disabled locale, a changed prefix); source and target already include the prefix, matched on the full path with the target used as is.
|
|
18
18
|
*/
|
|
19
19
|
locale?: string | null;
|
|
20
20
|
};
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* 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.
|
|
23
|
+
*
|
|
24
|
+
* A visitor's pathname arrives encoded (`/%E7%94%A2%E5%93%81.pdf`), while the source an owner typed may be raw characters
|
|
25
|
+
* 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.
|
|
26
|
+
* Both sides go through this function before comparing; it parses with the WHATWG URL, so the result agrees with browsers and `NextRequest.nextUrl.pathname`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function canonicalRedirectPath(path: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Resolves one path by the matching rules: exact match first, otherwise the longest wildcard prefix (a `sourcePath` ending in `/*`).
|
|
31
|
+
* A target ending in `/*` keeps the remaining sub-path; otherwise everything goes to the same target.
|
|
32
|
+
* 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.
|
|
33
|
+
* Path and source are both compared in the canonical form of `canonicalRedirectPath` (raw and percent-encoded are the same path).
|
|
25
34
|
*/
|
|
26
35
|
export declare function matchRedirect<R extends RedirectRuleLite>(pathname: string, rules: R[]): {
|
|
27
36
|
rule: R;
|
|
@@ -29,8 +38,8 @@ export declare function matchRedirect<R extends RedirectRuleLite>(pathname: stri
|
|
|
29
38
|
statusCode: number;
|
|
30
39
|
} | null;
|
|
31
40
|
/**
|
|
32
|
-
*
|
|
33
|
-
* `scoped`
|
|
41
|
+
* Redirect matching on multilingual sites: locale-specific rules first (full path, target as is), then shared rules (unprefixed path).
|
|
42
|
+
* 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).
|
|
34
43
|
*/
|
|
35
44
|
export declare function matchLocalizedRedirect<R extends RedirectRuleLite>(fullPathname: string, localePathname: string, rules: R[]): {
|
|
36
45
|
rule: R;
|
|
@@ -38,30 +47,30 @@ export declare function matchLocalizedRedirect<R extends RedirectRuleLite>(fullP
|
|
|
38
47
|
statusCode: number;
|
|
39
48
|
scoped: boolean;
|
|
40
49
|
} | null;
|
|
41
|
-
/**
|
|
50
|
+
/** Strips the trailing slash (keeping the root "/"), for self-redirect comparison. */
|
|
42
51
|
export declare function stripTrailingSlash(value: string): string;
|
|
43
|
-
/**
|
|
52
|
+
/** Exact self-redirect (/foo and /foo/ are the same). */
|
|
44
53
|
export declare function isExactSelf(source: string, target: string): boolean;
|
|
45
54
|
/**
|
|
46
|
-
* wildcard
|
|
47
|
-
*
|
|
55
|
+
* A wildcard source whose in-site relative target falls inside what the source covers ("/*" to "/", "/blog/*" to "/blog/x").
|
|
56
|
+
* Targets outside it ("/blog/*" to "/about") and external URLs do not count.
|
|
48
57
|
*/
|
|
49
58
|
export declare function isScopeLoop(source: string, target: string): boolean;
|
|
50
|
-
/**
|
|
59
|
+
/** Whether a single rule loops on itself (an exact self-redirect or a wildcard covering its own target). */
|
|
51
60
|
export declare function isSelfLoop(source: string, target: string): boolean;
|
|
52
61
|
/**
|
|
53
|
-
*
|
|
62
|
+
* Finds the keys of rules that take part in a redirect loop across rules (for example /a to /b and /b to /a).
|
|
54
63
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
64
|
+
* Starting from each rule's representative path, it applies `matchRedirect` hop by hop and detects a cycle with a visited set.
|
|
65
|
+
* Single-rule self-loops should be handled first by the caller with `isSelfLoop`; this focuses on rules pointing at each other.
|
|
57
66
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
67
|
+
* Precision: `maxHops = number of rules + 1`. In this functional structure an acyclic walk ends after visiting at most as many
|
|
68
|
+
* 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.
|
|
60
69
|
*/
|
|
61
70
|
export declare function findRedirectLoopKeys<R extends {
|
|
62
71
|
key: string;
|
|
63
72
|
sourcePath: string;
|
|
64
73
|
targetUrl: string;
|
|
65
74
|
}>(rules: R[]): Set<string>;
|
|
66
|
-
/**
|
|
75
|
+
/** Whether a redirect loop across rules exists. Check single-rule self-loops separately with `isSelfLoop`. */
|
|
67
76
|
export declare function hasRedirectChainLoop(rules: RedirectRuleLite[]): boolean;
|