retransmit.dev 0.3.0 → 0.5.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.
package/dist/index.js CHANGED
@@ -1,4 +1,22 @@
1
1
  // src/emails.ts
2
+ function toBase64(bytes) {
3
+ if (typeof Buffer !== "undefined") return Buffer.from(bytes).toString("base64");
4
+ let binary = "";
5
+ const CHUNK = 32768;
6
+ for (let i = 0; i < bytes.length; i += CHUNK) {
7
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
8
+ }
9
+ return btoa(binary);
10
+ }
11
+ function toWireAttachment(attachment) {
12
+ return {
13
+ filename: attachment.filename,
14
+ content: attachment.content === void 0 || typeof attachment.content === "string" ? attachment.content : toBase64(attachment.content),
15
+ path: attachment.path,
16
+ content_type: attachment.contentType,
17
+ content_id: attachment.contentId
18
+ };
19
+ }
2
20
  function toWirePayload(options) {
3
21
  return {
4
22
  from: options.from,
@@ -10,7 +28,9 @@ function toWirePayload(options) {
10
28
  html: options.html,
11
29
  text: options.text,
12
30
  marketing: options.marketing,
13
- tags: options.tags
31
+ tags: options.tags,
32
+ headers: options.headers,
33
+ attachments: options.attachments?.map(toWireAttachment)
14
34
  };
15
35
  }
16
36
  var Emails = class {
@@ -18,9 +38,18 @@ var Emails = class {
18
38
  this.client = client;
19
39
  }
20
40
  client;
21
- /** Queues a single email. Poll `get(id)` or subscribe to webhooks for the outcome. */
22
- send(options) {
23
- return this.client.request("POST", "/v1/emails", toWirePayload(options));
41
+ /**
42
+ * Queues a single email. Poll `get(id)` or subscribe to webhooks for the
43
+ * outcome. Pass `{ idempotencyKey }` to make the call safe to retry.
44
+ */
45
+ send(options, requestOptions) {
46
+ return this.client.request(
47
+ "POST",
48
+ "/v1/emails",
49
+ toWirePayload(options),
50
+ void 0,
51
+ requestOptions
52
+ );
24
53
  }
25
54
  /** Retrieves an email with its current status and event history. */
26
55
  get(id) {
@@ -43,6 +72,21 @@ var Emails = class {
43
72
  tags() {
44
73
  return this.client.request("GET", "/v1/emails/tags");
45
74
  }
75
+ /**
76
+ * The attachments of an email, each with a signed `download_url` valid for
77
+ * one hour. Files are kept for 30 days after the send; after that the link
78
+ * is `null` and only the metadata remains.
79
+ */
80
+ attachments(emailId) {
81
+ return this.client.request("GET", `/v1/emails/${encodeURIComponent(emailId)}/attachments`);
82
+ }
83
+ /** One attachment of an email with a signed download link. */
84
+ getAttachment(emailId, attachmentId) {
85
+ return this.client.request(
86
+ "GET",
87
+ `/v1/emails/${encodeURIComponent(emailId)}/attachments/${encodeURIComponent(attachmentId)}`
88
+ );
89
+ }
46
90
  };
47
91
 
48
92
  // src/batch.ts
@@ -51,11 +95,19 @@ var Batch = class {
51
95
  this.client = client;
52
96
  }
53
97
  client;
54
- /** Queues up to 10,000 emails in one request. Track progress with `get(id)`. */
55
- send(emails) {
56
- return this.client.request("POST", "/v1/emails/batch", {
57
- emails: emails.map(toWirePayload)
58
- });
98
+ /**
99
+ * Queues up to 10,000 emails in one request. Track progress with `get(id)`.
100
+ * Pass `{ idempotencyKey }` covering the whole batch to make the call safe
101
+ * to retry.
102
+ */
103
+ send(emails, requestOptions) {
104
+ return this.client.request(
105
+ "POST",
106
+ "/v1/emails/batch",
107
+ { emails: emails.map(toWirePayload) },
108
+ void 0,
109
+ requestOptions
110
+ );
59
111
  }
60
112
  /** Batch progress: how many emails are in each status so far. */
61
113
  get(id) {
@@ -114,7 +166,7 @@ var Whatsapp = class {
114
166
 
115
167
  // src/retransmit.ts
116
168
  var DEFAULT_BASE_URL = "https://api.retransmit.dev";
117
- var USER_AGENT = "retransmit.dev-node/0.3.0";
169
+ var USER_AGENT = "retransmit.dev-node/0.5.0";
118
170
  function readEnv(name) {
119
171
  return typeof process !== "undefined" ? process.env?.[name] : void 0;
120
172
  }
@@ -139,7 +191,7 @@ var Retransmit = class {
139
191
  );
140
192
  }
141
193
  /** Internal transport shared by the resource classes. API failures are returned, never thrown. */
142
- async request(method, path, body, query) {
194
+ async request(method, path, body, query, options = {}) {
143
195
  const params = new URLSearchParams();
144
196
  for (const [key, value] of Object.entries(query ?? {})) {
145
197
  if (value === void 0) continue;
@@ -147,15 +199,19 @@ var Retransmit = class {
147
199
  }
148
200
  const encoded = params.toString();
149
201
  const search = encoded ? `?${encoded}` : "";
202
+ const headers = {
203
+ Authorization: `Bearer ${this.apiKey}`,
204
+ "Content-Type": "application/json",
205
+ "User-Agent": USER_AGENT
206
+ };
207
+ if (options.idempotencyKey !== void 0) {
208
+ headers["Idempotency-Key"] = options.idempotencyKey;
209
+ }
150
210
  let response;
151
211
  try {
152
212
  response = await fetch(`${this.baseUrl}${path}${search}`, {
153
213
  method,
154
- headers: {
155
- Authorization: `Bearer ${this.apiKey}`,
156
- "Content-Type": "application/json",
157
- "User-Agent": USER_AGENT
158
- },
214
+ headers,
159
215
  body: body === void 0 ? void 0 : JSON.stringify(body)
160
216
  });
161
217
  } catch (cause) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retransmit.dev",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Node.js SDK for the Retransmit email, SMS and WhatsApp APIs",
5
5
  "keywords": [
6
6
  "email",
@@ -16,8 +16,10 @@
16
16
  "homepage": "https://docs.retransmit.dev",
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "git+https://github.com/retransmit-dev/retransmit.git",
20
- "directory": "packages/sdk"
19
+ "url": "git+https://github.com/retransmit-dev/retransmit-node.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/retransmit-dev/retransmit-node/issues"
21
23
  },
22
24
  "license": "MIT",
23
25
  "type": "module",
@@ -44,7 +46,7 @@
44
46
  "@types/node": "^26.2.0",
45
47
  "tsup": "^8.5.0",
46
48
  "typescript": "^6.0.3",
47
- "@retransmit/config": "0.0.0"
49
+ "vitest": "^5.0.0"
48
50
  },
49
51
  "publishConfig": {
50
52
  "access": "public"
@@ -54,6 +56,8 @@
54
56
  },
55
57
  "scripts": {
56
58
  "build": "tsup src/index.ts --format esm,cjs --dts --clean",
57
- "check-types": "tsc --noEmit"
59
+ "check-types": "tsc --noEmit",
60
+ "test": "vitest run",
61
+ "test:watch": "vitest"
58
62
  }
59
63
  }