Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace axios with node-fetch #149

Merged
merged 3 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"homepage": "https://github.com/resend-inc/resend-node#readme",
"dependencies": {
"@react-email/render": "0.0.7",
"axios": "1.4.0",
"node-fetch": "3.3.2",
"type-fest": "3.13.0"
},
"devDependencies": {
Expand Down
153 changes: 65 additions & 88 deletions src/resend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import fetch, { Headers } from 'node-fetch';
import * as React from 'react';
import { render } from '@react-email/render';
import { version } from '../package.json';
Expand All @@ -12,9 +12,8 @@ const baseUrl = process.env.RESEND_BASE_URL || 'https://api.resend.com';
const userAgent = process.env.RESEND_USER_AGENT || `resend-node:${version}`;

export class Resend {
private readonly headers: HeadersInit;
private readonly request: AxiosInstance;

private readonly headers: Headers;

readonly apiKeys = new ApiKeys(this);
readonly domains = new Domains(this);
readonly emails = new Emails(this);
Expand All @@ -30,113 +29,91 @@ export class Resend {
}
}

this.headers = {
this.headers = new Headers({
Authorization: `Bearer ${this.key}`,
'User-Agent': userAgent,
'Content-Type': 'application/json',
};
this.request = axios.create({
baseURL: baseUrl,
headers: this.headers,
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
}

async fetchRequest(path: string, options = {}) {
const response = await fetch(`${baseUrl}${path}`, options);

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Unknown error');
}

this.request.interceptors.response.use(
(response) => {
if (response.data) {
return response.data;
}

return response;
},
(error) => {
if (error?.response?.data?.error) {
return Promise.reject(error.response.data.error);
}

if (error?.response?.data) {
return Promise.reject(error.response.data);
}
return Promise.reject(error);
},
);
return await response.json();
}

async post<T>(path: string, entity?: any, options?: PostOptions): Promise<T> {
const requestHeaders: any = {};
bukinoshita marked this conversation as resolved.
Show resolved Hide resolved
try {
return await this.request.post(path, entity, {
params: options?.query,
headers: requestHeaders,
});
} catch (error) {
throw error;
}
const requestOptions = {
method: 'POST',
headers: this.headers,
body: JSON.stringify(entity),
...options,
};

return await this.fetchRequest(path, requestOptions);
}

async get<T>(path: string, options: GetOptions = {}): Promise<T> {
try {
return await this.request.get(path, {
params: options.query,
});
} catch (error) {
throw error;
}
const requestOptions = {
method: 'GET',
headers: this.headers,
...options,
};

return await this.fetchRequest(path, requestOptions);
}

async put(
path: string,
entity: any,
options: PutOptions = {},
): Promise<AxiosResponse> {
const requestHeaders: any = {};

try {
return await this.request.put(path, entity, {
params: options.query,
headers: requestHeaders,
});
} catch (error) {
throw error;
}
): Promise<any> {
const requestOptions = {
method: 'PUT',
headers: this.headers,
body: JSON.stringify(entity),
...options,
};

return await this.fetchRequest(path, requestOptions);
}

async delete(path: string, query?: any): Promise<AxiosResponse> {
try {
return await this.request.delete(path, {
params: query,
});
} catch (error) {
throw error;
}
async delete(path: string, query?: any): Promise<any> {
const requestOptions = {
method: 'DELETE',
headers: this.headers,
body: JSON.stringify(query),
};

return await this.fetchRequest(path, requestOptions);
}

async sendEmail(data: CreateEmailOptions): Promise<CreateEmailResponse> {
try {
const path = `${baseUrl}/email`;

if (data.react) {
data.html = render(data.react as React.ReactElement);
delete data.react;
}
const path = '/email';

const response = await this.post<CreateEmailResponse>(path, {
from: data.from,
to: data.to,
bcc: data.bcc,
cc: data.cc,
reply_to: data.reply_to,
subject: data.subject,
text: data.text,
html: data.html,
attachments: data.attachments,
tags: data.tags,
});

return response;
} catch (error) {
throw error;
if (data.react) {
data.html = render(data.react as React.ReactElement);
delete data.react;
}

const response = await this.post<CreateEmailResponse>(path, {
from: data.from,
to: data.to,
bcc: data.bcc,
cc: data.cc,
reply_to: data.reply_to,
subject: data.subject,
text: data.text,
html: data.html,
attachments: data.attachments,
tags: data.tags,
});

return response;
}
}
58 changes: 39 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -808,15 +808,6 @@ axios-mock-adapter@1.21.5:
fast-deep-equal "^3.1.3"
is-buffer "^2.0.5"

axios@1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f"
integrity sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

babel-jest@^29.6.1:
version "29.6.1"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.6.1.tgz#a7141ad1ed5ec50238f3cd36127636823111233a"
Expand Down Expand Up @@ -1111,6 +1102,11 @@ csstype@^3.0.2:
resolved "https://registry.npmjs.org/csstype/-/csstype-3.1.0.tgz"
integrity sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA==

data-uri-to-buffer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

debug@^4.1.0, debug@^4.1.1, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
Expand Down Expand Up @@ -1315,6 +1311,14 @@ fb-watchman@^2.0.0:
dependencies:
bser "2.1.1"

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

fill-range@^7.0.1:
version "7.0.1"
resolved "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz"
Expand All @@ -1330,11 +1334,6 @@ find-up@^4.0.0, find-up@^4.1.0:
locate-path "^5.0.0"
path-exists "^4.0.0"

follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz"
Expand All @@ -1344,6 +1343,13 @@ form-data@^4.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

formidable@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/formidable/-/formidable-2.1.1.tgz#81269cbea1a613240049f5f61a9d97731517414f"
Expand Down Expand Up @@ -2219,6 +2225,20 @@ natural-compare@^1.4.0:
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b"
integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz"
Expand Down Expand Up @@ -2403,11 +2423,6 @@ proto-list@~1.2.1:
resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
integrity sha512-vtK/94akxsTMhe0/cbfpR+syPuszcuwhqVjJq26CuNDgFGj682oRBXOP5MJpv2r7JtE8MsiepGIqvvOTBwn2vA==

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
Expand Down Expand Up @@ -2782,6 +2797,11 @@ walker@^1.0.8:
dependencies:
makeerror "1.0.12"

web-streams-polyfill@^3.0.3:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

which@^2.0.1:
version "2.0.2"
resolved "https://registry.npmjs.org/which/-/which-2.0.2.tgz"
Expand Down
Loading