@tratto/email 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.
package/dist/index.mjs ADDED
@@ -0,0 +1,375 @@
1
+ // src/error.ts
2
+ var TrattoError = class extends Error {
3
+ constructor(message, code, statusCode, docs) {
4
+ super(message);
5
+ this.code = code;
6
+ this.statusCode = statusCode;
7
+ this.docs = docs;
8
+ this.name = "TrattoError";
9
+ }
10
+ };
11
+
12
+ // src/resources/base.ts
13
+ var BaseResource = class {
14
+ constructor(apiKey, baseUrl) {
15
+ this.apiKey = apiKey;
16
+ this.baseUrl = baseUrl;
17
+ }
18
+ async fetch(method, path, options) {
19
+ const hasBody = options?.body !== void 0;
20
+ const contentType = options?.contentType ?? (hasBody ? "application/json" : void 0);
21
+ const headers = {
22
+ Authorization: `Bearer ${this.apiKey}`,
23
+ "User-Agent": "@tratto/email/0.1.0",
24
+ ...contentType ? { "Content-Type": contentType } : {},
25
+ ...options?.headers
26
+ };
27
+ let body;
28
+ if (hasBody) {
29
+ body = contentType === "application/json" ? JSON.stringify(options.body) : options.body;
30
+ }
31
+ const res = await globalThis.fetch(`${this.baseUrl}${path}`, { method, headers, body });
32
+ if (res.status === 204) return void 0;
33
+ const json = await res.json();
34
+ if (!res.ok) {
35
+ const err = json["error"] ?? {};
36
+ throw new TrattoError(
37
+ err.message ?? res.statusText,
38
+ err.code ?? "unknown_error",
39
+ res.status,
40
+ err.docs
41
+ );
42
+ }
43
+ return json;
44
+ }
45
+ async fetchData(method, path, options) {
46
+ const res = await this.fetch(method, path, options);
47
+ return res.data;
48
+ }
49
+ buildQuery(params) {
50
+ const qs = new URLSearchParams();
51
+ for (const [key, val] of Object.entries(params)) {
52
+ if (val == null) continue;
53
+ qs.set(key, val instanceof Date ? val.toISOString() : String(val));
54
+ }
55
+ const str = qs.toString();
56
+ return str ? `?${str}` : "";
57
+ }
58
+ };
59
+
60
+ // src/resources/emails.ts
61
+ var EmailsResource = class extends BaseResource {
62
+ send(params, idempotencyKey) {
63
+ return this.fetchData("POST", "/v1/emails", {
64
+ body: params,
65
+ headers: idempotencyKey ? { "Idempotency-Key": idempotencyKey } : void 0
66
+ });
67
+ }
68
+ list(params) {
69
+ const qs = this.buildQuery({
70
+ after: params?.after,
71
+ limit: params?.limit,
72
+ status: params?.status,
73
+ domainId: params?.domainId,
74
+ tags: params?.tags,
75
+ dateFrom: params?.dateFrom,
76
+ dateTo: params?.dateTo
77
+ });
78
+ return this.fetch("GET", `/v1/emails${qs}`);
79
+ }
80
+ get(id) {
81
+ return this.fetchData("GET", `/v1/emails/${id}`);
82
+ }
83
+ listEvents(id) {
84
+ return this.fetchData("GET", `/v1/emails/${id}/events`);
85
+ }
86
+ };
87
+
88
+ // src/resources/contacts.ts
89
+ var ContactsResource = class extends BaseResource {
90
+ create(params) {
91
+ return this.fetchData("POST", "/v1/contacts", { body: params });
92
+ }
93
+ list(params) {
94
+ const qs = this.buildQuery({
95
+ status: params?.status,
96
+ audienceId: params?.audienceId,
97
+ tag: params?.tag,
98
+ after: params?.after,
99
+ limit: params?.limit
100
+ });
101
+ return this.fetch("GET", `/v1/contacts${qs}`);
102
+ }
103
+ update(id, params) {
104
+ return this.fetchData("PATCH", `/v1/contacts/${id}`, { body: params });
105
+ }
106
+ importCsv(csvText) {
107
+ return this.fetchData(
108
+ "POST",
109
+ "/v1/contacts/import",
110
+ { body: csvText, contentType: "text/csv" }
111
+ );
112
+ }
113
+ getImportJob(jobId) {
114
+ return this.fetchData("GET", `/v1/contacts/import/${jobId}`);
115
+ }
116
+ };
117
+
118
+ // src/resources/audiences.ts
119
+ var AudiencesResource = class extends BaseResource {
120
+ create(params) {
121
+ return this.fetchData("POST", "/v1/audiences", { body: params });
122
+ }
123
+ list(params) {
124
+ const qs = this.buildQuery({ after: params?.after, limit: params?.limit });
125
+ return this.fetch("GET", `/v1/audiences${qs}`);
126
+ }
127
+ get(id) {
128
+ return this.fetchData("GET", `/v1/audiences/${id}`);
129
+ }
130
+ addContacts(audienceId, contactIds) {
131
+ return this.fetchData(
132
+ "POST",
133
+ `/v1/audiences/${audienceId}/contacts`,
134
+ { body: { contactIds } }
135
+ );
136
+ }
137
+ };
138
+
139
+ // src/resources/campaigns.ts
140
+ var CampaignsResource = class extends BaseResource {
141
+ create(params) {
142
+ return this.fetchData("POST", "/v1/campaigns", { body: params });
143
+ }
144
+ list(params) {
145
+ const qs = this.buildQuery({
146
+ status: params?.status,
147
+ after: params?.after,
148
+ limit: params?.limit
149
+ });
150
+ return this.fetch("GET", `/v1/campaigns${qs}`);
151
+ }
152
+ get(id) {
153
+ return this.fetchData("GET", `/v1/campaigns/${id}`);
154
+ }
155
+ getStats(id) {
156
+ return this.fetchData("GET", `/v1/campaigns/${id}/stats`);
157
+ }
158
+ send(id, params) {
159
+ const body = {};
160
+ if (params?.scheduledAt) {
161
+ body["scheduledAt"] = params.scheduledAt instanceof Date ? params.scheduledAt.toISOString() : params.scheduledAt;
162
+ }
163
+ return this.fetchData("POST", `/v1/campaigns/${id}/send`, { body });
164
+ }
165
+ pause(id) {
166
+ return this.fetchData("POST", `/v1/campaigns/${id}/pause`, { body: {} });
167
+ }
168
+ testSend(id, to) {
169
+ return this.fetchData(
170
+ "POST",
171
+ `/v1/campaigns/${id}/test-send`,
172
+ { body: { to } }
173
+ );
174
+ }
175
+ };
176
+
177
+ // src/resources/templates.ts
178
+ var TemplatesResource = class extends BaseResource {
179
+ list(params) {
180
+ const qs = this.buildQuery({
181
+ limit: params?.limit,
182
+ after: params?.after,
183
+ status: params?.status
184
+ });
185
+ return this.fetch("GET", `/v1/templates${qs}`);
186
+ }
187
+ create(params) {
188
+ return this.fetchData("POST", "/v1/templates", { body: params });
189
+ }
190
+ get(id) {
191
+ return this.fetchData("GET", `/v1/templates/${id}`);
192
+ }
193
+ update(id, params) {
194
+ return this.fetchData("PATCH", `/v1/templates/${id}`, { body: params });
195
+ }
196
+ delete(id) {
197
+ return this.fetch("DELETE", `/v1/templates/${id}`);
198
+ }
199
+ listVersions(id) {
200
+ return this.fetchData("GET", `/v1/templates/${id}/versions`);
201
+ }
202
+ getVersion(id, version) {
203
+ return this.fetchData("GET", `/v1/templates/${id}/versions/${version}`);
204
+ }
205
+ testSend(id, to, variables = {}) {
206
+ return this.fetchData(
207
+ "POST",
208
+ `/v1/templates/${id}/test-send`,
209
+ { body: { to, variables } }
210
+ );
211
+ }
212
+ };
213
+
214
+ // src/resources/webhooks.ts
215
+ var WebhooksResource = class extends BaseResource {
216
+ create(params) {
217
+ return this.fetchData("POST", "/v1/webhooks", { body: params });
218
+ }
219
+ list() {
220
+ return this.fetchData("GET", "/v1/webhooks");
221
+ }
222
+ delete(id) {
223
+ return this.fetch("DELETE", `/v1/webhooks/${id}`);
224
+ }
225
+ listDeliveries(id, params) {
226
+ const qs = this.buildQuery({ after: params?.after, limit: params?.limit });
227
+ return this.fetch(
228
+ "GET",
229
+ `/v1/webhooks/${id}/deliveries${qs}`
230
+ );
231
+ }
232
+ test(id) {
233
+ return this.fetchData("POST", `/v1/webhooks/${id}/test`, { body: {} });
234
+ }
235
+ rotateSecret(id) {
236
+ return this.fetchData(
237
+ "POST",
238
+ `/v1/webhooks/${id}/rotate-secret`,
239
+ { body: {} }
240
+ );
241
+ }
242
+ };
243
+
244
+ // src/resources/domains.ts
245
+ var DomainsResource = class extends BaseResource {
246
+ add(domain) {
247
+ return this.fetchData("POST", "/v1/domains", { body: { domain } });
248
+ }
249
+ list(params) {
250
+ const qs = this.buildQuery({ after: params?.after, limit: params?.limit });
251
+ return this.fetch("GET", `/v1/domains${qs}`);
252
+ }
253
+ get(id) {
254
+ return this.fetchData("GET", `/v1/domains/${id}`);
255
+ }
256
+ verify(id) {
257
+ return this.fetchData("POST", `/v1/domains/${id}/verify`, { body: {} });
258
+ }
259
+ delete(id) {
260
+ return this.fetchData("DELETE", `/v1/domains/${id}`);
261
+ }
262
+ };
263
+
264
+ // src/resources/api-keys.ts
265
+ var ApiKeysResource = class extends BaseResource {
266
+ create(params, idempotencyKey) {
267
+ return this.fetchData("POST", "/v1/api-keys", {
268
+ body: params,
269
+ headers: idempotencyKey ? { "Idempotency-Key": idempotencyKey } : void 0
270
+ });
271
+ }
272
+ list(params) {
273
+ const qs = this.buildQuery({ after: params?.after, limit: params?.limit });
274
+ return this.fetch("GET", `/v1/api-keys${qs}`);
275
+ }
276
+ revoke(id) {
277
+ return this.fetchData("DELETE", `/v1/api-keys/${id}`);
278
+ }
279
+ };
280
+
281
+ // src/resources/analytics.ts
282
+ var AnalyticsResource = class extends BaseResource {
283
+ getSummary(period = "30d") {
284
+ return this.fetchData(
285
+ "GET",
286
+ `/v1/analytics/summary${this.buildQuery({ period })}`
287
+ );
288
+ }
289
+ getTimeseries(period = "30d") {
290
+ return this.fetchData(
291
+ "GET",
292
+ `/v1/analytics/timeseries${this.buildQuery({ period })}`
293
+ );
294
+ }
295
+ };
296
+
297
+ // src/resources/flows.ts
298
+ var FlowsResource = class extends BaseResource {
299
+ list(params) {
300
+ const qs = this.buildQuery({ after: params?.after, limit: params?.limit });
301
+ return this.fetch("GET", `/v1/flows${qs}`);
302
+ }
303
+ create(params) {
304
+ return this.fetchData("POST", "/v1/flows", { body: params });
305
+ }
306
+ get(id) {
307
+ return this.fetchData("GET", `/v1/flows/${id}`);
308
+ }
309
+ update(id, params) {
310
+ return this.fetchData("PATCH", `/v1/flows/${id}`, { body: params });
311
+ }
312
+ delete(id) {
313
+ return this.fetchData("DELETE", `/v1/flows/${id}`);
314
+ }
315
+ activate(id) {
316
+ return this.fetchData("POST", `/v1/flows/${id}/activate`, { body: {} });
317
+ }
318
+ deactivate(id) {
319
+ return this.fetchData("POST", `/v1/flows/${id}/deactivate`, { body: {} });
320
+ }
321
+ };
322
+
323
+ // src/resources/workspace.ts
324
+ var WorkspaceResource = class extends BaseResource {
325
+ get() {
326
+ return this.fetchData("GET", "/v1/workspace");
327
+ }
328
+ update(params) {
329
+ return this.fetchData("PATCH", "/v1/workspace", { body: params });
330
+ }
331
+ delete() {
332
+ return this.fetch("DELETE", "/v1/workspace");
333
+ }
334
+ updatePreferences(params) {
335
+ return this.fetchData("PATCH", "/v1/workspace/preferences", {
336
+ body: params
337
+ });
338
+ }
339
+ inviteMember(params) {
340
+ return this.fetchData("POST", "/v1/workspace/members/invite", {
341
+ body: params
342
+ });
343
+ }
344
+ updateMember(userId, params) {
345
+ return this.fetchData("PATCH", `/v1/workspace/members/${userId}`, {
346
+ body: params
347
+ });
348
+ }
349
+ removeMember(userId) {
350
+ return this.fetch("DELETE", `/v1/workspace/members/${userId}`);
351
+ }
352
+ };
353
+
354
+ // src/client.ts
355
+ var Tratto = class {
356
+ constructor(apiKey, options) {
357
+ if (!apiKey) throw new Error("apiKey is required");
358
+ const baseUrl = (options?.baseUrl ?? "https://api.tratto.email").replace(/\/$/, "");
359
+ this.emails = new EmailsResource(apiKey, baseUrl);
360
+ this.contacts = new ContactsResource(apiKey, baseUrl);
361
+ this.audiences = new AudiencesResource(apiKey, baseUrl);
362
+ this.campaigns = new CampaignsResource(apiKey, baseUrl);
363
+ this.templates = new TemplatesResource(apiKey, baseUrl);
364
+ this.webhooks = new WebhooksResource(apiKey, baseUrl);
365
+ this.domains = new DomainsResource(apiKey, baseUrl);
366
+ this.apiKeys = new ApiKeysResource(apiKey, baseUrl);
367
+ this.analytics = new AnalyticsResource(apiKey, baseUrl);
368
+ this.flows = new FlowsResource(apiKey, baseUrl);
369
+ this.workspace = new WorkspaceResource(apiKey, baseUrl);
370
+ }
371
+ };
372
+ export {
373
+ Tratto,
374
+ TrattoError
375
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@tratto/email",
3
+ "version": "0.1.0",
4
+ "description": "Tratto Node.js SDK — send transactional and marketing email",
5
+ "author": "Tratto <hello@tratto.email>",
6
+ "license": "MIT",
7
+ "homepage": "https://tratto.email",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/tratto-email/tratto-node.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/tratto-email/tratto-node/issues"
14
+ },
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.mjs",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": ["dist"],
26
+ "sideEffects": false,
27
+ "scripts": {
28
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
29
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
30
+ "lint": "eslint src --ext .ts",
31
+ "typecheck": "tsc --noEmit",
32
+ "test": "vitest run",
33
+ "test:watch": "vitest"
34
+ },
35
+ "devDependencies": {
36
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
37
+ "@typescript-eslint/parser": "^8.0.0",
38
+ "eslint": "^9.0.0",
39
+ "tsup": "^8.0.0",
40
+ "typescript": "^5.5.0",
41
+ "vitest": "^2.0.0"
42
+ },
43
+ "engines": {
44
+ "node": ">=18"
45
+ }
46
+ }