@sess.si/sdk 0.0.0-stage → 0.1.0-rc.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/CHANGELOG.md +185 -0
- package/LICENSE +201 -0
- package/NOTICE +27 -0
- package/README.md +327 -2
- package/dist/ask.d.ts +161 -0
- package/dist/ask.js +499 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +7 -0
- package/dist/cli/verify.d.ts +13 -0
- package/dist/cli/verify.js +322 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +43 -0
- package/dist/commerce/builders.d.ts +99 -0
- package/dist/commerce/builders.js +208 -0
- package/dist/commerce/currency.d.ts +4 -0
- package/dist/commerce/currency.js +25 -0
- package/dist/commerce/items.d.ts +119 -0
- package/dist/commerce/items.js +280 -0
- package/dist/commerce/lint.d.ts +16 -0
- package/dist/commerce/lint.js +56 -0
- package/dist/errors.d.ts +61 -0
- package/dist/errors.js +63 -0
- package/dist/generated/openapi.d.ts +8360 -0
- package/dist/generated/openapi.js +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/internal/decode.d.ts +20 -0
- package/dist/internal/decode.js +380 -0
- package/dist/internal/json.d.ts +3 -0
- package/dist/internal/json.js +74 -0
- package/dist/internal/time.d.ts +10 -0
- package/dist/internal/time.js +70 -0
- package/dist/internal/version.d.ts +2 -0
- package/dist/internal/version.js +2 -0
- package/dist/polling.d.ts +11 -0
- package/dist/polling.js +137 -0
- package/dist/qr/encoder.d.ts +10 -0
- package/dist/qr/encoder.js +343 -0
- package/dist/qr/index.d.ts +55 -0
- package/dist/qr/index.js +150 -0
- package/dist/receipts/bundle.d.ts +123 -0
- package/dist/receipts/bundle.js +423 -0
- package/dist/receipts/index.d.ts +8 -0
- package/dist/receipts/index.js +3 -0
- package/dist/receipts/json.d.ts +21 -0
- package/dist/receipts/json.js +151 -0
- package/dist/receipts/key-cache.d.ts +27 -0
- package/dist/receipts/key-cache.js +109 -0
- package/dist/receipts/renderers.d.ts +57 -0
- package/dist/receipts/renderers.js +153 -0
- package/dist/receipts/resource.d.ts +38 -0
- package/dist/receipts/resource.js +145 -0
- package/dist/receipts/verify.d.ts +112 -0
- package/dist/receipts/verify.js +339 -0
- package/dist/sessions.d.ts +18 -0
- package/dist/sessions.js +151 -0
- package/dist/transport.d.ts +30 -0
- package/dist/transport.js +511 -0
- package/dist/types.d.ts +283 -0
- package/dist/types.js +8 -0
- package/dist/webhook-resource.d.ts +12 -0
- package/dist/webhook-resource.js +51 -0
- package/dist/webhooks/index.d.ts +21 -0
- package/dist/webhooks/index.js +183 -0
- package/package.json +82 -4
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// `sess verify <file>`: the CLI face of decision-bundle verification. Offline only: it reads the
|
|
2
|
+
// bundle and the keys the caller trusts from local files, verifies with verifyDecisionBundle and
|
|
3
|
+
// prints a short summary (or the result object with --json). It opens no network connection and
|
|
4
|
+
// never prints the receipt, the disclosed content or any key material.
|
|
5
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
6
|
+
import { parseArgs } from 'node:util';
|
|
7
|
+
import { verifyDecisionBundle, } from '../receipts/bundle.js';
|
|
8
|
+
import { BUNDLED_RENDERER_IDENTITIES } from '../receipts/renderers.js';
|
|
9
|
+
import { validateKeySet, verifyRendererIdentities } from '../receipts/verify.js';
|
|
10
|
+
/** Exit codes: valid, invalid bundle, usage or file error. */
|
|
11
|
+
export const EXIT = Object.freeze({ valid: 0, invalid: 1, usage: 2 });
|
|
12
|
+
/** A bundle file larger than this is not read (the bundle bound itself is 1 MiB canonical). */
|
|
13
|
+
const MAX_BUNDLE_FILE_BYTES = 16 * 1024 * 1024;
|
|
14
|
+
const MAX_KEYS_FILE_BYTES = 1024 * 1024;
|
|
15
|
+
const MAX_RENDERERS_FILE_BYTES = 64 * 1024;
|
|
16
|
+
export const USAGE = `Usage: sess verify <bundle.json> --issuer <url> --keys <jwks.json> [options]
|
|
17
|
+
|
|
18
|
+
Checks a Sess decision bundle on this computer against the issuer and keys you trust.
|
|
19
|
+
Nothing is sent anywhere. Keys inside the bundle are never trusted.
|
|
20
|
+
|
|
21
|
+
Required:
|
|
22
|
+
--issuer <url> the receipt issuer you trust, for example https://api.sess.si
|
|
23
|
+
--keys <file> that issuer's public keys (a JWK Set) you obtained yourself
|
|
24
|
+
|
|
25
|
+
Expected context (optional; only what you pass is checked):
|
|
26
|
+
--audience <id> the workspace the decision must belong to
|
|
27
|
+
--workspace <id> the workspace ID
|
|
28
|
+
--session <id> the Session ID
|
|
29
|
+
--event <id> the Event ID
|
|
30
|
+
--revision <n> the Surface revision
|
|
31
|
+
--action <id> the action ID
|
|
32
|
+
|
|
33
|
+
Renderer identity (optional):
|
|
34
|
+
--renderers <file> the issuer's signed renderer list you obtained yourself
|
|
35
|
+
(<issuer>/.well-known/sess-renderer-identities.json); also checks that the
|
|
36
|
+
receipt's renderer identity is one this release knows or one on that list
|
|
37
|
+
|
|
38
|
+
Other:
|
|
39
|
+
--now <time> check at this RFC 3339 time instead of now
|
|
40
|
+
--json print the full result as JSON
|
|
41
|
+
-h, --help show this help
|
|
42
|
+
|
|
43
|
+
Exit status: 0 valid, 1 invalid, 2 usage or file error.
|
|
44
|
+
`;
|
|
45
|
+
class UsageError extends Error {
|
|
46
|
+
}
|
|
47
|
+
const STRING_OPTIONS = [
|
|
48
|
+
'issuer',
|
|
49
|
+
'keys',
|
|
50
|
+
'audience',
|
|
51
|
+
'workspace',
|
|
52
|
+
'session',
|
|
53
|
+
'event',
|
|
54
|
+
'revision',
|
|
55
|
+
'action',
|
|
56
|
+
'now',
|
|
57
|
+
'renderers',
|
|
58
|
+
];
|
|
59
|
+
const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
60
|
+
/** Escapes characters a terminal could interpret (controls, bidi overrides, line separators). */
|
|
61
|
+
function safe(text) {
|
|
62
|
+
return text.replace(/[\u0000-\u001f\u007f-\u009f\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
63
|
+
}
|
|
64
|
+
async function readBounded(path, max, what) {
|
|
65
|
+
let size;
|
|
66
|
+
try {
|
|
67
|
+
const info = await stat(path);
|
|
68
|
+
if (!info.isFile())
|
|
69
|
+
throw new UsageError(`${what} is not a file: ${safe(path)}`);
|
|
70
|
+
size = info.size;
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
if (e instanceof UsageError)
|
|
74
|
+
throw e;
|
|
75
|
+
throw new UsageError(`cannot read ${what}: ${safe(path)}`);
|
|
76
|
+
}
|
|
77
|
+
if (size > max)
|
|
78
|
+
throw new UsageError(`${what} is larger than ${max} bytes: ${safe(path)}`);
|
|
79
|
+
try {
|
|
80
|
+
return await readFile(path);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
throw new UsageError(`cannot read ${what}: ${safe(path)}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function parseJson(bytes) {
|
|
87
|
+
try {
|
|
88
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
89
|
+
return { ok: true, value: JSON.parse(text) };
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return { ok: false };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function parse(argv) {
|
|
96
|
+
let parsed;
|
|
97
|
+
try {
|
|
98
|
+
parsed = parseArgs({
|
|
99
|
+
args: [...argv],
|
|
100
|
+
allowPositionals: true,
|
|
101
|
+
strict: true,
|
|
102
|
+
tokens: true,
|
|
103
|
+
options: {
|
|
104
|
+
...Object.fromEntries(STRING_OPTIONS.map((name) => [name, { type: 'string' }])),
|
|
105
|
+
json: { type: 'boolean' },
|
|
106
|
+
help: { type: 'boolean', short: 'h' },
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw new UsageError('unknown option or missing option value');
|
|
112
|
+
}
|
|
113
|
+
const values = parsed.values;
|
|
114
|
+
if (values.help)
|
|
115
|
+
return 'help';
|
|
116
|
+
// A trust setting given twice is ambiguous: refuse rather than pick one.
|
|
117
|
+
const seen = new Set();
|
|
118
|
+
for (const token of parsed.tokens ?? []) {
|
|
119
|
+
if (token.kind !== 'option')
|
|
120
|
+
continue;
|
|
121
|
+
if (seen.has(token.name))
|
|
122
|
+
throw new UsageError(`--${token.name} is given more than once`);
|
|
123
|
+
seen.add(token.name);
|
|
124
|
+
}
|
|
125
|
+
const [command, file, ...rest] = parsed.positionals;
|
|
126
|
+
if (command !== 'verify')
|
|
127
|
+
throw new UsageError('the only command is: verify <bundle.json>');
|
|
128
|
+
if (file === undefined || rest.length > 0)
|
|
129
|
+
throw new UsageError('give exactly one bundle file');
|
|
130
|
+
const str = (name) => {
|
|
131
|
+
const v = values[name];
|
|
132
|
+
if (v === undefined)
|
|
133
|
+
return undefined;
|
|
134
|
+
if (typeof v !== 'string' || v.length === 0)
|
|
135
|
+
throw new UsageError(`--${name} needs a value`);
|
|
136
|
+
return v;
|
|
137
|
+
};
|
|
138
|
+
const issuer = str('issuer');
|
|
139
|
+
const keysPath = str('keys');
|
|
140
|
+
if (issuer === undefined || keysPath === undefined)
|
|
141
|
+
throw new UsageError('--issuer and --keys are required: verification only uses keys you trust');
|
|
142
|
+
const revisionText = str('revision');
|
|
143
|
+
let revision;
|
|
144
|
+
if (revisionText !== undefined) {
|
|
145
|
+
revision = Number(revisionText);
|
|
146
|
+
if (!/^[1-9][0-9]*$/.test(revisionText) || !Number.isSafeInteger(revision))
|
|
147
|
+
throw new UsageError('--revision must be a positive integer');
|
|
148
|
+
}
|
|
149
|
+
const nowText = str('now');
|
|
150
|
+
let now;
|
|
151
|
+
if (nowText !== undefined) {
|
|
152
|
+
now = new Date(nowText);
|
|
153
|
+
if (!RFC3339.test(nowText) || Number.isNaN(now.getTime()))
|
|
154
|
+
throw new UsageError('--now must be an RFC 3339 time, for example 2026-10-09T12:05:00Z');
|
|
155
|
+
}
|
|
156
|
+
const context = {};
|
|
157
|
+
const audience = str('audience');
|
|
158
|
+
const workspaceId = str('workspace');
|
|
159
|
+
const sessionId = str('session');
|
|
160
|
+
const eventId = str('event');
|
|
161
|
+
const actionId = str('action');
|
|
162
|
+
if (audience !== undefined)
|
|
163
|
+
context.audience = audience;
|
|
164
|
+
if (workspaceId !== undefined)
|
|
165
|
+
context.workspaceId = workspaceId;
|
|
166
|
+
if (sessionId !== undefined)
|
|
167
|
+
context.sessionId = sessionId;
|
|
168
|
+
if (eventId !== undefined)
|
|
169
|
+
context.eventId = eventId;
|
|
170
|
+
if (revision !== undefined)
|
|
171
|
+
context.revision = revision;
|
|
172
|
+
if (actionId !== undefined)
|
|
173
|
+
context.actionId = actionId;
|
|
174
|
+
return {
|
|
175
|
+
file,
|
|
176
|
+
issuer,
|
|
177
|
+
keysPath,
|
|
178
|
+
renderersPath: str('renderers'),
|
|
179
|
+
context: Object.keys(context).length > 0 ? context : undefined,
|
|
180
|
+
now,
|
|
181
|
+
json: values.json === true,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const REASON_TEXT = {
|
|
185
|
+
malformed: 'the file is not a bundle-v1 decision bundle',
|
|
186
|
+
oversized: 'the bundle exceeds a bundle-v1 size bound',
|
|
187
|
+
unsupported_version: 'the bundle uses a later format version',
|
|
188
|
+
receipt_malformed: 'the receipt cannot be read',
|
|
189
|
+
algorithm: 'the receipt does not use the EdDSA algorithm',
|
|
190
|
+
missing_kid: 'the receipt names no key',
|
|
191
|
+
unknown_kid: 'the receipt was signed with a key you do not trust',
|
|
192
|
+
type: 'the receipt has the wrong type',
|
|
193
|
+
signature: 'the signature does not verify with the key you trust',
|
|
194
|
+
issuer: 'the receipt is from another issuer',
|
|
195
|
+
audience: 'the receipt belongs to another workspace',
|
|
196
|
+
expired: 'the receipt has expired',
|
|
197
|
+
claims: 'the receipt claims are not valid receipt-v1 claims',
|
|
198
|
+
not_yet_valid: 'the receipt is dated in the future',
|
|
199
|
+
surface_mismatch: 'the disclosed Surface is not the one the receipt signed',
|
|
200
|
+
input_mismatch: 'the disclosed input is not the one the receipt signed',
|
|
201
|
+
context_mismatch: 'the decision is not the one you expected',
|
|
202
|
+
renderer: 'the receipt names a renderer identity that is neither known nor on the signed list',
|
|
203
|
+
};
|
|
204
|
+
function summary(r, renderer) {
|
|
205
|
+
const lines = [];
|
|
206
|
+
const row = (label, value) => lines.push(` ${label.padEnd(11)} ${value}`);
|
|
207
|
+
const context = r.context.status === 'mismatch'
|
|
208
|
+
? `mismatch: ${r.context.mismatches.join(', ')}`
|
|
209
|
+
: r.context.status === 'unchecked'
|
|
210
|
+
? 'unchecked (no expected context given)'
|
|
211
|
+
: r.context.checked.length > 0
|
|
212
|
+
? `${r.context.status}: ${r.context.checked.join(', ')}`
|
|
213
|
+
: r.context.status;
|
|
214
|
+
if (r.valid && r.receipt) {
|
|
215
|
+
const c = r.receipt;
|
|
216
|
+
lines.push('Valid decision bundle.');
|
|
217
|
+
row('Accepted', `${safe(c.accepted_at)}, action ${safe(c.action.id)} on revision ${c.surface.revision}`);
|
|
218
|
+
row('Session', `${safe(c.session_id)} (event ${safe(c.event_id)}, workspace ${safe(c.workspace_id)})`);
|
|
219
|
+
row('Issuer', `${safe(c.issuer)} (key ${safe(c.key_id)})`);
|
|
220
|
+
row('Actor', "someone holding the Session's human link (not a verified person)");
|
|
221
|
+
row('Content', 'verified');
|
|
222
|
+
row('Context', context);
|
|
223
|
+
row('Purpose', r.application_purpose);
|
|
224
|
+
row('Expires', safe(c.expires_at));
|
|
225
|
+
if (renderer)
|
|
226
|
+
row('Renderer', `${safe(c.surface.renderer_version)} (listed)`);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
lines.push(`Invalid decision bundle: ${r.reason}`);
|
|
230
|
+
lines.push(` ${REASON_TEXT[r.reason ?? ''] ?? ''}`);
|
|
231
|
+
row('Content', r.content);
|
|
232
|
+
row('Context', context);
|
|
233
|
+
if (renderer?.identity)
|
|
234
|
+
row('Renderer', `${safe(renderer.identity)} (${renderer.status})`);
|
|
235
|
+
}
|
|
236
|
+
row('Checked at', r.checked_at);
|
|
237
|
+
return `${lines.join('\n')}\n`;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* JSON with every terminal-sensitive character escaped; parses to the same value. JSON.stringify
|
|
241
|
+
* already escapes C0 controls inside strings, and the only raw characters it can leave are inside
|
|
242
|
+
* strings, where a \u escape denotes the same character.
|
|
243
|
+
*/
|
|
244
|
+
function safeJson(value) {
|
|
245
|
+
const text = JSON.stringify(value, null, 2).replace(/[\u007f-\u009f\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
246
|
+
return `${text}\n`;
|
|
247
|
+
}
|
|
248
|
+
/** Runs `sess <argv>`; resolves to the exit code. Never throws. */
|
|
249
|
+
export async function runCli(argv, io) {
|
|
250
|
+
try {
|
|
251
|
+
const parsed = parse(argv);
|
|
252
|
+
if (parsed === 'help') {
|
|
253
|
+
io.stdout(USAGE);
|
|
254
|
+
return EXIT.valid;
|
|
255
|
+
}
|
|
256
|
+
const keyBytes = await readBounded(parsed.keysPath, MAX_KEYS_FILE_BYTES, 'the --keys file');
|
|
257
|
+
const keyJson = parseJson(keyBytes);
|
|
258
|
+
let trustedKeys;
|
|
259
|
+
try {
|
|
260
|
+
if (!keyJson.ok)
|
|
261
|
+
throw new Error('not JSON');
|
|
262
|
+
trustedKeys = validateKeySet(keyJson.value);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
throw new UsageError('the --keys file must be a JWK Set of public Ed25519 keys with unique kids (private keys are refused)');
|
|
266
|
+
}
|
|
267
|
+
// Decision 0009 Amendment 13: the issuer's signed renderer list, trusted only through its
|
|
268
|
+
// signature under the keys above.
|
|
269
|
+
let listed;
|
|
270
|
+
if (parsed.renderersPath !== undefined) {
|
|
271
|
+
const listJson = parseJson(await readBounded(parsed.renderersPath, MAX_RENDERERS_FILE_BYTES, 'the --renderers file'));
|
|
272
|
+
try {
|
|
273
|
+
if (!listJson.ok)
|
|
274
|
+
throw new Error('not JSON');
|
|
275
|
+
listed = (await verifyRendererIdentities(listJson.value, {
|
|
276
|
+
issuer: parsed.issuer,
|
|
277
|
+
keys: trustedKeys,
|
|
278
|
+
})).identities.map((i) => i.renderer_version);
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
throw new UsageError("the --renderers file must be the issuer's renderer list, signed with the keys you trust");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const bundleBytes = await readBounded(parsed.file, MAX_BUNDLE_FILE_BYTES, 'the bundle file');
|
|
285
|
+
const bundleJson = parseJson(bundleBytes);
|
|
286
|
+
let result;
|
|
287
|
+
try {
|
|
288
|
+
// A file that is not JSON is not a bundle: the same facade reports it as malformed.
|
|
289
|
+
result = await verifyDecisionBundle(bundleJson.ok ? bundleJson.value : undefined, {
|
|
290
|
+
issuer: parsed.issuer,
|
|
291
|
+
trustedKeys,
|
|
292
|
+
...(parsed.context ? { expectedContext: parsed.context } : {}),
|
|
293
|
+
...(parsed.now ? { now: parsed.now } : {}),
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
throw new UsageError('invalid verification options');
|
|
298
|
+
}
|
|
299
|
+
let renderer;
|
|
300
|
+
if (listed !== undefined) {
|
|
301
|
+
const identity = result.receipt?.surface.renderer_version ?? null;
|
|
302
|
+
renderer =
|
|
303
|
+
identity === null
|
|
304
|
+
? { identity, status: 'not_checked' }
|
|
305
|
+
: {
|
|
306
|
+
identity,
|
|
307
|
+
status: BUNDLED_RENDERER_IDENTITIES.includes(identity) || listed.includes(identity)
|
|
308
|
+
? 'listed'
|
|
309
|
+
: 'unlisted',
|
|
310
|
+
};
|
|
311
|
+
if (renderer.status === 'unlisted')
|
|
312
|
+
result = { ...result, valid: false, reason: 'renderer' };
|
|
313
|
+
}
|
|
314
|
+
io.stdout(parsed.json ? safeJson(renderer ? { ...result, renderer } : result) : summary(result, renderer));
|
|
315
|
+
return result.valid ? EXIT.valid : EXIT.invalid;
|
|
316
|
+
}
|
|
317
|
+
catch (e) {
|
|
318
|
+
const message = e instanceof UsageError ? e.message : 'unexpected error';
|
|
319
|
+
io.stderr(`sess: ${message}\nRun "sess --help" for usage.\n`);
|
|
320
|
+
return EXIT.usage;
|
|
321
|
+
}
|
|
322
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SessOptions } from './types.js';
|
|
2
|
+
import { Sessions } from './sessions.js';
|
|
3
|
+
import { WebhookResource } from './webhook-resource.js';
|
|
4
|
+
import { createReceiptResource } from './receipts/resource.js';
|
|
5
|
+
import { type AskAnswer, type AskField, type AskHandle, type AskInput, type AskOptions } from './ask.js';
|
|
6
|
+
/** Server-side client. Construction has no network or installation side effects. */
|
|
7
|
+
export declare class Sess {
|
|
8
|
+
readonly sessions: Sessions;
|
|
9
|
+
readonly webhooks: WebhookResource;
|
|
10
|
+
readonly receipts: ReturnType<typeof createReceiptResource>;
|
|
11
|
+
constructor(options: SessOptions);
|
|
12
|
+
/**
|
|
13
|
+
* Asks one question with simple typed fields. Invalid input fails before credential lookup or
|
|
14
|
+
* network access. The Session is created once with the whole interaction, and the returned
|
|
15
|
+
* handle carries the private link before any wait. `answer()` verifies the answer, checks that it
|
|
16
|
+
* is still current, completes the Session and returns the typed values.
|
|
17
|
+
*/
|
|
18
|
+
ask<const F extends readonly AskField[]>(input: AskInput<F>, options?: AskOptions): Promise<AskHandle<AskAnswer<F>>>;
|
|
19
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Transport } from './transport.js';
|
|
2
|
+
import { Sessions } from './sessions.js';
|
|
3
|
+
import { WebhookResource } from './webhook-resource.js';
|
|
4
|
+
import { createReceiptResource } from './receipts/resource.js';
|
|
5
|
+
import { ask } from './ask.js';
|
|
6
|
+
/** Server-side client. Construction has no network or installation side effects. */
|
|
7
|
+
export class Sess {
|
|
8
|
+
sessions;
|
|
9
|
+
webhooks;
|
|
10
|
+
receipts;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
const transport = new Transport(options);
|
|
13
|
+
this.sessions = new Sessions(transport);
|
|
14
|
+
this.webhooks = new WebhookResource(transport);
|
|
15
|
+
this.receipts = createReceiptResource({
|
|
16
|
+
baseUrl: transport.baseUrl,
|
|
17
|
+
receiptIssuer: transport.receiptIssuer,
|
|
18
|
+
expectedWorkspaceId: transport.expectedWorkspaceId,
|
|
19
|
+
operationTimeoutMs: transport.operationTimeoutMs,
|
|
20
|
+
retrieveSession: (id, requestOptions) => this.sessions.retrieve(id, requestOptions),
|
|
21
|
+
retrieveSurface: (id, revision, requestOptions) => this.sessions.retrieveSurface(id, revision, requestOptions),
|
|
22
|
+
publicGet: (path, requestOptions) => transport.request({
|
|
23
|
+
name: 'receipts.keys',
|
|
24
|
+
method: 'GET',
|
|
25
|
+
path,
|
|
26
|
+
status: 200,
|
|
27
|
+
retry: 'safe',
|
|
28
|
+
authenticated: false,
|
|
29
|
+
parse: (value) => value,
|
|
30
|
+
}, requestOptions),
|
|
31
|
+
});
|
|
32
|
+
Object.freeze(this);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Asks one question with simple typed fields. Invalid input fails before credential lookup or
|
|
36
|
+
* network access. The Session is created once with the whole interaction, and the returned
|
|
37
|
+
* handle carries the private link before any wait. `answer()` verifies the answer, checks that it
|
|
38
|
+
* is still current, completes the Session and returns the typed values.
|
|
39
|
+
*/
|
|
40
|
+
ask(input, options) {
|
|
41
|
+
return ask(this.sessions, this.receipts, input, options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { InteractiveInteraction } from '../types.js';
|
|
2
|
+
import { type ItemsInput } from './items.js';
|
|
3
|
+
type Action = InteractiveInteraction['actions'][number];
|
|
4
|
+
type RecordValue = string | number | boolean | null;
|
|
5
|
+
/** Common input of the five builders: the items input plus the page around it. */
|
|
6
|
+
export interface BillInput extends ItemsInput {
|
|
7
|
+
/** The page's heading, about 40 characters; at most 80. Each builder has a default. */
|
|
8
|
+
readonly title?: string;
|
|
9
|
+
/** Optional context under the title, one or two sentences; at most 280. */
|
|
10
|
+
readonly description?: string;
|
|
11
|
+
/** Advanced: the BCP 47 locale the page formats amounts in (default: the page's language). */
|
|
12
|
+
readonly locale?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The document's number (invoice, receipt, quote, refund or purchase order number), shown in a
|
|
15
|
+
* record under its plain name, such as `Invoice` → `INV-2041`; at most 200.
|
|
16
|
+
*/
|
|
17
|
+
readonly reference?: string;
|
|
18
|
+
/** Other facts beside the bill, shown verbatim in the same record (`Due`, `Valid until` …). */
|
|
19
|
+
readonly record?: Readonly<Record<string, RecordValue>>;
|
|
20
|
+
/**
|
|
21
|
+
* Advanced: the buttons, replacing the builder's (1–5). A bill with editable quantities needs
|
|
22
|
+
* one with `intent: 'request_change'`.
|
|
23
|
+
*/
|
|
24
|
+
readonly actions?: readonly Action[];
|
|
25
|
+
}
|
|
26
|
+
/** A refund: positive amounts under refund labels, never a negative total. No payments. */
|
|
27
|
+
export type RefundInput = Omit<BillInput, 'payments'>;
|
|
28
|
+
/**
|
|
29
|
+
* The words a builder puts on the page when you give none (proposed copy; `Request changes` is
|
|
30
|
+
* Sess's signed label for a change request). Every one can be replaced through the input.
|
|
31
|
+
*/
|
|
32
|
+
export declare const COMMERCE_DEFAULTS: Readonly<{
|
|
33
|
+
titles: Readonly<{
|
|
34
|
+
invoice: "Approve this invoice?";
|
|
35
|
+
receipt: "Your receipt";
|
|
36
|
+
quote: "Accept this quote?";
|
|
37
|
+
refund: "Approve the refund?";
|
|
38
|
+
purchaseOrder: "Approve the order?";
|
|
39
|
+
}>;
|
|
40
|
+
references: Readonly<{
|
|
41
|
+
invoice: "Invoice";
|
|
42
|
+
receipt: "Receipt";
|
|
43
|
+
quote: "Quote";
|
|
44
|
+
refund: "Refund";
|
|
45
|
+
purchaseOrder: "Purchase order";
|
|
46
|
+
}>;
|
|
47
|
+
actions: Readonly<{
|
|
48
|
+
approve: Readonly<{
|
|
49
|
+
id: "approve";
|
|
50
|
+
label: "Approve";
|
|
51
|
+
intent: "approve";
|
|
52
|
+
}>;
|
|
53
|
+
accept: Readonly<{
|
|
54
|
+
id: "accept";
|
|
55
|
+
label: "Accept";
|
|
56
|
+
intent: "approve";
|
|
57
|
+
}>;
|
|
58
|
+
continue: Readonly<{
|
|
59
|
+
id: "continue";
|
|
60
|
+
label: "Continue";
|
|
61
|
+
intent: "continue";
|
|
62
|
+
}>;
|
|
63
|
+
decline: Readonly<{
|
|
64
|
+
id: "decline";
|
|
65
|
+
label: "Decline";
|
|
66
|
+
intent: "reject";
|
|
67
|
+
}>;
|
|
68
|
+
change: Readonly<{
|
|
69
|
+
id: "change";
|
|
70
|
+
label: "Request changes";
|
|
71
|
+
intent: "request_change";
|
|
72
|
+
}>;
|
|
73
|
+
}>;
|
|
74
|
+
refundTotal: "Refund total";
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* An invoice to approve: the items card with its totals, `Decline` and `Approve` (and
|
|
78
|
+
* `Request changes` when a row has an editable quantity). Returns an `interaction` for
|
|
79
|
+
* `sess.sessions.create({ interaction })`. Sess takes no payment: approval is the person's
|
|
80
|
+
* decision and its signed receipt; collecting the money stays with your payment processor.
|
|
81
|
+
*/
|
|
82
|
+
export declare function invoice(input: BillInput): InteractiveInteraction;
|
|
83
|
+
/** A receipt or order confirmation for the person to check, with one `Continue` button. */
|
|
84
|
+
export declare function receipt(input: BillInput): InteractiveInteraction;
|
|
85
|
+
/** A quote or estimate to accept: `Decline` and `Accept`. Put its validity in `record`. */
|
|
86
|
+
export declare function quote(input: BillInput): InteractiveInteraction;
|
|
87
|
+
/**
|
|
88
|
+
* A refund to approve, stated in positive amounts: every row is zero or more and the total,
|
|
89
|
+
* labelled `Refund total`, is never negative. A deduction (such as a restocking fee) is a
|
|
90
|
+
* negative line. With `select: 'many'` the person chooses the rows to refund.
|
|
91
|
+
*/
|
|
92
|
+
export declare function refund(input: RefundInput): InteractiveInteraction;
|
|
93
|
+
/**
|
|
94
|
+
* A purchase order to approve. A row with a `quantity` lets the person change it; the order
|
|
95
|
+
* always offers `Request changes` (a changed quantity is sent only with it, and your agent replies
|
|
96
|
+
* with a new revision and updated amounts) beside `Decline` and `Approve`.
|
|
97
|
+
*/
|
|
98
|
+
export declare function purchaseOrder(input: BillInput): InteractiveInteraction;
|
|
99
|
+
export {};
|