@apideck/unify 0.40.0 → 0.41.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 (96) hide show
  1. package/jsr.json +1 -1
  2. package/lib/config.d.ts +4 -4
  3. package/lib/config.js +4 -4
  4. package/package.json +6 -2
  5. package/src/__tests__/activities.test.ts +1141 -0
  6. package/src/__tests__/agedcreditors.test.ts +280 -0
  7. package/src/__tests__/ageddebtors.test.ts +280 -0
  8. package/src/__tests__/apideckcompanies.test.ts +1082 -0
  9. package/src/__tests__/apideckcustomers.test.ts +364 -0
  10. package/src/__tests__/apideckdepartments.test.ts +497 -0
  11. package/src/__tests__/apiresources.test.ts +62 -0
  12. package/src/__tests__/apis.test.ts +216 -0
  13. package/src/__tests__/applicants.test.ts +1445 -0
  14. package/src/__tests__/applications.test.ts +515 -0
  15. package/src/__tests__/assertions.ts +13 -0
  16. package/src/__tests__/attachments.test.ts +371 -0
  17. package/src/__tests__/balancesheet.test.ts +453 -0
  18. package/src/__tests__/bankaccounts.test.ts +324 -0
  19. package/src/__tests__/bankfeedaccounts.test.ts +343 -0
  20. package/src/__tests__/bankfeedstatements.test.ts +408 -0
  21. package/src/__tests__/billpayments.test.ts +1031 -0
  22. package/src/__tests__/bills.test.ts +1630 -0
  23. package/src/__tests__/categories.test.ts +127 -0
  24. package/src/__tests__/collections.test.ts +120 -0
  25. package/src/__tests__/collectiontags.test.ts +68 -0
  26. package/src/__tests__/collectionticketcomments.test.ts +501 -0
  27. package/src/__tests__/collectiontickets.test.ts +627 -0
  28. package/src/__tests__/collectionusers.test.ts +126 -0
  29. package/src/__tests__/companies.test.ts +1642 -0
  30. package/src/__tests__/companyinfo.test.ts +219 -0
  31. package/src/__tests__/connectionconsent.test.ts +89 -0
  32. package/src/__tests__/connectionconsents.test.ts +31 -0
  33. package/src/__tests__/connectioncustommappings.test.ts +69 -0
  34. package/src/__tests__/connections.test.ts +2127 -0
  35. package/src/__tests__/connectionsettings.test.ts +687 -0
  36. package/src/__tests__/connectordocs.test.ts +29 -0
  37. package/src/__tests__/connectors.test.ts +753 -0
  38. package/src/__tests__/consumerrequestcounts.test.ts +42 -0
  39. package/src/__tests__/consumers.test.ts +523 -0
  40. package/src/__tests__/contacts.test.ts +1421 -0
  41. package/src/__tests__/createcallback.test.ts +36 -0
  42. package/src/__tests__/creditnotes.test.ts +2040 -0
  43. package/src/__tests__/customers.test.ts +1600 -0
  44. package/src/__tests__/customfields.test.ts +52 -0
  45. package/src/__tests__/custommappings.test.ts +65 -0
  46. package/src/__tests__/customobjects.test.ts +408 -0
  47. package/src/__tests__/customobjectschemas.test.ts +734 -0
  48. package/src/__tests__/departments.test.ts +529 -0
  49. package/src/__tests__/drivegroups.test.ts +496 -0
  50. package/src/__tests__/drives.test.ts +487 -0
  51. package/src/__tests__/employeepayrolls.test.ts +491 -0
  52. package/src/__tests__/employees.test.ts +2968 -0
  53. package/src/__tests__/employeeschedules.test.ts +457 -0
  54. package/src/__tests__/eventlogs.test.ts +158 -0
  55. package/src/__tests__/expensecategories.test.ts +324 -0
  56. package/src/__tests__/expensereports.test.ts +634 -0
  57. package/src/__tests__/expenses.test.ts +867 -0
  58. package/src/__tests__/files.test.ts +506 -0
  59. package/src/__tests__/files.ts +56 -0
  60. package/src/__tests__/folders.test.ts +390 -0
  61. package/src/__tests__/invoiceitems.test.ts +855 -0
  62. package/src/__tests__/invoices.test.ts +2173 -0
  63. package/src/__tests__/jobs.test.ts +1150 -0
  64. package/src/__tests__/journalentries.test.ts +1542 -0
  65. package/src/__tests__/leads.test.ts +1341 -0
  66. package/src/__tests__/ledgeraccounts.test.ts +829 -0
  67. package/src/__tests__/locations.test.ts +856 -0
  68. package/src/__tests__/logs.test.ts +137 -0
  69. package/src/__tests__/messages.test.ts +588 -0
  70. package/src/__tests__/notes.test.ts +523 -0
  71. package/src/__tests__/opportunities.test.ts +718 -0
  72. package/src/__tests__/orders.test.ts +854 -0
  73. package/src/__tests__/payments.test.ts +867 -0
  74. package/src/__tests__/payrolls.test.ts +485 -0
  75. package/src/__tests__/pipelines.test.ts +574 -0
  76. package/src/__tests__/products.test.ts +509 -0
  77. package/src/__tests__/profitandloss.test.ts +223 -0
  78. package/src/__tests__/projects.test.ts +480 -0
  79. package/src/__tests__/proxy.test.ts +41 -0
  80. package/src/__tests__/purchaseorders.test.ts +2139 -0
  81. package/src/__tests__/quotes.test.ts +678 -0
  82. package/src/__tests__/refunds.test.ts +812 -0
  83. package/src/__tests__/sessions.test.ts +72 -0
  84. package/src/__tests__/sharedlinks.test.ts +511 -0
  85. package/src/__tests__/stores.test.ts +41 -0
  86. package/src/__tests__/subsidiaries.test.ts +496 -0
  87. package/src/__tests__/suppliers.test.ts +1574 -0
  88. package/src/__tests__/taxrates.test.ts +701 -0
  89. package/src/__tests__/testclient.ts +48 -0
  90. package/src/__tests__/timeoffrequests.test.ts +588 -0
  91. package/src/__tests__/trackingcategories.test.ts +515 -0
  92. package/src/__tests__/uploadsessions.test.ts +221 -0
  93. package/src/__tests__/users.test.ts +1020 -0
  94. package/src/__tests__/validateconnection.test.ts +34 -0
  95. package/src/__tests__/webhooks.test.ts +254 -0
  96. package/src/lib/config.ts +4 -4
@@ -0,0 +1,506 @@
1
+ /*
2
+ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ */
4
+
5
+ import { expect, test } from "vitest";
6
+ import { Apideck } from "../index.js";
7
+ import { DownloadAcceptEnum, ExportAcceptEnum } from "../sdk/files.js";
8
+ import { filesToByteArray, streamToByteArray } from "./files.js";
9
+ import { createTestHTTPClient } from "./testclient.js";
10
+
11
+ test("Files File Storage Files All", async () => {
12
+ const testHttpClient = createTestHTTPClient("fileStorage.filesAll");
13
+
14
+ const apideck = new Apideck({
15
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
16
+ httpClient: testHttpClient,
17
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
18
+ });
19
+
20
+ const result = await apideck.fileStorage.files.list({
21
+ consumerId: "test-consumer",
22
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
23
+ serviceId: "salesforce",
24
+ filter: {
25
+ driveId: "1234",
26
+ folderId: "root",
27
+ shared: true,
28
+ },
29
+ sort: {
30
+ by: "updated_at",
31
+ direction: "desc",
32
+ },
33
+ passThrough: {
34
+ "search": "San Francisco",
35
+ },
36
+ fields: "id,updated_at",
37
+ });
38
+ expect(result.httpMeta.response.status).toBe(200);
39
+ expect(result.getFilesResponse).toBeDefined();
40
+ expect(result.getFilesResponse).toEqual({
41
+ statusCode: 200,
42
+ status: "OK",
43
+ service: "google-drive",
44
+ resource: "files",
45
+ operation: "all",
46
+ data: [
47
+ {
48
+ id: "12345",
49
+ downstreamId: "12345",
50
+ name: "sample.jpg",
51
+ description: "A sample image",
52
+ type: "file",
53
+ path: "/Documents/sample.jpg",
54
+ mimeType: "image/jpeg",
55
+ size: 1810673,
56
+ owner: {
57
+ id: "12345",
58
+ email: "hello@apideck.com",
59
+ name: "Elon Musk",
60
+ },
61
+ parentFolders: [
62
+ {
63
+ id: "12345",
64
+ name: "Personal",
65
+ },
66
+ {
67
+ id: "12345",
68
+ name: "Personal",
69
+ },
70
+ ],
71
+ exportFormats: [
72
+ "application/pdf",
73
+ "application/vnd.oasis.opendocument.presentation",
74
+ "text/plain",
75
+ ],
76
+ updatedBy: "12345",
77
+ createdBy: "12345",
78
+ updatedAt: new Date("2020-09-30T07:43:32.000Z"),
79
+ createdAt: new Date("2020-09-30T07:43:32.000Z"),
80
+ },
81
+ {
82
+ id: "12345",
83
+ downstreamId: "12345",
84
+ name: "sample.jpg",
85
+ description: "A sample image",
86
+ type: "file",
87
+ path: "/Documents/sample.jpg",
88
+ mimeType: "image/jpeg",
89
+ size: 1810673,
90
+ owner: {
91
+ id: "12345",
92
+ email: "hello@apideck.com",
93
+ name: "Elon Musk",
94
+ },
95
+ parentFolders: [
96
+ {
97
+ id: "12345",
98
+ name: "Personal",
99
+ },
100
+ {
101
+ id: "12345",
102
+ name: "Personal",
103
+ },
104
+ {
105
+ id: "12345",
106
+ name: "Personal",
107
+ },
108
+ ],
109
+ exportFormats: [
110
+ "application/pdf",
111
+ "application/vnd.oasis.opendocument.presentation",
112
+ "text/plain",
113
+ ],
114
+ updatedBy: "12345",
115
+ createdBy: "12345",
116
+ updatedAt: new Date("2020-09-30T07:43:32.000Z"),
117
+ createdAt: new Date("2020-09-30T07:43:32.000Z"),
118
+ },
119
+ {
120
+ id: "12345",
121
+ downstreamId: "12345",
122
+ name: "sample.jpg",
123
+ description: "A sample image",
124
+ type: "file",
125
+ path: "/Documents/sample.jpg",
126
+ mimeType: "image/jpeg",
127
+ size: 1810673,
128
+ owner: {
129
+ id: "12345",
130
+ email: "hello@apideck.com",
131
+ name: "Elon Musk",
132
+ },
133
+ parentFolders: [
134
+ {
135
+ id: "12345",
136
+ name: "Personal",
137
+ },
138
+ ],
139
+ exportFormats: [
140
+ "application/pdf",
141
+ "application/vnd.oasis.opendocument.presentation",
142
+ "text/plain",
143
+ ],
144
+ updatedBy: "12345",
145
+ createdBy: "12345",
146
+ updatedAt: new Date("2020-09-30T07:43:32.000Z"),
147
+ createdAt: new Date("2020-09-30T07:43:32.000Z"),
148
+ },
149
+ ],
150
+ meta: {
151
+ itemsOnPage: 50,
152
+ cursors: {
153
+ previous: "em9oby1jcm06OnBhZ2U6OjE=",
154
+ current: "em9oby1jcm06OnBhZ2U6OjI=",
155
+ next: "em9oby1jcm06OnBhZ2U6OjM=",
156
+ },
157
+ },
158
+ links: {
159
+ previous:
160
+ "https://unify.apideck.com/crm/companies?cursor=em9oby1jcm06OnBhZ2U6OjE%3D",
161
+ current: "https://unify.apideck.com/crm/companies",
162
+ next:
163
+ "https://unify.apideck.com/crm/companies?cursor=em9oby1jcm06OnBhZ2U6OjM",
164
+ },
165
+ });
166
+ });
167
+
168
+ test("Files File Storage Files Search", async () => {
169
+ const testHttpClient = createTestHTTPClient("fileStorage.filesSearch");
170
+
171
+ const apideck = new Apideck({
172
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
173
+ httpClient: testHttpClient,
174
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
175
+ });
176
+
177
+ const result = await apideck.fileStorage.files.search({
178
+ consumerId: "test-consumer",
179
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
180
+ serviceId: "salesforce",
181
+ passThrough: {
182
+ "search": "San Francisco",
183
+ },
184
+ fields: "id,updated_at",
185
+ filter: {
186
+ driveId: "1234",
187
+ folderId: "root",
188
+ shared: true,
189
+ },
190
+ filesSearch: {
191
+ query: "logo jpg",
192
+ },
193
+ });
194
+ expect(result.httpMeta.response.status).toBe(200);
195
+ expect(result.getFilesResponse).toBeDefined();
196
+ expect(result.getFilesResponse).toEqual({
197
+ statusCode: 200,
198
+ status: "OK",
199
+ service: "google-drive",
200
+ resource: "files",
201
+ operation: "all",
202
+ data: [
203
+ {
204
+ id: "12345",
205
+ downstreamId: "12345",
206
+ name: "sample.jpg",
207
+ description: "A sample image",
208
+ type: "file",
209
+ path: "/Documents/sample.jpg",
210
+ mimeType: "image/jpeg",
211
+ size: 1810673,
212
+ owner: {
213
+ id: "12345",
214
+ email: "hello@apideck.com",
215
+ name: "Elon Musk",
216
+ },
217
+ parentFolders: [
218
+ {
219
+ id: "12345",
220
+ name: "Personal",
221
+ },
222
+ {
223
+ id: "12345",
224
+ name: "Personal",
225
+ },
226
+ ],
227
+ exportFormats: [
228
+ "application/pdf",
229
+ "application/vnd.oasis.opendocument.presentation",
230
+ "text/plain",
231
+ ],
232
+ updatedBy: "12345",
233
+ createdBy: "12345",
234
+ updatedAt: new Date("2020-09-30T07:43:32.000Z"),
235
+ createdAt: new Date("2020-09-30T07:43:32.000Z"),
236
+ },
237
+ ],
238
+ meta: {
239
+ itemsOnPage: 50,
240
+ cursors: {
241
+ previous: "em9oby1jcm06OnBhZ2U6OjE=",
242
+ current: "em9oby1jcm06OnBhZ2U6OjI=",
243
+ next: "em9oby1jcm06OnBhZ2U6OjM=",
244
+ },
245
+ },
246
+ links: {
247
+ previous:
248
+ "https://unify.apideck.com/crm/companies?cursor=em9oby1jcm06OnBhZ2U6OjE%3D",
249
+ current: "https://unify.apideck.com/crm/companies",
250
+ next:
251
+ "https://unify.apideck.com/crm/companies?cursor=em9oby1jcm06OnBhZ2U6OjM",
252
+ },
253
+ });
254
+ });
255
+
256
+ test("Files File Storage Files One", async () => {
257
+ const testHttpClient = createTestHTTPClient("fileStorage.filesOne");
258
+
259
+ const apideck = new Apideck({
260
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
261
+ httpClient: testHttpClient,
262
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
263
+ });
264
+
265
+ const result = await apideck.fileStorage.files.get({
266
+ id: "<id>",
267
+ consumerId: "test-consumer",
268
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
269
+ serviceId: "salesforce",
270
+ fields: "id,updated_at",
271
+ });
272
+ expect(result.httpMeta.response.status).toBe(200);
273
+ expect(result.getFileResponse).toBeDefined();
274
+ expect(result.getFileResponse).toEqual({
275
+ statusCode: 200,
276
+ status: "OK",
277
+ service: "google-drive",
278
+ resource: "files",
279
+ operation: "one",
280
+ data: {
281
+ id: "12345",
282
+ downstreamId: "12345",
283
+ name: "sample.jpg",
284
+ description: "A sample image",
285
+ type: "file",
286
+ path: "/Documents/sample.jpg",
287
+ mimeType: "image/jpeg",
288
+ size: 1810673,
289
+ owner: {
290
+ id: "12345",
291
+ email: "hello@apideck.com",
292
+ name: "Elon Musk",
293
+ },
294
+ parentFolders: [
295
+ {
296
+ id: "12345",
297
+ name: "Personal",
298
+ },
299
+ {
300
+ id: "12345",
301
+ name: "Personal",
302
+ },
303
+ {
304
+ id: "12345",
305
+ name: "Personal",
306
+ },
307
+ ],
308
+ exportFormats: [
309
+ "application/pdf",
310
+ "application/vnd.oasis.opendocument.presentation",
311
+ "text/plain",
312
+ ],
313
+ updatedBy: "12345",
314
+ createdBy: "12345",
315
+ updatedAt: new Date("2020-09-30T07:43:32.000Z"),
316
+ createdAt: new Date("2020-09-30T07:43:32.000Z"),
317
+ },
318
+ });
319
+ });
320
+
321
+ test("Files File Storage Files Update", async () => {
322
+ const testHttpClient = createTestHTTPClient("fileStorage.filesUpdate");
323
+
324
+ const apideck = new Apideck({
325
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
326
+ httpClient: testHttpClient,
327
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
328
+ });
329
+
330
+ const result = await apideck.fileStorage.files.update({
331
+ id: "<id>",
332
+ consumerId: "test-consumer",
333
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
334
+ serviceId: "salesforce",
335
+ updateFileRequest: {
336
+ name: "New Name.pdf",
337
+ description: "Renamed PDF Document",
338
+ parentFolderId: "1234",
339
+ passThrough: [
340
+ {
341
+ serviceId: "<id>",
342
+ extendPaths: [
343
+ {
344
+ path: "$.nested.property",
345
+ value: {
346
+ "TaxClassificationRef": {
347
+ "value": "EUC-99990201-V1-00020000",
348
+ },
349
+ },
350
+ },
351
+ {
352
+ path: "$.nested.property",
353
+ value: {
354
+ "TaxClassificationRef": {
355
+ "value": "EUC-99990201-V1-00020000",
356
+ },
357
+ },
358
+ },
359
+ ],
360
+ },
361
+ {
362
+ serviceId: "<id>",
363
+ extendPaths: [
364
+ {
365
+ path: "$.nested.property",
366
+ value: {
367
+ "TaxClassificationRef": {
368
+ "value": "EUC-99990201-V1-00020000",
369
+ },
370
+ },
371
+ },
372
+ {
373
+ path: "$.nested.property",
374
+ value: {
375
+ "TaxClassificationRef": {
376
+ "value": "EUC-99990201-V1-00020000",
377
+ },
378
+ },
379
+ },
380
+ ],
381
+ },
382
+ {
383
+ serviceId: "<id>",
384
+ extendPaths: [
385
+ {
386
+ path: "$.nested.property",
387
+ value: {
388
+ "TaxClassificationRef": {
389
+ "value": "EUC-99990201-V1-00020000",
390
+ },
391
+ },
392
+ },
393
+ {
394
+ path: "$.nested.property",
395
+ value: {
396
+ "TaxClassificationRef": {
397
+ "value": "EUC-99990201-V1-00020000",
398
+ },
399
+ },
400
+ },
401
+ {
402
+ path: "$.nested.property",
403
+ value: {
404
+ "TaxClassificationRef": {
405
+ "value": "EUC-99990201-V1-00020000",
406
+ },
407
+ },
408
+ },
409
+ ],
410
+ },
411
+ ],
412
+ },
413
+ });
414
+ expect(result.httpMeta.response.status).toBe(200);
415
+ expect(result.updateFileResponse).toBeDefined();
416
+ expect(result.updateFileResponse).toEqual({
417
+ statusCode: 200,
418
+ status: "OK",
419
+ service: "google-drive",
420
+ resource: "files",
421
+ operation: "update",
422
+ data: {
423
+ id: "12345",
424
+ },
425
+ });
426
+ });
427
+
428
+ test("Files File Storage Files Delete", async () => {
429
+ const testHttpClient = createTestHTTPClient("fileStorage.filesDelete");
430
+
431
+ const apideck = new Apideck({
432
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
433
+ httpClient: testHttpClient,
434
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
435
+ });
436
+
437
+ const result = await apideck.fileStorage.files.delete({
438
+ id: "<id>",
439
+ consumerId: "test-consumer",
440
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
441
+ serviceId: "salesforce",
442
+ });
443
+ expect(result.httpMeta.response.status).toBe(200);
444
+ expect(result.deleteFileResponse).toBeDefined();
445
+ expect(result.deleteFileResponse).toEqual({
446
+ statusCode: 200,
447
+ status: "OK",
448
+ service: "google-drive",
449
+ resource: "files",
450
+ operation: "delete",
451
+ data: {
452
+ id: "12345",
453
+ },
454
+ });
455
+ });
456
+
457
+ test("Files File Storage Files Download", async () => {
458
+ const testHttpClient = createTestHTTPClient("fileStorage.filesDownload");
459
+
460
+ const apideck = new Apideck({
461
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
462
+ httpClient: testHttpClient,
463
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
464
+ });
465
+
466
+ const result = await apideck.fileStorage.files.download({
467
+ id: "<id>",
468
+ consumerId: "test-consumer",
469
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
470
+ serviceId: "salesforce",
471
+ fields: "id,updated_at",
472
+ }, {
473
+ acceptHeaderOverride: DownloadAcceptEnum.wildcardRootWildcard,
474
+ });
475
+ expect(result.httpMeta.response.status).toBe(200);
476
+ expect(result.getFileDownloadResponse).toBeDefined();
477
+ expect(
478
+ new Uint8Array(await streamToByteArray(result.getFileDownloadResponse)),
479
+ ).toEqual(await filesToByteArray(".speakeasy/testfiles/example.file"));
480
+ });
481
+
482
+ test("Files File Storage Files Export", async () => {
483
+ const testHttpClient = createTestHTTPClient("fileStorage.filesExport");
484
+
485
+ const apideck = new Apideck({
486
+ serverURL: process.env["TEST_SERVER_URL"] ?? "http://localhost:18080",
487
+ httpClient: testHttpClient,
488
+ apiKey: process.env["APIDECK_API_KEY"] ?? "value",
489
+ });
490
+
491
+ const result = await apideck.fileStorage.files.export({
492
+ id: "<id>",
493
+ consumerId: "test-consumer",
494
+ appId: "dSBdXd2H6Mqwfg0atXHXYcysLJE9qyn1VwBtXHX",
495
+ serviceId: "salesforce",
496
+ fields: "id,updated_at",
497
+ format: "pdf",
498
+ }, {
499
+ acceptHeaderOverride: ExportAcceptEnum.wildcardRootWildcard,
500
+ });
501
+ expect(result.httpMeta.response.status).toBe(200);
502
+ expect(result.getFileDownloadResponse).toBeDefined();
503
+ expect(
504
+ new Uint8Array(await streamToByteArray(result.getFileDownloadResponse)),
505
+ ).toEqual(await filesToByteArray(".speakeasy/testfiles/example.file"));
506
+ });
@@ -0,0 +1,56 @@
1
+ /*
2
+ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ */
4
+
5
+ import { createReadStream } from "node:fs";
6
+ import { readFile } from "node:fs/promises";
7
+ import { Readable } from "node:stream";
8
+
9
+ export function filesToStream(filePath: string): ReadableStream<Uint8Array> {
10
+ return Readable.toWeb(
11
+ createReadStream(filePath),
12
+ ) as unknown as ReadableStream<Uint8Array>;
13
+ }
14
+
15
+ export async function filesToByteArray(filePath: string): Promise<Uint8Array> {
16
+ return new Uint8Array(await readFile(filePath));
17
+ }
18
+
19
+ export async function filesToString(filePath: string): Promise<string> {
20
+ return readFile(filePath, "utf8");
21
+ }
22
+
23
+ export async function streamToByteArray(
24
+ stream?: ReadableStream<Uint8Array>,
25
+ ): Promise<Buffer> {
26
+ if (!stream) {
27
+ return Buffer.from("");
28
+ }
29
+
30
+ const chunks = [];
31
+ const reader = stream.getReader();
32
+
33
+ let done = false;
34
+ while (!done) {
35
+ const res = await reader.read();
36
+ done = res.done;
37
+ if (res.value) {
38
+ chunks.push(res.value);
39
+ }
40
+ }
41
+
42
+ return Buffer.concat(chunks);
43
+ }
44
+
45
+ export function bytesToStream(bytes: Uint8Array): ReadableStream<Uint8Array> {
46
+ return new ReadableStream({
47
+ start(controller) {
48
+ controller.enqueue(bytes);
49
+ },
50
+ pull(controller) {
51
+ controller.close();
52
+ },
53
+ cancel() {
54
+ },
55
+ });
56
+ }