-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathresend.ts
More file actions
169 lines (145 loc) · 4.68 KB
/
resend.ts
File metadata and controls
169 lines (145 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { version } from '../package.json';
import { ApiKeys } from './api-keys/api-keys';
import { Audiences } from './audiences/audiences';
import { Batch } from './batch/batch';
import { Broadcasts } from './broadcasts/broadcasts';
import type { GetOptions, PostOptions, PutOptions } from './common/interfaces';
import type { IdempotentRequest } from './common/interfaces/idempotent-request.interface';
import type { PatchOptions } from './common/interfaces/patch-option.interface';
import { Contacts } from './contacts/contacts';
import { Domains } from './domains/domains';
import { Emails } from './emails/emails';
import type { ErrorResponse } from './interfaces';
import { Topics } from './topics/topics';
const defaultBaseUrl = 'https://api.resend.com';
const defaultUserAgent = `resend-node:${version}`;
const baseUrl =
typeof process !== 'undefined' && process.env
? process.env.RESEND_BASE_URL || defaultBaseUrl
: defaultBaseUrl;
const userAgent =
typeof process !== 'undefined' && process.env
? process.env.RESEND_USER_AGENT || defaultUserAgent
: defaultUserAgent;
export class Resend {
private readonly headers: Headers;
readonly apiKeys = new ApiKeys(this);
readonly audiences = new Audiences(this);
readonly batch = new Batch(this);
readonly broadcasts = new Broadcasts(this);
readonly contacts = new Contacts(this);
readonly domains = new Domains(this);
readonly emails = new Emails(this);
readonly topics = new Topics(this);
constructor(readonly key?: string) {
if (!key) {
if (typeof process !== 'undefined' && process.env) {
this.key = process.env.RESEND_API_KEY;
}
if (!this.key) {
throw new Error(
'Missing API key. Pass it to the constructor `new Resend("re_123")`',
);
}
}
this.headers = new Headers({
Authorization: `Bearer ${this.key}`,
'User-Agent': userAgent,
'Content-Type': 'application/json',
});
}
async fetchRequest<T>(
path: string,
options = {},
): Promise<{ data: T | null; error: ErrorResponse | null }> {
try {
const response = await fetch(`${baseUrl}${path}`, options);
if (!response.ok) {
try {
const rawError = await response.text();
return { data: null, error: JSON.parse(rawError) };
} catch (err) {
if (err instanceof SyntaxError) {
return {
data: null,
error: {
name: 'application_error',
message:
'Internal server error. We are unable to process your request right now, please try again later.',
},
};
}
const error: ErrorResponse = {
message: response.statusText,
name: 'application_error',
};
if (err instanceof Error) {
return { data: null, error: { ...error, message: err.message } };
}
return { data: null, error };
}
}
const data = await response.json();
return { data, error: null };
} catch (error) {
return {
data: null,
error: {
name: 'application_error',
message: 'Unable to fetch data. The request could not be resolved.',
},
};
}
}
async post<T>(
path: string,
entity?: unknown,
options: PostOptions & IdempotentRequest = {},
) {
const headers = new Headers(this.headers);
if (options.idempotencyKey) {
headers.set('Idempotency-Key', options.idempotencyKey);
}
const requestOptions = {
method: 'POST',
headers: headers,
body: JSON.stringify(entity),
...options,
};
return this.fetchRequest<T>(path, requestOptions);
}
async get<T>(path: string, options: GetOptions = {}) {
const requestOptions = {
method: 'GET',
headers: this.headers,
...options,
};
return this.fetchRequest<T>(path, requestOptions);
}
async put<T>(path: string, entity: unknown, options: PutOptions = {}) {
const requestOptions = {
method: 'PUT',
headers: this.headers,
body: JSON.stringify(entity),
...options,
};
return this.fetchRequest<T>(path, requestOptions);
}
async patch<T>(path: string, entity: unknown, options: PatchOptions = {}) {
const requestOptions = {
method: 'PATCH',
headers: this.headers,
body: JSON.stringify(entity),
...options,
};
return this.fetchRequest<T>(path, requestOptions);
}
async delete<T>(path: string, query?: unknown) {
const requestOptions = {
method: 'DELETE',
headers: this.headers,
body: JSON.stringify(query),
};
return this.fetchRequest<T>(path, requestOptions);
}
}