agentmail 0.2.19 → 0.2.21

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/README.md CHANGED
@@ -0,0 +1,658 @@
1
+ # Agentmail TypeScript Library
2
+
3
+ [![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fagentmail-to%2Fagentmail-node)
4
+ [![npm shield](https://img.shields.io/npm/v/agentmail)](https://www.npmjs.com/package/agentmail)
5
+
6
+ The Agentmail TypeScript library provides convenient access to the Agentmail APIs from TypeScript.
7
+
8
+ ## Table of Contents
9
+
10
+ - [Installation](#installation)
11
+ - [Reference](#reference)
12
+ - [Usage](#usage)
13
+ - [Request and Response Types](#request-and-response-types)
14
+ - [Exception Handling](#exception-handling)
15
+ - [Binary Response](#binary-response)
16
+ - [Advanced](#advanced)
17
+ - [Additional Headers](#additional-headers)
18
+ - [Additional Query String Parameters](#additional-query-string-parameters)
19
+ - [Retries](#retries)
20
+ - [Timeouts](#timeouts)
21
+ - [Aborting Requests](#aborting-requests)
22
+ - [Access Raw Response Data](#access-raw-response-data)
23
+ - [Logging](#logging)
24
+ - [Runtime Compatibility](#runtime-compatibility)
25
+ - [Contributing](#contributing)
26
+
27
+ ## Installation
28
+
29
+ ```sh
30
+ npm i -s agentmail
31
+ ```
32
+
33
+ ## Reference
34
+
35
+ A full reference for this library is available [here](https://github.com/agentmail-to/agentmail-node/blob/HEAD/./reference.md).
36
+
37
+ ## Usage
38
+
39
+ Instantiate and use the client with the following:
40
+
41
+ ```typescript
42
+ import { AgentMailClient } from "agentmail";
43
+
44
+ const client = new AgentMailClient({ apiKey: "YOUR_API_KEY" });
45
+ await client.inboxes.create(undefined);
46
+ ```
47
+
48
+ ## Request and Response Types
49
+
50
+ The SDK exports all request and response types as TypeScript interfaces. Simply import them with the
51
+ following namespace:
52
+
53
+ ```typescript
54
+ import { AgentMail } from "agentmail";
55
+
56
+ const request: AgentMail.ListInboxesRequest = {
57
+ ...
58
+ };
59
+ ```
60
+
61
+ ## Exception Handling
62
+
63
+ When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error
64
+ will be thrown.
65
+
66
+ ```typescript
67
+ import { AgentMailError } from "agentmail";
68
+
69
+ try {
70
+ await client.inboxes.create(...);
71
+ } catch (err) {
72
+ if (err instanceof AgentMailError) {
73
+ console.log(err.statusCode);
74
+ console.log(err.message);
75
+ console.log(err.body);
76
+ console.log(err.rawResponse);
77
+ }
78
+ }
79
+ ```
80
+
81
+ ## Binary Response
82
+
83
+ You can consume binary data from endpoints using the `BinaryResponse` type which lets you choose how to consume the data:
84
+
85
+ ```typescript
86
+ const response = await client.domains.getZoneFile(...);
87
+ const stream: ReadableStream<Uint8Array> = response.stream();
88
+ // const arrayBuffer: ArrayBuffer = await response.arrayBuffer();
89
+ // const blob: Blob = response.blob();
90
+ // const bytes: Uint8Array = response.bytes();
91
+ // You can only use the response body once, so you must choose one of the above methods.
92
+ // If you want to check if the response body has been used, you can use the following property.
93
+ const bodyUsed = response.bodyUsed;
94
+ ```
95
+ <details>
96
+ <summary>Save binary response to a file</summary>
97
+
98
+ <blockquote>
99
+ <details>
100
+ <summary>Node.js</summary>
101
+
102
+ <blockquote>
103
+ <details>
104
+ <summary>ReadableStream (most-efficient)</summary>
105
+
106
+ ```ts
107
+ import { createWriteStream } from 'fs';
108
+ import { Readable } from 'stream';
109
+ import { pipeline } from 'stream/promises';
110
+
111
+ const response = await client.domains.getZoneFile(...);
112
+
113
+ const stream = response.stream();
114
+ const nodeStream = Readable.fromWeb(stream);
115
+ const writeStream = createWriteStream('path/to/file');
116
+
117
+ await pipeline(nodeStream, writeStream);
118
+ ```
119
+
120
+ </details>
121
+ </blockquote>
122
+
123
+ <blockquote>
124
+ <details>
125
+ <summary>ArrayBuffer</summary>
126
+
127
+ ```ts
128
+ import { writeFile } from 'fs/promises';
129
+
130
+ const response = await client.domains.getZoneFile(...);
131
+
132
+ const arrayBuffer = await response.arrayBuffer();
133
+ await writeFile('path/to/file', Buffer.from(arrayBuffer));
134
+ ```
135
+
136
+ </details>
137
+ </blockquote>
138
+
139
+ <blockquote>
140
+ <details>
141
+ <summary>Blob</summary>
142
+
143
+ ```ts
144
+ import { writeFile } from 'fs/promises';
145
+
146
+ const response = await client.domains.getZoneFile(...);
147
+
148
+ const blob = await response.blob();
149
+ const arrayBuffer = await blob.arrayBuffer();
150
+ await writeFile('output.bin', Buffer.from(arrayBuffer));
151
+ ```
152
+
153
+ </details>
154
+ </blockquote>
155
+
156
+ <blockquote>
157
+ <details>
158
+ <summary>Bytes (UIntArray8)</summary>
159
+
160
+ ```ts
161
+ import { writeFile } from 'fs/promises';
162
+
163
+ const response = await client.domains.getZoneFile(...);
164
+
165
+ const bytes = await response.bytes();
166
+ await writeFile('path/to/file', bytes);
167
+ ```
168
+
169
+ </details>
170
+ </blockquote>
171
+
172
+ </details>
173
+ </blockquote>
174
+
175
+ <blockquote>
176
+ <details>
177
+ <summary>Bun</summary>
178
+
179
+ <blockquote>
180
+ <details>
181
+ <summary>ReadableStream (most-efficient)</summary>
182
+
183
+ ```ts
184
+ const response = await client.domains.getZoneFile(...);
185
+
186
+ const stream = response.stream();
187
+ await Bun.write('path/to/file', stream);
188
+ ```
189
+
190
+ </details>
191
+ </blockquote>
192
+
193
+ <blockquote>
194
+ <details>
195
+ <summary>ArrayBuffer</summary>
196
+
197
+ ```ts
198
+ const response = await client.domains.getZoneFile(...);
199
+
200
+ const arrayBuffer = await response.arrayBuffer();
201
+ await Bun.write('path/to/file', arrayBuffer);
202
+ ```
203
+
204
+ </details>
205
+ </blockquote>
206
+
207
+ <blockquote>
208
+ <details>
209
+ <summary>Blob</summary>
210
+
211
+ ```ts
212
+ const response = await client.domains.getZoneFile(...);
213
+
214
+ const blob = await response.blob();
215
+ await Bun.write('path/to/file', blob);
216
+ ```
217
+
218
+ </details>
219
+ </blockquote>
220
+
221
+ <blockquote>
222
+ <details>
223
+ <summary>Bytes (UIntArray8)</summary>
224
+
225
+ ```ts
226
+ const response = await client.domains.getZoneFile(...);
227
+
228
+ const bytes = await response.bytes();
229
+ await Bun.write('path/to/file', bytes);
230
+ ```
231
+
232
+ </details>
233
+ </blockquote>
234
+
235
+ </details>
236
+ </blockquote>
237
+
238
+ <blockquote>
239
+ <details>
240
+ <summary>Deno</summary>
241
+
242
+ <blockquote>
243
+ <details>
244
+ <summary>ReadableStream (most-efficient)</summary>
245
+
246
+ ```ts
247
+ const response = await client.domains.getZoneFile(...);
248
+
249
+ const stream = response.stream();
250
+ const file = await Deno.open('path/to/file', { write: true, create: true });
251
+ await stream.pipeTo(file.writable);
252
+ ```
253
+
254
+ </details>
255
+ </blockquote>
256
+
257
+ <blockquote>
258
+ <details>
259
+ <summary>ArrayBuffer</summary>
260
+
261
+ ```ts
262
+ const response = await client.domains.getZoneFile(...);
263
+
264
+ const arrayBuffer = await response.arrayBuffer();
265
+ await Deno.writeFile('path/to/file', new Uint8Array(arrayBuffer));
266
+ ```
267
+
268
+ </details>
269
+ </blockquote>
270
+
271
+ <blockquote>
272
+ <details>
273
+ <summary>Blob</summary>
274
+
275
+ ```ts
276
+ const response = await client.domains.getZoneFile(...);
277
+
278
+ const blob = await response.blob();
279
+ const arrayBuffer = await blob.arrayBuffer();
280
+ await Deno.writeFile('path/to/file', new Uint8Array(arrayBuffer));
281
+ ```
282
+
283
+ </details>
284
+ </blockquote>
285
+
286
+ <blockquote>
287
+ <details>
288
+ <summary>Bytes (UIntArray8)</summary>
289
+
290
+ ```ts
291
+ const response = await client.domains.getZoneFile(...);
292
+
293
+ const bytes = await response.bytes();
294
+ await Deno.writeFile('path/to/file', bytes);
295
+ ```
296
+
297
+ </details>
298
+ </blockquote>
299
+
300
+ </details>
301
+ </blockquote>
302
+
303
+ <blockquote>
304
+ <details>
305
+ <summary>Browser</summary>
306
+
307
+ <blockquote>
308
+ <details>
309
+ <summary>Blob (most-efficient)</summary>
310
+
311
+ ```ts
312
+ const response = await client.domains.getZoneFile(...);
313
+
314
+ const blob = await response.blob();
315
+ const url = URL.createObjectURL(blob);
316
+
317
+ // trigger download
318
+ const a = document.createElement('a');
319
+ a.href = url;
320
+ a.download = 'filename';
321
+ a.click();
322
+ URL.revokeObjectURL(url);
323
+ ```
324
+
325
+ </details>
326
+ </blockquote>
327
+
328
+ <blockquote>
329
+ <details>
330
+ <summary>ReadableStream</summary>
331
+
332
+ ```ts
333
+ const response = await client.domains.getZoneFile(...);
334
+
335
+ const stream = response.stream();
336
+ const reader = stream.getReader();
337
+ const chunks = [];
338
+
339
+ while (true) {
340
+ const { done, value } = await reader.read();
341
+ if (done) break;
342
+ chunks.push(value);
343
+ }
344
+
345
+ const blob = new Blob(chunks);
346
+ const url = URL.createObjectURL(blob);
347
+
348
+ // trigger download
349
+ const a = document.createElement('a');
350
+ a.href = url;
351
+ a.download = 'filename';
352
+ a.click();
353
+ URL.revokeObjectURL(url);
354
+ ```
355
+
356
+ </details>
357
+ </blockquote>
358
+
359
+ <blockquote>
360
+ <details>
361
+ <summary>ArrayBuffer</summary>
362
+
363
+ ```ts
364
+ const response = await client.domains.getZoneFile(...);
365
+
366
+ const arrayBuffer = await response.arrayBuffer();
367
+ const blob = new Blob([arrayBuffer]);
368
+ const url = URL.createObjectURL(blob);
369
+
370
+ // trigger download
371
+ const a = document.createElement('a');
372
+ a.href = url;
373
+ a.download = 'filename';
374
+ a.click();
375
+ URL.revokeObjectURL(url);
376
+ ```
377
+
378
+ </details>
379
+ </blockquote>
380
+
381
+ <blockquote>
382
+ <details>
383
+ <summary>Bytes (UIntArray8)</summary>
384
+
385
+ ```ts
386
+ const response = await client.domains.getZoneFile(...);
387
+
388
+ const bytes = await response.bytes();
389
+ const blob = new Blob([bytes]);
390
+ const url = URL.createObjectURL(blob);
391
+
392
+ // trigger download
393
+ const a = document.createElement('a');
394
+ a.href = url;
395
+ a.download = 'filename';
396
+ a.click();
397
+ URL.revokeObjectURL(url);
398
+ ```
399
+
400
+ </details>
401
+ </blockquote>
402
+
403
+ </details>
404
+ </blockquote>
405
+
406
+ </details>
407
+ </blockquote>
408
+
409
+ <details>
410
+ <summary>Convert binary response to text</summary>
411
+
412
+ <blockquote>
413
+ <details>
414
+ <summary>ReadableStream</summary>
415
+
416
+ ```ts
417
+ const response = await client.domains.getZoneFile(...);
418
+
419
+ const stream = response.stream();
420
+ const text = await new Response(stream).text();
421
+ ```
422
+
423
+ </details>
424
+ </blockquote>
425
+
426
+ <blockquote>
427
+ <details>
428
+ <summary>ArrayBuffer</summary>
429
+
430
+ ```ts
431
+ const response = await client.domains.getZoneFile(...);
432
+
433
+ const arrayBuffer = await response.arrayBuffer();
434
+ const text = new TextDecoder().decode(arrayBuffer);
435
+ ```
436
+
437
+ </details>
438
+ </blockquote>
439
+
440
+ <blockquote>
441
+ <details>
442
+ <summary>Blob</summary>
443
+
444
+ ```ts
445
+ const response = await client.domains.getZoneFile(...);
446
+
447
+ const blob = await response.blob();
448
+ const text = await blob.text();
449
+ ```
450
+
451
+ </details>
452
+ </blockquote>
453
+
454
+ <blockquote>
455
+ <details>
456
+ <summary>Bytes (UIntArray8)</summary>
457
+
458
+ ```ts
459
+ const response = await client.domains.getZoneFile(...);
460
+
461
+ const bytes = await response.bytes();
462
+ const text = new TextDecoder().decode(bytes);
463
+ ```
464
+
465
+ </details>
466
+ </blockquote>
467
+
468
+ </details>
469
+
470
+ ## Advanced
471
+
472
+ ### Additional Headers
473
+
474
+ If you would like to send additional headers as part of the request, use the `headers` request option.
475
+
476
+ ```typescript
477
+ import { AgentMailClient } from "agentmail";
478
+
479
+ const client = new AgentMailClient({
480
+ ...
481
+ headers: {
482
+ 'X-Custom-Header': 'custom value'
483
+ }
484
+ });
485
+
486
+ const response = await client.inboxes.create(..., {
487
+ headers: {
488
+ 'X-Custom-Header': 'custom value'
489
+ }
490
+ });
491
+ ```
492
+
493
+ ### Additional Query String Parameters
494
+
495
+ If you would like to send additional query string parameters as part of the request, use the `queryParams` request option.
496
+
497
+ ```typescript
498
+ const response = await client.inboxes.create(..., {
499
+ queryParams: {
500
+ 'customQueryParamKey': 'custom query param value'
501
+ }
502
+ });
503
+ ```
504
+
505
+ ### Retries
506
+
507
+ The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
508
+ as the request is deemed retryable and the number of retry attempts has not grown larger than the configured
509
+ retry limit (default: 2).
510
+
511
+ A request is deemed retryable when any of the following HTTP status codes is returned:
512
+
513
+ - [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
514
+ - [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
515
+ - [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
516
+
517
+ Use the `maxRetries` request option to configure this behavior.
518
+
519
+ ```typescript
520
+ const response = await client.inboxes.create(..., {
521
+ maxRetries: 0 // override maxRetries at the request level
522
+ });
523
+ ```
524
+
525
+ ### Timeouts
526
+
527
+ The SDK defaults to a 60 second timeout. Use the `timeoutInSeconds` option to configure this behavior.
528
+
529
+ ```typescript
530
+ const response = await client.inboxes.create(..., {
531
+ timeoutInSeconds: 30 // override timeout to 30s
532
+ });
533
+ ```
534
+
535
+ ### Aborting Requests
536
+
537
+ The SDK allows users to abort requests at any point by passing in an abort signal.
538
+
539
+ ```typescript
540
+ const controller = new AbortController();
541
+ const response = await client.inboxes.create(..., {
542
+ abortSignal: controller.signal
543
+ });
544
+ controller.abort(); // aborts the request
545
+ ```
546
+
547
+ ### Access Raw Response Data
548
+
549
+ The SDK provides access to raw response data, including headers, through the `.withRawResponse()` method.
550
+ The `.withRawResponse()` method returns a promise that results to an object with a `data` and a `rawResponse` property.
551
+
552
+ ```typescript
553
+ const { data, rawResponse } = await client.inboxes.create(...).withRawResponse();
554
+
555
+ console.log(data);
556
+ console.log(rawResponse.headers['X-My-Header']);
557
+ ```
558
+
559
+ ### Logging
560
+
561
+ The SDK supports logging. You can configure the logger by passing in a `logging` object to the client options.
562
+
563
+ ```typescript
564
+ import { AgentMailClient, logging } from "agentmail";
565
+
566
+ const client = new AgentMailClient({
567
+ ...
568
+ logging: {
569
+ level: logging.LogLevel.Debug, // defaults to logging.LogLevel.Info
570
+ logger: new logging.ConsoleLogger(), // defaults to ConsoleLogger
571
+ silent: false, // defaults to true, set to false to enable logging
572
+ }
573
+ });
574
+ ```
575
+ The `logging` object can have the following properties:
576
+ - `level`: The log level to use. Defaults to `logging.LogLevel.Info`.
577
+ - `logger`: The logger to use. Defaults to a `logging.ConsoleLogger`.
578
+ - `silent`: Whether to silence the logger. Defaults to `true`.
579
+
580
+ The `level` property can be one of the following values:
581
+ - `logging.LogLevel.Debug`
582
+ - `logging.LogLevel.Info`
583
+ - `logging.LogLevel.Warn`
584
+ - `logging.LogLevel.Error`
585
+
586
+ To provide a custom logger, you can pass in an object that implements the `logging.ILogger` interface.
587
+
588
+ <details>
589
+ <summary>Custom logger examples</summary>
590
+
591
+ Here's an example using the popular `winston` logging library.
592
+ ```ts
593
+ import winston from 'winston';
594
+
595
+ const winstonLogger = winston.createLogger({...});
596
+
597
+ const logger: logging.ILogger = {
598
+ debug: (msg, ...args) => winstonLogger.debug(msg, ...args),
599
+ info: (msg, ...args) => winstonLogger.info(msg, ...args),
600
+ warn: (msg, ...args) => winstonLogger.warn(msg, ...args),
601
+ error: (msg, ...args) => winstonLogger.error(msg, ...args),
602
+ };
603
+ ```
604
+
605
+ Here's an example using the popular `pino` logging library.
606
+
607
+ ```ts
608
+ import pino from 'pino';
609
+
610
+ const pinoLogger = pino({...});
611
+
612
+ const logger: logging.ILogger = {
613
+ debug: (msg, ...args) => pinoLogger.debug(args, msg),
614
+ info: (msg, ...args) => pinoLogger.info(args, msg),
615
+ warn: (msg, ...args) => pinoLogger.warn(args, msg),
616
+ error: (msg, ...args) => pinoLogger.error(args, msg),
617
+ };
618
+ ```
619
+ </details>
620
+
621
+
622
+ ### Runtime Compatibility
623
+
624
+
625
+ The SDK works in the following runtimes:
626
+
627
+
628
+
629
+ - Node.js 18+
630
+ - Vercel
631
+ - Cloudflare Workers
632
+ - Deno v1.25+
633
+ - Bun 1.0+
634
+ - React Native
635
+
636
+ ### Customizing Fetch Client
637
+
638
+ The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're running in an
639
+ unsupported environment, this provides a way for you to break glass and ensure the SDK works.
640
+
641
+ ```typescript
642
+ import { AgentMailClient } from "agentmail";
643
+
644
+ const client = new AgentMailClient({
645
+ ...
646
+ fetcher: // provide your implementation here
647
+ });
648
+ ```
649
+
650
+ ## Contributing
651
+
652
+ While we value open-source contributions to this SDK, this library is generated programmatically.
653
+ Additions made directly to this library would have to be moved over to our generation code,
654
+ otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
655
+ a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
656
+ an issue first to discuss with us!
657
+
658
+ On the other hand, contributions to the README are always very welcome!
@@ -43,8 +43,8 @@ function normalizeClientOptions(options) {
43
43
  const headers = (0, headers_js_1.mergeHeaders)({
44
44
  "X-Fern-Language": "JavaScript",
45
45
  "X-Fern-SDK-Name": "agentmail",
46
- "X-Fern-SDK-Version": "0.2.19",
47
- "User-Agent": "agentmail/0.2.19",
46
+ "X-Fern-SDK-Version": "0.2.21",
47
+ "User-Agent": "agentmail/0.2.21",
48
48
  "X-Fern-Runtime": core.RUNTIME.type,
49
49
  "X-Fern-Runtime-Version": core.RUNTIME.version,
50
50
  }, options === null || options === void 0 ? void 0 : options.headers);
@@ -156,13 +156,7 @@ function getHeaders(args) {
156
156
  return __awaiter(this, void 0, void 0, function* () {
157
157
  var _a;
158
158
  const newHeaders = new Headers_js_1.Headers();
159
- newHeaders.set("Accept", args.responseType === "json"
160
- ? "application/json"
161
- : args.responseType === "text"
162
- ? "text/plain"
163
- : args.responseType === "sse"
164
- ? "text/event-stream"
165
- : "*/*");
159
+ newHeaders.set("Accept", args.responseType === "json" ? "application/json" : args.responseType === "text" ? "text/plain" : "*/*");
166
160
  if (args.body !== undefined && args.contentType != null) {
167
161
  newHeaders.set("Content-Type", args.contentType);
168
162
  }
@@ -31,19 +31,24 @@ function validateAndTransformArray(value, transformItem) {
31
31
  ],
32
32
  };
33
33
  }
34
- const result = [];
35
- const errors = [];
36
- for (let i = 0; i < value.length; i++) {
37
- const item = transformItem(value[i], i);
38
- if (item.ok) {
39
- result.push(item.value);
34
+ const maybeValidItems = value.map((item, index) => transformItem(item, index));
35
+ return maybeValidItems.reduce((acc, item) => {
36
+ if (acc.ok && item.ok) {
37
+ return {
38
+ ok: true,
39
+ value: [...acc.value, item.value],
40
+ };
40
41
  }
41
- else {
42
+ const errors = [];
43
+ if (!acc.ok) {
44
+ errors.push(...acc.errors);
45
+ }
46
+ if (!item.ok) {
42
47
  errors.push(...item.errors);
43
48
  }
44
- }
45
- if (errors.length === 0) {
46
- return { ok: true, value: result };
47
- }
48
- return { ok: false, errors };
49
+ return {
50
+ ok: false,
51
+ errors,
52
+ };
53
+ }, { ok: true, value: [] });
49
54
  }