@eigenpal/sdk 0.25.13 → 1.0.0-rc.1

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 (73) hide show
  1. package/README.md +27 -105
  2. package/dist/client.d.ts +164 -0
  3. package/dist/client.js +234 -0
  4. package/dist/core.d.ts +82 -0
  5. package/dist/core.js +311 -0
  6. package/dist/errors.d.ts +64 -0
  7. package/dist/errors.js +132 -0
  8. package/dist/generated/operations.gen.d.ts +336 -0
  9. package/dist/generated/operations.gen.js +135 -0
  10. package/dist/generated/public.gen.d.ts +2 -0
  11. package/dist/generated/public.gen.js +2 -0
  12. package/dist/generated/resources.gen.d.ts +490 -0
  13. package/dist/generated/resources.gen.js +659 -0
  14. package/dist/generated/types.gen.d.ts +2457 -0
  15. package/dist/generated/types.gen.js +2 -0
  16. package/dist/index.d.ts +20 -0
  17. package/dist/index.js +15 -0
  18. package/dist/lib/fetch-body.d.ts +23 -0
  19. package/dist/lib/fetch-body.js +66 -0
  20. package/dist/lib/files.d.ts +134 -0
  21. package/dist/lib/files.js +296 -0
  22. package/dist/lib/upload-app-chunked.d.ts +36 -0
  23. package/dist/lib/upload-app-chunked.js +94 -0
  24. package/dist/lib/upload-limits.d.ts +32 -0
  25. package/dist/lib/upload-limits.js +60 -0
  26. package/dist/lib/upload-presigned-multipart.d.ts +74 -0
  27. package/dist/lib/upload-presigned-multipart.js +212 -0
  28. package/dist/page.d.ts +30 -0
  29. package/dist/page.js +38 -0
  30. package/dist/resources.d.ts +85 -0
  31. package/dist/resources.js +247 -0
  32. package/dist/telemetry.d.ts +20 -0
  33. package/{src/telemetry.ts → dist/telemetry.js} +26 -31
  34. package/dist/upload-file.d.ts +31 -0
  35. package/dist/upload-file.js +286 -0
  36. package/{src/upload.ts → dist/upload.d.ts} +4 -4
  37. package/dist/upload.js +12 -0
  38. package/package.json +19 -15
  39. package/src/client.ts +0 -656
  40. package/src/errors.ts +0 -130
  41. package/src/generated/client/client.gen.ts +0 -277
  42. package/src/generated/client/index.ts +0 -27
  43. package/src/generated/client/types.gen.ts +0 -218
  44. package/src/generated/client/utils.gen.ts +0 -316
  45. package/src/generated/client.gen.ts +0 -18
  46. package/src/generated/core/auth.gen.ts +0 -48
  47. package/src/generated/core/bodySerializer.gen.ts +0 -82
  48. package/src/generated/core/params.gen.ts +0 -178
  49. package/src/generated/core/pathSerializer.gen.ts +0 -171
  50. package/src/generated/core/queryKeySerializer.gen.ts +0 -117
  51. package/src/generated/core/serverSentEvents.gen.ts +0 -242
  52. package/src/generated/core/types.gen.ts +0 -110
  53. package/src/generated/core/utils.gen.ts +0 -140
  54. package/src/generated/index.ts +0 -4
  55. package/src/generated/sdk.gen.ts +0 -1361
  56. package/src/generated/types.gen.ts +0 -9282
  57. package/src/index.ts +0 -120
  58. package/src/lib/fetch-body.ts +0 -92
  59. package/src/lib/files.ts +0 -390
  60. package/src/lib/upload-app-chunked.ts +0 -121
  61. package/src/lib/upload-limits.ts +0 -72
  62. package/src/lib/upload-presigned-multipart.ts +0 -285
  63. package/src/resources/auth.ts +0 -18
  64. package/src/resources/automations.ts +0 -830
  65. package/src/resources/dataset-review-requests.ts +0 -38
  66. package/src/resources/email-servers.ts +0 -95
  67. package/src/resources/files.ts +0 -592
  68. package/src/resources/folders.ts +0 -77
  69. package/src/resources/human-reviews.ts +0 -106
  70. package/src/resources/models.ts +0 -37
  71. package/src/resources/runs.ts +0 -350
  72. package/src/resources/templates.ts +0 -153
  73. package/src/runtime-config.ts +0 -14
@@ -1,592 +0,0 @@
1
- import type { OperationResult, RequestDispatchOptions } from '../client';
2
- import { EigenpalError } from '../errors';
3
- import type { Client } from '../generated/client';
4
- import {
5
- filesContentGet,
6
- filesDelete,
7
- filesGet,
8
- filesUploadsAbort,
9
- filesUploadsComplete,
10
- filesUploadsCreate,
11
- filesUploadsGet,
12
- filesUploadsPartsList,
13
- filesUploadsPartsPresign,
14
- } from '../generated/sdk.gen';
15
- import type {
16
- FilesDeleteResponse,
17
- FilesGetResponse,
18
- FilesUploadsAbortResponse,
19
- FilesUploadsChunksPutResponse,
20
- FilesUploadsCompleteResponse,
21
- FilesUploadsCreateResponse,
22
- FilesUploadsGetResponse,
23
- FilesUploadsPartsListResponse,
24
- FilesUploadsPartsPresignResponse,
25
- } from '../generated/types.gen';
26
- import {
27
- byteViewToArrayBuffer,
28
- destroyUnreadNodeReadable,
29
- ensureNodeReadableFullySent,
30
- isNodeReadableStream,
31
- toPutBody,
32
- type StreamableRequestInit,
33
- } from '../lib/fetch-body';
34
- import {
35
- isFileDescriptor,
36
- isFileInput,
37
- isPathFileInput,
38
- isStreamFactoryFileInput,
39
- resolveFileBlob,
40
- statUploadSize,
41
- type FileInput,
42
- } from '../lib/files';
43
- import {
44
- annotateHttpChunkedCompleteFailure,
45
- ChunkUploadHttpError,
46
- shouldAbortHttpChunkedUploadSession,
47
- uploadHttpChunks,
48
- } from '../lib/upload-app-chunked';
49
- import {
50
- annotateMultipartCompleteFailure,
51
- annotatePresignedPutCompleteFailure,
52
- filterStorageHeaders,
53
- PartUploadHttpError,
54
- shouldAbortMultipartUploadSession,
55
- shouldAbortPresignedPutUploadSession,
56
- uploadPresignedMultipartParts,
57
- type ListedUploadPart,
58
- } from '../lib/upload-presigned-multipart';
59
-
60
- type Dispatch = <T>(
61
- call: () => Promise<OperationResult<T>>,
62
- options?: RequestDispatchOptions
63
- ) => Promise<T>;
64
- type SignalOptions = { signal?: AbortSignal };
65
- type UploadOptions = SignalOptions & {
66
- /** Required only when `file` is a nameless Blob. */
67
- filename?: string;
68
- /**
69
- * Tenant-scoped idempotency key for upload-session creation. Generated when
70
- * omitted so SDK retries of a lost create response reuse the same reservation.
71
- */
72
- idempotencyKey?: string;
73
- /**
74
- * Optional lifecycle marker. Pass `run-input` for automatic `client.run`
75
- * pre-uploads so the server can retain them for retries and reap them after 24 hours.
76
- * Explicit `files.upload` callers should omit this (durable reusable file).
77
- */
78
- purpose?: 'run-input';
79
- onProgress?: (uploadedBytes: number, totalBytes: number) => void;
80
- };
81
-
82
- const UPLOAD_ABORT_CLEANUP_TIMEOUT_MS = 10_000;
83
-
84
- function uploadAbortCleanupSignal(): AbortSignal {
85
- return AbortSignal.timeout(UPLOAD_ABORT_CLEANUP_TIMEOUT_MS);
86
- }
87
-
88
- export type CreateUploadInput = {
89
- filename: string;
90
- contentType: string;
91
- size: number;
92
- /** Compatible with server `CreateFileUploadSessionRequest.idempotencyKey`. */
93
- idempotencyKey?: string;
94
- purpose?: 'run-input';
95
- };
96
-
97
- function newIdempotencyKey(): string {
98
- if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
99
- return crypto.randomUUID();
100
- }
101
- return `upload_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
102
- }
103
-
104
- type UploadableFile = FileInput | Uint8Array | ArrayBuffer;
105
-
106
- export class FilesResource {
107
- constructor(
108
- private readonly client: Client,
109
- private readonly dispatch: Dispatch
110
- ) {}
111
-
112
- async upload(
113
- file: UploadableFile,
114
- options: UploadOptions = {}
115
- ): Promise<FilesUploadsCompleteResponse> {
116
- const source = await resolveUploadableSource(file, options.filename);
117
- const idempotencyKey = options.idempotencyKey ?? newIdempotencyKey();
118
- const negotiation = await this.createUpload(
119
- {
120
- filename: source.filename,
121
- contentType: source.contentType,
122
- size: source.size,
123
- idempotencyKey,
124
- ...(options.purpose ? { purpose: options.purpose } : {}),
125
- },
126
- options
127
- );
128
-
129
- if (negotiation.transport === 'presigned-multipart') {
130
- if (!source.replayable) {
131
- throw new Error(
132
- 'Cannot resume a multipart upload from a one-shot stream. Pass a file path, Blob, or a reusable stream factory.'
133
- );
134
- }
135
- let partsReady = false;
136
- try {
137
- await uploadPresignedMultipartParts({
138
- partCount: negotiation.partCount,
139
- partSizeBytes: negotiation.partSizeBytes,
140
- totalSize: source.size,
141
- signal: options.signal,
142
- onProgress: options.onProgress,
143
- listParts: async () => {
144
- const listed = await this.dispatch(
145
- () =>
146
- this.client.get({
147
- url: negotiation.partsUrl,
148
- signal: options.signal,
149
- }) as Promise<OperationResult<{ parts?: ListedUploadPart[] }>>
150
- );
151
- return listed.parts ?? [];
152
- },
153
- presignPart: async (partNumber) => {
154
- return this.dispatch(
155
- () =>
156
- this.client.post({
157
- url: negotiation.partsUrl,
158
- body: { partNumber } as never,
159
- signal: options.signal,
160
- }) as Promise<
161
- OperationResult<{
162
- url: string;
163
- headers?: Record<string, string>;
164
- partSizeBytes: number;
165
- }>
166
- >
167
- );
168
- },
169
- putPart: async ({ url, headers, start, length }) => {
170
- const body = await source.openPart(start, length);
171
- const response = await putStorage(
172
- url,
173
- headers,
174
- body,
175
- options.signal,
176
- source.stripContentLength,
177
- length
178
- );
179
- if (!response.ok) throw new PartUploadHttpError(response.status);
180
- },
181
- });
182
- partsReady = true;
183
- return await this.dispatch(
184
- () =>
185
- this.client.post({
186
- url: negotiation.completeUrl,
187
- body: {} as never,
188
- signal: options.signal,
189
- }) as Promise<OperationResult<FilesUploadsCompleteResponse>>
190
- );
191
- } catch (error) {
192
- if (shouldAbortMultipartUploadSession({ partsReady })) {
193
- await this.abortUpload(negotiation.uploadId, {
194
- signal: uploadAbortCleanupSignal(),
195
- }).catch(() => undefined);
196
- throw error;
197
- }
198
- throw annotateMultipartCompleteFailure(negotiation.uploadId, error);
199
- }
200
- }
201
-
202
- if (negotiation.transport === 'http-chunked') {
203
- if (!source.replayable) {
204
- throw new Error(
205
- 'Cannot resume a chunked upload from a one-shot stream. Pass a file path, Blob, or a reusable stream factory.'
206
- );
207
- }
208
- let chunksReady = false;
209
- try {
210
- await uploadHttpChunks({
211
- chunkCount: negotiation.chunkCount,
212
- chunkSizeBytes: negotiation.chunkSizeBytes,
213
- totalSize: source.size,
214
- signal: options.signal,
215
- onProgress: options.onProgress,
216
- putChunk: async ({ chunkIndex, start, length }) => {
217
- const body = await source.openPart(start, length);
218
- try {
219
- await this.dispatch(
220
- () =>
221
- this.client.put({
222
- url: `${negotiation.chunksUrl}/${chunkIndex}`,
223
- body: body as never,
224
- bodySerializer: null,
225
- headers: { 'Content-Type': 'application/octet-stream' },
226
- signal: options.signal,
227
- }) as Promise<OperationResult<FilesUploadsChunksPutResponse>>
228
- );
229
- } catch (error) {
230
- if (error instanceof EigenpalError) throw new ChunkUploadHttpError(error.status);
231
- throw error;
232
- }
233
- },
234
- });
235
- chunksReady = true;
236
- return await this.dispatch(
237
- () =>
238
- this.client.post({
239
- url: negotiation.completeUrl,
240
- body: {} as never,
241
- signal: options.signal,
242
- }) as Promise<OperationResult<FilesUploadsCompleteResponse>>
243
- );
244
- } catch (error) {
245
- if (shouldAbortHttpChunkedUploadSession({ chunksReady })) {
246
- await this.abortUpload(negotiation.uploadId, {
247
- signal: uploadAbortCleanupSignal(),
248
- }).catch(() => undefined);
249
- throw error;
250
- }
251
- throw annotateHttpChunkedCompleteFailure(negotiation.uploadId, error);
252
- }
253
- }
254
-
255
- if (negotiation.transport === 'presigned-put') {
256
- let putReady = false;
257
- try {
258
- const body = await source.openPart(0, source.size);
259
- const response = await putStorage(
260
- negotiation.url,
261
- (negotiation.headers ?? {}) as Record<string, string>,
262
- body,
263
- options.signal,
264
- source.stripContentLength,
265
- source.size
266
- );
267
- if (!response.ok) {
268
- throw new Error(`Storage upload failed (${response.status}); retry the upload`);
269
- }
270
- putReady = true;
271
- options.onProgress?.(source.size, source.size);
272
- } catch (error) {
273
- if (shouldAbortPresignedPutUploadSession({ putReady })) {
274
- await this.abortUpload(negotiation.uploadId, {
275
- signal: uploadAbortCleanupSignal(),
276
- }).catch(() => undefined);
277
- }
278
- throw error;
279
- }
280
- try {
281
- return await this.completeUpload(negotiation.uploadId, options);
282
- } catch (error) {
283
- throw annotatePresignedPutCompleteFailure(negotiation.uploadId, error);
284
- }
285
- }
286
-
287
- const blob = await source.asBlob();
288
- const form = new FormData();
289
- form.append('file', blob, source.filename);
290
- if (options.purpose) form.append('purpose', options.purpose);
291
- const uploaded = await this.dispatch(
292
- () =>
293
- this.client.post({
294
- url: negotiation.url,
295
- body: form as never,
296
- bodySerializer: null,
297
- headers: { 'Content-Type': null },
298
- signal: options.signal,
299
- }) as Promise<OperationResult<FilesUploadsCompleteResponse>>
300
- );
301
- options.onProgress?.(source.size, source.size);
302
- return uploaded;
303
- }
304
-
305
- async createUpload(
306
- input: CreateUploadInput,
307
- options: SignalOptions = {}
308
- ): Promise<FilesUploadsCreateResponse> {
309
- const body = {
310
- filename: input.filename,
311
- contentType: input.contentType,
312
- size: input.size,
313
- ...(input.idempotencyKey ? { idempotencyKey: input.idempotencyKey } : {}),
314
- ...(input.purpose ? { purpose: input.purpose } : {}),
315
- };
316
- return this.dispatch(() =>
317
- filesUploadsCreate({
318
- client: this.client,
319
- // Server accepts optional idempotencyKey; generated types may lag the schema.
320
- body: body as never,
321
- signal: options.signal,
322
- })
323
- );
324
- }
325
-
326
- async completeUpload(
327
- uploadId: string,
328
- options: SignalOptions = {}
329
- ): Promise<FilesUploadsCompleteResponse> {
330
- return this.dispatch(() =>
331
- filesUploadsComplete({
332
- client: this.client,
333
- path: { uploadId },
334
- signal: options.signal,
335
- })
336
- );
337
- }
338
-
339
- async abortUpload(
340
- uploadId: string,
341
- options: SignalOptions = {}
342
- ): Promise<FilesUploadsAbortResponse> {
343
- return this.dispatch(() =>
344
- filesUploadsAbort({
345
- client: this.client,
346
- path: { uploadId },
347
- signal: options.signal,
348
- })
349
- );
350
- }
351
-
352
- async getUpload(uploadId: string, options: SignalOptions = {}): Promise<FilesUploadsGetResponse> {
353
- return this.dispatch(() =>
354
- filesUploadsGet({
355
- client: this.client,
356
- path: { uploadId },
357
- signal: options.signal,
358
- })
359
- );
360
- }
361
-
362
- async listUploadParts(
363
- uploadId: string,
364
- options: SignalOptions = {}
365
- ): Promise<FilesUploadsPartsListResponse> {
366
- return this.dispatch(() =>
367
- filesUploadsPartsList({
368
- client: this.client,
369
- path: { uploadId },
370
- signal: options.signal,
371
- })
372
- );
373
- }
374
-
375
- async presignUploadPart(
376
- uploadId: string,
377
- partNumber: number,
378
- options: SignalOptions = {}
379
- ): Promise<FilesUploadsPartsPresignResponse> {
380
- return this.dispatch(() =>
381
- filesUploadsPartsPresign({
382
- client: this.client,
383
- path: { uploadId },
384
- body: { partNumber },
385
- signal: options.signal,
386
- })
387
- );
388
- }
389
-
390
- async get(fileId: string, options: SignalOptions = {}): Promise<FilesGetResponse> {
391
- return this.dispatch(() =>
392
- filesGet({ client: this.client, path: { id: fileId }, signal: options.signal })
393
- );
394
- }
395
-
396
- async download(fileId: string, options: SignalOptions = {}): Promise<Blob> {
397
- return this.dispatch(
398
- async () => {
399
- const response = await filesContentGet({
400
- client: this.client,
401
- path: { id: fileId },
402
- parseAs: 'blob',
403
- signal: options.signal,
404
- });
405
- return response as OperationResult<Blob>;
406
- },
407
- { responseKind: 'binary' }
408
- );
409
- }
410
-
411
- async delete(fileId: string, options: SignalOptions = {}): Promise<FilesDeleteResponse> {
412
- return this.dispatch(() =>
413
- filesDelete({ client: this.client, path: { id: fileId }, signal: options.signal })
414
- );
415
- }
416
- }
417
-
418
- type ResolvedUploadSource = {
419
- filename: string;
420
- contentType: string;
421
- size: number;
422
- replayable: boolean;
423
- stripContentLength: boolean;
424
- openPart: (start: number, length: number) => Promise<BodyInit>;
425
- asBlob: () => Promise<Blob>;
426
- };
427
-
428
- async function resolveUploadableSource(
429
- file: UploadableFile,
430
- filenameOption?: string
431
- ): Promise<ResolvedUploadSource> {
432
- if (file instanceof Uint8Array) {
433
- const filename = filenameOption || 'file';
434
- return {
435
- filename,
436
- contentType: 'application/octet-stream',
437
- size: file.byteLength,
438
- replayable: true,
439
- stripContentLength: false,
440
- openPart: async (start, length) =>
441
- byteViewToArrayBuffer(file.subarray(start, start + length)),
442
- asBlob: async () => new Blob([file as BlobPart]),
443
- };
444
- }
445
- if (file instanceof ArrayBuffer) {
446
- const filename = filenameOption || 'file';
447
- const bytes = new Uint8Array(file);
448
- return {
449
- filename,
450
- contentType: 'application/octet-stream',
451
- size: bytes.byteLength,
452
- replayable: true,
453
- stripContentLength: false,
454
- openPart: async (start, length) =>
455
- byteViewToArrayBuffer(bytes.subarray(start, start + length)),
456
- asBlob: async () => new Blob([bytes as BlobPart]),
457
- };
458
- }
459
-
460
- if (!isFileInput(file)) {
461
- throw new Error('filename is required when uploading a Blob');
462
- }
463
-
464
- if (typeof Blob !== 'undefined' && file instanceof Blob) {
465
- const filename =
466
- filenameOption ??
467
- (typeof File !== 'undefined' && file instanceof File ? file.name : undefined);
468
- if (!filename) throw new Error('filename is required when uploading a Blob');
469
- return {
470
- filename,
471
- contentType: file.type || 'application/octet-stream',
472
- size: file.size,
473
- replayable: true,
474
- stripContentLength: true,
475
- openPart: async (start, length) => file.slice(start, start + length),
476
- asBlob: async () => file,
477
- };
478
- }
479
-
480
- if (isPathFileInput(file) || hasNodePath(file)) {
481
- const filePath = isPathFileInput(file) ? file.path : (file as { path: string }).path;
482
- const meta = await statUploadSize(file);
483
- const filename = filenameOption || meta.filename;
484
- return {
485
- filename,
486
- contentType: meta.contentType,
487
- size: meta.size,
488
- replayable: true,
489
- stripContentLength: false,
490
- openPart: async (start, length) => {
491
- if (length === 0) return byteViewToArrayBuffer(new Uint8Array());
492
- const { createReadStream } = await import('node:fs');
493
- return toPutBody(
494
- createReadStream(filePath, {
495
- start,
496
- end: start + length - 1,
497
- })
498
- );
499
- },
500
- asBlob: async () => (await resolveFileBlob(file)).blob,
501
- };
502
- }
503
-
504
- if (isStreamFactoryFileInput(file)) {
505
- return {
506
- filename: filenameOption || file.filename,
507
- contentType: file.mimeType || 'application/octet-stream',
508
- size: file.size,
509
- replayable: true,
510
- stripContentLength: false,
511
- openPart: async (start, length) => {
512
- const part = file.open(start, length);
513
- if (part instanceof Uint8Array) return byteViewToArrayBuffer(part);
514
- if (isNodeReadableStream(part)) return toPutBody(part);
515
- return part;
516
- },
517
- asBlob: async () => (await resolveFileBlob(file)).blob,
518
- };
519
- }
520
-
521
- if (isFileDescriptor(file)) {
522
- const filename = filenameOption || file.filename;
523
- const type = file.mimeType || 'application/octet-stream';
524
- if (typeof Blob !== 'undefined' && file.content instanceof Blob) {
525
- const blob = file.content;
526
- return {
527
- filename,
528
- contentType: type,
529
- size: blob.size,
530
- replayable: true,
531
- stripContentLength: true,
532
- openPart: async (start, length) => blob.slice(start, start + length),
533
- asBlob: async () => blob,
534
- };
535
- }
536
- const bytes =
537
- file.content instanceof ArrayBuffer
538
- ? new Uint8Array(file.content)
539
- : new Uint8Array(
540
- (file.content as ArrayBufferView).buffer,
541
- (file.content as ArrayBufferView).byteOffset,
542
- (file.content as ArrayBufferView).byteLength
543
- );
544
- return {
545
- filename,
546
- contentType: type,
547
- size: bytes.byteLength,
548
- replayable: true,
549
- stripContentLength: false,
550
- openPart: async (start, length) =>
551
- byteViewToArrayBuffer(bytes.subarray(start, start + length)),
552
- asBlob: async () => new Blob([bytes as BlobPart], { type }),
553
- };
554
- }
555
-
556
- throw new Error(
557
- 'Cannot upload a one-shot stream. Pass a file path, Blob, or a reusable stream factory.'
558
- );
559
- }
560
-
561
- function hasNodePath(file: FileInput): file is FileInput & { path: string } {
562
- return typeof (file as { path?: unknown }).path === 'string';
563
- }
564
-
565
- async function putStorage(
566
- url: string,
567
- headers: Record<string, string>,
568
- body: BodyInit,
569
- signal: AbortSignal | undefined,
570
- stripContentLength: boolean,
571
- expectedByteLength?: number
572
- ): Promise<Response> {
573
- const stream = isNodeReadableStream(body);
574
- const init: StreamableRequestInit = {
575
- method: 'PUT',
576
- headers: filterStorageHeaders(headers, stripContentLength && !stream),
577
- body,
578
- signal,
579
- ...(stream ? { duplex: 'half' as const } : {}),
580
- };
581
- let response: Response;
582
- try {
583
- response = await fetch(url, init);
584
- } catch (error) {
585
- if (stream) destroyUnreadNodeReadable(body as NodeJS.ReadableStream);
586
- throw error;
587
- }
588
- if (stream) {
589
- await ensureNodeReadableFullySent(body as NodeJS.ReadableStream, expectedByteLength);
590
- }
591
- return response;
592
- }
@@ -1,77 +0,0 @@
1
- import type { OperationResult } from '../client';
2
- import type { Client } from '../generated/client';
3
- import {
4
- foldersCreate,
5
- foldersDelete,
6
- foldersGet,
7
- foldersList,
8
- foldersUpdate,
9
- } from '../generated/sdk.gen';
10
- import type {
11
- CreateFolderRequest,
12
- DeleteFolderResponse,
13
- Folder,
14
- FoldersListData,
15
- ListFoldersResponse,
16
- UpdateFolderRequest,
17
- } from '../generated/types.gen';
18
-
19
- type Dispatch = <T>(call: () => Promise<OperationResult<T>>) => Promise<T>;
20
- type SignalOptions = { signal?: AbortSignal };
21
-
22
- export type ListFoldersOptions = NonNullable<FoldersListData['query']> & SignalOptions;
23
-
24
- export class FoldersResource {
25
- constructor(
26
- private readonly client: Client,
27
- private readonly dispatch: Dispatch
28
- ) {}
29
-
30
- async list(options: ListFoldersOptions): Promise<ListFoldersResponse> {
31
- const { signal, ...query } = options;
32
- return this.dispatch(() =>
33
- foldersList({
34
- client: this.client,
35
- query,
36
- signal,
37
- })
38
- );
39
- }
40
-
41
- async get(id: string, options: SignalOptions = {}): Promise<Folder> {
42
- return this.dispatch(() =>
43
- foldersGet({ client: this.client, path: { id }, signal: options.signal })
44
- );
45
- }
46
-
47
- async create(body: CreateFolderRequest, options: SignalOptions = {}): Promise<Folder> {
48
- return this.dispatch(() =>
49
- foldersCreate({
50
- client: this.client,
51
- body,
52
- signal: options.signal,
53
- })
54
- );
55
- }
56
-
57
- async update(
58
- id: string,
59
- body: UpdateFolderRequest,
60
- options: SignalOptions = {}
61
- ): Promise<Folder> {
62
- return this.dispatch(() =>
63
- foldersUpdate({
64
- client: this.client,
65
- path: { id },
66
- body,
67
- signal: options.signal,
68
- })
69
- );
70
- }
71
-
72
- async delete(id: string, options: SignalOptions = {}): Promise<DeleteFolderResponse> {
73
- return this.dispatch(() =>
74
- foldersDelete({ client: this.client, path: { id }, signal: options.signal })
75
- );
76
- }
77
- }