@hirefinn/sdk 0.0.0-stage → 0.2.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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +217 -2
  3. package/dist/cjs/approval.d.ts +12 -0
  4. package/dist/cjs/approval.js +19 -0
  5. package/dist/cjs/client.d.ts +30 -0
  6. package/dist/cjs/client.js +35 -0
  7. package/dist/cjs/core.d.ts +81 -0
  8. package/dist/cjs/core.js +300 -0
  9. package/dist/cjs/errors.d.ts +90 -0
  10. package/dist/cjs/errors.js +165 -0
  11. package/dist/cjs/generated/openapi.d.ts +10729 -0
  12. package/dist/cjs/generated/openapi.js +2 -0
  13. package/dist/cjs/index.d.ts +13 -0
  14. package/dist/cjs/index.js +55 -0
  15. package/dist/cjs/package.json +1 -0
  16. package/dist/cjs/pagination.d.ts +36 -0
  17. package/dist/cjs/pagination.js +124 -0
  18. package/dist/cjs/resources.d.ts +845 -0
  19. package/dist/cjs/resources.js +395 -0
  20. package/dist/cjs/types.d.ts +148 -0
  21. package/dist/cjs/types.js +2 -0
  22. package/dist/cjs/webhooks.d.ts +57 -0
  23. package/dist/cjs/webhooks.js +181 -0
  24. package/dist/esm/approval.d.ts +12 -0
  25. package/dist/esm/approval.js +16 -0
  26. package/dist/esm/client.d.ts +30 -0
  27. package/dist/esm/client.js +31 -0
  28. package/dist/esm/core.d.ts +81 -0
  29. package/dist/esm/core.js +294 -0
  30. package/dist/esm/errors.d.ts +90 -0
  31. package/dist/esm/errors.js +155 -0
  32. package/dist/esm/generated/openapi.d.ts +10729 -0
  33. package/dist/esm/generated/openapi.js +1 -0
  34. package/dist/esm/index.d.ts +13 -0
  35. package/dist/esm/index.js +8 -0
  36. package/dist/esm/package.json +1 -0
  37. package/dist/esm/pagination.d.ts +36 -0
  38. package/dist/esm/pagination.js +116 -0
  39. package/dist/esm/resources.d.ts +845 -0
  40. package/dist/esm/resources.js +377 -0
  41. package/dist/esm/types.d.ts +148 -0
  42. package/dist/esm/types.js +1 -0
  43. package/dist/esm/webhooks.d.ts +57 -0
  44. package/dist/esm/webhooks.js +140 -0
  45. package/package.json +53 -4
@@ -0,0 +1,395 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Chat = exports.ChatContacts = exports.ChatConversations = exports.Agents = exports.Approvals = exports.Cases = exports.Contacts = exports.DoNotCall = exports.Wallet = exports.Deployments = exports.Audiences = exports.PhoneNumbers = exports.Voices = exports.Finns = exports.Calls = void 0;
4
+ const errors_js_1 = require("./errors.js");
5
+ const pagination_js_1 = require("./pagination.js");
6
+ class Resource {
7
+ constructor(core) {
8
+ this.core = core;
9
+ }
10
+ async do(method, path, params = {}, options) {
11
+ const res = await this.core.rawRequest({
12
+ method,
13
+ path,
14
+ pathParams: params.path,
15
+ query: params.query,
16
+ body: params.body,
17
+ }, options);
18
+ return res.data;
19
+ }
20
+ pager(path, strategy, query, pathParams, options) {
21
+ return new pagination_js_1.Paginator(async (q) => (await this.core.rawRequest({ method: 'get', path, pathParams, query: q }, options)).data, strategy, (query ?? {}));
22
+ }
23
+ }
24
+ // ---------------------------------------------------------------- Voice
25
+ class Calls extends Resource {
26
+ /** Place an outbound call. Gets an automatic Idempotency-Key. May answer `pending_approval` (see isPendingApproval). */
27
+ create(body, options) {
28
+ return this.do('post', '/calls', { body }, options);
29
+ }
30
+ list(query, options) {
31
+ return this.do('get', '/calls', { query }, options);
32
+ }
33
+ /** Every call matching the filters, page by page. */
34
+ listAll(query, options) {
35
+ return this.pager('/calls', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
36
+ }
37
+ retrieve(callUuid, options) {
38
+ return this.do('get', '/calls/{call_uuid}', { path: { call_uuid: callUuid } }, options);
39
+ }
40
+ }
41
+ exports.Calls = Calls;
42
+ class Finns extends Resource {
43
+ list(query, options) {
44
+ return this.do('get', '/finns', { query }, options);
45
+ }
46
+ listAll(query, options) {
47
+ return this.pager('/finns', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
48
+ }
49
+ create(body, options) {
50
+ return this.do('post', '/finns', { body }, options);
51
+ }
52
+ retrieve(finnId, options) {
53
+ return this.do('get', '/finns/{finn_id}', { path: { finn_id: finnId } }, options);
54
+ }
55
+ update(finnId, body, options) {
56
+ return this.do('patch', '/finns/{finn_id}', { path: { finn_id: finnId }, body }, options);
57
+ }
58
+ delete(finnId, options) {
59
+ return this.do('delete', '/finns/{finn_id}', { path: { finn_id: finnId } }, options);
60
+ }
61
+ listKnowledgeBase(finnId, options) {
62
+ return this.do('get', '/finns/{finn_id}/knowledge_base', { path: { finn_id: finnId } }, options);
63
+ }
64
+ /** JSON body, or a FormData with the file for multipart upload. */
65
+ addKnowledgeBaseDocument(finnId, body, options) {
66
+ return this.do('post', '/finns/{finn_id}/knowledge_base', { path: { finn_id: finnId }, body }, options);
67
+ }
68
+ postCallAnalysis(finnId, query, options) {
69
+ return this.do('get', '/finns/{finn_id}/post_call_analysis', { path: { finn_id: finnId }, query }, options);
70
+ }
71
+ }
72
+ exports.Finns = Finns;
73
+ class Voices extends Resource {
74
+ list(query, options) {
75
+ return this.do('get', '/voices', { query }, options);
76
+ }
77
+ listAll(query, options) {
78
+ return this.pager('/voices', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
79
+ }
80
+ retrieve(voiceId, options) {
81
+ return this.do('get', '/voices/{voice_id}', { path: { voice_id: voiceId } }, options);
82
+ }
83
+ }
84
+ exports.Voices = Voices;
85
+ class PhoneNumbers extends Resource {
86
+ list(query, options) {
87
+ return this.do('get', '/phone_numbers', { query }, options);
88
+ }
89
+ listAll(query, options) {
90
+ return this.pager('/phone_numbers', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
91
+ }
92
+ searchAvailable(query, options) {
93
+ return this.do('get', '/phone_numbers/available', { query }, options);
94
+ }
95
+ /** Rent a number (spends credits). Gets an automatic Idempotency-Key. May answer `pending_approval`. */
96
+ rent(body, options) {
97
+ return this.do('post', '/phone_numbers', { body }, options);
98
+ }
99
+ retrieve(id, options) {
100
+ return this.do('get', '/phone_numbers/{id}', { path: { id } }, options);
101
+ }
102
+ release(id, options) {
103
+ return this.do('delete', '/phone_numbers/{id}', { path: { id } }, options);
104
+ }
105
+ }
106
+ exports.PhoneNumbers = PhoneNumbers;
107
+ class Audiences extends Resource {
108
+ list(query, options) {
109
+ return this.do('get', '/audiences', { query }, options);
110
+ }
111
+ listAll(query, options) {
112
+ return this.pager('/audiences', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
113
+ }
114
+ create(body, options) {
115
+ return this.do('post', '/audiences', { body }, options);
116
+ }
117
+ retrieve(audienceId, options) {
118
+ return this.do('get', '/audiences/{audience_id}', { path: { audience_id: audienceId } }, options);
119
+ }
120
+ archive(audienceId, options) {
121
+ return this.do('delete', '/audiences/{audience_id}', { path: { audience_id: audienceId } }, options);
122
+ }
123
+ listContacts(audienceId, query, options) {
124
+ return this.do('get', '/audiences/{audience_id}/contacts', { path: { audience_id: audienceId }, query }, options);
125
+ }
126
+ listAllContacts(audienceId, query, options) {
127
+ return this.pager('/audiences/{audience_id}/contacts', (0, pagination_js_1.offsetStrategy)(), query, { audience_id: audienceId }, options);
128
+ }
129
+ addContacts(audienceId, body, options) {
130
+ return this.do('post', '/audiences/{audience_id}/contacts', { path: { audience_id: audienceId }, body }, options);
131
+ }
132
+ removeContact(audienceId, phone, options) {
133
+ return this.do('delete', '/audiences/{audience_id}/contacts/{phone}', { path: { audience_id: audienceId, phone } }, options);
134
+ }
135
+ }
136
+ exports.Audiences = Audiences;
137
+ class Deployments extends Resource {
138
+ list(query, options) {
139
+ return this.do('get', '/deployments', { query }, options);
140
+ }
141
+ listAll(query, options) {
142
+ return this.pager('/deployments', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
143
+ }
144
+ /** Start an outbound campaign (spends credits). Gets an automatic Idempotency-Key. May answer `pending_approval`. */
145
+ createOutbound(body, options) {
146
+ return this.do('post', '/deployments', { body }, options);
147
+ }
148
+ /** Gets an automatic Idempotency-Key. */
149
+ createInbound(body, options) {
150
+ return this.do('post', '/deployments/inbound', { body }, options);
151
+ }
152
+ retrieve(id, options) {
153
+ return this.do('get', '/deployments/{id}', { path: { id } }, options);
154
+ }
155
+ stop(id, options) {
156
+ return this.do('post', '/deployments/{id}/stop', { path: { id } }, options);
157
+ }
158
+ stopInbound(finnId, options) {
159
+ return this.do('post', '/deployments/inbound/{finn_id}/stop', { path: { finn_id: finnId } }, options);
160
+ }
161
+ analytics(id, query, options) {
162
+ return this.do('get', '/deployments/{id}/analytics', { path: { id }, query }, options);
163
+ }
164
+ }
165
+ exports.Deployments = Deployments;
166
+ class Wallet extends Resource {
167
+ retrieve(options) {
168
+ return this.do('get', '/wallet', {}, options);
169
+ }
170
+ listTransactions(query, options) {
171
+ return this.do('get', '/wallet/transactions', { query }, options);
172
+ }
173
+ listAllTransactions(query, options) {
174
+ return this.pager('/wallet/transactions', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
175
+ }
176
+ }
177
+ exports.Wallet = Wallet;
178
+ class DoNotCall extends Resource {
179
+ list(query, options) {
180
+ return this.do('get', '/do_not_call', { query }, options);
181
+ }
182
+ listAll(query, options) {
183
+ return this.pager('/do_not_call', (0, pagination_js_1.offsetStrategy)(), query, undefined, options);
184
+ }
185
+ add(body, options) {
186
+ return this.do('post', '/do_not_call', { body }, options);
187
+ }
188
+ /** Needs the `voice:dnc:write` scope. */
189
+ remove(phone, options) {
190
+ return this.do('delete', '/do_not_call/{phone}', { path: { phone } }, options);
191
+ }
192
+ }
193
+ exports.DoNotCall = DoNotCall;
194
+ // ---------------------------------------------------------------- CRM
195
+ class Contacts extends Resource {
196
+ list(query, options) {
197
+ return this.do('get', '/contacts', { query }, options);
198
+ }
199
+ listAll(query, options) {
200
+ return this.pager('/contacts', (0, pagination_js_1.pageStrategy)('contacts'), query, undefined, options);
201
+ }
202
+ create(body, options) {
203
+ return this.do('post', '/contacts', { body }, options);
204
+ }
205
+ retrieve(id, options) {
206
+ return this.do('get', '/contacts/{id}', { path: { id } }, options);
207
+ }
208
+ update(id, body, options) {
209
+ return this.do('patch', '/contacts/{id}', { path: { id }, body }, options);
210
+ }
211
+ timeline(id, query, options) {
212
+ return this.do('get', '/contacts/{id}/timeline', { path: { id }, query }, options);
213
+ }
214
+ timelineAll(id, query, options) {
215
+ return this.pager('/contacts/{id}/timeline', (0, pagination_js_1.beforeStrategy)('items', 'next'), query, { id }, options);
216
+ }
217
+ addNote(id, body, options) {
218
+ return this.do('post', '/contacts/{id}/notes', { path: { id }, body }, options);
219
+ }
220
+ /** Record kinds (contact, company, ...). */
221
+ listKinds(options) {
222
+ return this.do('get', '/record_kinds', {}, options);
223
+ }
224
+ listFields(query, options) {
225
+ return this.do('get', '/fields', { query }, options);
226
+ }
227
+ }
228
+ exports.Contacts = Contacts;
229
+ // ---------------------------------------------------------------- Operator
230
+ class Cases extends Resource {
231
+ list(query, options) {
232
+ return this.do('get', '/cases', { query }, options);
233
+ }
234
+ listAll(query, options) {
235
+ return this.pager('/cases', (0, pagination_js_1.beforeStrategy)('cases', 'next_before'), query, undefined, options);
236
+ }
237
+ retrieve(id, options) {
238
+ return this.do('get', '/cases/{id}', { path: { id } }, options);
239
+ }
240
+ listEvents(id, options) {
241
+ return this.do('get', '/cases/{id}/events', { path: { id } }, options);
242
+ }
243
+ /** Start a case on an agent. Gets an automatic Idempotency-Key. May answer `pending_approval` (see isPendingApproval). */
244
+ start(agentId, body, options) {
245
+ return this.do('post', '/agents/{id}/cases', { path: { id: agentId }, body }, options);
246
+ }
247
+ }
248
+ exports.Cases = Cases;
249
+ /** Statuses that are still waiting: holds (`pending`, `running`) and Operator approvals (`open`). */
250
+ const WAITING_STATUSES = new Set(['pending', 'running', 'open', 'pending_approval']);
251
+ class Approvals extends Resource {
252
+ list(query, options) {
253
+ return this.do('get', '/approvals', { query }, options);
254
+ }
255
+ listAll(query, options) {
256
+ return this.pager('/approvals', (0, pagination_js_1.cursorStrategy)('approvals'), query, undefined, options);
257
+ }
258
+ /** An Operator approval, or a held API request (`source: 'api_hold'`) made by this caller. */
259
+ async retrieve(id, options) {
260
+ const body = await this.do('get', '/approvals/{id}', { path: { id } }, options);
261
+ return unwrapApproval(body);
262
+ }
263
+ decide(id, body, options) {
264
+ return this.do('post', '/approvals/{id}/decide', { path: { id }, body }, options);
265
+ }
266
+ approve(id, body, options) {
267
+ return this.do('post', '/approvals/{id}/approve', { path: { id }, body }, options);
268
+ }
269
+ reject(id, body, options) {
270
+ return this.do('post', '/approvals/{id}/reject', { path: { id }, body }, options);
271
+ }
272
+ /**
273
+ * Polls `GET /approvals/{id}` until the approval leaves `pending` / `running` / `open`, and returns it.
274
+ * For a held API request, `status` ends as `succeeded`, `failed`, `declined` or `expired`, and `result` holds
275
+ * the held request's own answer.
276
+ */
277
+ async wait(id, opts = {}) {
278
+ const interval = opts.intervalMs ?? 3000;
279
+ const deadline = Date.now() + (opts.timeoutMs ?? 15 * 60000);
280
+ const sleep = opts.sleep ?? defaultPollSleep;
281
+ let last = null;
282
+ for (;;) {
283
+ const approval = await this.retrieve(id, { signal: opts.signal });
284
+ opts.onPoll?.(approval);
285
+ last = typeof approval.status === 'string' ? approval.status : null;
286
+ if (last !== null && !WAITING_STATUSES.has(last))
287
+ return approval;
288
+ if (Date.now() + interval > deadline)
289
+ throw new errors_js_1.FinnApprovalTimeoutError(id, last);
290
+ await sleep(interval, opts.signal);
291
+ }
292
+ }
293
+ }
294
+ exports.Approvals = Approvals;
295
+ function unwrapApproval(body) {
296
+ const b = (body ?? {});
297
+ const inner = (typeof b.data === 'object' && b.data) || (typeof b.approval === 'object' && b.approval) || b;
298
+ return inner;
299
+ }
300
+ function defaultPollSleep(ms, signal) {
301
+ return new Promise((resolve, reject) => {
302
+ if (signal?.aborted)
303
+ return reject(signal.reason);
304
+ const t = setTimeout(resolve, ms);
305
+ signal?.addEventListener('abort', () => (clearTimeout(t), reject(signal.reason)), { once: true });
306
+ });
307
+ }
308
+ class Agents extends Resource {
309
+ list(query, options) {
310
+ return this.do('get', '/agents', { query }, options);
311
+ }
312
+ listAll(query, options) {
313
+ return this.pager('/agents', (0, pagination_js_1.cursorStrategy)('data'), query, undefined, options);
314
+ }
315
+ /** Create a draft agent (never live). Gets an automatic Idempotency-Key. */
316
+ create(body, options) {
317
+ return this.do('post', '/agents', { body }, options);
318
+ }
319
+ /** Draft an agent from a description, without saving it. */
320
+ draft(body, options) {
321
+ return this.do('post', '/agents/draft', { body }, options);
322
+ }
323
+ retrieve(id, options) {
324
+ return this.do('get', '/agents/{id}', { path: { id } }, options);
325
+ }
326
+ saveDraft(id, body, options) {
327
+ return this.do('put', '/agents/{id}/draft', { path: { id }, body }, options);
328
+ }
329
+ validate(id, options) {
330
+ return this.do('post', '/agents/{id}/validate', { path: { id } }, options);
331
+ }
332
+ test(id, body, options) {
333
+ return this.do('post', '/agents/{id}/test', { path: { id }, body }, options);
334
+ }
335
+ publish(id, options) {
336
+ return this.do('post', '/agents/{id}/publish', { path: { id } }, options);
337
+ }
338
+ /** Drive a test case one step (`action` as the spec lists it). */
339
+ driveTest(caseId, action, body, options) {
340
+ return this.do('post', '/cases/{id}/test/{action}', { path: { id: caseId, action }, body }, options);
341
+ }
342
+ }
343
+ exports.Agents = Agents;
344
+ class ChatConversations extends Resource {
345
+ list(query, options) {
346
+ return this.do('get', '/chat/conversations', { query }, options);
347
+ }
348
+ listAll(query, options) {
349
+ return this.pager('/chat/conversations', (0, pagination_js_1.cursorStrategy)('data'), query, undefined, options);
350
+ }
351
+ /** Start a conversation (sends the first message). Gets an automatic Idempotency-Key (the API requires one). May answer `pending_approval`. */
352
+ start(body, options) {
353
+ return this.do('post', '/chat/conversations', { body }, options);
354
+ }
355
+ retrieve(id, options) {
356
+ return this.do('get', '/chat/conversations/{id}', { path: { id } }, options);
357
+ }
358
+ listMessages(id, query, options) {
359
+ return this.do('get', '/chat/conversations/{id}/messages', { path: { id }, query }, options);
360
+ }
361
+ listAllMessages(id, query, options) {
362
+ return this.pager('/chat/conversations/{id}/messages', (0, pagination_js_1.cursorStrategy)('data'), query, { id }, options);
363
+ }
364
+ /** Send a message. Gets an automatic Idempotency-Key (the API requires one). May answer `pending_approval`. */
365
+ send(id, body, options) {
366
+ return this.do('post', '/chat/conversations/{id}/messages', { path: { id }, body }, options);
367
+ }
368
+ }
369
+ exports.ChatConversations = ChatConversations;
370
+ class ChatContacts extends Resource {
371
+ list(query, options) {
372
+ return this.do('get', '/chat/contacts', { query }, options);
373
+ }
374
+ listAll(query, options) {
375
+ return this.pager('/chat/contacts', (0, pagination_js_1.cursorStrategy)('data'), query, undefined, options);
376
+ }
377
+ retrieve(id, options) {
378
+ return this.do('get', '/chat/contacts/{id}', { path: { id } }, options);
379
+ }
380
+ update(id, body, options) {
381
+ return this.do('patch', '/chat/contacts/{id}', { path: { id }, body }, options);
382
+ }
383
+ }
384
+ exports.ChatContacts = ChatContacts;
385
+ class Chat extends Resource {
386
+ constructor(core) {
387
+ super(core);
388
+ this.conversations = new ChatConversations(core);
389
+ this.contacts = new ChatContacts(core);
390
+ }
391
+ listChannels(options) {
392
+ return this.do('get', '/chat/channels', {}, options);
393
+ }
394
+ }
395
+ exports.Chat = Chat;
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Type helpers over the generated OpenAPI types. They turn a (path, method) pair into its path params, query,
3
+ * body and success response, so `finn.request('get', '/calls/{call_uuid}', ...)` is checked end to end.
4
+ */
5
+ import type { components, operations, paths } from './generated/openapi.js';
6
+ export type { components, operations, paths };
7
+ /** Every schema in the spec, e.g. `Schemas['Call']`. */
8
+ export type Schemas = components['schemas'];
9
+ export type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
10
+ /** Paths that support method M. */
11
+ export type PathsFor<M extends HttpMethod> = {
12
+ [P in keyof paths]: paths[P] extends {
13
+ [K in M]: infer O;
14
+ } ? ([O] extends [undefined] ? never : P) : never;
15
+ }[keyof paths];
16
+ /** The operation at (P, M). */
17
+ export type Operation<P extends keyof paths, M extends HttpMethod> = paths[P] extends {
18
+ [K in M]: infer O;
19
+ } ? O : never;
20
+ type ParamsOf<O> = O extends {
21
+ parameters: infer X;
22
+ } ? X : Record<string, never>;
23
+ type NonNever<T> = [T] extends [never] ? undefined : T;
24
+ export type PathParamsOf<O> = ParamsOf<O> extends {
25
+ path?: infer X;
26
+ } ? NonNever<Exclude<X, undefined>> : undefined;
27
+ export type QueryOf<O> = ParamsOf<O> extends {
28
+ query?: infer X;
29
+ } ? NonNever<Exclude<X, undefined>> : undefined;
30
+ export type BodyOf<O> = O extends {
31
+ requestBody?: infer RB;
32
+ } ? Exclude<RB, undefined> extends {
33
+ content: infer C;
34
+ } ? C extends {
35
+ 'application/json': infer B;
36
+ } ? B : C extends {
37
+ 'multipart/form-data': infer B;
38
+ } ? B : undefined : undefined : undefined;
39
+ type BodyRequired<O> = O extends {
40
+ requestBody: {
41
+ content: unknown;
42
+ };
43
+ } ? true : false;
44
+ type JsonOf<R> = R extends {
45
+ content: {
46
+ 'application/json': infer B;
47
+ };
48
+ } ? B : undefined;
49
+ /** The JSON body of every 2xx response of O, as a union. */
50
+ export type ResponseOf<O> = O extends {
51
+ responses: infer R;
52
+ } ? {
53
+ [S in keyof R]: S extends 200 | 201 | 202 | 204 | '200' | '201' | '202' | '204' ? JsonOf<R[S]> : never;
54
+ }[keyof R] : unknown;
55
+ /** The `params` argument of `finn.request(method, path, params)`. */
56
+ export type RequestParams<O> = (PathParamsOf<O> extends undefined ? {
57
+ path?: undefined;
58
+ } : {
59
+ path: PathParamsOf<O>;
60
+ }) & (QueryOf<O> extends undefined ? {
61
+ query?: undefined;
62
+ } : {
63
+ query?: QueryOf<O>;
64
+ }) & (BodyOf<O> extends undefined ? {
65
+ body?: undefined;
66
+ } : BodyRequired<O> extends true ? {
67
+ body: BodyOf<O>;
68
+ } : {
69
+ body?: BodyOf<O>;
70
+ });
71
+ type HasRequiredKeys<T> = {
72
+ [K in keyof T]-?: object extends Pick<T, K> ? never : K;
73
+ }[keyof T] extends never ? false : true;
74
+ /** Rest arguments after (method, path): params are required only when the operation has required inputs. */
75
+ export type RequestArgs<O> = HasRequiredKeys<RequestParams<O>> extends true ? [params: RequestParams<O>, options?: RequestOptions] : [params?: RequestParams<O>, options?: RequestOptions];
76
+ /** Per-request options. Each one overrides the client's setting for this call only. */
77
+ export interface RequestOptions {
78
+ /**
79
+ * Idempotency-Key for this request. A retry with the same key and body within 24 hours returns the first answer.
80
+ * Pass `false` to send none, even on an endpoint where the SDK would make one up.
81
+ */
82
+ idempotencyKey?: string | false;
83
+ /** Org to act in (Finn-Org header), for tokens that can use more than one org. */
84
+ org?: string;
85
+ /** API version date (Finn-Version header), e.g. `2026-11-01`. */
86
+ version?: string;
87
+ /** Retries after the first attempt. */
88
+ maxRetries?: number;
89
+ /** Timeout per attempt, in milliseconds. */
90
+ timeoutMs?: number;
91
+ /** Cancels the request (and its retries). */
92
+ signal?: AbortSignal;
93
+ /** Extra headers. They win over the SDK's own, except Authorization. */
94
+ headers?: Record<string, string>;
95
+ }
96
+ /** The answer of an outward or spending action that is waiting for a person to approve it (HTTP 202). */
97
+ export interface PendingApproval {
98
+ status: 'pending_approval';
99
+ /** Poll `GET /approvals/{approval_id}` (or call `finn.approvals.wait(id)`) for the outcome. */
100
+ approval_id: string;
101
+ /** The page in Finn where a person approves it. */
102
+ approve_url: string;
103
+ expires_at: string | null;
104
+ /** What was held. */
105
+ action: HoldAction;
106
+ /** A short description of the held request, for showing to the person. */
107
+ summary: unknown;
108
+ }
109
+ /** The actions the API can hold for approval. */
110
+ export type HoldAction = 'call.create' | 'phone_number.rent' | 'deployment.create' | 'case.start' | 'chat.start' | 'chat.send' | 'contact.stage_move' | 'contact.import' | 'contact.form_link' | 'contact.form_submit' | 'contact.upload_link' | (string & {});
111
+ /** Status of a held API request as `GET /approvals/{id}` reports it. */
112
+ export type HoldStatus = 'pending' | 'running' | 'succeeded' | 'failed' | 'declined' | 'expired';
113
+ /** A held API request as `GET /approvals/{id}` returns it to the caller that made it. */
114
+ export interface ApprovalHold {
115
+ id: string;
116
+ object: 'approval';
117
+ source: 'api_hold';
118
+ status: HoldStatus;
119
+ action: string;
120
+ class: string;
121
+ summary: unknown;
122
+ approve_url: string | null;
123
+ expires_at: string | null;
124
+ created_at: string | null;
125
+ decided_at: string | null;
126
+ finished_at: string | null;
127
+ /** True when a run started long ago and never reported back. */
128
+ outcome_unknown: boolean;
129
+ /** The held request's own answer once it ran. */
130
+ result: {
131
+ status: number;
132
+ body: unknown;
133
+ } | null;
134
+ }
135
+ /** A response together with its HTTP status and headers. */
136
+ export interface FinnResponse<T> {
137
+ data: T;
138
+ status: number;
139
+ headers: Headers;
140
+ /** X-Request-Id. */
141
+ requestId: string | null;
142
+ /** True when the API replayed a saved answer for the same Idempotency-Key. */
143
+ idempotentReplayed: boolean;
144
+ }
145
+ /** Routes whose writes the API may hold for approval (HTTP 202 `pending_approval`). */
146
+ export type HoldableRoute = 'post /calls' | 'post /deployments' | 'post /phone_numbers' | 'post /agents/{id}/cases' | 'post /chat/conversations' | 'post /chat/conversations/{id}/messages';
147
+ /** What `request()` resolves to: the spec's success body, or PendingApproval on holdable routes. */
148
+ export type ResultOf<P extends string, M extends HttpMethod, O> = `${M} ${P}` extends HoldableRoute ? ResponseOf<O> | PendingApproval : ResponseOf<O>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,57 @@
1
+ import { FinnSdkError } from './errors.js';
2
+ /**
3
+ * Webhook signature checks.
4
+ *
5
+ * Finn signs every delivery with `X-Finn-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`
6
+ * and names the event in `X-Finn-Event`. verifyWebhook() checks the signature in constant time and refuses
7
+ * deliveries whose timestamp is too far from now, so a captured request cannot be replayed later.
8
+ */
9
+ /** Header that carries the signature. */
10
+ export declare const WEBHOOK_SIGNATURE_HEADER = "X-Finn-Signature";
11
+ /** Header that carries the event name. */
12
+ export declare const WEBHOOK_EVENT_HEADER = "X-Finn-Event";
13
+ /** Default largest gap, in seconds, between the signed timestamp and now. */
14
+ export declare const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
15
+ /** The body of every webhook delivery. */
16
+ export interface WebhookEvent<T = Record<string, unknown>> {
17
+ /** e.g. `call.completed`, `contact.created`, `case.completed`. */
18
+ event: string;
19
+ created_at: string;
20
+ data: T;
21
+ }
22
+ /** The delivery's signature is missing, malformed, wrong or too old. Answer the request with a 400. */
23
+ export declare class FinnWebhookVerificationError extends FinnSdkError {
24
+ constructor(message: string);
25
+ }
26
+ export interface VerifyWebhookOptions {
27
+ /** The raw request body, exactly as received (not re-serialised JSON). */
28
+ payload: string | Uint8Array;
29
+ /** The `X-Finn-Signature` value, or the request's headers to read it from. */
30
+ header?: string | null;
31
+ headers?: Headers | Record<string, string | string[] | undefined>;
32
+ /** The endpoint's signing secret (`whsec_...`). Pass several while you rotate secrets. */
33
+ secret: string | readonly string[];
34
+ /** Largest gap between the signed timestamp and now, in seconds. Default 300. */
35
+ toleranceSeconds?: number;
36
+ /** Current time in unix seconds (for tests). */
37
+ now?: number;
38
+ }
39
+ /** Splits `t=...,v1=...` into its timestamp and v1 signatures. */
40
+ export declare function parseSignatureHeader(header: string): {
41
+ timestamp: number;
42
+ signatures: string[];
43
+ };
44
+ /** Compares two byte arrays in time that depends only on their length. */
45
+ export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
46
+ /** Hex HMAC-SHA256 of `<timestamp>.<payload>`, the way Finn signs deliveries. */
47
+ export declare function signWebhookPayload(secret: string, timestamp: number | string, payload: string | Uint8Array): Promise<string>;
48
+ /**
49
+ * Checks a webhook delivery and returns its parsed body. Throws FinnWebhookVerificationError when the signature is
50
+ * missing, malformed or wrong, or the timestamp is more than `toleranceSeconds` (default 300) away from now.
51
+ *
52
+ * app.post('/finn-webhook', express.raw({ type: 'application/json' }), async (req, res) => {
53
+ * const event = await verifyWebhook({ payload: req.body, headers: req.headers, secret: process.env.FINN_WEBHOOK_SECRET! });
54
+ * ...
55
+ * });
56
+ */
57
+ export declare function verifyWebhook<T = Record<string, unknown>>(options: VerifyWebhookOptions): Promise<WebhookEvent<T>>;