@hirefinn/sdk 0.0.0-stage → 0.1.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +177 -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 +69 -0
  8. package/dist/cjs/core.js +265 -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 +11 -0
  14. package/dist/cjs/index.js +44 -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 +144 -0
  21. package/dist/cjs/types.js +2 -0
  22. package/dist/esm/approval.d.ts +12 -0
  23. package/dist/esm/approval.js +16 -0
  24. package/dist/esm/client.d.ts +30 -0
  25. package/dist/esm/client.js +31 -0
  26. package/dist/esm/core.d.ts +69 -0
  27. package/dist/esm/core.js +260 -0
  28. package/dist/esm/errors.d.ts +90 -0
  29. package/dist/esm/errors.js +155 -0
  30. package/dist/esm/generated/openapi.d.ts +10729 -0
  31. package/dist/esm/generated/openapi.js +1 -0
  32. package/dist/esm/index.d.ts +11 -0
  33. package/dist/esm/index.js +7 -0
  34. package/dist/esm/package.json +1 -0
  35. package/dist/esm/pagination.d.ts +36 -0
  36. package/dist/esm/pagination.js +116 -0
  37. package/dist/esm/resources.d.ts +845 -0
  38. package/dist/esm/resources.js +377 -0
  39. package/dist/esm/types.d.ts +144 -0
  40. package/dist/esm/types.js +1 -0
  41. package/package.json +51 -4
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ export { Finn } from './client.js';
2
+ export { Finn as default } from './client.js';
3
+ export { FinnCore, DEFAULT_BASE_URL, SDK_VERSION, AUTO_IDEMPOTENCY_ROUTES, idempotencyKey } from './core.js';
4
+ export type { FinnOptions, RawRequest } from './core.js';
5
+ export { FinnSdkError, FinnError, FinnConnectionError, FinnTimeoutError, FinnApprovalTimeoutError, codeForStatus, parseRetryAfterSeconds, } from './errors.js';
6
+ export type { FinnErrorCode, FinnFieldError } from './errors.js';
7
+ export { isPendingApproval } from './approval.js';
8
+ export { Paginator, cursorStrategy, offsetStrategy, pageStrategy, beforeStrategy } from './pagination.js';
9
+ export type { PageStrategy } from './pagination.js';
10
+ export * from './resources.js';
11
+ export type { ApprovalHold, BodyOf, FinnResponse, HoldStatus, HttpMethod, Operation, PathParamsOf, PathsFor, PendingApproval, QueryOf, RequestOptions, RequestParams, ResponseOf, ResultOf, Schemas, components, operations, paths, } from './types.js';
@@ -0,0 +1,7 @@
1
+ export { Finn } from './client.js';
2
+ export { Finn as default } from './client.js';
3
+ export { FinnCore, DEFAULT_BASE_URL, SDK_VERSION, AUTO_IDEMPOTENCY_ROUTES, idempotencyKey } from './core.js';
4
+ export { FinnSdkError, FinnError, FinnConnectionError, FinnTimeoutError, FinnApprovalTimeoutError, codeForStatus, parseRetryAfterSeconds, } from './errors.js';
5
+ export { isPendingApproval } from './approval.js';
6
+ export { Paginator, cursorStrategy, offsetStrategy, pageStrategy, beforeStrategy } from './pagination.js';
7
+ export * from './resources.js';
@@ -0,0 +1 @@
1
+ { "type": "module" }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Auto-pagination. The API has a few list styles, and `Paginator` hides them behind one async iterator:
3
+ *
4
+ * - cursor (new lists: agents, chat, approvals): `?cursor=` → `{ data|approvals, next_cursor, has_more? }`
5
+ * - offset (older voice lists: calls, finns, audiences, ...): `?limit=&offset=` → `{ data, limit, offset, total? }`
6
+ * - page (contacts): `?page=&page_size=` → `{ contacts, page, totalPages }`
7
+ * - before (cases, timeline): `?before=` → `{ cases, next_before }` or `{ items, next }`
8
+ *
9
+ * for await (const call of finn.calls.listAll({ status: 'completed' })) { ... }
10
+ * for await (const page of finn.calls.listAll().pages()) { ... }
11
+ */
12
+ type Query = Record<string, unknown>;
13
+ export interface PageStrategy<Res, Item> {
14
+ /** The items on one page. */
15
+ items(res: Res): Item[];
16
+ /** The query for the next page, or null when this was the last one. */
17
+ next(res: Res, query: Query, itemCount: number): Query | null;
18
+ }
19
+ export declare class Paginator<Item, Res = unknown> implements AsyncIterable<Item> {
20
+ #private;
21
+ constructor(fetchPage: (query: Query) => Promise<Res>, strategy: PageStrategy<Res, Item>, query?: Query);
22
+ /** Each raw page response. */
23
+ pages(): AsyncGenerator<Res, void, undefined>;
24
+ [Symbol.asyncIterator](): AsyncGenerator<Item, void, undefined>;
25
+ /** Collects items into an array (stops at `limit` items, default 10000). */
26
+ toArray(limit?: number): Promise<Item[]>;
27
+ }
28
+ /** `?cursor=` lists answering `{ [key], next_cursor, has_more? }`. */
29
+ export declare function cursorStrategy<Item>(key?: string): PageStrategy<unknown, Item>;
30
+ /** `?limit=&offset=` lists answering `{ data, limit, offset, total? }`. */
31
+ export declare function offsetStrategy<Item>(key?: string): PageStrategy<unknown, Item>;
32
+ /** `?page=&page_size=` lists answering `{ [key], page, totalPages }`. */
33
+ export declare function pageStrategy<Item>(key: string): PageStrategy<unknown, Item>;
34
+ /** `?before=` lists answering `{ [key], [nextKey] }`. */
35
+ export declare function beforeStrategy<Item>(key: string, nextKey: string): PageStrategy<unknown, Item>;
36
+ export {};
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Auto-pagination. The API has a few list styles, and `Paginator` hides them behind one async iterator:
3
+ *
4
+ * - cursor (new lists: agents, chat, approvals): `?cursor=` → `{ data|approvals, next_cursor, has_more? }`
5
+ * - offset (older voice lists: calls, finns, audiences, ...): `?limit=&offset=` → `{ data, limit, offset, total? }`
6
+ * - page (contacts): `?page=&page_size=` → `{ contacts, page, totalPages }`
7
+ * - before (cases, timeline): `?before=` → `{ cases, next_before }` or `{ items, next }`
8
+ *
9
+ * for await (const call of finn.calls.listAll({ status: 'completed' })) { ... }
10
+ * for await (const page of finn.calls.listAll().pages()) { ... }
11
+ */
12
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
13
+ if (kind === "m") throw new TypeError("Private method is not writable");
14
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
15
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
16
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
17
+ };
18
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
19
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
20
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
21
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
22
+ };
23
+ var _Paginator_fetchPage, _Paginator_strategy, _Paginator_query;
24
+ export class Paginator {
25
+ constructor(fetchPage, strategy, query = {}) {
26
+ _Paginator_fetchPage.set(this, void 0);
27
+ _Paginator_strategy.set(this, void 0);
28
+ _Paginator_query.set(this, void 0);
29
+ __classPrivateFieldSet(this, _Paginator_fetchPage, fetchPage, "f");
30
+ __classPrivateFieldSet(this, _Paginator_strategy, strategy, "f");
31
+ __classPrivateFieldSet(this, _Paginator_query, { ...query }, "f");
32
+ }
33
+ /** Each raw page response. */
34
+ async *pages() {
35
+ let query = __classPrivateFieldGet(this, _Paginator_query, "f");
36
+ while (query) {
37
+ const res = await __classPrivateFieldGet(this, _Paginator_fetchPage, "f").call(this, query);
38
+ yield res;
39
+ const count = __classPrivateFieldGet(this, _Paginator_strategy, "f").items(res).length;
40
+ query = count === 0 ? null : __classPrivateFieldGet(this, _Paginator_strategy, "f").next(res, query, count);
41
+ }
42
+ }
43
+ async *[(_Paginator_fetchPage = new WeakMap(), _Paginator_strategy = new WeakMap(), _Paginator_query = new WeakMap(), Symbol.asyncIterator)]() {
44
+ for await (const page of this.pages()) {
45
+ for (const item of __classPrivateFieldGet(this, _Paginator_strategy, "f").items(page))
46
+ yield item;
47
+ }
48
+ }
49
+ /** Collects items into an array (stops at `limit` items, default 10000). */
50
+ async toArray(limit = 10000) {
51
+ const out = [];
52
+ for await (const item of this) {
53
+ out.push(item);
54
+ if (out.length >= limit)
55
+ break;
56
+ }
57
+ return out;
58
+ }
59
+ }
60
+ const arr = (v) => (Array.isArray(v) ? v : []);
61
+ const rec = (v) => (typeof v === 'object' && v !== null ? v : {});
62
+ /** `?cursor=` lists answering `{ [key], next_cursor, has_more? }`. */
63
+ export function cursorStrategy(key = 'data') {
64
+ return {
65
+ items: (res) => arr(rec(res)[key]),
66
+ next: (res, query) => {
67
+ const r = rec(res);
68
+ const cursor = r.next_cursor;
69
+ if (r.has_more === false || typeof cursor !== 'string' || cursor === '')
70
+ return null;
71
+ return { ...query, cursor };
72
+ },
73
+ };
74
+ }
75
+ /** `?limit=&offset=` lists answering `{ data, limit, offset, total? }`. */
76
+ export function offsetStrategy(key = 'data') {
77
+ return {
78
+ items: (res) => arr(rec(res)[key]),
79
+ next: (res, query, count) => {
80
+ const r = rec(res);
81
+ const offset = typeof r.offset === 'number' ? r.offset : Number(query.offset ?? 0);
82
+ const limit = typeof r.limit === 'number' ? r.limit : Number(query.limit ?? count);
83
+ const nextOffset = offset + count;
84
+ if (typeof r.total === 'number' && nextOffset >= r.total)
85
+ return null;
86
+ if (count < limit)
87
+ return null;
88
+ return { ...query, offset: nextOffset };
89
+ },
90
+ };
91
+ }
92
+ /** `?page=&page_size=` lists answering `{ [key], page, totalPages }`. */
93
+ export function pageStrategy(key) {
94
+ return {
95
+ items: (res) => arr(rec(res)[key]),
96
+ next: (res, query) => {
97
+ const r = rec(res);
98
+ const page = typeof r.page === 'number' ? r.page : Number(query.page ?? 1);
99
+ if (typeof r.totalPages === 'number' && page >= r.totalPages)
100
+ return null;
101
+ return { ...query, page: page + 1 };
102
+ },
103
+ };
104
+ }
105
+ /** `?before=` lists answering `{ [key], [nextKey] }`. */
106
+ export function beforeStrategy(key, nextKey) {
107
+ return {
108
+ items: (res) => arr(rec(res)[key]),
109
+ next: (res, query) => {
110
+ const next = rec(res)[nextKey];
111
+ if (next === null || next === undefined || next === '')
112
+ return null;
113
+ return { ...query, before: next };
114
+ },
115
+ };
116
+ }