@unsent/sdk 0.25.2

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,228 @@
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
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ unsent: () => unsent
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/contact.ts
28
+ var Contacts = class {
29
+ constructor(unsent2) {
30
+ this.unsent = unsent2;
31
+ this.unsent = unsent2;
32
+ }
33
+ async create(contactBookId, payload) {
34
+ const data = await this.unsent.post(
35
+ `/contactBooks/${contactBookId}/contacts`,
36
+ payload
37
+ );
38
+ return data;
39
+ }
40
+ async get(contactBookId, contactId) {
41
+ const data = await this.unsent.get(
42
+ `/contactBooks/${contactBookId}/contacts/${contactId}`
43
+ );
44
+ return data;
45
+ }
46
+ async update(contactBookId, contactId, payload) {
47
+ const data = await this.unsent.patch(
48
+ `/contactBooks/${contactBookId}/contacts/${contactId}`,
49
+ payload
50
+ );
51
+ return data;
52
+ }
53
+ async upsert(contactBookId, contactId, payload) {
54
+ const data = await this.unsent.put(
55
+ `/contactBooks/${contactBookId}/contacts/${contactId}`,
56
+ payload
57
+ );
58
+ return data;
59
+ }
60
+ async delete(contactBookId, contactId) {
61
+ const data = await this.unsent.delete(
62
+ `/contactBooks/${contactBookId}/contacts/${contactId}`
63
+ );
64
+ return data;
65
+ }
66
+ };
67
+
68
+ // src/email.ts
69
+ var import_render = require("@react-email/render");
70
+ var Emails = class {
71
+ constructor(unsent2) {
72
+ this.unsent = unsent2;
73
+ this.unsent = unsent2;
74
+ }
75
+ async send(payload) {
76
+ return this.create(payload);
77
+ }
78
+ async create(payload) {
79
+ if (payload.react) {
80
+ payload.html = await (0, import_render.render)(payload.react);
81
+ delete payload.react;
82
+ }
83
+ const data = await this.unsent.post(
84
+ "/emails",
85
+ payload
86
+ );
87
+ return data;
88
+ }
89
+ /**
90
+ * Send up to 100 emails in a single request.
91
+ *
92
+ * @param payload An array of email payloads. Max 100 emails.
93
+ * @returns A promise that resolves to the list of created email IDs or an error.
94
+ */
95
+ async batch(payload) {
96
+ const response = await this.unsent.post(
97
+ "/emails/batch",
98
+ payload
99
+ );
100
+ return {
101
+ data: response.data ? response.data.data : null,
102
+ error: response.error
103
+ };
104
+ }
105
+ async get(id) {
106
+ const data = await this.unsent.get(
107
+ `/emails/${id}`
108
+ );
109
+ return data;
110
+ }
111
+ async update(id, payload) {
112
+ const data = await this.unsent.patch(
113
+ `/emails/${id}`,
114
+ payload
115
+ );
116
+ return data;
117
+ }
118
+ async cancel(id) {
119
+ const data = await this.unsent.post(
120
+ `/emails/${id}/cancel`,
121
+ {}
122
+ );
123
+ return data;
124
+ }
125
+ };
126
+
127
+ // src/unsent.ts
128
+ var defaultBaseUrl = "https://app.unsent.dev";
129
+ var baseUrl = `${process?.env?.UNSENT_BASE_URL ?? process?.env?.UNSENT_BASE_URL ?? defaultBaseUrl}/api/v1`;
130
+ function isunsentErrorResponse(error) {
131
+ return error.error.code !== void 0;
132
+ }
133
+ var unsent = class {
134
+ constructor(key, url) {
135
+ this.key = key;
136
+ if (!key) {
137
+ if (typeof process !== "undefined" && process.env) {
138
+ this.key = process.env.unsent_API_KEY ?? process.env.UNSENT_API_KEY;
139
+ }
140
+ if (!this.key) {
141
+ throw new Error(
142
+ 'Missing API key. Pass it to the constructor `new unsent("us_123")`'
143
+ );
144
+ }
145
+ }
146
+ if (url) {
147
+ this.url = `${url}/api/v1`;
148
+ }
149
+ this.headers = new Headers({
150
+ Authorization: `Bearer ${this.key}`,
151
+ "Content-Type": "application/json"
152
+ });
153
+ }
154
+ headers;
155
+ // readonly domains = new Domains(this);
156
+ emails = new Emails(this);
157
+ contacts = new Contacts(this);
158
+ url = baseUrl;
159
+ async fetchRequest(path, options = {}) {
160
+ const response = await fetch(`${this.url}${path}`, options);
161
+ const defaultError = {
162
+ code: "INTERNAL_SERVER_ERROR",
163
+ message: response.statusText
164
+ };
165
+ if (!response.ok) {
166
+ try {
167
+ const resp = await response.json();
168
+ if (isunsentErrorResponse(resp)) {
169
+ return { data: null, error: resp };
170
+ }
171
+ return { data: null, error: resp.error };
172
+ } catch (err) {
173
+ if (err instanceof Error) {
174
+ return {
175
+ data: null,
176
+ error: defaultError
177
+ };
178
+ }
179
+ return { data: null, error: defaultError };
180
+ }
181
+ }
182
+ const data = await response.json();
183
+ return { data, error: null };
184
+ }
185
+ async post(path, body) {
186
+ const requestOptions = {
187
+ method: "POST",
188
+ headers: this.headers,
189
+ body: JSON.stringify(body)
190
+ };
191
+ return this.fetchRequest(path, requestOptions);
192
+ }
193
+ async get(path) {
194
+ const requestOptions = {
195
+ method: "GET",
196
+ headers: this.headers
197
+ };
198
+ return this.fetchRequest(path, requestOptions);
199
+ }
200
+ async put(path, body) {
201
+ const requestOptions = {
202
+ method: "PUT",
203
+ headers: this.headers,
204
+ body: JSON.stringify(body)
205
+ };
206
+ return this.fetchRequest(path, requestOptions);
207
+ }
208
+ async patch(path, body) {
209
+ const requestOptions = {
210
+ method: "PATCH",
211
+ headers: this.headers,
212
+ body: JSON.stringify(body)
213
+ };
214
+ return this.fetchRequest(path, requestOptions);
215
+ }
216
+ async delete(path, body) {
217
+ const requestOptions = {
218
+ method: "DELETE",
219
+ headers: this.headers,
220
+ body: JSON.stringify(body)
221
+ };
222
+ return this.fetchRequest(path, requestOptions);
223
+ }
224
+ };
225
+ // Annotate the CommonJS export names for ESM import in node:
226
+ 0 && (module.exports = {
227
+ unsent
228
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,201 @@
1
+ // src/contact.ts
2
+ var Contacts = class {
3
+ constructor(unsent2) {
4
+ this.unsent = unsent2;
5
+ this.unsent = unsent2;
6
+ }
7
+ async create(contactBookId, payload) {
8
+ const data = await this.unsent.post(
9
+ `/contactBooks/${contactBookId}/contacts`,
10
+ payload
11
+ );
12
+ return data;
13
+ }
14
+ async get(contactBookId, contactId) {
15
+ const data = await this.unsent.get(
16
+ `/contactBooks/${contactBookId}/contacts/${contactId}`
17
+ );
18
+ return data;
19
+ }
20
+ async update(contactBookId, contactId, payload) {
21
+ const data = await this.unsent.patch(
22
+ `/contactBooks/${contactBookId}/contacts/${contactId}`,
23
+ payload
24
+ );
25
+ return data;
26
+ }
27
+ async upsert(contactBookId, contactId, payload) {
28
+ const data = await this.unsent.put(
29
+ `/contactBooks/${contactBookId}/contacts/${contactId}`,
30
+ payload
31
+ );
32
+ return data;
33
+ }
34
+ async delete(contactBookId, contactId) {
35
+ const data = await this.unsent.delete(
36
+ `/contactBooks/${contactBookId}/contacts/${contactId}`
37
+ );
38
+ return data;
39
+ }
40
+ };
41
+
42
+ // src/email.ts
43
+ import { render } from "@react-email/render";
44
+ var Emails = class {
45
+ constructor(unsent2) {
46
+ this.unsent = unsent2;
47
+ this.unsent = unsent2;
48
+ }
49
+ async send(payload) {
50
+ return this.create(payload);
51
+ }
52
+ async create(payload) {
53
+ if (payload.react) {
54
+ payload.html = await render(payload.react);
55
+ delete payload.react;
56
+ }
57
+ const data = await this.unsent.post(
58
+ "/emails",
59
+ payload
60
+ );
61
+ return data;
62
+ }
63
+ /**
64
+ * Send up to 100 emails in a single request.
65
+ *
66
+ * @param payload An array of email payloads. Max 100 emails.
67
+ * @returns A promise that resolves to the list of created email IDs or an error.
68
+ */
69
+ async batch(payload) {
70
+ const response = await this.unsent.post(
71
+ "/emails/batch",
72
+ payload
73
+ );
74
+ return {
75
+ data: response.data ? response.data.data : null,
76
+ error: response.error
77
+ };
78
+ }
79
+ async get(id) {
80
+ const data = await this.unsent.get(
81
+ `/emails/${id}`
82
+ );
83
+ return data;
84
+ }
85
+ async update(id, payload) {
86
+ const data = await this.unsent.patch(
87
+ `/emails/${id}`,
88
+ payload
89
+ );
90
+ return data;
91
+ }
92
+ async cancel(id) {
93
+ const data = await this.unsent.post(
94
+ `/emails/${id}/cancel`,
95
+ {}
96
+ );
97
+ return data;
98
+ }
99
+ };
100
+
101
+ // src/unsent.ts
102
+ var defaultBaseUrl = "https://app.unsent.dev";
103
+ var baseUrl = `${process?.env?.UNSENT_BASE_URL ?? process?.env?.UNSENT_BASE_URL ?? defaultBaseUrl}/api/v1`;
104
+ function isunsentErrorResponse(error) {
105
+ return error.error.code !== void 0;
106
+ }
107
+ var unsent = class {
108
+ constructor(key, url) {
109
+ this.key = key;
110
+ if (!key) {
111
+ if (typeof process !== "undefined" && process.env) {
112
+ this.key = process.env.unsent_API_KEY ?? process.env.UNSENT_API_KEY;
113
+ }
114
+ if (!this.key) {
115
+ throw new Error(
116
+ 'Missing API key. Pass it to the constructor `new unsent("us_123")`'
117
+ );
118
+ }
119
+ }
120
+ if (url) {
121
+ this.url = `${url}/api/v1`;
122
+ }
123
+ this.headers = new Headers({
124
+ Authorization: `Bearer ${this.key}`,
125
+ "Content-Type": "application/json"
126
+ });
127
+ }
128
+ headers;
129
+ // readonly domains = new Domains(this);
130
+ emails = new Emails(this);
131
+ contacts = new Contacts(this);
132
+ url = baseUrl;
133
+ async fetchRequest(path, options = {}) {
134
+ const response = await fetch(`${this.url}${path}`, options);
135
+ const defaultError = {
136
+ code: "INTERNAL_SERVER_ERROR",
137
+ message: response.statusText
138
+ };
139
+ if (!response.ok) {
140
+ try {
141
+ const resp = await response.json();
142
+ if (isunsentErrorResponse(resp)) {
143
+ return { data: null, error: resp };
144
+ }
145
+ return { data: null, error: resp.error };
146
+ } catch (err) {
147
+ if (err instanceof Error) {
148
+ return {
149
+ data: null,
150
+ error: defaultError
151
+ };
152
+ }
153
+ return { data: null, error: defaultError };
154
+ }
155
+ }
156
+ const data = await response.json();
157
+ return { data, error: null };
158
+ }
159
+ async post(path, body) {
160
+ const requestOptions = {
161
+ method: "POST",
162
+ headers: this.headers,
163
+ body: JSON.stringify(body)
164
+ };
165
+ return this.fetchRequest(path, requestOptions);
166
+ }
167
+ async get(path) {
168
+ const requestOptions = {
169
+ method: "GET",
170
+ headers: this.headers
171
+ };
172
+ return this.fetchRequest(path, requestOptions);
173
+ }
174
+ async put(path, body) {
175
+ const requestOptions = {
176
+ method: "PUT",
177
+ headers: this.headers,
178
+ body: JSON.stringify(body)
179
+ };
180
+ return this.fetchRequest(path, requestOptions);
181
+ }
182
+ async patch(path, body) {
183
+ const requestOptions = {
184
+ method: "PATCH",
185
+ headers: this.headers,
186
+ body: JSON.stringify(body)
187
+ };
188
+ return this.fetchRequest(path, requestOptions);
189
+ }
190
+ async delete(path, body) {
191
+ const requestOptions = {
192
+ method: "DELETE",
193
+ headers: this.headers,
194
+ body: JSON.stringify(body)
195
+ };
196
+ return this.fetchRequest(path, requestOptions);
197
+ }
198
+ };
199
+ export {
200
+ unsent
201
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@unsent/sdk",
3
+ "version": "0.25.2",
4
+ "description": "",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "keywords": [],
9
+ "author": "",
10
+ "license": "MIT",
11
+ "devDependencies": {
12
+ "@types/node": "^24.7.0",
13
+ "@types/react": "^19.1.2",
14
+ "openapi-typescript": "^7.6.1",
15
+ "tsup": "^8.4.0",
16
+ "typescript": "^5.8.3",
17
+ "@workspace/typescript-config": "0.0.0",
18
+ "@workspace/eslint-config": "0.0.0"
19
+ },
20
+ "dependencies": {
21
+ "@react-email/render": "^1.0.6",
22
+ "react": "^19.1.0"
23
+ },
24
+ "scripts": {
25
+ "build": "rm -rf dist && tsup index.ts --format esm,cjs --dts",
26
+ "publish-sdk": "pnpm run build && pnpm publish --no-git-checks --access public",
27
+ "openapi-typegen": "openapi-typescript ../../apps/docs/api-reference/openapi.json -o types/schema.d.ts"
28
+ }
29
+ }