@relai-team/access-sdk 0.12.4 → 0.13.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 +68 -14
- package/dist/{chunk-MNXTMLX2.js → chunk-U4CSAG73.js} +15 -4
- package/dist/errors-N4TYPXJ4.js +1 -0
- package/dist/index.cjs +93 -9
- package/dist/index.d.cts +225 -4
- package/dist/index.d.ts +225 -4
- package/dist/index.js +79 -10
- package/package.json +2 -1
- package/dist/errors-L7B45UIZ.js +0 -1
package/README.md
CHANGED
|
@@ -248,7 +248,7 @@ the whole exchange. A payment-gated open answers 402 `payment_required`, and
|
|
|
248
248
|
by itself.
|
|
249
249
|
|
|
250
250
|
Install the peer and hand the module to the client. **You import it, the SDK
|
|
251
|
-
does not
|
|
251
|
+
does not**. React Native resolves imports at build time, so a specifier
|
|
252
252
|
written inside this package would force Stripe on every app, including the
|
|
253
253
|
ones that want their own payment UI:
|
|
254
254
|
|
|
@@ -371,24 +371,78 @@ authenticated path and its reject cases. Sandbox keys only.
|
|
|
371
371
|
## Push notifications
|
|
372
372
|
|
|
373
373
|
Register the end-user's device push token so drop-off / pick-up events notify
|
|
374
|
-
them (delivery is enabled server-side per app):
|
|
374
|
+
them (delivery is enabled server-side per app). Requires a signed-in end user:
|
|
375
375
|
|
|
376
376
|
```ts
|
|
377
|
-
await
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
377
|
+
await relai.push.register(expoPushToken); // platform defaults to "expo"
|
|
378
|
+
await relai.push.unregister(expoPushToken); // on sign-out
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
Safe to call on every launch: re-registering the same token updates it rather
|
|
382
|
+
than duplicating it. Unregister on sign-out, or the next person to use the
|
|
383
|
+
device gets their alerts.
|
|
384
|
+
|
|
385
|
+
This replaces the hand-rolled `fetch` this section used to show (0.13.0). That
|
|
386
|
+
snippet sent only a Bearer token, so it answered 401 `missing_publishable_key`
|
|
387
|
+
while the reader held a perfectly valid session: the route authenticates the app
|
|
388
|
+
with the API key FIRST and the person with the bearer second. `relai.push` sends
|
|
389
|
+
both, because the client already holds both.
|
|
390
|
+
|
|
391
|
+
## Sandbox fault injection
|
|
392
|
+
|
|
393
|
+
Exercise the unhappy paths without hardware. Sandbox keys only; the platform
|
|
394
|
+
answers `forbidden` for a production key and `not_simulated` for an Exchange
|
|
395
|
+
Zone that is not simulated.
|
|
396
|
+
|
|
397
|
+
```ts
|
|
398
|
+
await relai.sandbox.injectFault({
|
|
399
|
+
exchangeZoneId: "ez_123",
|
|
400
|
+
fault: "jammed_door",
|
|
401
|
+
nodeId: "cmp_456", // required for jammed_door
|
|
388
402
|
});
|
|
389
|
-
|
|
403
|
+
|
|
404
|
+
await relai.sandbox.injectFault({ exchangeZoneId: "ez_123", fault: "dead_battery" });
|
|
405
|
+
await relai.sandbox.clearFault({ exchangeZoneId: "ez_123", nodeId: "cmp_456" });
|
|
390
406
|
```
|
|
391
407
|
|
|
408
|
+
Faults: `jammed_door`, `dead_battery`, `out_of_service`, and `clear`.
|
|
409
|
+
|
|
410
|
+
## Knowing your own app
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
const app = await relai.app.self();
|
|
414
|
+
app.plan; // { id, name } or null when unmetered
|
|
415
|
+
app.real_token_balance; // remaining real opens, or null when unmetered
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Worth calling at launch. Without it the first sign that an app is out of real
|
|
419
|
+
opens is a door that did not open, which is both the worst moment to find out
|
|
420
|
+
and the worst possible messenger. Warn while there is still time to top up.
|
|
421
|
+
|
|
422
|
+
## Handling webhooks
|
|
423
|
+
|
|
424
|
+
`WebhookEvent` and its payload map are exported, so a partner backend can switch
|
|
425
|
+
on the event type instead of hand-casting `any`:
|
|
426
|
+
|
|
427
|
+
```ts
|
|
428
|
+
import { verifyWebhook, type AnyWebhookEvent } from "@relai-team/access-sdk";
|
|
429
|
+
|
|
430
|
+
const event = JSON.parse(body) as AnyWebhookEvent;
|
|
431
|
+
switch (event.type) {
|
|
432
|
+
case "order.completed":
|
|
433
|
+
event.data.order; // typed
|
|
434
|
+
event.data.reason; // "node_out_of_service" when the platform completed it
|
|
435
|
+
break;
|
|
436
|
+
case "order.abandoned":
|
|
437
|
+
event.data.order_id;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
There is deliberately no `payment.authorized`: Relai checkout intents capture
|
|
443
|
+
immediately, so success IS the capture. The published contract listed one until
|
|
444
|
+
0.13.0, and omitted `order.abandoned`, which really is delivered.
|
|
445
|
+
|
|
392
446
|
## Offline-durable telemetry
|
|
393
447
|
|
|
394
448
|
Every open reports best-effort telemetry. If the network is down when a node
|
|
@@ -6,18 +6,29 @@ var RelaiError = class extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
};
|
|
8
8
|
var RelaiApiError = class extends RelaiError {
|
|
9
|
-
constructor(status, body) {
|
|
9
|
+
constructor(status, body, ctx = {}) {
|
|
10
10
|
super(body.error.message);
|
|
11
11
|
this.name = "RelaiApiError";
|
|
12
12
|
this.code = body.error.code;
|
|
13
13
|
this.status = status;
|
|
14
14
|
this.docsUrl = body.error.docs_url;
|
|
15
15
|
this.details = body.error.details;
|
|
16
|
+
this.requestId = ctx.requestId;
|
|
17
|
+
this.retryAfterSeconds = ctx.retryAfterSeconds;
|
|
18
|
+
this.method = ctx.method;
|
|
19
|
+
this.path = ctx.path;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var RelaiNetworkError = class extends RelaiApiError {
|
|
23
|
+
constructor(message, ctx = {}, cause) {
|
|
24
|
+
super(0, { error: { code: "network_error", message } }, ctx);
|
|
25
|
+
this.name = "RelaiNetworkError";
|
|
26
|
+
this.cause = cause;
|
|
16
27
|
}
|
|
17
28
|
};
|
|
18
29
|
var RelaiLateFeeDue = class extends RelaiApiError {
|
|
19
|
-
constructor(status, body) {
|
|
20
|
-
super(status, body);
|
|
30
|
+
constructor(status, body, ctx = {}) {
|
|
31
|
+
super(status, body, ctx);
|
|
21
32
|
this.feeCents = typeof this.details?.["fee_cents"] === "number" ? this.details["fee_cents"] : 0;
|
|
22
33
|
const cs = this.details?.["client_secret"];
|
|
23
34
|
this.clientSecret = typeof cs === "string" ? cs : null;
|
|
@@ -47,4 +58,4 @@ var RelaiTransportError = class extends RelaiError {
|
|
|
47
58
|
}
|
|
48
59
|
};
|
|
49
60
|
|
|
50
|
-
export { RelaiApiError, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiTransportError, RelaiUnlockRejected };
|
|
61
|
+
export { RelaiApiError, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiNetworkError, RelaiTransportError, RelaiUnlockRejected };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RelaiApiError, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiNetworkError, RelaiTransportError, RelaiUnlockRejected } from './chunk-U4CSAG73.js';
|
package/dist/index.cjs
CHANGED
|
@@ -17,10 +17,11 @@ __export(errors_exports, {
|
|
|
17
17
|
RelaiError: () => exports.RelaiError,
|
|
18
18
|
RelaiLateFeeDue: () => exports.RelaiLateFeeDue,
|
|
19
19
|
RelaiMissingKeyError: () => exports.RelaiMissingKeyError,
|
|
20
|
+
RelaiNetworkError: () => exports.RelaiNetworkError,
|
|
20
21
|
RelaiTransportError: () => exports.RelaiTransportError,
|
|
21
22
|
RelaiUnlockRejected: () => exports.RelaiUnlockRejected
|
|
22
23
|
});
|
|
23
|
-
exports.RelaiError = void 0; exports.RelaiApiError = void 0; exports.RelaiLateFeeDue = void 0; exports.RelaiMissingKeyError = void 0; exports.RelaiUnlockRejected = void 0; exports.RelaiTransportError = void 0;
|
|
24
|
+
exports.RelaiError = void 0; exports.RelaiApiError = void 0; exports.RelaiNetworkError = void 0; exports.RelaiLateFeeDue = void 0; exports.RelaiMissingKeyError = void 0; exports.RelaiUnlockRejected = void 0; exports.RelaiTransportError = void 0;
|
|
24
25
|
var init_errors = __esm({
|
|
25
26
|
"src/errors.ts"() {
|
|
26
27
|
exports.RelaiError = class extends Error {
|
|
@@ -30,18 +31,29 @@ var init_errors = __esm({
|
|
|
30
31
|
}
|
|
31
32
|
};
|
|
32
33
|
exports.RelaiApiError = class extends exports.RelaiError {
|
|
33
|
-
constructor(status, body) {
|
|
34
|
+
constructor(status, body, ctx = {}) {
|
|
34
35
|
super(body.error.message);
|
|
35
36
|
this.name = "RelaiApiError";
|
|
36
37
|
this.code = body.error.code;
|
|
37
38
|
this.status = status;
|
|
38
39
|
this.docsUrl = body.error.docs_url;
|
|
39
40
|
this.details = body.error.details;
|
|
41
|
+
this.requestId = ctx.requestId;
|
|
42
|
+
this.retryAfterSeconds = ctx.retryAfterSeconds;
|
|
43
|
+
this.method = ctx.method;
|
|
44
|
+
this.path = ctx.path;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
exports.RelaiNetworkError = class extends exports.RelaiApiError {
|
|
48
|
+
constructor(message, ctx = {}, cause) {
|
|
49
|
+
super(0, { error: { code: "network_error", message } }, ctx);
|
|
50
|
+
this.name = "RelaiNetworkError";
|
|
51
|
+
this.cause = cause;
|
|
40
52
|
}
|
|
41
53
|
};
|
|
42
54
|
exports.RelaiLateFeeDue = class extends exports.RelaiApiError {
|
|
43
|
-
constructor(status, body) {
|
|
44
|
-
super(status, body);
|
|
55
|
+
constructor(status, body, ctx = {}) {
|
|
56
|
+
super(status, body, ctx);
|
|
45
57
|
this.feeCents = typeof this.details?.["fee_cents"] === "number" ? this.details["fee_cents"] : 0;
|
|
46
58
|
const cs = this.details?.["client_secret"];
|
|
47
59
|
this.clientSecret = typeof cs === "string" ? cs : null;
|
|
@@ -164,14 +176,18 @@ var HttpClient = class {
|
|
|
164
176
|
await sleep(backoffMs(i));
|
|
165
177
|
continue;
|
|
166
178
|
}
|
|
167
|
-
throw new exports.
|
|
179
|
+
throw new exports.RelaiNetworkError(
|
|
180
|
+
`Network request failed: ${netErr.message}`,
|
|
181
|
+
{ method: opts.method ?? "GET", path: opts.path },
|
|
182
|
+
netErr
|
|
183
|
+
);
|
|
168
184
|
}
|
|
169
185
|
if (res.ok) return await this.parseJson(res);
|
|
170
186
|
if (RETRYABLE_STATUS.has(res.status) && i < this.cfg.maxRetries) {
|
|
171
187
|
await sleep(retryDelayMs(res, i));
|
|
172
188
|
continue;
|
|
173
189
|
}
|
|
174
|
-
const apiErr = await this.toApiError(res);
|
|
190
|
+
const apiErr = await this.toApiError(res, opts);
|
|
175
191
|
if (apiErr.code === "idempotency_in_progress" && i < this.cfg.maxRetries) {
|
|
176
192
|
await sleep(retryDelayMs(res, i));
|
|
177
193
|
continue;
|
|
@@ -209,7 +225,7 @@ var HttpClient = class {
|
|
|
209
225
|
throw new exports.RelaiError(`Expected JSON from ${res.url} but got: ${text.slice(0, 120)}`);
|
|
210
226
|
}
|
|
211
227
|
}
|
|
212
|
-
async toApiError(res) {
|
|
228
|
+
async toApiError(res, opts) {
|
|
213
229
|
let body;
|
|
214
230
|
try {
|
|
215
231
|
body = await this.parseJson(res);
|
|
@@ -217,11 +233,18 @@ var HttpClient = class {
|
|
|
217
233
|
body = { error: { code: "http_error", message: `HTTP ${res.status}` } };
|
|
218
234
|
}
|
|
219
235
|
if (!body?.error?.code) body = { error: { code: "http_error", message: `HTTP ${res.status}` } };
|
|
236
|
+
const ra = Number(res.headers.get("retry-after"));
|
|
237
|
+
const ctx = {
|
|
238
|
+
requestId: res.headers.get("x-relai-request-id") ?? void 0,
|
|
239
|
+
retryAfterSeconds: Number.isFinite(ra) && res.headers.get("retry-after") !== null ? ra : void 0,
|
|
240
|
+
method: opts.method ?? "GET",
|
|
241
|
+
path: opts.path
|
|
242
|
+
};
|
|
220
243
|
if (body.error.code === "late_fee_due") {
|
|
221
244
|
const { RelaiLateFeeDue: RelaiLateFeeDue2 } = await Promise.resolve().then(() => (init_errors(), errors_exports));
|
|
222
|
-
return new RelaiLateFeeDue2(res.status, body);
|
|
245
|
+
return new RelaiLateFeeDue2(res.status, body, ctx);
|
|
223
246
|
}
|
|
224
|
-
return new exports.RelaiApiError(res.status, body);
|
|
247
|
+
return new exports.RelaiApiError(res.status, body, ctx);
|
|
225
248
|
}
|
|
226
249
|
};
|
|
227
250
|
function backoffMs(attempt) {
|
|
@@ -512,6 +535,61 @@ var TelemetryModule = class {
|
|
|
512
535
|
return this.http.request({ method: "POST", path: "/telemetry", body });
|
|
513
536
|
}
|
|
514
537
|
};
|
|
538
|
+
var PushModule = class {
|
|
539
|
+
constructor(http) {
|
|
540
|
+
this.http = http;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Register this device for handoff and pick-up notifications. Requires a
|
|
544
|
+
* signed-in end user. Safe to call on every launch: re-registering the same
|
|
545
|
+
* token updates it rather than duplicating it.
|
|
546
|
+
*/
|
|
547
|
+
register(token, platform = "expo") {
|
|
548
|
+
return this.http.request({
|
|
549
|
+
method: "POST",
|
|
550
|
+
path: "/push/tokens",
|
|
551
|
+
body: { token, platform }
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
/** Unregister on sign-out, or the next person to use the device gets their alerts. */
|
|
555
|
+
unregister(token) {
|
|
556
|
+
return this.http.request({
|
|
557
|
+
method: "DELETE",
|
|
558
|
+
path: "/push/tokens",
|
|
559
|
+
body: { token }
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
var SandboxModule = class {
|
|
564
|
+
constructor(http) {
|
|
565
|
+
this.http = http;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Force a fault so the app's unhappy paths can be exercised without hardware.
|
|
569
|
+
* `jammed_door` needs a `nodeId`; `dead_battery` applies to the whole
|
|
570
|
+
* Exchange Zone.
|
|
571
|
+
*/
|
|
572
|
+
injectFault(input) {
|
|
573
|
+
const body = { ez_id: input.exchangeZoneId, fault: input.fault };
|
|
574
|
+
if (input.nodeId !== void 0) body["node_id"] = input.nodeId;
|
|
575
|
+
return this.http.request({ method: "POST", path: "/sandbox/faults", body });
|
|
576
|
+
}
|
|
577
|
+
/** Clear an injected fault and return the Exchange Zone to service. */
|
|
578
|
+
clearFault(input) {
|
|
579
|
+
const body = { ez_id: input.exchangeZoneId, fault: "clear" };
|
|
580
|
+
if (input.nodeId !== void 0) body["node_id"] = input.nodeId;
|
|
581
|
+
return this.http.request({ method: "POST", path: "/sandbox/faults", body });
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
var AppModule = class {
|
|
585
|
+
constructor(http) {
|
|
586
|
+
this.http = http;
|
|
587
|
+
}
|
|
588
|
+
/** Config for the app the current key belongs to. */
|
|
589
|
+
self() {
|
|
590
|
+
return this.http.request({ path: "/apps/self" });
|
|
591
|
+
}
|
|
592
|
+
};
|
|
515
593
|
|
|
516
594
|
// src/telemetry-uploader.ts
|
|
517
595
|
init_errors();
|
|
@@ -676,6 +754,9 @@ var RelaiClient = class {
|
|
|
676
754
|
this.overrideLinks = new OverrideLinksModule(this.http);
|
|
677
755
|
this.endUsers = new EndUsersModule(this.http);
|
|
678
756
|
this.telemetry = new TelemetryModule(this.http);
|
|
757
|
+
this.push = new PushModule(this.http);
|
|
758
|
+
this.sandbox = new SandboxModule(this.http);
|
|
759
|
+
this.app = new AppModule(this.http);
|
|
679
760
|
this.telemetryUploader = new TelemetryUploader(this.telemetry, options.telemetryQueue);
|
|
680
761
|
}
|
|
681
762
|
/** Drain any telemetry queued after a failed send (e.g. on app foreground). */
|
|
@@ -1122,6 +1203,7 @@ var Intents = {
|
|
|
1122
1203
|
};
|
|
1123
1204
|
|
|
1124
1205
|
exports.AccessLinksModule = AccessLinksModule;
|
|
1206
|
+
exports.AppModule = AppModule;
|
|
1125
1207
|
exports.AuthModule = AuthModule;
|
|
1126
1208
|
exports.BleTransport = BleTransport;
|
|
1127
1209
|
exports.EndUsersModule = EndUsersModule;
|
|
@@ -1132,12 +1214,14 @@ exports.MemoryTelemetryQueue = MemoryTelemetryQueue;
|
|
|
1132
1214
|
exports.MemoryTokenStorage = MemoryTokenStorage;
|
|
1133
1215
|
exports.OrdersModule = OrdersModule;
|
|
1134
1216
|
exports.OverrideLinksModule = OverrideLinksModule;
|
|
1217
|
+
exports.PushModule = PushModule;
|
|
1135
1218
|
exports.REJECT_REASONS = REJECT_REASONS;
|
|
1136
1219
|
exports.RELAI_RX_CHAR = RELAI_RX_CHAR;
|
|
1137
1220
|
exports.RELAI_TX_CHAR = RELAI_TX_CHAR;
|
|
1138
1221
|
exports.RELAI_UART_SERVICE = RELAI_UART_SERVICE;
|
|
1139
1222
|
exports.RecipientsModule = RecipientsModule;
|
|
1140
1223
|
exports.RelaiClient = RelaiClient;
|
|
1224
|
+
exports.SandboxModule = SandboxModule;
|
|
1141
1225
|
exports.SimAuthenticatedTransport = SimAuthenticatedTransport;
|
|
1142
1226
|
exports.SimulatedTransport = SimulatedTransport;
|
|
1143
1227
|
exports.StorageTelemetryQueue = StorageTelemetryQueue;
|
package/dist/index.d.cts
CHANGED
|
@@ -307,6 +307,111 @@ interface TelemetryEvent {
|
|
|
307
307
|
transport?: "ble" | "simulated";
|
|
308
308
|
at: string;
|
|
309
309
|
}
|
|
310
|
+
type PushPlatform = "expo" | "ios" | "android" | "web";
|
|
311
|
+
type SandboxFault = "jammed_door" | "dead_battery" | "out_of_service" | "clear";
|
|
312
|
+
interface SandboxFaultResult {
|
|
313
|
+
ez_id: string;
|
|
314
|
+
node_id: string | null;
|
|
315
|
+
fault: SandboxFault;
|
|
316
|
+
}
|
|
317
|
+
interface AppSelf {
|
|
318
|
+
id: string;
|
|
319
|
+
name: string;
|
|
320
|
+
environment: ApiEnvironment;
|
|
321
|
+
feature_flags: Record<string, unknown>;
|
|
322
|
+
webhook_url: string | null;
|
|
323
|
+
/** Null when the app is unmetered (no active subscription). */
|
|
324
|
+
plan: {
|
|
325
|
+
id: string;
|
|
326
|
+
name: string;
|
|
327
|
+
} | null;
|
|
328
|
+
/** Remaining real opens. Null when unmetered. Warn the user before this hits zero. */
|
|
329
|
+
real_token_balance: number | null;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Every event the platform can deliver.
|
|
333
|
+
*
|
|
334
|
+
* The SDK had no webhook type at all, so every partner backend hand-cast at the
|
|
335
|
+
* one boundary where a wrong guess is silent: an unhandled event is simply
|
|
336
|
+
* ignored, and a misspelled one never fires. The published contract disagreed
|
|
337
|
+
* with reality in both directions when this was written (it listed a
|
|
338
|
+
* `payment.authorized` that nothing sends, and omitted `order.abandoned`, which
|
|
339
|
+
* the hourly sweep really does send), so these names are now pinned by a test in
|
|
340
|
+
* the platform repo against `lib/webhooks.ts`, which is what `emitWebhook`
|
|
341
|
+
* accepts.
|
|
342
|
+
*
|
|
343
|
+
* There is deliberately no `payment.authorized`: Relai checkout intents capture
|
|
344
|
+
* immediately, so success IS the capture.
|
|
345
|
+
*/
|
|
346
|
+
type WebhookEventType = "order.created" | "order.opened" | "order.completed" | "order.cancelled" | "order.refunded" | "order.abandoned" | "payment.captured" | "payment.failed" | "node.out_of_service" | "ez.status_changed";
|
|
347
|
+
/**
|
|
348
|
+
* The envelope every delivery arrives in.
|
|
349
|
+
*
|
|
350
|
+
* `K` is the discriminant, and it has to be a parameter rather than the whole
|
|
351
|
+
* `WebhookEventType` union: with `type: WebhookEventType` on every member,
|
|
352
|
+
* `switch (event.type)` narrows nothing and `event.data` stays the union of all
|
|
353
|
+
* payloads. A consumer typecheck against the built package caught that before
|
|
354
|
+
* this shipped, which is the only reason it is not decoration.
|
|
355
|
+
*/
|
|
356
|
+
interface WebhookEvent<T = Record<string, unknown>, K extends WebhookEventType = WebhookEventType> {
|
|
357
|
+
id: string;
|
|
358
|
+
type: K;
|
|
359
|
+
created_at: string;
|
|
360
|
+
data: T;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Payload per event type, so `switch (event.type)` narrows `event.data`.
|
|
364
|
+
*
|
|
365
|
+
* `order.completed` carries `reason` only when the platform completed the order
|
|
366
|
+
* itself rather than a person doing it: today that means a node forced out of
|
|
367
|
+
* service. It used to ship two incompatible shapes depending on which code path
|
|
368
|
+
* fired, with nothing saying so; one shape now, `reason` additive.
|
|
369
|
+
*/
|
|
370
|
+
interface WebhookPayloads {
|
|
371
|
+
"order.created": {
|
|
372
|
+
order: Order;
|
|
373
|
+
};
|
|
374
|
+
"order.opened": {
|
|
375
|
+
order: Order;
|
|
376
|
+
open: Open;
|
|
377
|
+
};
|
|
378
|
+
"order.completed": {
|
|
379
|
+
order: Order;
|
|
380
|
+
reason?: "node_out_of_service";
|
|
381
|
+
};
|
|
382
|
+
"order.cancelled": {
|
|
383
|
+
order: Order;
|
|
384
|
+
};
|
|
385
|
+
"order.refunded": {
|
|
386
|
+
order: Order;
|
|
387
|
+
};
|
|
388
|
+
"order.abandoned": {
|
|
389
|
+
order_id: string;
|
|
390
|
+
node_id: string | null;
|
|
391
|
+
};
|
|
392
|
+
"payment.captured": {
|
|
393
|
+
order_id: string;
|
|
394
|
+
amount_cents: number;
|
|
395
|
+
};
|
|
396
|
+
"payment.failed": {
|
|
397
|
+
order_id: string;
|
|
398
|
+
reason?: string;
|
|
399
|
+
};
|
|
400
|
+
"node.out_of_service": {
|
|
401
|
+
node_id: string;
|
|
402
|
+
order_id?: string;
|
|
403
|
+
};
|
|
404
|
+
"ez.status_changed": {
|
|
405
|
+
ez_id: string;
|
|
406
|
+
status: string;
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
/** A delivery narrowed to one event type: the argument to a typed handler. */
|
|
410
|
+
type WebhookEventOf<K extends WebhookEventType> = WebhookEvent<WebhookPayloads[K], K>;
|
|
411
|
+
/** The discriminated union to switch on. */
|
|
412
|
+
type AnyWebhookEvent = {
|
|
413
|
+
[K in WebhookEventType]: WebhookEventOf<K>;
|
|
414
|
+
}[WebhookEventType];
|
|
310
415
|
|
|
311
416
|
/**
|
|
312
417
|
* End-user auth: the OTP flow, rotating refresh (single-flight), and a pluggable
|
|
@@ -562,6 +667,76 @@ declare class TelemetryModule {
|
|
|
562
667
|
ok?: boolean;
|
|
563
668
|
} & Record<string, unknown>>;
|
|
564
669
|
}
|
|
670
|
+
/**
|
|
671
|
+
* Device push tokens.
|
|
672
|
+
*
|
|
673
|
+
* Both docs pages told builders to call `POST /v1/push/tokens` by hand, and the
|
|
674
|
+
* README's snippet sent only a Bearer token, so it 401'd with
|
|
675
|
+
* `missing_publishable_key` while the builder held a perfectly valid session.
|
|
676
|
+
* That is the whole argument for the method existing: the endpoint needs BOTH
|
|
677
|
+
* the API key and the end-user bearer, and the client already knows how to send
|
|
678
|
+
* both.
|
|
679
|
+
*/
|
|
680
|
+
declare class PushModule {
|
|
681
|
+
private readonly http;
|
|
682
|
+
constructor(http: HttpClient);
|
|
683
|
+
/**
|
|
684
|
+
* Register this device for handoff and pick-up notifications. Requires a
|
|
685
|
+
* signed-in end user. Safe to call on every launch: re-registering the same
|
|
686
|
+
* token updates it rather than duplicating it.
|
|
687
|
+
*/
|
|
688
|
+
register(token: string, platform?: PushPlatform): Promise<{
|
|
689
|
+
ok: boolean;
|
|
690
|
+
}>;
|
|
691
|
+
/** Unregister on sign-out, or the next person to use the device gets their alerts. */
|
|
692
|
+
unregister(token: string): Promise<{
|
|
693
|
+
ok: boolean;
|
|
694
|
+
}>;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Sandbox fault injection.
|
|
698
|
+
*
|
|
699
|
+
* `app/api/v1/sandbox/faults/route.ts` has claimed in a comment that it "backs
|
|
700
|
+
* the SDK's `relai.sandbox.injectFault`" since it was written, and no such
|
|
701
|
+
* method existed. That is the same defect as the documented-but-absent
|
|
702
|
+
* `client.me()`, which a builder found for us: something referring confidently
|
|
703
|
+
* to an SDK surface nobody had built.
|
|
704
|
+
*
|
|
705
|
+
* Sandbox keys only. The platform rejects production keys with `forbidden`, and
|
|
706
|
+
* a non-simulated Exchange Zone with `not_simulated`.
|
|
707
|
+
*/
|
|
708
|
+
declare class SandboxModule {
|
|
709
|
+
private readonly http;
|
|
710
|
+
constructor(http: HttpClient);
|
|
711
|
+
/**
|
|
712
|
+
* Force a fault so the app's unhappy paths can be exercised without hardware.
|
|
713
|
+
* `jammed_door` needs a `nodeId`; `dead_battery` applies to the whole
|
|
714
|
+
* Exchange Zone.
|
|
715
|
+
*/
|
|
716
|
+
injectFault(input: {
|
|
717
|
+
exchangeZoneId: string;
|
|
718
|
+
fault: Exclude<SandboxFault, "clear">;
|
|
719
|
+
nodeId?: string;
|
|
720
|
+
}): Promise<SandboxFaultResult>;
|
|
721
|
+
/** Clear an injected fault and return the Exchange Zone to service. */
|
|
722
|
+
clearFault(input: {
|
|
723
|
+
exchangeZoneId: string;
|
|
724
|
+
nodeId?: string;
|
|
725
|
+
}): Promise<SandboxFaultResult>;
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* The calling app itself: configuration, feature flags, plan and token balance.
|
|
729
|
+
*
|
|
730
|
+
* Without this a builder learns they are out of real opens from a door that did
|
|
731
|
+
* not open, which is the worst possible moment and the worst possible messenger.
|
|
732
|
+
* `realTokenBalance` lets an app warn before it gets there.
|
|
733
|
+
*/
|
|
734
|
+
declare class AppModule {
|
|
735
|
+
private readonly http;
|
|
736
|
+
constructor(http: HttpClient);
|
|
737
|
+
/** Config for the app the current key belongs to. */
|
|
738
|
+
self(): Promise<AppSelf>;
|
|
739
|
+
}
|
|
565
740
|
|
|
566
741
|
/**
|
|
567
742
|
* Telemetry queue-and-retry (owner rule 2026-07-23). A delivered-but-unreported
|
|
@@ -856,6 +1031,12 @@ declare class RelaiClient {
|
|
|
856
1031
|
/** SERVER ONLY (secret key): your app's end users, name, email, access tag. */
|
|
857
1032
|
readonly endUsers: EndUsersModule;
|
|
858
1033
|
readonly telemetry: TelemetryModule;
|
|
1034
|
+
/** Device push tokens for handoff and pick-up alerts. Needs a signed-in end user. */
|
|
1035
|
+
readonly push: PushModule;
|
|
1036
|
+
/** SANDBOX ONLY: inject and clear simulated faults, to exercise unhappy paths. */
|
|
1037
|
+
readonly sandbox: SandboxModule;
|
|
1038
|
+
/** The calling app: config, feature flags, plan and remaining real-open balance. */
|
|
1039
|
+
readonly app: AppModule;
|
|
859
1040
|
/** Queue-and-retry telemetry uploader (used by unlock; flush opportunistically). */
|
|
860
1041
|
readonly telemetryUploader: TelemetryUploader;
|
|
861
1042
|
private readonly http;
|
|
@@ -1112,15 +1293,55 @@ declare class RelaiError extends Error {
|
|
|
1112
1293
|
readonly name: string;
|
|
1113
1294
|
constructor(message: string);
|
|
1114
1295
|
}
|
|
1296
|
+
/** Everything a caller needs to report a failed call without guessing. */
|
|
1297
|
+
interface RelaiErrorContext {
|
|
1298
|
+
/** `x-relai-request-id` from the response. Quote it in a support request. */
|
|
1299
|
+
requestId?: string | undefined;
|
|
1300
|
+
/** `Retry-After`, in seconds, when the server sent one. */
|
|
1301
|
+
retryAfterSeconds?: number | undefined;
|
|
1302
|
+
/** HTTP method of the failed request. */
|
|
1303
|
+
method?: string | undefined;
|
|
1304
|
+
/** Path of the failed request, e.g. `/orders/ord_1/opens`. */
|
|
1305
|
+
path?: string | undefined;
|
|
1306
|
+
}
|
|
1115
1307
|
/** A structured error response from the platform. */
|
|
1116
1308
|
declare class RelaiApiError extends RelaiError {
|
|
1117
|
-
readonly name
|
|
1309
|
+
readonly name: string;
|
|
1118
1310
|
readonly code: string;
|
|
1119
1311
|
readonly status: number;
|
|
1120
1312
|
readonly docsUrl: string | undefined;
|
|
1121
1313
|
/** Machine-readable payload for codes that carry one (e.g. late_fee_due). */
|
|
1122
1314
|
readonly details: Record<string, unknown> | undefined;
|
|
1123
|
-
|
|
1315
|
+
/**
|
|
1316
|
+
* The id the platform stamps on EVERY response (`x-relai-request-id`) so a
|
|
1317
|
+
* partner can quote one line and have us find the request. It was being
|
|
1318
|
+
* dropped here, which made the header pointless: the server went to the
|
|
1319
|
+
* trouble of sending it and the SDK threw it away before anyone saw it.
|
|
1320
|
+
*/
|
|
1321
|
+
readonly requestId: string | undefined;
|
|
1322
|
+
/** `Retry-After` in seconds. Used internally for backoff and then discarded
|
|
1323
|
+
* until now, so callers implementing their own queueing had to re-parse it. */
|
|
1324
|
+
readonly retryAfterSeconds: number | undefined;
|
|
1325
|
+
readonly method: string | undefined;
|
|
1326
|
+
readonly path: string | undefined;
|
|
1327
|
+
constructor(status: number, body: ApiErrorBody, ctx?: RelaiErrorContext);
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* The request never reached the platform: no connection, DNS failure, TLS
|
|
1331
|
+
* failure, or the socket died mid-flight.
|
|
1332
|
+
*
|
|
1333
|
+
* This is the SINGLE most common failure on mobile and it used to throw a bare
|
|
1334
|
+
* `RelaiError` carrying nothing but a message string. Callers had no `code` to
|
|
1335
|
+
* branch on and no way to tell "you are offline, try again" apart from "the
|
|
1336
|
+
* platform refused this, do not retry", so the honest handler had to substring
|
|
1337
|
+
* match the message. It extends `RelaiApiError` so `catch (e) { e.code }` works
|
|
1338
|
+
* uniformly, with `status = 0` standing for "no response was ever received".
|
|
1339
|
+
*/
|
|
1340
|
+
declare class RelaiNetworkError extends RelaiApiError {
|
|
1341
|
+
readonly name = "RelaiNetworkError";
|
|
1342
|
+
/** The underlying fetch rejection, when the runtime gave us one. */
|
|
1343
|
+
readonly cause: unknown;
|
|
1344
|
+
constructor(message: string, ctx?: RelaiErrorContext, cause?: unknown);
|
|
1124
1345
|
}
|
|
1125
1346
|
/**
|
|
1126
1347
|
* `late_fee_due` (402): a late pick-up fee must be paid before the node opens.
|
|
@@ -1132,7 +1353,7 @@ declare class RelaiApiError extends RelaiError {
|
|
|
1132
1353
|
declare class RelaiLateFeeDue extends RelaiApiError {
|
|
1133
1354
|
readonly feeCents: number;
|
|
1134
1355
|
readonly clientSecret: string | null;
|
|
1135
|
-
constructor(status: number, body: ApiErrorBody);
|
|
1356
|
+
constructor(status: number, body: ApiErrorBody, ctx?: RelaiErrorContext);
|
|
1136
1357
|
}
|
|
1137
1358
|
/**
|
|
1138
1359
|
* The "doorway" error: the SDK was called with no publishable key. Thrown
|
|
@@ -1163,4 +1384,4 @@ declare class RelaiTransportError extends RelaiError {
|
|
|
1163
1384
|
constructor(message: string);
|
|
1164
1385
|
}
|
|
1165
1386
|
|
|
1166
|
-
export { type AccessLinkTarget, AccessLinksModule, type ApiEnvironment, type ApiErrorBody, AuthModule, type BleDeviceLike, type BleManagerLike, BleTransport, type BleTransportOptions, type CommandAck, type DeliverOptions, type DiscoveredParent, type EndUser, type EndUserPage, EndUsersModule, type ExchangeZone, type ExchangeZoneDetail, type ExchangeZonePage, ExchangeZonesModule, type GrantAlgorithm, GrantsModule, type HandoffMode, type Intent, Intents, type KeyValueStorage, type Me, MemoryTelemetryQueue, MemoryTokenStorage, type MintedCommand, type NotifyLine, type Open, type Order, type OrderStatusFields, OrdersModule, type OverrideLink, OverrideLinksModule, type ParentTransport, type PaymentRequiredDetails, type QueuedTelemetry, REJECT_REASONS, RELAI_RX_CHAR, RELAI_TX_CHAR, RELAI_UART_SERVICE, type RecipientMatch, RecipientsModule, RelaiApiError, RelaiClient, type RelaiClientOptions, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiTransportError, RelaiUnlockRejected, type SelfBillingChoice, type Session, SimAuthenticatedTransport, type SimAuthenticatedTransportOptions, type SimulatedParentConfig, SimulatedTransport, StorageTelemetryQueue, type StorageTelemetryQueueOptions, type StripeSdk, type TelemetryAction, type TelemetryEvent, TelemetryModule, type TelemetryPayload, type TelemetryQueue, TelemetryUploader, type TokenPair, type TokenStorage, type Tower, type TowerNode, type TowerTelemetry, type UnlockDeps, type UnlockGrant, type UnlockInput, type UnlockResult, type VerifyWebhookInput, asPaymentRequired, deliverOpen, parseNotifyLine, presentRelaiPaymentSheet, verifyWebhook };
|
|
1387
|
+
export { type AccessLinkTarget, AccessLinksModule, type AnyWebhookEvent, type ApiEnvironment, type ApiErrorBody, AppModule, type AppSelf, AuthModule, type BleDeviceLike, type BleManagerLike, BleTransport, type BleTransportOptions, type CommandAck, type DeliverOptions, type DiscoveredParent, type EndUser, type EndUserPage, EndUsersModule, type ExchangeZone, type ExchangeZoneDetail, type ExchangeZonePage, ExchangeZonesModule, type GrantAlgorithm, GrantsModule, type HandoffMode, type Intent, Intents, type KeyValueStorage, type Me, MemoryTelemetryQueue, MemoryTokenStorage, type MintedCommand, type NotifyLine, type Open, type Order, type OrderStatusFields, OrdersModule, type OverrideLink, OverrideLinksModule, type ParentTransport, type PaymentRequiredDetails, PushModule, type PushPlatform, type QueuedTelemetry, REJECT_REASONS, RELAI_RX_CHAR, RELAI_TX_CHAR, RELAI_UART_SERVICE, type RecipientMatch, RecipientsModule, RelaiApiError, RelaiClient, type RelaiClientOptions, RelaiError, type RelaiErrorContext, RelaiLateFeeDue, RelaiMissingKeyError, RelaiNetworkError, RelaiTransportError, RelaiUnlockRejected, type SandboxFault, type SandboxFaultResult, SandboxModule, type SelfBillingChoice, type Session, SimAuthenticatedTransport, type SimAuthenticatedTransportOptions, type SimulatedParentConfig, SimulatedTransport, StorageTelemetryQueue, type StorageTelemetryQueueOptions, type StripeSdk, type TelemetryAction, type TelemetryEvent, TelemetryModule, type TelemetryPayload, type TelemetryQueue, TelemetryUploader, type TokenPair, type TokenStorage, type Tower, type TowerNode, type TowerTelemetry, type UnlockDeps, type UnlockGrant, type UnlockInput, type UnlockResult, type VerifyWebhookInput, type WebhookEvent, type WebhookEventOf, type WebhookEventType, type WebhookPayloads, asPaymentRequired, deliverOpen, parseNotifyLine, presentRelaiPaymentSheet, verifyWebhook };
|
package/dist/index.d.ts
CHANGED
|
@@ -307,6 +307,111 @@ interface TelemetryEvent {
|
|
|
307
307
|
transport?: "ble" | "simulated";
|
|
308
308
|
at: string;
|
|
309
309
|
}
|
|
310
|
+
type PushPlatform = "expo" | "ios" | "android" | "web";
|
|
311
|
+
type SandboxFault = "jammed_door" | "dead_battery" | "out_of_service" | "clear";
|
|
312
|
+
interface SandboxFaultResult {
|
|
313
|
+
ez_id: string;
|
|
314
|
+
node_id: string | null;
|
|
315
|
+
fault: SandboxFault;
|
|
316
|
+
}
|
|
317
|
+
interface AppSelf {
|
|
318
|
+
id: string;
|
|
319
|
+
name: string;
|
|
320
|
+
environment: ApiEnvironment;
|
|
321
|
+
feature_flags: Record<string, unknown>;
|
|
322
|
+
webhook_url: string | null;
|
|
323
|
+
/** Null when the app is unmetered (no active subscription). */
|
|
324
|
+
plan: {
|
|
325
|
+
id: string;
|
|
326
|
+
name: string;
|
|
327
|
+
} | null;
|
|
328
|
+
/** Remaining real opens. Null when unmetered. Warn the user before this hits zero. */
|
|
329
|
+
real_token_balance: number | null;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Every event the platform can deliver.
|
|
333
|
+
*
|
|
334
|
+
* The SDK had no webhook type at all, so every partner backend hand-cast at the
|
|
335
|
+
* one boundary where a wrong guess is silent: an unhandled event is simply
|
|
336
|
+
* ignored, and a misspelled one never fires. The published contract disagreed
|
|
337
|
+
* with reality in both directions when this was written (it listed a
|
|
338
|
+
* `payment.authorized` that nothing sends, and omitted `order.abandoned`, which
|
|
339
|
+
* the hourly sweep really does send), so these names are now pinned by a test in
|
|
340
|
+
* the platform repo against `lib/webhooks.ts`, which is what `emitWebhook`
|
|
341
|
+
* accepts.
|
|
342
|
+
*
|
|
343
|
+
* There is deliberately no `payment.authorized`: Relai checkout intents capture
|
|
344
|
+
* immediately, so success IS the capture.
|
|
345
|
+
*/
|
|
346
|
+
type WebhookEventType = "order.created" | "order.opened" | "order.completed" | "order.cancelled" | "order.refunded" | "order.abandoned" | "payment.captured" | "payment.failed" | "node.out_of_service" | "ez.status_changed";
|
|
347
|
+
/**
|
|
348
|
+
* The envelope every delivery arrives in.
|
|
349
|
+
*
|
|
350
|
+
* `K` is the discriminant, and it has to be a parameter rather than the whole
|
|
351
|
+
* `WebhookEventType` union: with `type: WebhookEventType` on every member,
|
|
352
|
+
* `switch (event.type)` narrows nothing and `event.data` stays the union of all
|
|
353
|
+
* payloads. A consumer typecheck against the built package caught that before
|
|
354
|
+
* this shipped, which is the only reason it is not decoration.
|
|
355
|
+
*/
|
|
356
|
+
interface WebhookEvent<T = Record<string, unknown>, K extends WebhookEventType = WebhookEventType> {
|
|
357
|
+
id: string;
|
|
358
|
+
type: K;
|
|
359
|
+
created_at: string;
|
|
360
|
+
data: T;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Payload per event type, so `switch (event.type)` narrows `event.data`.
|
|
364
|
+
*
|
|
365
|
+
* `order.completed` carries `reason` only when the platform completed the order
|
|
366
|
+
* itself rather than a person doing it: today that means a node forced out of
|
|
367
|
+
* service. It used to ship two incompatible shapes depending on which code path
|
|
368
|
+
* fired, with nothing saying so; one shape now, `reason` additive.
|
|
369
|
+
*/
|
|
370
|
+
interface WebhookPayloads {
|
|
371
|
+
"order.created": {
|
|
372
|
+
order: Order;
|
|
373
|
+
};
|
|
374
|
+
"order.opened": {
|
|
375
|
+
order: Order;
|
|
376
|
+
open: Open;
|
|
377
|
+
};
|
|
378
|
+
"order.completed": {
|
|
379
|
+
order: Order;
|
|
380
|
+
reason?: "node_out_of_service";
|
|
381
|
+
};
|
|
382
|
+
"order.cancelled": {
|
|
383
|
+
order: Order;
|
|
384
|
+
};
|
|
385
|
+
"order.refunded": {
|
|
386
|
+
order: Order;
|
|
387
|
+
};
|
|
388
|
+
"order.abandoned": {
|
|
389
|
+
order_id: string;
|
|
390
|
+
node_id: string | null;
|
|
391
|
+
};
|
|
392
|
+
"payment.captured": {
|
|
393
|
+
order_id: string;
|
|
394
|
+
amount_cents: number;
|
|
395
|
+
};
|
|
396
|
+
"payment.failed": {
|
|
397
|
+
order_id: string;
|
|
398
|
+
reason?: string;
|
|
399
|
+
};
|
|
400
|
+
"node.out_of_service": {
|
|
401
|
+
node_id: string;
|
|
402
|
+
order_id?: string;
|
|
403
|
+
};
|
|
404
|
+
"ez.status_changed": {
|
|
405
|
+
ez_id: string;
|
|
406
|
+
status: string;
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
/** A delivery narrowed to one event type: the argument to a typed handler. */
|
|
410
|
+
type WebhookEventOf<K extends WebhookEventType> = WebhookEvent<WebhookPayloads[K], K>;
|
|
411
|
+
/** The discriminated union to switch on. */
|
|
412
|
+
type AnyWebhookEvent = {
|
|
413
|
+
[K in WebhookEventType]: WebhookEventOf<K>;
|
|
414
|
+
}[WebhookEventType];
|
|
310
415
|
|
|
311
416
|
/**
|
|
312
417
|
* End-user auth: the OTP flow, rotating refresh (single-flight), and a pluggable
|
|
@@ -562,6 +667,76 @@ declare class TelemetryModule {
|
|
|
562
667
|
ok?: boolean;
|
|
563
668
|
} & Record<string, unknown>>;
|
|
564
669
|
}
|
|
670
|
+
/**
|
|
671
|
+
* Device push tokens.
|
|
672
|
+
*
|
|
673
|
+
* Both docs pages told builders to call `POST /v1/push/tokens` by hand, and the
|
|
674
|
+
* README's snippet sent only a Bearer token, so it 401'd with
|
|
675
|
+
* `missing_publishable_key` while the builder held a perfectly valid session.
|
|
676
|
+
* That is the whole argument for the method existing: the endpoint needs BOTH
|
|
677
|
+
* the API key and the end-user bearer, and the client already knows how to send
|
|
678
|
+
* both.
|
|
679
|
+
*/
|
|
680
|
+
declare class PushModule {
|
|
681
|
+
private readonly http;
|
|
682
|
+
constructor(http: HttpClient);
|
|
683
|
+
/**
|
|
684
|
+
* Register this device for handoff and pick-up notifications. Requires a
|
|
685
|
+
* signed-in end user. Safe to call on every launch: re-registering the same
|
|
686
|
+
* token updates it rather than duplicating it.
|
|
687
|
+
*/
|
|
688
|
+
register(token: string, platform?: PushPlatform): Promise<{
|
|
689
|
+
ok: boolean;
|
|
690
|
+
}>;
|
|
691
|
+
/** Unregister on sign-out, or the next person to use the device gets their alerts. */
|
|
692
|
+
unregister(token: string): Promise<{
|
|
693
|
+
ok: boolean;
|
|
694
|
+
}>;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Sandbox fault injection.
|
|
698
|
+
*
|
|
699
|
+
* `app/api/v1/sandbox/faults/route.ts` has claimed in a comment that it "backs
|
|
700
|
+
* the SDK's `relai.sandbox.injectFault`" since it was written, and no such
|
|
701
|
+
* method existed. That is the same defect as the documented-but-absent
|
|
702
|
+
* `client.me()`, which a builder found for us: something referring confidently
|
|
703
|
+
* to an SDK surface nobody had built.
|
|
704
|
+
*
|
|
705
|
+
* Sandbox keys only. The platform rejects production keys with `forbidden`, and
|
|
706
|
+
* a non-simulated Exchange Zone with `not_simulated`.
|
|
707
|
+
*/
|
|
708
|
+
declare class SandboxModule {
|
|
709
|
+
private readonly http;
|
|
710
|
+
constructor(http: HttpClient);
|
|
711
|
+
/**
|
|
712
|
+
* Force a fault so the app's unhappy paths can be exercised without hardware.
|
|
713
|
+
* `jammed_door` needs a `nodeId`; `dead_battery` applies to the whole
|
|
714
|
+
* Exchange Zone.
|
|
715
|
+
*/
|
|
716
|
+
injectFault(input: {
|
|
717
|
+
exchangeZoneId: string;
|
|
718
|
+
fault: Exclude<SandboxFault, "clear">;
|
|
719
|
+
nodeId?: string;
|
|
720
|
+
}): Promise<SandboxFaultResult>;
|
|
721
|
+
/** Clear an injected fault and return the Exchange Zone to service. */
|
|
722
|
+
clearFault(input: {
|
|
723
|
+
exchangeZoneId: string;
|
|
724
|
+
nodeId?: string;
|
|
725
|
+
}): Promise<SandboxFaultResult>;
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* The calling app itself: configuration, feature flags, plan and token balance.
|
|
729
|
+
*
|
|
730
|
+
* Without this a builder learns they are out of real opens from a door that did
|
|
731
|
+
* not open, which is the worst possible moment and the worst possible messenger.
|
|
732
|
+
* `realTokenBalance` lets an app warn before it gets there.
|
|
733
|
+
*/
|
|
734
|
+
declare class AppModule {
|
|
735
|
+
private readonly http;
|
|
736
|
+
constructor(http: HttpClient);
|
|
737
|
+
/** Config for the app the current key belongs to. */
|
|
738
|
+
self(): Promise<AppSelf>;
|
|
739
|
+
}
|
|
565
740
|
|
|
566
741
|
/**
|
|
567
742
|
* Telemetry queue-and-retry (owner rule 2026-07-23). A delivered-but-unreported
|
|
@@ -856,6 +1031,12 @@ declare class RelaiClient {
|
|
|
856
1031
|
/** SERVER ONLY (secret key): your app's end users, name, email, access tag. */
|
|
857
1032
|
readonly endUsers: EndUsersModule;
|
|
858
1033
|
readonly telemetry: TelemetryModule;
|
|
1034
|
+
/** Device push tokens for handoff and pick-up alerts. Needs a signed-in end user. */
|
|
1035
|
+
readonly push: PushModule;
|
|
1036
|
+
/** SANDBOX ONLY: inject and clear simulated faults, to exercise unhappy paths. */
|
|
1037
|
+
readonly sandbox: SandboxModule;
|
|
1038
|
+
/** The calling app: config, feature flags, plan and remaining real-open balance. */
|
|
1039
|
+
readonly app: AppModule;
|
|
859
1040
|
/** Queue-and-retry telemetry uploader (used by unlock; flush opportunistically). */
|
|
860
1041
|
readonly telemetryUploader: TelemetryUploader;
|
|
861
1042
|
private readonly http;
|
|
@@ -1112,15 +1293,55 @@ declare class RelaiError extends Error {
|
|
|
1112
1293
|
readonly name: string;
|
|
1113
1294
|
constructor(message: string);
|
|
1114
1295
|
}
|
|
1296
|
+
/** Everything a caller needs to report a failed call without guessing. */
|
|
1297
|
+
interface RelaiErrorContext {
|
|
1298
|
+
/** `x-relai-request-id` from the response. Quote it in a support request. */
|
|
1299
|
+
requestId?: string | undefined;
|
|
1300
|
+
/** `Retry-After`, in seconds, when the server sent one. */
|
|
1301
|
+
retryAfterSeconds?: number | undefined;
|
|
1302
|
+
/** HTTP method of the failed request. */
|
|
1303
|
+
method?: string | undefined;
|
|
1304
|
+
/** Path of the failed request, e.g. `/orders/ord_1/opens`. */
|
|
1305
|
+
path?: string | undefined;
|
|
1306
|
+
}
|
|
1115
1307
|
/** A structured error response from the platform. */
|
|
1116
1308
|
declare class RelaiApiError extends RelaiError {
|
|
1117
|
-
readonly name
|
|
1309
|
+
readonly name: string;
|
|
1118
1310
|
readonly code: string;
|
|
1119
1311
|
readonly status: number;
|
|
1120
1312
|
readonly docsUrl: string | undefined;
|
|
1121
1313
|
/** Machine-readable payload for codes that carry one (e.g. late_fee_due). */
|
|
1122
1314
|
readonly details: Record<string, unknown> | undefined;
|
|
1123
|
-
|
|
1315
|
+
/**
|
|
1316
|
+
* The id the platform stamps on EVERY response (`x-relai-request-id`) so a
|
|
1317
|
+
* partner can quote one line and have us find the request. It was being
|
|
1318
|
+
* dropped here, which made the header pointless: the server went to the
|
|
1319
|
+
* trouble of sending it and the SDK threw it away before anyone saw it.
|
|
1320
|
+
*/
|
|
1321
|
+
readonly requestId: string | undefined;
|
|
1322
|
+
/** `Retry-After` in seconds. Used internally for backoff and then discarded
|
|
1323
|
+
* until now, so callers implementing their own queueing had to re-parse it. */
|
|
1324
|
+
readonly retryAfterSeconds: number | undefined;
|
|
1325
|
+
readonly method: string | undefined;
|
|
1326
|
+
readonly path: string | undefined;
|
|
1327
|
+
constructor(status: number, body: ApiErrorBody, ctx?: RelaiErrorContext);
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* The request never reached the platform: no connection, DNS failure, TLS
|
|
1331
|
+
* failure, or the socket died mid-flight.
|
|
1332
|
+
*
|
|
1333
|
+
* This is the SINGLE most common failure on mobile and it used to throw a bare
|
|
1334
|
+
* `RelaiError` carrying nothing but a message string. Callers had no `code` to
|
|
1335
|
+
* branch on and no way to tell "you are offline, try again" apart from "the
|
|
1336
|
+
* platform refused this, do not retry", so the honest handler had to substring
|
|
1337
|
+
* match the message. It extends `RelaiApiError` so `catch (e) { e.code }` works
|
|
1338
|
+
* uniformly, with `status = 0` standing for "no response was ever received".
|
|
1339
|
+
*/
|
|
1340
|
+
declare class RelaiNetworkError extends RelaiApiError {
|
|
1341
|
+
readonly name = "RelaiNetworkError";
|
|
1342
|
+
/** The underlying fetch rejection, when the runtime gave us one. */
|
|
1343
|
+
readonly cause: unknown;
|
|
1344
|
+
constructor(message: string, ctx?: RelaiErrorContext, cause?: unknown);
|
|
1124
1345
|
}
|
|
1125
1346
|
/**
|
|
1126
1347
|
* `late_fee_due` (402): a late pick-up fee must be paid before the node opens.
|
|
@@ -1132,7 +1353,7 @@ declare class RelaiApiError extends RelaiError {
|
|
|
1132
1353
|
declare class RelaiLateFeeDue extends RelaiApiError {
|
|
1133
1354
|
readonly feeCents: number;
|
|
1134
1355
|
readonly clientSecret: string | null;
|
|
1135
|
-
constructor(status: number, body: ApiErrorBody);
|
|
1356
|
+
constructor(status: number, body: ApiErrorBody, ctx?: RelaiErrorContext);
|
|
1136
1357
|
}
|
|
1137
1358
|
/**
|
|
1138
1359
|
* The "doorway" error: the SDK was called with no publishable key. Thrown
|
|
@@ -1163,4 +1384,4 @@ declare class RelaiTransportError extends RelaiError {
|
|
|
1163
1384
|
constructor(message: string);
|
|
1164
1385
|
}
|
|
1165
1386
|
|
|
1166
|
-
export { type AccessLinkTarget, AccessLinksModule, type ApiEnvironment, type ApiErrorBody, AuthModule, type BleDeviceLike, type BleManagerLike, BleTransport, type BleTransportOptions, type CommandAck, type DeliverOptions, type DiscoveredParent, type EndUser, type EndUserPage, EndUsersModule, type ExchangeZone, type ExchangeZoneDetail, type ExchangeZonePage, ExchangeZonesModule, type GrantAlgorithm, GrantsModule, type HandoffMode, type Intent, Intents, type KeyValueStorage, type Me, MemoryTelemetryQueue, MemoryTokenStorage, type MintedCommand, type NotifyLine, type Open, type Order, type OrderStatusFields, OrdersModule, type OverrideLink, OverrideLinksModule, type ParentTransport, type PaymentRequiredDetails, type QueuedTelemetry, REJECT_REASONS, RELAI_RX_CHAR, RELAI_TX_CHAR, RELAI_UART_SERVICE, type RecipientMatch, RecipientsModule, RelaiApiError, RelaiClient, type RelaiClientOptions, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiTransportError, RelaiUnlockRejected, type SelfBillingChoice, type Session, SimAuthenticatedTransport, type SimAuthenticatedTransportOptions, type SimulatedParentConfig, SimulatedTransport, StorageTelemetryQueue, type StorageTelemetryQueueOptions, type StripeSdk, type TelemetryAction, type TelemetryEvent, TelemetryModule, type TelemetryPayload, type TelemetryQueue, TelemetryUploader, type TokenPair, type TokenStorage, type Tower, type TowerNode, type TowerTelemetry, type UnlockDeps, type UnlockGrant, type UnlockInput, type UnlockResult, type VerifyWebhookInput, asPaymentRequired, deliverOpen, parseNotifyLine, presentRelaiPaymentSheet, verifyWebhook };
|
|
1387
|
+
export { type AccessLinkTarget, AccessLinksModule, type AnyWebhookEvent, type ApiEnvironment, type ApiErrorBody, AppModule, type AppSelf, AuthModule, type BleDeviceLike, type BleManagerLike, BleTransport, type BleTransportOptions, type CommandAck, type DeliverOptions, type DiscoveredParent, type EndUser, type EndUserPage, EndUsersModule, type ExchangeZone, type ExchangeZoneDetail, type ExchangeZonePage, ExchangeZonesModule, type GrantAlgorithm, GrantsModule, type HandoffMode, type Intent, Intents, type KeyValueStorage, type Me, MemoryTelemetryQueue, MemoryTokenStorage, type MintedCommand, type NotifyLine, type Open, type Order, type OrderStatusFields, OrdersModule, type OverrideLink, OverrideLinksModule, type ParentTransport, type PaymentRequiredDetails, PushModule, type PushPlatform, type QueuedTelemetry, REJECT_REASONS, RELAI_RX_CHAR, RELAI_TX_CHAR, RELAI_UART_SERVICE, type RecipientMatch, RecipientsModule, RelaiApiError, RelaiClient, type RelaiClientOptions, RelaiError, type RelaiErrorContext, RelaiLateFeeDue, RelaiMissingKeyError, RelaiNetworkError, RelaiTransportError, RelaiUnlockRejected, type SandboxFault, type SandboxFaultResult, SandboxModule, type SelfBillingChoice, type Session, SimAuthenticatedTransport, type SimAuthenticatedTransportOptions, type SimulatedParentConfig, SimulatedTransport, StorageTelemetryQueue, type StorageTelemetryQueueOptions, type StripeSdk, type TelemetryAction, type TelemetryEvent, TelemetryModule, type TelemetryPayload, type TelemetryQueue, TelemetryUploader, type TokenPair, type TokenStorage, type Tower, type TowerNode, type TowerTelemetry, type UnlockDeps, type UnlockGrant, type UnlockInput, type UnlockResult, type VerifyWebhookInput, type WebhookEvent, type WebhookEventOf, type WebhookEventType, type WebhookPayloads, asPaymentRequired, deliverOpen, parseNotifyLine, presentRelaiPaymentSheet, verifyWebhook };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RelaiApiError, RelaiTransportError, RelaiUnlockRejected, RelaiMissingKeyError, RelaiError } from './chunk-
|
|
2
|
-
export { RelaiApiError, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiTransportError, RelaiUnlockRejected } from './chunk-
|
|
1
|
+
import { RelaiApiError, RelaiTransportError, RelaiUnlockRejected, RelaiMissingKeyError, RelaiError, RelaiNetworkError } from './chunk-U4CSAG73.js';
|
|
2
|
+
export { RelaiApiError, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiNetworkError, RelaiTransportError, RelaiUnlockRejected } from './chunk-U4CSAG73.js';
|
|
3
3
|
export { asPaymentRequired, presentRelaiPaymentSheet } from './chunk-3WZBCXG3.js';
|
|
4
4
|
|
|
5
5
|
// src/http.ts
|
|
@@ -48,14 +48,18 @@ var HttpClient = class {
|
|
|
48
48
|
await sleep(backoffMs(i));
|
|
49
49
|
continue;
|
|
50
50
|
}
|
|
51
|
-
throw new
|
|
51
|
+
throw new RelaiNetworkError(
|
|
52
|
+
`Network request failed: ${netErr.message}`,
|
|
53
|
+
{ method: opts.method ?? "GET", path: opts.path },
|
|
54
|
+
netErr
|
|
55
|
+
);
|
|
52
56
|
}
|
|
53
57
|
if (res.ok) return await this.parseJson(res);
|
|
54
58
|
if (RETRYABLE_STATUS.has(res.status) && i < this.cfg.maxRetries) {
|
|
55
59
|
await sleep(retryDelayMs(res, i));
|
|
56
60
|
continue;
|
|
57
61
|
}
|
|
58
|
-
const apiErr = await this.toApiError(res);
|
|
62
|
+
const apiErr = await this.toApiError(res, opts);
|
|
59
63
|
if (apiErr.code === "idempotency_in_progress" && i < this.cfg.maxRetries) {
|
|
60
64
|
await sleep(retryDelayMs(res, i));
|
|
61
65
|
continue;
|
|
@@ -93,7 +97,7 @@ var HttpClient = class {
|
|
|
93
97
|
throw new RelaiError(`Expected JSON from ${res.url} but got: ${text.slice(0, 120)}`);
|
|
94
98
|
}
|
|
95
99
|
}
|
|
96
|
-
async toApiError(res) {
|
|
100
|
+
async toApiError(res, opts) {
|
|
97
101
|
let body;
|
|
98
102
|
try {
|
|
99
103
|
body = await this.parseJson(res);
|
|
@@ -101,11 +105,18 @@ var HttpClient = class {
|
|
|
101
105
|
body = { error: { code: "http_error", message: `HTTP ${res.status}` } };
|
|
102
106
|
}
|
|
103
107
|
if (!body?.error?.code) body = { error: { code: "http_error", message: `HTTP ${res.status}` } };
|
|
108
|
+
const ra = Number(res.headers.get("retry-after"));
|
|
109
|
+
const ctx = {
|
|
110
|
+
requestId: res.headers.get("x-relai-request-id") ?? void 0,
|
|
111
|
+
retryAfterSeconds: Number.isFinite(ra) && res.headers.get("retry-after") !== null ? ra : void 0,
|
|
112
|
+
method: opts.method ?? "GET",
|
|
113
|
+
path: opts.path
|
|
114
|
+
};
|
|
104
115
|
if (body.error.code === "late_fee_due") {
|
|
105
|
-
const { RelaiLateFeeDue: RelaiLateFeeDue2 } = await import('./errors-
|
|
106
|
-
return new RelaiLateFeeDue2(res.status, body);
|
|
116
|
+
const { RelaiLateFeeDue: RelaiLateFeeDue2 } = await import('./errors-N4TYPXJ4.js');
|
|
117
|
+
return new RelaiLateFeeDue2(res.status, body, ctx);
|
|
107
118
|
}
|
|
108
|
-
return new RelaiApiError(res.status, body);
|
|
119
|
+
return new RelaiApiError(res.status, body, ctx);
|
|
109
120
|
}
|
|
110
121
|
};
|
|
111
122
|
function backoffMs(attempt) {
|
|
@@ -395,6 +406,61 @@ var TelemetryModule = class {
|
|
|
395
406
|
return this.http.request({ method: "POST", path: "/telemetry", body });
|
|
396
407
|
}
|
|
397
408
|
};
|
|
409
|
+
var PushModule = class {
|
|
410
|
+
constructor(http) {
|
|
411
|
+
this.http = http;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Register this device for handoff and pick-up notifications. Requires a
|
|
415
|
+
* signed-in end user. Safe to call on every launch: re-registering the same
|
|
416
|
+
* token updates it rather than duplicating it.
|
|
417
|
+
*/
|
|
418
|
+
register(token, platform = "expo") {
|
|
419
|
+
return this.http.request({
|
|
420
|
+
method: "POST",
|
|
421
|
+
path: "/push/tokens",
|
|
422
|
+
body: { token, platform }
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/** Unregister on sign-out, or the next person to use the device gets their alerts. */
|
|
426
|
+
unregister(token) {
|
|
427
|
+
return this.http.request({
|
|
428
|
+
method: "DELETE",
|
|
429
|
+
path: "/push/tokens",
|
|
430
|
+
body: { token }
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
var SandboxModule = class {
|
|
435
|
+
constructor(http) {
|
|
436
|
+
this.http = http;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Force a fault so the app's unhappy paths can be exercised without hardware.
|
|
440
|
+
* `jammed_door` needs a `nodeId`; `dead_battery` applies to the whole
|
|
441
|
+
* Exchange Zone.
|
|
442
|
+
*/
|
|
443
|
+
injectFault(input) {
|
|
444
|
+
const body = { ez_id: input.exchangeZoneId, fault: input.fault };
|
|
445
|
+
if (input.nodeId !== void 0) body["node_id"] = input.nodeId;
|
|
446
|
+
return this.http.request({ method: "POST", path: "/sandbox/faults", body });
|
|
447
|
+
}
|
|
448
|
+
/** Clear an injected fault and return the Exchange Zone to service. */
|
|
449
|
+
clearFault(input) {
|
|
450
|
+
const body = { ez_id: input.exchangeZoneId, fault: "clear" };
|
|
451
|
+
if (input.nodeId !== void 0) body["node_id"] = input.nodeId;
|
|
452
|
+
return this.http.request({ method: "POST", path: "/sandbox/faults", body });
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
var AppModule = class {
|
|
456
|
+
constructor(http) {
|
|
457
|
+
this.http = http;
|
|
458
|
+
}
|
|
459
|
+
/** Config for the app the current key belongs to. */
|
|
460
|
+
self() {
|
|
461
|
+
return this.http.request({ path: "/apps/self" });
|
|
462
|
+
}
|
|
463
|
+
};
|
|
398
464
|
|
|
399
465
|
// src/telemetry-queue.ts
|
|
400
466
|
var MemoryTelemetryQueue = class {
|
|
@@ -552,6 +618,9 @@ var RelaiClient = class {
|
|
|
552
618
|
this.overrideLinks = new OverrideLinksModule(this.http);
|
|
553
619
|
this.endUsers = new EndUsersModule(this.http);
|
|
554
620
|
this.telemetry = new TelemetryModule(this.http);
|
|
621
|
+
this.push = new PushModule(this.http);
|
|
622
|
+
this.sandbox = new SandboxModule(this.http);
|
|
623
|
+
this.app = new AppModule(this.http);
|
|
555
624
|
this.telemetryUploader = new TelemetryUploader(this.telemetry, options.telemetryQueue);
|
|
556
625
|
}
|
|
557
626
|
/** Drain any telemetry queued after a failed send (e.g. on app foreground). */
|
|
@@ -589,7 +658,7 @@ var RelaiClient = class {
|
|
|
589
658
|
try {
|
|
590
659
|
open = await requestOpen();
|
|
591
660
|
} catch (err) {
|
|
592
|
-
const { RelaiApiError: RelaiApiError2 } = await import('./errors-
|
|
661
|
+
const { RelaiApiError: RelaiApiError2 } = await import('./errors-N4TYPXJ4.js');
|
|
593
662
|
if (input.presentPaymentSheet !== false && err instanceof RelaiApiError2 && err.code === "payment_required") {
|
|
594
663
|
const { asPaymentRequired: asPaymentRequired2, presentRelaiPaymentSheet: presentRelaiPaymentSheet2 } = await import('./payment-sheet-XIZYASSI.js');
|
|
595
664
|
const details = asPaymentRequired2(err.details);
|
|
@@ -987,4 +1056,4 @@ var Intents = {
|
|
|
987
1056
|
MakeAvailable: "make_available"
|
|
988
1057
|
};
|
|
989
1058
|
|
|
990
|
-
export { AccessLinksModule, AuthModule, BleTransport, EndUsersModule, ExchangeZonesModule, GrantsModule, Intents, MemoryTelemetryQueue, MemoryTokenStorage, OrdersModule, OverrideLinksModule, REJECT_REASONS, RELAI_RX_CHAR, RELAI_TX_CHAR, RELAI_UART_SERVICE, RecipientsModule, RelaiClient, SimAuthenticatedTransport, SimulatedTransport, StorageTelemetryQueue, TelemetryModule, TelemetryUploader, deliverOpen, parseNotifyLine, verifyWebhook };
|
|
1059
|
+
export { AccessLinksModule, AppModule, AuthModule, BleTransport, EndUsersModule, ExchangeZonesModule, GrantsModule, Intents, MemoryTelemetryQueue, MemoryTokenStorage, OrdersModule, OverrideLinksModule, PushModule, REJECT_REASONS, RELAI_RX_CHAR, RELAI_TX_CHAR, RELAI_UART_SERVICE, RecipientsModule, RelaiClient, SandboxModule, SimAuthenticatedTransport, SimulatedTransport, StorageTelemetryQueue, TelemetryModule, TelemetryUploader, deliverOpen, parseNotifyLine, verifyWebhook };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relai-team/access-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Official Relai Access SDK, open Relai Exchange Zone nodes over BLE, backed by the Relai Access platform (server-owns-trust).",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"type": "module",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "tsup",
|
|
25
25
|
"typecheck": "tsc --noEmit",
|
|
26
|
+
"pretest": "npm run build",
|
|
26
27
|
"test": "vitest run",
|
|
27
28
|
"test:watch": "vitest",
|
|
28
29
|
"spec:types": "openapi-typescript openapi.json -o src/generated/api.ts",
|
package/dist/errors-L7B45UIZ.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { RelaiApiError, RelaiError, RelaiLateFeeDue, RelaiMissingKeyError, RelaiTransportError, RelaiUnlockRejected } from './chunk-MNXTMLX2.js';
|