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