@kanorio/sdk 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +66 -2
- package/dist/customer-oauth.d.ts +86 -0
- package/dist/customer-oauth.js +112 -0
- package/dist/customer.d.ts +176 -0
- package/dist/customer.js +125 -0
- package/dist/delivery.d.ts +35 -0
- package/dist/delivery.js +49 -0
- package/dist/edge.d.ts +108 -0
- package/dist/edge.js +287 -0
- package/dist/forms.d.ts +123 -0
- package/dist/forms.js +130 -0
- package/dist/generated/customer.d.ts +3157 -0
- package/dist/generated/delivery.d.ts +2408 -0
- package/dist/generated/management.d.ts +38333 -0
- package/dist/http.d.ts +73 -0
- package/dist/http.js +162 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +17 -0
- package/dist/locales.d.ts +80 -0
- package/dist/locales.js +81 -0
- package/dist/management.d.ts +19 -0
- package/dist/management.js +22 -0
- package/dist/paginate.d.ts +18 -0
- package/dist/paginate.js +27 -0
- package/dist/preview.d.ts +40 -0
- package/dist/preview.js +131 -0
- package/dist/protection.d.ts +74 -0
- package/dist/protection.js +68 -0
- package/dist/revalidate.d.ts +84 -0
- package/dist/revalidate.js +143 -0
- package/dist/seo.d.ts +120 -0
- package/dist/seo.js +125 -0
- package/dist/server-key.d.ts +71 -0
- package/dist/server-key.js +103 -0
- package/dist/shared/api/delivery/cache-tags.d.ts +142 -0
- package/dist/shared/api/delivery/cache-tags.js +212 -0
- package/dist/shared/api/server-key.d.ts +90 -0
- package/dist/shared/api/server-key.js +260 -0
- package/dist/shared/business-registry/attribution.d.ts +14 -0
- package/dist/shared/business-registry/attribution.js +28 -0
- package/dist/shared/business-registry/define.d.ts +3 -0
- package/dist/shared/business-registry/define.js +4 -0
- package/dist/shared/business-registry/formats/eu-vat.d.ts +2 -0
- package/dist/shared/business-registry/formats/eu-vat.js +54 -0
- package/dist/shared/business-registry/formats/hk-br.d.ts +5 -0
- package/dist/shared/business-registry/formats/hk-br.js +23 -0
- package/dist/shared/business-registry/formats/jp-trn.d.ts +6 -0
- package/dist/shared/business-registry/formats/jp-trn.js +23 -0
- package/dist/shared/business-registry/formats/kr-brn.d.ts +6 -0
- package/dist/shared/business-registry/formats/kr-brn.js +30 -0
- package/dist/shared/business-registry/formats/my.d.ts +7 -0
- package/dist/shared/business-registry/formats/my.js +32 -0
- package/dist/shared/business-registry/formats/sg-uen.d.ts +1 -0
- package/dist/shared/business-registry/formats/sg-uen.js +21 -0
- package/dist/shared/business-registry/formats/tw-vat.d.ts +10 -0
- package/dist/shared/business-registry/formats/tw-vat.js +32 -0
- package/dist/shared/business-registry/index.d.ts +9 -0
- package/dist/shared/business-registry/index.js +10 -0
- package/dist/shared/business-registry/lookup-wire.d.ts +33 -0
- package/dist/shared/business-registry/lookup-wire.js +8 -0
- package/dist/shared/business-registry/normalize.d.ts +7 -0
- package/dist/shared/business-registry/normalize.js +12 -0
- package/dist/shared/business-registry/registry.d.ts +26 -0
- package/dist/shared/business-registry/registry.js +67 -0
- package/dist/shared/business-registry/types.d.ts +60 -0
- package/dist/shared/business-registry/types.js +10 -0
- package/dist/shared/localization/locale-path.d.ts +38 -0
- package/dist/shared/localization/locale-path.js +76 -0
- package/dist/shared/localization/request-locale.d.ts +42 -0
- package/dist/shared/localization/request-locale.js +61 -0
- package/dist/shared/seo/alternates.d.ts +61 -0
- package/dist/shared/seo/alternates.js +71 -0
- package/dist/shared/types/forms-logic.d.ts +96 -0
- package/dist/shared/types/forms-logic.js +209 -0
- package/dist/shared/types/number-input.d.ts +5 -0
- package/dist/shared/types/number-input.js +16 -0
- package/dist/shared/utils/redirect-matching.d.ts +76 -0
- package/dist/shared/utils/redirect-matching.js +181 -0
- package/dist/tax-id.d.ts +7 -0
- package/dist/tax-id.js +6 -0
- package/dist/typed.d.ts +85 -0
- package/dist/typed.js +28 -0
- package/package.json +86 -3
package/dist/forms.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for headless forms: read the form definition, get the bot-protection challenge, request a file upload ticket, submit the response.
|
|
3
|
+
* For developers who build their own form UI, with `useKanorioBotProtection` or a server-side flow.
|
|
4
|
+
*
|
|
5
|
+
* Responses follow the contract shared by the three APIs: success is the object itself, failure is `{ error: { code, message, details? } }`.
|
|
6
|
+
* Here both become `{ ok: true, ... }` or `{ ok: false, error }`, so a form UI does not have to read HTTP statuses.
|
|
7
|
+
*/
|
|
8
|
+
import { EDGE_CHALLENGE_CODE, edgeChallengeMessage, isEdgeChallenge, serverKeyRequest } from "./server-key.js";
|
|
9
|
+
import { deliveryBaseUrl } from "./delivery.js";
|
|
10
|
+
import { resolveVisibleFieldIds } from "./shared/types/forms-logic.js";
|
|
11
|
+
import { protectionFromChallenge as protectionFieldsFromChallenge, uploadProtectionFromChallenge as uploadFieldsFromChallenge } from "./protection.js";
|
|
12
|
+
/**
|
|
13
|
+
* Builds the protection fields from a challenge: call `challenge()` when the form is shown, and pass the result here on submit.
|
|
14
|
+
* The built-in challenge is solved here (proof of work, about 0.5 to 1 second); when the site uses Turnstile, reCAPTCHA or hCaptcha, render that widget with the challenge's
|
|
15
|
+
* `siteKey` and `action` (Turnstile also has `cData`), then pass its token in as `captchaToken`.
|
|
16
|
+
* `renderedAt` is when the form was shown (milliseconds). Challenges and tokens are single-use: get a new one before every submit.
|
|
17
|
+
*/
|
|
18
|
+
export function protectionFromChallenge(formId, challenge, renderedAt, options = {}) {
|
|
19
|
+
return protectionFieldsFromChallenge(challenge, { context: formId, renderedAt, captchaToken: options.captchaToken, signal: options.signal });
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Protection fields for an attachment upload ticket: with the built-in challenge, pass the ones used for submitting (`solved`; the same solution can be reused for uploads);
|
|
23
|
+
* with a third-party provider, use the challenge's `uploadToken` (the widget token is single-use and is kept for the submit).
|
|
24
|
+
*/
|
|
25
|
+
export function uploadProtectionFromChallenge(formId, challenge, renderedAt, solved) {
|
|
26
|
+
return uploadFieldsFromChallenge(challenge, { context: formId, renderedAt, solved });
|
|
27
|
+
}
|
|
28
|
+
async function readFormError(res, hasServerKey) {
|
|
29
|
+
// The edge's challenge page (a server call without a server key): return an error that says what to do next
|
|
30
|
+
if (isEdgeChallenge(res))
|
|
31
|
+
return { code: EDGE_CHALLENGE_CODE, message: edgeChallengeMessage(hasServerKey) };
|
|
32
|
+
const body = (await res.json().catch(() => null));
|
|
33
|
+
const error = body?.error;
|
|
34
|
+
if (error && typeof error.code === "string") {
|
|
35
|
+
const details = error.details;
|
|
36
|
+
return {
|
|
37
|
+
code: error.code,
|
|
38
|
+
message: typeof error.message === "string" ? error.message : error.code,
|
|
39
|
+
...(details?.issues !== undefined ? { issues: details.issues } : {}),
|
|
40
|
+
...(error.details !== undefined ? { details: error.details } : {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return { code: res.status === 429 ? "RATE_LIMITED" : "UPSTREAM_UNAVAILABLE", message: `HTTP ${res.status}` };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Pass `serverKey` (and `visitorIp`) when your **server** submits on behalf of a visitor. The key proves the
|
|
47
|
+
* request comes from your server; it does not replace form protection, so the submission still needs the
|
|
48
|
+
* `protection` fields the visitor's browser obtained.
|
|
49
|
+
*/
|
|
50
|
+
export function createFormsHelpers(options) {
|
|
51
|
+
const apiBase = deliveryBaseUrl(options.siteUrl);
|
|
52
|
+
const f = options.fetch ?? globalThis.fetch;
|
|
53
|
+
const post = async (path, body) => {
|
|
54
|
+
const url = `${apiBase}${path}`;
|
|
55
|
+
const viaServerKey = await serverKeyRequest(options, url);
|
|
56
|
+
return f(url, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: { "Content-Type": "application/json", Accept: "application/json", ...(viaServerKey?.headers ?? {}) },
|
|
59
|
+
body: JSON.stringify(body),
|
|
60
|
+
...(viaServerKey ? { redirect: viaServerKey.redirect } : {}),
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
const readError = (res) => readFormError(res, Boolean(options.serverKey));
|
|
64
|
+
return {
|
|
65
|
+
async get(formId) {
|
|
66
|
+
const res = await f(`${apiBase}/forms/${encodeURIComponent(formId)}`, { headers: { Accept: "application/json" } });
|
|
67
|
+
if (!res.ok)
|
|
68
|
+
throw new Error(`Failed to fetch form ${formId}: HTTP ${res.status}`);
|
|
69
|
+
return res.json();
|
|
70
|
+
},
|
|
71
|
+
async challenge(formId) {
|
|
72
|
+
const res = await f(`${apiBase}/forms/${encodeURIComponent(formId)}/challenge`, {
|
|
73
|
+
headers: { Accept: "application/json" },
|
|
74
|
+
cache: "no-store",
|
|
75
|
+
});
|
|
76
|
+
if (!res.ok)
|
|
77
|
+
throw new Error(`Failed to fetch form challenge: HTTP ${res.status}`);
|
|
78
|
+
return (await res.json());
|
|
79
|
+
},
|
|
80
|
+
async requestUpload(formId, input) {
|
|
81
|
+
const { protection, ...rest } = input;
|
|
82
|
+
const res = await post(`/forms/${encodeURIComponent(formId)}/upload-request`, { ...rest, ...(protection ?? {}) }).catch(() => null);
|
|
83
|
+
if (!res)
|
|
84
|
+
return { ok: false, error: { code: "UPSTREAM_UNAVAILABLE", message: "Network error" } };
|
|
85
|
+
if (!res.ok)
|
|
86
|
+
return { ok: false, error: await readError(res) };
|
|
87
|
+
const data = (await res.json());
|
|
88
|
+
return { ok: true, ...data };
|
|
89
|
+
},
|
|
90
|
+
async lookupTaxId(formId, input) {
|
|
91
|
+
const { protection, ...rest } = input;
|
|
92
|
+
const res = await post(`/forms/${encodeURIComponent(formId)}/tax-id-lookup`, { ...rest, ...(protection ?? {}) }).catch(() => null);
|
|
93
|
+
// Being unable to look up is not an error: always treated as nothing to prefill
|
|
94
|
+
if (!res || !res.ok)
|
|
95
|
+
return { ok: false, reason: res?.status === 429 ? "rate_limited" : "unavailable" };
|
|
96
|
+
// Delivery responses carry no `ok`: `{ status, ... }`, or `{ status: "UNAVAILABLE", reason }` when no lookup ran
|
|
97
|
+
const data = (await res.json().catch(() => null));
|
|
98
|
+
if (!data || typeof data.status !== "string")
|
|
99
|
+
return { ok: false, reason: "unavailable" };
|
|
100
|
+
if (data.status === "UNAVAILABLE")
|
|
101
|
+
return { ok: false, reason: data.reason ?? "unavailable" };
|
|
102
|
+
const { reason: _reason, ...wire } = data;
|
|
103
|
+
void _reason;
|
|
104
|
+
return { ok: true, ...wire };
|
|
105
|
+
},
|
|
106
|
+
async submit(formId, input) {
|
|
107
|
+
const { protection, ...rest } = input;
|
|
108
|
+
const res = await post(`/forms/${encodeURIComponent(formId)}/submit`, { ...rest, ...(protection ?? {}) }).catch(() => null);
|
|
109
|
+
if (!res)
|
|
110
|
+
return { ok: false, error: { code: "UPSTREAM_UNAVAILABLE", message: "Network error" } };
|
|
111
|
+
if (!res.ok)
|
|
112
|
+
return { ok: false, error: await readError(res) };
|
|
113
|
+
const data = (await res.json().catch(() => ({})));
|
|
114
|
+
return data.state ? { ok: true, state: data.state } : { ok: true };
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Which questions to show for the current answers. A question can carry `visibleWhen` (conditions on earlier questions
|
|
120
|
+
* or hidden fields); the server applies the same function on submit, so a question hidden here is never required and
|
|
121
|
+
* its answer is not saved. Pass the raw values the visitor has entered so far, keyed by question id, together with
|
|
122
|
+
* any hidden field values.
|
|
123
|
+
*/
|
|
124
|
+
export { resolveVisibleFieldIds as visibleFieldIds, hasVisibilityRules } from "./shared/types/forms-logic.js";
|
|
125
|
+
export const forms = {
|
|
126
|
+
client: createFormsHelpers,
|
|
127
|
+
protectionFromChallenge,
|
|
128
|
+
uploadProtectionFromChallenge,
|
|
129
|
+
visibleFieldIds: resolveVisibleFieldIds,
|
|
130
|
+
};
|