@sippulse/niva-sdk 0.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/README.md +153 -0
- package/dts/src/client.d.ts +6 -0
- package/dts/src/entry/index.bun.d.ts +3 -0
- package/dts/src/entry/index.default.d.ts +3 -0
- package/dts/src/entry/index.deno.d.ts +3 -0
- package/dts/src/entry/index.node.d.ts +3 -0
- package/dts/src/errors.d.ts +98 -0
- package/dts/src/generated/client-impl.d.ts +8 -0
- package/dts/src/generated/client.d.ts +1011 -0
- package/dts/src/generated/meta.d.ts +19 -0
- package/dts/src/generated/operations.d.ts +11394 -0
- package/dts/src/generated/webhooks.d.ts +425 -0
- package/dts/src/handshake.d.ts +21 -0
- package/dts/src/index.d.ts +20 -0
- package/dts/src/sse.d.ts +7 -0
- package/dts/src/transport.d.ts +56 -0
- package/dts/src/types.d.ts +40 -0
- package/dts/src/webhook/handler.d.ts +20 -0
- package/dts/src/webhook/serve-bun.d.ts +10 -0
- package/dts/src/webhook/serve-default.d.ts +6 -0
- package/dts/src/webhook/serve-deno.d.ts +10 -0
- package/dts/src/webhook/serve-node.d.ts +6 -0
- package/dts/src/webhook/server.d.ts +61 -0
- package/dts/src/webhook/verify.d.ts +17 -0
- package/index.bun.js +2829 -0
- package/index.d.ts +3 -0
- package/index.default.js +2756 -0
- package/index.deno.js +2831 -0
- package/index.node.js +2892 -0
- package/package.json +43 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GENERATED FILE - do not edit. Run `bun run generate` in packages/sdk
|
|
3
|
+
* (NIVA-259). Source: webhookDeliveryEnvelopeSchema in @niva/shared.
|
|
4
|
+
*/
|
|
5
|
+
import type { NivaJsonValue } from "../types.js";
|
|
6
|
+
/** Registry literals mirrored from WEBHOOK_EVENT_TYPES; runtime const for the registry gate. */
|
|
7
|
+
export declare const NIVA_WEBHOOK_EVENT_TYPES: readonly ["alert.fired", "alert.resolved", "call.amd_completed", "call.analysis_completed", "call.answered", "call.hangup", "call.intent_matched", "call.node_entered", "call.node_error", "call.node_warning", "call.recording_available", "call.rejected", "call.started", "call.transcribed", "call.transferred", "dialer.attempt.finished", "message.sent", "plan.published", "service.down", "service.up"];
|
|
8
|
+
/** Union of the registry event literals. */
|
|
9
|
+
export type NivaWebhookEventType = (typeof NIVA_WEBHOOK_EVENT_TYPES)[number];
|
|
10
|
+
/** Per-event data payloads of the external delivery envelope (no org_id). */
|
|
11
|
+
export interface NivaWebhookEventMap {
|
|
12
|
+
"alert.fired": {
|
|
13
|
+
alert_type: string;
|
|
14
|
+
severity: "critical" | "warning" | "info";
|
|
15
|
+
title: string;
|
|
16
|
+
body: string;
|
|
17
|
+
metric_value?: number;
|
|
18
|
+
threshold?: number;
|
|
19
|
+
};
|
|
20
|
+
"alert.resolved": {
|
|
21
|
+
alert_type: string;
|
|
22
|
+
title: string;
|
|
23
|
+
body: string;
|
|
24
|
+
};
|
|
25
|
+
"call.amd_completed": {
|
|
26
|
+
/**
|
|
27
|
+
* minLength: 1
|
|
28
|
+
*/
|
|
29
|
+
call_id: `call_${string}`;
|
|
30
|
+
/**
|
|
31
|
+
* minLength: 1
|
|
32
|
+
*/
|
|
33
|
+
channel_uuid: string;
|
|
34
|
+
result: "machine" | "human" | "silence" | "unknown";
|
|
35
|
+
machine_type?: "voicemail" | "announcement" | "ivr" | "screening" | "unknown";
|
|
36
|
+
completed_at: string;
|
|
37
|
+
metadata: Record<string, string>;
|
|
38
|
+
};
|
|
39
|
+
"call.analysis_completed": {
|
|
40
|
+
/**
|
|
41
|
+
* minLength: 1
|
|
42
|
+
*/
|
|
43
|
+
call_id: `call_${string}`;
|
|
44
|
+
/**
|
|
45
|
+
* minLength: 1
|
|
46
|
+
*/
|
|
47
|
+
call_record_id: `call_${string}`;
|
|
48
|
+
/**
|
|
49
|
+
* minLength: 1
|
|
50
|
+
*/
|
|
51
|
+
transcription_id: `ctr_${string}`;
|
|
52
|
+
visual_plan_id: `vp_${string}`;
|
|
53
|
+
analyses: ({
|
|
54
|
+
source: "native";
|
|
55
|
+
id: null;
|
|
56
|
+
template_id: null;
|
|
57
|
+
name: string;
|
|
58
|
+
category: null;
|
|
59
|
+
status: "completed" | "failed";
|
|
60
|
+
result: {
|
|
61
|
+
[key: string]: NivaJsonValue;
|
|
62
|
+
} | null;
|
|
63
|
+
error_message: string | null;
|
|
64
|
+
error_code: string | null;
|
|
65
|
+
} | {
|
|
66
|
+
source: "local";
|
|
67
|
+
/**
|
|
68
|
+
* minLength: 1
|
|
69
|
+
*/
|
|
70
|
+
id: `ca_${string}`;
|
|
71
|
+
/**
|
|
72
|
+
* minLength: 1
|
|
73
|
+
*/
|
|
74
|
+
template_id: `at_${string}`;
|
|
75
|
+
name: string;
|
|
76
|
+
category: string | null;
|
|
77
|
+
status: "completed" | "failed";
|
|
78
|
+
result: {
|
|
79
|
+
[key: string]: NivaJsonValue;
|
|
80
|
+
} | null;
|
|
81
|
+
error_message: string | null;
|
|
82
|
+
error_code: string | null;
|
|
83
|
+
} | {
|
|
84
|
+
source: "sippulse_ai";
|
|
85
|
+
/**
|
|
86
|
+
* minLength: 1
|
|
87
|
+
*/
|
|
88
|
+
id: `ca_${string}`;
|
|
89
|
+
template_id: null;
|
|
90
|
+
/**
|
|
91
|
+
* minLength: 1
|
|
92
|
+
*/
|
|
93
|
+
external_id: string;
|
|
94
|
+
/**
|
|
95
|
+
* minLength: 1
|
|
96
|
+
*/
|
|
97
|
+
slug: string;
|
|
98
|
+
name: string;
|
|
99
|
+
category: null;
|
|
100
|
+
model: string | null;
|
|
101
|
+
status: "completed" | "failed";
|
|
102
|
+
result: {
|
|
103
|
+
[key: string]: NivaJsonValue;
|
|
104
|
+
} | null;
|
|
105
|
+
error_message: string | null;
|
|
106
|
+
error_code: string | null;
|
|
107
|
+
})[];
|
|
108
|
+
analyzed_at: string;
|
|
109
|
+
metadata: Record<string, string>;
|
|
110
|
+
};
|
|
111
|
+
"call.answered": {
|
|
112
|
+
/**
|
|
113
|
+
* minLength: 1
|
|
114
|
+
*/
|
|
115
|
+
call_id: `call_${string}`;
|
|
116
|
+
caller_id: string;
|
|
117
|
+
callee_id: string;
|
|
118
|
+
answered_at: string;
|
|
119
|
+
metadata: Record<string, string>;
|
|
120
|
+
};
|
|
121
|
+
"call.hangup": {
|
|
122
|
+
/**
|
|
123
|
+
* minLength: 1
|
|
124
|
+
*/
|
|
125
|
+
call_id: `call_${string}`;
|
|
126
|
+
caller_id: string;
|
|
127
|
+
callee_id: string;
|
|
128
|
+
direction: "inbound" | "outbound";
|
|
129
|
+
/**
|
|
130
|
+
* minLength: 1
|
|
131
|
+
*/
|
|
132
|
+
visual_plan_id: `vp_${string}`;
|
|
133
|
+
visual_plan_name: string;
|
|
134
|
+
started_at: string;
|
|
135
|
+
answered_at: string | null;
|
|
136
|
+
ended_at: string;
|
|
137
|
+
/**
|
|
138
|
+
* minimum: 0
|
|
139
|
+
*/
|
|
140
|
+
duration_ms: number;
|
|
141
|
+
hangup_cause: string;
|
|
142
|
+
hangup_disposition: string;
|
|
143
|
+
was_recorded: boolean;
|
|
144
|
+
variables: Record<string, string>;
|
|
145
|
+
sip_code: string | null;
|
|
146
|
+
/**
|
|
147
|
+
* minimum: 0
|
|
148
|
+
*/
|
|
149
|
+
billsec: number | null;
|
|
150
|
+
answer_recognition_results?: {
|
|
151
|
+
amd: "machine" | "human" | "silence" | "unknown";
|
|
152
|
+
};
|
|
153
|
+
metadata: Record<string, string>;
|
|
154
|
+
};
|
|
155
|
+
"call.intent_matched": {
|
|
156
|
+
/**
|
|
157
|
+
* minLength: 1
|
|
158
|
+
*/
|
|
159
|
+
call_id: `call_${string}`;
|
|
160
|
+
caller_id: string;
|
|
161
|
+
callee_id: string;
|
|
162
|
+
/**
|
|
163
|
+
* minLength: 1
|
|
164
|
+
*/
|
|
165
|
+
node_id: string;
|
|
166
|
+
/**
|
|
167
|
+
* minLength: 1
|
|
168
|
+
*/
|
|
169
|
+
visual_plan_id: `vp_${string}`;
|
|
170
|
+
transcript: string;
|
|
171
|
+
intent: string;
|
|
172
|
+
confidence: number;
|
|
173
|
+
alternatives: {
|
|
174
|
+
intent: string;
|
|
175
|
+
confidence: number;
|
|
176
|
+
}[];
|
|
177
|
+
language: string | null;
|
|
178
|
+
matched_at: string;
|
|
179
|
+
metadata: Record<string, string>;
|
|
180
|
+
};
|
|
181
|
+
"call.node_entered": {
|
|
182
|
+
/**
|
|
183
|
+
* minLength: 1
|
|
184
|
+
*/
|
|
185
|
+
call_id: `call_${string}`;
|
|
186
|
+
caller_id: string;
|
|
187
|
+
callee_id: string;
|
|
188
|
+
/**
|
|
189
|
+
* minLength: 1
|
|
190
|
+
*/
|
|
191
|
+
node_id: string;
|
|
192
|
+
node_type: string;
|
|
193
|
+
node_label: string;
|
|
194
|
+
handle_used: string;
|
|
195
|
+
digit: string | null;
|
|
196
|
+
entered_at: string;
|
|
197
|
+
exited_at: string;
|
|
198
|
+
/**
|
|
199
|
+
* minimum: 0
|
|
200
|
+
*/
|
|
201
|
+
duration_ms: number;
|
|
202
|
+
/**
|
|
203
|
+
* minLength: 1
|
|
204
|
+
*/
|
|
205
|
+
visual_plan_id: `vp_${string}`;
|
|
206
|
+
metadata: Record<string, string>;
|
|
207
|
+
};
|
|
208
|
+
"call.node_error": {
|
|
209
|
+
/**
|
|
210
|
+
* minLength: 1
|
|
211
|
+
*/
|
|
212
|
+
call_id: `call_${string}`;
|
|
213
|
+
caller_id: string;
|
|
214
|
+
callee_id: string;
|
|
215
|
+
/**
|
|
216
|
+
* minLength: 1
|
|
217
|
+
*/
|
|
218
|
+
node_id: string;
|
|
219
|
+
node_type: string;
|
|
220
|
+
node_label: string;
|
|
221
|
+
error_code: string;
|
|
222
|
+
error_message: string;
|
|
223
|
+
/**
|
|
224
|
+
* minLength: 1
|
|
225
|
+
*/
|
|
226
|
+
visual_plan_id: `vp_${string}`;
|
|
227
|
+
visual_plan_name: string;
|
|
228
|
+
occurred_at: string;
|
|
229
|
+
metadata: Record<string, string>;
|
|
230
|
+
};
|
|
231
|
+
"call.node_warning": {
|
|
232
|
+
/**
|
|
233
|
+
* minLength: 1
|
|
234
|
+
*/
|
|
235
|
+
call_id: `call_${string}`;
|
|
236
|
+
caller_id: string;
|
|
237
|
+
callee_id: string;
|
|
238
|
+
/**
|
|
239
|
+
* minLength: 1
|
|
240
|
+
*/
|
|
241
|
+
node_id: string;
|
|
242
|
+
node_type: string;
|
|
243
|
+
node_label: string;
|
|
244
|
+
error_code: string;
|
|
245
|
+
error_message: string;
|
|
246
|
+
/**
|
|
247
|
+
* minLength: 1
|
|
248
|
+
*/
|
|
249
|
+
visual_plan_id: `vp_${string}`;
|
|
250
|
+
visual_plan_name: string;
|
|
251
|
+
occurred_at: string;
|
|
252
|
+
metadata: Record<string, string>;
|
|
253
|
+
};
|
|
254
|
+
"call.recording_available": {
|
|
255
|
+
/**
|
|
256
|
+
* minLength: 1
|
|
257
|
+
*/
|
|
258
|
+
call_id: `call_${string}`;
|
|
259
|
+
/**
|
|
260
|
+
* minLength: 1
|
|
261
|
+
*/
|
|
262
|
+
call_record_id: `call_${string}`;
|
|
263
|
+
recording_url: string;
|
|
264
|
+
storage_path: string;
|
|
265
|
+
/**
|
|
266
|
+
* minimum: 0
|
|
267
|
+
*/
|
|
268
|
+
duration_seconds: number | null;
|
|
269
|
+
format: string;
|
|
270
|
+
/**
|
|
271
|
+
* minimum: 0
|
|
272
|
+
*/
|
|
273
|
+
size_bytes: number | null;
|
|
274
|
+
available_at: string;
|
|
275
|
+
metadata: Record<string, string>;
|
|
276
|
+
};
|
|
277
|
+
"call.rejected": {
|
|
278
|
+
caller: string;
|
|
279
|
+
destination: string;
|
|
280
|
+
reason: "not_found" | "channel_inactive" | "channel_deleted" | "plan_deleted" | "no_published_version" | "org_deleted" | "acl_denied" | "capacity_global" | "capacity_org" | "capacity_campaign" | "capacity_reservation";
|
|
281
|
+
hangup_cause: string;
|
|
282
|
+
};
|
|
283
|
+
"call.started": {
|
|
284
|
+
/**
|
|
285
|
+
* minLength: 1
|
|
286
|
+
*/
|
|
287
|
+
call_id: `call_${string}`;
|
|
288
|
+
caller_id: string;
|
|
289
|
+
callee_id: string;
|
|
290
|
+
direction: "inbound" | "outbound";
|
|
291
|
+
/**
|
|
292
|
+
* minLength: 1
|
|
293
|
+
*/
|
|
294
|
+
visual_plan_id: `vp_${string}`;
|
|
295
|
+
visual_plan_name: string;
|
|
296
|
+
started_at: string;
|
|
297
|
+
answer_recognition?: {
|
|
298
|
+
amd: true;
|
|
299
|
+
machine_type?: true;
|
|
300
|
+
mode?: "pre" | "post";
|
|
301
|
+
/**
|
|
302
|
+
* minimum: 3
|
|
303
|
+
* maximum: 10
|
|
304
|
+
*/
|
|
305
|
+
max_wait_seconds?: number;
|
|
306
|
+
};
|
|
307
|
+
metadata: Record<string, string>;
|
|
308
|
+
};
|
|
309
|
+
"call.transcribed": {
|
|
310
|
+
/**
|
|
311
|
+
* minLength: 1
|
|
312
|
+
*/
|
|
313
|
+
call_record_id: `call_${string}`;
|
|
314
|
+
/**
|
|
315
|
+
* minLength: 1
|
|
316
|
+
*/
|
|
317
|
+
transcription_id: `ctr_${string}`;
|
|
318
|
+
full_text: string;
|
|
319
|
+
is_anonymized: boolean;
|
|
320
|
+
metadata: Record<string, string>;
|
|
321
|
+
};
|
|
322
|
+
"call.transferred": {
|
|
323
|
+
/**
|
|
324
|
+
* minLength: 1
|
|
325
|
+
*/
|
|
326
|
+
call_id: `call_${string}`;
|
|
327
|
+
caller_id: string;
|
|
328
|
+
callee_id: string;
|
|
329
|
+
to_destination: string;
|
|
330
|
+
method: "bridge" | "sip_message" | "blind";
|
|
331
|
+
disposition: "answered" | "busy" | "noAnswer" | "failed";
|
|
332
|
+
status: string;
|
|
333
|
+
/**
|
|
334
|
+
* minimum: 0
|
|
335
|
+
*/
|
|
336
|
+
duration_ms: number | null;
|
|
337
|
+
transferred_at: string;
|
|
338
|
+
metadata: Record<string, string>;
|
|
339
|
+
};
|
|
340
|
+
"dialer.attempt.finished": {
|
|
341
|
+
/**
|
|
342
|
+
* minLength: 1
|
|
343
|
+
*/
|
|
344
|
+
campaign_id: `dcamp_${string}`;
|
|
345
|
+
campaign_name: string;
|
|
346
|
+
/**
|
|
347
|
+
* minLength: 1
|
|
348
|
+
*/
|
|
349
|
+
contact_id: `dct_${string}`;
|
|
350
|
+
phone_number: string;
|
|
351
|
+
/**
|
|
352
|
+
* minimum: 0
|
|
353
|
+
* maximum: 9007199254740991
|
|
354
|
+
*/
|
|
355
|
+
attempt_number: number;
|
|
356
|
+
status: "answered" | "machine" | "silence" | "busy" | "no_answer" | "failed" | "originate_error" | "cancelled";
|
|
357
|
+
hangup_cause: string | null;
|
|
358
|
+
sip_code: string | null;
|
|
359
|
+
call_uuid: string | null;
|
|
360
|
+
started_at: string | null;
|
|
361
|
+
answered_at: string | null;
|
|
362
|
+
ended_at: string | null;
|
|
363
|
+
/**
|
|
364
|
+
* minimum: 0
|
|
365
|
+
*/
|
|
366
|
+
duration_s: number | null;
|
|
367
|
+
custom_fields: Record<string, string>;
|
|
368
|
+
};
|
|
369
|
+
"message.sent": {
|
|
370
|
+
/**
|
|
371
|
+
* minLength: 1
|
|
372
|
+
*/
|
|
373
|
+
session_id: `call_${string}`;
|
|
374
|
+
channel_type: string;
|
|
375
|
+
to: string;
|
|
376
|
+
message_type: "text" | "media" | "audio" | "image" | "video" | "document" | "location" | "interactive";
|
|
377
|
+
text: string | null;
|
|
378
|
+
media_url: string | null;
|
|
379
|
+
node_id: string | null;
|
|
380
|
+
sent_at: string;
|
|
381
|
+
};
|
|
382
|
+
"plan.published": {
|
|
383
|
+
/**
|
|
384
|
+
* minLength: 1
|
|
385
|
+
*/
|
|
386
|
+
plan_id: `vp_${string}`;
|
|
387
|
+
plan_name: string;
|
|
388
|
+
/**
|
|
389
|
+
* minLength: 1
|
|
390
|
+
*/
|
|
391
|
+
version_id: `vpv_${string}`;
|
|
392
|
+
/**
|
|
393
|
+
* minimum: 0
|
|
394
|
+
* maximum: 9007199254740991
|
|
395
|
+
*/
|
|
396
|
+
version_number: number;
|
|
397
|
+
/**
|
|
398
|
+
* minLength: 1
|
|
399
|
+
*/
|
|
400
|
+
published_by_user_id: `usr_${string}`;
|
|
401
|
+
notes: string | null;
|
|
402
|
+
published_at: string;
|
|
403
|
+
};
|
|
404
|
+
"service.down": {
|
|
405
|
+
component: string;
|
|
406
|
+
checked_at: string;
|
|
407
|
+
};
|
|
408
|
+
"service.up": {
|
|
409
|
+
component: string;
|
|
410
|
+
/**
|
|
411
|
+
* minimum: 0
|
|
412
|
+
*/
|
|
413
|
+
downtime_seconds: number | null;
|
|
414
|
+
recovered_at: string;
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/** One delivery: event, event_id, timestamp plus data narrowed by K. */
|
|
418
|
+
export type NivaWebhookEvent<K extends NivaWebhookEventType = NivaWebhookEventType> = {
|
|
419
|
+
[E in K]: {
|
|
420
|
+
event: E;
|
|
421
|
+
event_id: string;
|
|
422
|
+
timestamp: string;
|
|
423
|
+
data: NivaWebhookEventMap[E];
|
|
424
|
+
};
|
|
425
|
+
}[K];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface CapabilitySnapshot {
|
|
2
|
+
/**
|
|
3
|
+
* Sorted operationIds, or `undefined` when capabilities degraded
|
|
4
|
+
* (404/403): unknown means SKIP the operation and module checks, which is
|
|
5
|
+
* distinct from an authoritative (possibly empty) list that rejects all.
|
|
6
|
+
*/
|
|
7
|
+
operations: string[] | undefined;
|
|
8
|
+
modules: Record<string, boolean>;
|
|
9
|
+
requiredModules: Record<string, string[]>;
|
|
10
|
+
}
|
|
11
|
+
export interface HandshakeInput {
|
|
12
|
+
fetchImpl: typeof fetch;
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
headers: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
15
|
+
timeout?: number | undefined;
|
|
16
|
+
}
|
|
17
|
+
export declare function createHandshake(input: HandshakeInput): {
|
|
18
|
+
ensure(): Promise<CapabilitySnapshot>;
|
|
19
|
+
};
|
|
20
|
+
/** Throws NivaUnavailableError when the snapshot lacks the op or its modules. No I/O. Unknown capabilities skip every check. */
|
|
21
|
+
export declare function assertOperationAvailable(snapshot: CapabilitySnapshot, opId: string): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@niva/sdk` root export (NIVA-258 phase B: typed client over fetch).
|
|
3
|
+
*
|
|
4
|
+
* `src/generated/` is produced by `bun run generate` from the live
|
|
5
|
+
* `/api/v1` OpenAPI document; `generate:check` fails CI on drift.
|
|
6
|
+
*/
|
|
7
|
+
export { createNivaClient } from "./client.js";
|
|
8
|
+
export type { OperationCall } from "./client.js";
|
|
9
|
+
export { isNivaApiError, NivaApiError, NivaConfigurationError, NivaConnectionError, NivaError, NivaIncompatibleServerError, NivaUnavailableError, NivaWebhookError, } from "./errors.js";
|
|
10
|
+
export type { NivaApiErrorDetails, NivaConnectionReason, NivaErrorCode, NivaIncompatibleServerDetails, NivaUnavailableReason, NivaWebhookErrorCode, NivaWebhookErrorKind, } from "./errors.js";
|
|
11
|
+
export * from "./generated/operations.js";
|
|
12
|
+
export type { NivaClient } from "./generated/client.js";
|
|
13
|
+
export { buildNivaClient } from "./generated/client-impl.js";
|
|
14
|
+
export type { NivaClientOptions, NivaJsonValue, NivaRequestOptions, NivaStream } from "./types.js";
|
|
15
|
+
export * from "./generated/webhooks.js";
|
|
16
|
+
export { verifyNivaWebhook, DEFAULT_TOLERANCE_SECONDS } from "./webhook/verify.js";
|
|
17
|
+
export type { VerifyNivaWebhookOptions, WebhookHeaders } from "./webhook/verify.js";
|
|
18
|
+
export { createNivaWebhookHandler, DEFAULT_MAX_BODY_BYTES } from "./webhook/handler.js";
|
|
19
|
+
export type { NivaWebhookContext, NivaWebhookHandler, NivaWebhookHandlerFn, NivaWebhookHandlerOptions, } from "./webhook/handler.js";
|
|
20
|
+
export type { NivaWebhookFetch, NivaWebhookServeOptions, NivaWebhookServer, NivaWebhookServerAdapter, NivaWebhookServerAddress, } from "./webhook/server.js";
|
package/dts/src/sse.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { NivaStream } from "./types.js";
|
|
2
|
+
export interface SseStreamOptions {
|
|
3
|
+
signal?: AbortSignal | undefined;
|
|
4
|
+
operation?: string | undefined;
|
|
5
|
+
}
|
|
6
|
+
export declare function createSseStream<E>(response: Response, options?: SseStreamOptions): NivaStream<E>;
|
|
7
|
+
export declare function streamFromResponse<E>(responsePromise: Promise<Response>, options?: SseStreamOptions): NivaStream<E>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP transport for `@niva/sdk` (NIVA-258 phase B): URL building, encoding,
|
|
3
|
+
* fetch with timeout/abort mapping, and response/error parsing. Zero runtime
|
|
4
|
+
* dependencies. No oRPC imports: the wire encoding below replicates oRPC's
|
|
5
|
+
* `StandardOpenAPISerializer` + bracket notation (verified against the
|
|
6
|
+
* installed oRPC dist files) rather than depending on it.
|
|
7
|
+
*
|
|
8
|
+
* Encoding rules (mirrored):
|
|
9
|
+
* - path params: `encodeURIComponent(String(value))` into `{name}` segments.
|
|
10
|
+
* - query: JSON-scalar pass first (Date -> ISO; NaN -> dropped via null),
|
|
11
|
+
* then bracket paths (`columns[0]`, `filter[x]`); only string | number |
|
|
12
|
+
* boolean values are sent (via String()), so null, undefined and Blob are
|
|
13
|
+
* dropped while false, 0 and "" are kept.
|
|
14
|
+
* - multipart: FormData with bracket keys; Blob/File raw; scalars String();
|
|
15
|
+
* null/undefined dropped. fetch sets the boundary: we never set
|
|
16
|
+
* content-type for multipart.
|
|
17
|
+
* - which keys go to path / query / body comes ONLY from the OPERATIONS
|
|
18
|
+
* table entry. Unknown keys are dropped (never guessed).
|
|
19
|
+
*/
|
|
20
|
+
import { NivaApiError } from "./errors.js";
|
|
21
|
+
export type OperationTableEntry = {
|
|
22
|
+
method: string;
|
|
23
|
+
path: string;
|
|
24
|
+
pathParams: readonly string[];
|
|
25
|
+
queryParams: readonly string[];
|
|
26
|
+
bodyKind: "json" | "multipart" | "none";
|
|
27
|
+
responseKind: "json" | "sse" | "binary" | "empty";
|
|
28
|
+
};
|
|
29
|
+
export interface SplitInput {
|
|
30
|
+
path: Record<string, string>;
|
|
31
|
+
query: Record<string, unknown>;
|
|
32
|
+
body: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
export declare function splitOperationInput(op: OperationTableEntry, input: unknown): SplitInput;
|
|
35
|
+
export declare function encodeQueryParams(query: Record<string, unknown>): string;
|
|
36
|
+
export declare function encodeMultipartForm(body: Record<string, unknown>): FormData;
|
|
37
|
+
export interface EncodedBody {
|
|
38
|
+
contentType: string | undefined;
|
|
39
|
+
body: BodyInit | undefined;
|
|
40
|
+
}
|
|
41
|
+
export declare function encodeBody(bodyKind: "json" | "multipart", body: Record<string, unknown>): EncodedBody;
|
|
42
|
+
export declare function mergeRequestHeaders(base: HeadersInit | undefined, extra?: HeadersInit): Headers;
|
|
43
|
+
/**
|
|
44
|
+
* fetch with a timeout covering until response headers arrive. Streams and
|
|
45
|
+
* binary bodies are NOT cut after headers. Combines the timeout with the
|
|
46
|
+
* caller signal: caller abort -> "aborted", timeout -> "timeout", any other
|
|
47
|
+
* rejection -> "network".
|
|
48
|
+
*/
|
|
49
|
+
export declare function fetchWithTimeout(fetchImpl: typeof fetch, url: string, init: RequestInit, timeoutMs: number): Promise<Response>;
|
|
50
|
+
/**
|
|
51
|
+
* Builds the NivaApiError for a non-2xx response. NIVA sets no request-id
|
|
52
|
+
* header today; `x-request-id` is read when a proxy provides one.
|
|
53
|
+
*/
|
|
54
|
+
export declare function parseApiError(response: Response, operation: string): Promise<NivaApiError>;
|
|
55
|
+
/** 204 -> undefined, anything else -> parsed JSON (wire boundary, see below). */
|
|
56
|
+
export declare function readJsonResponse<T>(response: Response): Promise<T | undefined>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-written public primitives for `@niva/sdk` (NIVA-258 phase A).
|
|
3
|
+
*
|
|
4
|
+
* The transport behind these types lands in phase B; this file only declares
|
|
5
|
+
* the shapes the generated `NivaClient` (src/generated/client.ts) refers to.
|
|
6
|
+
*/
|
|
7
|
+
/** Per-request overrides accepted by every JSON operation. */
|
|
8
|
+
export interface NivaRequestOptions {
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
timeout?: number;
|
|
11
|
+
headers?: HeadersInit;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Any JSON value: the SDK's spelling of the server's free-form JSON schemas
|
|
15
|
+
* (user maps, analysis results, enrichment fields). Recursive by design;
|
|
16
|
+
* see the generator's cut recognition for how the served shape maps here.
|
|
17
|
+
*/
|
|
18
|
+
export type NivaJsonValue = string | number | boolean | null | NivaJsonValue[] | {
|
|
19
|
+
[key: string]: NivaJsonValue;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* An SSE subscription: the event iterator plus an explicit close.
|
|
23
|
+
* The runtime (phase B) backs it with fetch + AbortController.
|
|
24
|
+
*/
|
|
25
|
+
export interface NivaStream<E> extends AsyncIterable<E> {
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/** Options for {@link createNivaClient} (declared here; implemented in client.ts). */
|
|
29
|
+
export interface NivaClientOptions {
|
|
30
|
+
/** Server root WITHOUT /api/v1 (appended by the client); trailing slash tolerated. */
|
|
31
|
+
baseUrl: string;
|
|
32
|
+
/** Fixed key or provider re-read on every request (for rotation). */
|
|
33
|
+
apiKey: string | (() => string | Promise<string>);
|
|
34
|
+
/** Per-request timeout in ms until response headers arrive. Defaults to 30_000. */
|
|
35
|
+
timeout?: number | undefined;
|
|
36
|
+
/** Extra headers or a provider resolving them per request. */
|
|
37
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>) | undefined;
|
|
38
|
+
/** Injectable fetch; defaults to globalThis.fetch. */
|
|
39
|
+
fetch?: typeof fetch | undefined;
|
|
40
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { NivaWebhookEvent, NivaWebhookEventType } from "../generated/webhooks.js";
|
|
2
|
+
export declare const DEFAULT_MAX_BODY_BYTES: number;
|
|
3
|
+
/** Handler context: abort on shutdown/cancel, attempt when a future header carries it. */
|
|
4
|
+
export interface NivaWebhookContext {
|
|
5
|
+
signal: AbortSignal;
|
|
6
|
+
attempt?: number | undefined;
|
|
7
|
+
}
|
|
8
|
+
export type NivaWebhookHandlerFn<K extends NivaWebhookEventType> = (event: NivaWebhookEvent<K>, context: NivaWebhookContext) => void | Promise<void>;
|
|
9
|
+
export interface NivaWebhookHandler {
|
|
10
|
+
on<K extends NivaWebhookEventType>(event: K, handler: NivaWebhookHandlerFn<K>): void;
|
|
11
|
+
off(event: NivaWebhookEventType): void;
|
|
12
|
+
fetch(request: Request): Promise<Response>;
|
|
13
|
+
}
|
|
14
|
+
export interface NivaWebhookHandlerOptions {
|
|
15
|
+
secret: string;
|
|
16
|
+
path?: string;
|
|
17
|
+
maxBodyBytes?: number;
|
|
18
|
+
toleranceSeconds?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function createNivaWebhookHandler(options: NivaWebhookHandlerOptions): NivaWebhookHandler;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bun adapter (NIVA-259): `Bun.serve` already speaks `Request`/`Response`,
|
|
3
|
+
* so the universal fetch plugs in directly. Only the `bun` bundle imports this.
|
|
4
|
+
*/
|
|
5
|
+
import { type NivaWebhookHandlerOptions } from "./handler.js";
|
|
6
|
+
import { type NivaWebhookServer } from "./server.js";
|
|
7
|
+
/** Handler + native Bun `serve`. `close()` is idempotent. */
|
|
8
|
+
export declare function createNivaWebhookServer(options: NivaWebhookHandlerOptions): NivaWebhookServer;
|
|
9
|
+
/** Which native adapter this bundle carries (the runtime matrix asserts it). */
|
|
10
|
+
export declare const WEBHOOK_RUNTIME = "bun";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type NivaWebhookHandlerOptions } from "./handler.js";
|
|
2
|
+
import { type NivaWebhookServer } from "./server.js";
|
|
3
|
+
/** Handler without a native server: `.serve()` throws `unsupported_runtime`. */
|
|
4
|
+
export declare function createNivaWebhookServer(options: NivaWebhookHandlerOptions): NivaWebhookServer;
|
|
5
|
+
/** Which native adapter this bundle carries (the runtime matrix asserts it). */
|
|
6
|
+
export declare const WEBHOOK_RUNTIME = "default";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deno adapter (NIVA-259): `Deno.serve` already speaks `Request`/`Response`,
|
|
3
|
+
* so the universal fetch plugs in directly. Only the `deno` bundle imports this.
|
|
4
|
+
*/
|
|
5
|
+
import { type NivaWebhookHandlerOptions } from "./handler.js";
|
|
6
|
+
import { type NivaWebhookServer } from "./server.js";
|
|
7
|
+
/** Handler + native Deno `serve`. `close()` is idempotent and waits for in-flight requests. */
|
|
8
|
+
export declare function createNivaWebhookServer(options: NivaWebhookHandlerOptions): NivaWebhookServer;
|
|
9
|
+
/** Which native adapter this bundle carries (the runtime matrix asserts it). */
|
|
10
|
+
export declare const WEBHOOK_RUNTIME = "deno";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type NivaWebhookHandlerOptions } from "./handler.js";
|
|
2
|
+
import { type NivaWebhookServer } from "./server.js";
|
|
3
|
+
/** Handler + native Node.js `serve`. `close()` is idempotent and waits for in-flight requests. */
|
|
4
|
+
export declare function createNivaWebhookServer(options: NivaWebhookHandlerOptions): NivaWebhookServer;
|
|
5
|
+
/** Which native adapter this bundle carries (the runtime matrix asserts it). */
|
|
6
|
+
export declare const WEBHOOK_RUNTIME = "node";
|