totalum-sdk 0.1.0-dev.10
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 +18 -0
- package/dist/_types/billing.d-BbSZh1wY.d.ts +38 -0
- package/dist/_types/coerce.d-C0KIW76L.d.ts +6 -0
- package/dist/_types/errors.d-Cu3q_E_r.d.ts +3093 -0
- package/dist/_types/integrations.d-BNGV70e3.d.ts +793 -0
- package/dist/_types/ops.d-C9375KIG.d.ts +117 -0
- package/dist/ai/index.d.ts +424 -0
- package/dist/ai/index.js +185 -0
- package/dist/analytics/index.d.ts +35 -0
- package/dist/analytics/index.js +15 -0
- package/dist/browser/index.d.ts +1595 -0
- package/dist/browser/index.js +168 -0
- package/dist/cron/index.d.ts +180 -0
- package/dist/cron/index.js +61 -0
- package/dist/d1/errors.js +31 -0
- package/dist/d1/https.js +103 -0
- package/dist/d1/index.d.ts +107 -0
- package/dist/d1/index.js +73 -0
- package/dist/d1/lazy.js +112 -0
- package/dist/d1/libsql.js +125 -0
- package/dist/d1/session.js +38 -0
- package/dist/d1/sql.js +87 -0
- package/dist/d1/types.js +1 -0
- package/dist/email/index.d.ts +31 -0
- package/dist/email/index.js +30 -0
- package/dist/errors.js +35 -0
- package/dist/files/index.d.ts +119 -0
- package/dist/files/index.js +63 -0
- package/dist/http.js +85 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +68 -0
- package/dist/logs/index.d.ts +51 -0
- package/dist/logs/index.js +18 -0
- package/dist/payments/index.d.ts +47 -0
- package/dist/payments/index.js +20 -0
- package/dist/pdf/index.d.ts +42 -0
- package/dist/pdf/index.js +17 -0
- package/dist/react/index.d.ts +91 -0
- package/dist/react/index.js +178 -0
- package/dist/realtime/index.d.ts +103 -0
- package/dist/realtime/index.js +30 -0
- package/dist/scan/index.d.ts +46 -0
- package/dist/scan/index.js +12 -0
- package/dist/seo/index.d.ts +23 -0
- package/dist/seo/index.js +12 -0
- package/dist/speech/index.d.ts +41 -0
- package/dist/speech/index.js +22 -0
- package/dist/web/index.d.ts +170 -0
- package/dist/web/index.js +31 -0
- package/dist/webhooks/index.d.ts +48 -0
- package/dist/webhooks/index.js +61 -0
- package/package.json +146 -0
- package/totalum-sdk.md +1088 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { a as WebhookEvent } from '../_types/billing.d-BbSZh1wY.js';
|
|
2
|
+
export { W as WebhookConfig, b as WebhookSecretOutput } from '../_types/billing.d-BbSZh1wY.js';
|
|
3
|
+
import { T as TotalumClientOptions, a as TotalumError } from '../_types/errors.d-Cu3q_E_r.js';
|
|
4
|
+
export { i as isTotalumError } from '../_types/errors.d-Cu3q_E_r.js';
|
|
5
|
+
|
|
6
|
+
/** A webhook refusal (plan 05 §7.2): the codes never travel, the app answers 400. */
|
|
7
|
+
declare class TotalumWebhookError extends TotalumError {
|
|
8
|
+
name: string;
|
|
9
|
+
readonly errorCode: 'WEBHOOK_SECRET_MISSING' | 'WEBHOOK_SIGNATURE_INVALID' | 'WEBHOOK_TIMESTAMP_EXPIRED';
|
|
10
|
+
constructor(errorCode: TotalumWebhookError['errorCode'], message: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* `totalum.webhooks.verify` (plan 04 §5.13): checks `Totalum-Signature: t=…,v1=…` — HMAC-SHA256 keyed with the
|
|
14
|
+
* endpoint's secret string over `${t}.${rawBody}` — within ±300 s, and returns the parsed event. Pass the body exactly
|
|
15
|
+
* as received (`await request.text()`). `secret` defaults to `TOTALUM_WEBHOOK_SECRET`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const event = await verifyWebhook(await request.text(), request.headers);
|
|
19
|
+
*/
|
|
20
|
+
declare function verifyWebhook(rawBody: string, headers: Headers | Record<string, string | undefined>, secret?: string | undefined, now?: number): Promise<WebhookEvent>;
|
|
21
|
+
/**
|
|
22
|
+
* `totalum.webhooks` (plan 04 §5.13) over SDK-API `/v1/webhooks` (a key with scope `*`): where the project receives its
|
|
23
|
+
* signed events (payments, e-mail, browser programs, `project.credits.exhausted`) and the secret they are signed with.
|
|
24
|
+
*/
|
|
25
|
+
declare function totalumWebhooks(options?: TotalumClientOptions): {
|
|
26
|
+
/** Same as `verifyWebhook`: checks `Totalum-Signature` and returns the parsed event. */
|
|
27
|
+
verify: typeof verifyWebhook;
|
|
28
|
+
/** The endpoint and the first characters of its secret (the secret itself is shown only by `rotateSecret`). */
|
|
29
|
+
get: () => Promise<{
|
|
30
|
+
url: string | null;
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
secretPrefix: string | null;
|
|
33
|
+
}>;
|
|
34
|
+
/** Sets the `https://` URL the project's events are delivered to. */
|
|
35
|
+
setUrl: (url: string) => Promise<{
|
|
36
|
+
url: string | null;
|
|
37
|
+
enabled: boolean;
|
|
38
|
+
secretPrefix: string | null;
|
|
39
|
+
}>;
|
|
40
|
+
/** A new signing secret, shown once; the old one keeps co-signing for 24 h. Store it as `TOTALUM_WEBHOOK_SECRET`. */
|
|
41
|
+
rotateSecret: () => Promise<{
|
|
42
|
+
secret: string;
|
|
43
|
+
}>;
|
|
44
|
+
};
|
|
45
|
+
type TotalumWebhooks = ReturnType<typeof totalumWebhooks>;
|
|
46
|
+
|
|
47
|
+
export { TotalumClientOptions, TotalumError, TotalumWebhookError, WebhookEvent, totalumWebhooks, verifyWebhook };
|
|
48
|
+
export type { TotalumWebhooks };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { TotalumError } from '../errors.js';
|
|
2
|
+
import { jsonClient, readEnv } from '../http.js';
|
|
3
|
+
export { TotalumError, isTotalumError } from '../errors.js';
|
|
4
|
+
/** Literals type-locked to contracts, so the published package imports nothing at runtime (plan 05 §1). */
|
|
5
|
+
const SIGNATURE_HEADER = 'Totalum-Signature';
|
|
6
|
+
const TOLERANCE_SECONDS = 300;
|
|
7
|
+
/** A webhook refusal (plan 05 §7.2): the codes never travel, the app answers 400. */
|
|
8
|
+
export class TotalumWebhookError extends TotalumError {
|
|
9
|
+
name = 'TotalumWebhookError';
|
|
10
|
+
constructor(errorCode, message) {
|
|
11
|
+
super(errorCode, message, errorCode === 'WEBHOOK_SECRET_MISSING' ? 500 : 401);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const enc = new TextEncoder();
|
|
15
|
+
/**
|
|
16
|
+
* `totalum.webhooks.verify` (plan 04 §5.13): checks `Totalum-Signature: t=…,v1=…` — HMAC-SHA256 keyed with the
|
|
17
|
+
* endpoint's secret string over `${t}.${rawBody}` — within ±300 s, and returns the parsed event. Pass the body exactly
|
|
18
|
+
* as received (`await request.text()`). `secret` defaults to `TOTALUM_WEBHOOK_SECRET`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const event = await verifyWebhook(await request.text(), request.headers);
|
|
22
|
+
*/
|
|
23
|
+
export async function verifyWebhook(rawBody, headers, secret = readEnv('TOTALUM_WEBHOOK_SECRET'), now = Date.now()) {
|
|
24
|
+
if (!secret)
|
|
25
|
+
throw new TotalumWebhookError('WEBHOOK_SECRET_MISSING', 'Set TOTALUM_WEBHOOK_SECRET or pass the secret.');
|
|
26
|
+
const name = SIGNATURE_HEADER;
|
|
27
|
+
const header = headers instanceof Headers ? headers.get(name) : (headers[name] ?? headers[name.toLowerCase()] ?? null);
|
|
28
|
+
const parts = (header ?? '').split(',').map((p) => p.split('=', 2));
|
|
29
|
+
const t = Number(parts.find(([k]) => k === 't')?.[1]);
|
|
30
|
+
if (!Number.isInteger(t))
|
|
31
|
+
throw new TotalumWebhookError('WEBHOOK_SIGNATURE_INVALID', 'Missing Totalum-Signature');
|
|
32
|
+
if (Math.abs(now / 1000 - t) > TOLERANCE_SECONDS)
|
|
33
|
+
throw new TotalumWebhookError('WEBHOOK_TIMESTAMP_EXPIRED', 'Signature timestamp outside ±300 s');
|
|
34
|
+
const key = await crypto.subtle.importKey('raw', enc.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['verify']);
|
|
35
|
+
const payload = enc.encode(`${String(t)}.${rawBody}`);
|
|
36
|
+
for (const [k, v] of parts) {
|
|
37
|
+
if (k !== 'v1' || !v || !/^[0-9a-f]{64}$/.test(v))
|
|
38
|
+
continue;
|
|
39
|
+
const sig = new Uint8Array(v.match(/../g)?.map((h) => parseInt(h, 16)) ?? []);
|
|
40
|
+
if (await crypto.subtle.verify('HMAC', key, sig, payload))
|
|
41
|
+
return JSON.parse(rawBody);
|
|
42
|
+
}
|
|
43
|
+
throw new TotalumWebhookError('WEBHOOK_SIGNATURE_INVALID', 'Totalum-Signature does not match');
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `totalum.webhooks` (plan 04 §5.13) over SDK-API `/v1/webhooks` (a key with scope `*`): where the project receives its
|
|
47
|
+
* signed events (payments, e-mail, browser programs, `project.credits.exhausted`) and the secret they are signed with.
|
|
48
|
+
*/
|
|
49
|
+
export function totalumWebhooks(options = {}) {
|
|
50
|
+
const call = jsonClient(options);
|
|
51
|
+
return {
|
|
52
|
+
/** Same as `verifyWebhook`: checks `Totalum-Signature` and returns the parsed event. */
|
|
53
|
+
verify: verifyWebhook,
|
|
54
|
+
/** The endpoint and the first characters of its secret (the secret itself is shown only by `rotateSecret`). */
|
|
55
|
+
get: () => call('GET', '/v1/webhooks'),
|
|
56
|
+
/** Sets the `https://` URL the project's events are delivered to. */
|
|
57
|
+
setUrl: (url) => call('PUT', '/v1/webhooks', { url }),
|
|
58
|
+
/** A new signing secret, shown once; the old one keeps co-signing for 24 h. Store it as `TOTALUM_WEBHOOK_SECRET`. */
|
|
59
|
+
rotateSecret: () => call('POST', '/v1/webhooks/rotate-secret'),
|
|
60
|
+
};
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "totalum-sdk",
|
|
3
|
+
"version": "0.1.0-dev.10",
|
|
4
|
+
"description": "The SDK generated Totalum apps import: every integration namespace over SDK-API (totalum-sdk, or one subpath each: /files, /ai, /web, …) and totalum-sdk/d1, the D1 driver",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@10.34.5",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22 <23"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"totalum-sdk.md"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./d1": {
|
|
21
|
+
"types": "./dist/d1/index.d.ts",
|
|
22
|
+
"default": "./dist/d1/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./files": {
|
|
25
|
+
"types": "./dist/files/index.d.ts",
|
|
26
|
+
"default": "./dist/files/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./payments": {
|
|
29
|
+
"types": "./dist/payments/index.d.ts",
|
|
30
|
+
"default": "./dist/payments/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./webhooks": {
|
|
33
|
+
"types": "./dist/webhooks/index.d.ts",
|
|
34
|
+
"default": "./dist/webhooks/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./web": {
|
|
37
|
+
"types": "./dist/web/index.d.ts",
|
|
38
|
+
"default": "./dist/web/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./pdf": {
|
|
41
|
+
"types": "./dist/pdf/index.d.ts",
|
|
42
|
+
"default": "./dist/pdf/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./scan": {
|
|
45
|
+
"types": "./dist/scan/index.d.ts",
|
|
46
|
+
"default": "./dist/scan/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./speech": {
|
|
49
|
+
"types": "./dist/speech/index.d.ts",
|
|
50
|
+
"default": "./dist/speech/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./ai": {
|
|
53
|
+
"types": "./dist/ai/index.d.ts",
|
|
54
|
+
"default": "./dist/ai/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./email": {
|
|
57
|
+
"types": "./dist/email/index.d.ts",
|
|
58
|
+
"default": "./dist/email/index.js"
|
|
59
|
+
},
|
|
60
|
+
"./cron": {
|
|
61
|
+
"types": "./dist/cron/index.d.ts",
|
|
62
|
+
"default": "./dist/cron/index.js"
|
|
63
|
+
},
|
|
64
|
+
"./seo": {
|
|
65
|
+
"types": "./dist/seo/index.d.ts",
|
|
66
|
+
"default": "./dist/seo/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./browser": {
|
|
69
|
+
"types": "./dist/browser/index.d.ts",
|
|
70
|
+
"default": "./dist/browser/index.js"
|
|
71
|
+
},
|
|
72
|
+
"./realtime": {
|
|
73
|
+
"types": "./dist/realtime/index.d.ts",
|
|
74
|
+
"default": "./dist/realtime/index.js"
|
|
75
|
+
},
|
|
76
|
+
"./logs": {
|
|
77
|
+
"types": "./dist/logs/index.d.ts",
|
|
78
|
+
"default": "./dist/logs/index.js"
|
|
79
|
+
},
|
|
80
|
+
"./analytics": {
|
|
81
|
+
"types": "./dist/analytics/index.d.ts",
|
|
82
|
+
"default": "./dist/analytics/index.js"
|
|
83
|
+
},
|
|
84
|
+
"./react": {
|
|
85
|
+
"types": "./dist/react/index.d.ts",
|
|
86
|
+
"default": "./dist/react/index.js"
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"sideEffects": false,
|
|
90
|
+
"scripts": {
|
|
91
|
+
"build": "rm -rf dist build && tsc -p tsconfig.build.json && rollup -c scripts/dts.config.mjs --silent",
|
|
92
|
+
"size": "node scripts/size.mjs",
|
|
93
|
+
"docs": "node scripts/gen-docs.mjs",
|
|
94
|
+
"docs:check": "node scripts/gen-docs.mjs --check",
|
|
95
|
+
"release:dev": "rm -rf dist && pnpm build && npm publish --tag dev",
|
|
96
|
+
"typecheck": "tsc --noEmit",
|
|
97
|
+
"lint": "eslint . && prettier --check .",
|
|
98
|
+
"format": "prettier --write .",
|
|
99
|
+
"test": "vitest run",
|
|
100
|
+
"test:node": "vitest run --project node",
|
|
101
|
+
"test:workerd": "vitest run --project workerd",
|
|
102
|
+
"test:dev": "vitest run --config test/dev/vitest.config.ts",
|
|
103
|
+
"prepare": "lefthook install || echo \"lefthook: hooks not installed (git >= 2.31 required)\"",
|
|
104
|
+
"types:check": "node scripts/check-types.mjs"
|
|
105
|
+
},
|
|
106
|
+
"peerDependencies": {
|
|
107
|
+
"@libsql/client": "0.18.0",
|
|
108
|
+
"react": ">=18"
|
|
109
|
+
},
|
|
110
|
+
"peerDependenciesMeta": {
|
|
111
|
+
"@libsql/client": {
|
|
112
|
+
"optional": true
|
|
113
|
+
},
|
|
114
|
+
"react": {
|
|
115
|
+
"optional": true
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
"devDependencies": {
|
|
119
|
+
"@cloudflare/vitest-pool-workers": "0.22.0",
|
|
120
|
+
"@cloudflare/workers-types": "5.20260922.1",
|
|
121
|
+
"@commitlint/cli": "21.2.3",
|
|
122
|
+
"@commitlint/config-conventional": "21.2.3",
|
|
123
|
+
"@eslint/js": "9.39.5",
|
|
124
|
+
"@libsql/client": "0.18.0",
|
|
125
|
+
"@speed-paradigm/totalum-contracts": "link:../totalum-contracts",
|
|
126
|
+
"@types/node": "22.20.4",
|
|
127
|
+
"@types/react": "19.3.0",
|
|
128
|
+
"drizzle-orm": "0.45.3",
|
|
129
|
+
"esbuild": "0.28.1",
|
|
130
|
+
"eslint": "9.39.5",
|
|
131
|
+
"eslint-plugin-import-x": "4.17.1",
|
|
132
|
+
"eslint-plugin-no-only-tests": "3.4.0",
|
|
133
|
+
"lefthook": "2.1.14",
|
|
134
|
+
"miniflare": "5.20260921.0-alpha",
|
|
135
|
+
"prettier": "3.9.8",
|
|
136
|
+
"react": "19.3.0",
|
|
137
|
+
"rollup": "4.63.4",
|
|
138
|
+
"rollup-plugin-dts": "6.5.1",
|
|
139
|
+
"totalum-sdk-api": "link:../totalum-sdk-api",
|
|
140
|
+
"typescript": "5.8.3",
|
|
141
|
+
"typescript-eslint": "8.70.1",
|
|
142
|
+
"vitest": "4.1.11",
|
|
143
|
+
"wrangler": "4.136.2"
|
|
144
|
+
},
|
|
145
|
+
"prettier": "@speed-paradigm/totalum-contracts/configs/prettier.json"
|
|
146
|
+
}
|