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

feat: Ability to modify headers and custom fetch implementation #269

Open
wants to merge 4 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"homepage": "https://github.com/resendlabs/resend-node#readme",
"dependencies": {
"@react-email/render": "0.0.9"
"@react-email/render": "0.0.9",
"undici": "^5.27.2"
},
"devDependencies": {
"@types/jest": "29.5.7",
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import type { fetch } from 'undici';

export type Fetch = typeof fetch;

export type Options = {
fetch?: Fetch;
headers?: Record<string, string>;
};

export const RESEND_ERROR_CODES_BY_KEY = {
missing_required_field: 422,
invalid_access: 422,
Expand Down
25 changes: 20 additions & 5 deletions src/resend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { Contacts } from './contacts/contacts';
import { Domains } from './domains/domains';
import { Emails } from './emails/emails';
import { isResendErrorResponse } from './guards';
import { ErrorResponse } from './interfaces';
import { ErrorResponse, Fetch, Options } from './interfaces';
import { fetch } from 'undici';

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 fetch: Fetch = fetch;
private readonly headers: Headers;

readonly apiKeys = new ApiKeys(this);
Expand All @@ -22,7 +24,10 @@ export class Resend {
readonly domains = new Domains(this);
readonly emails = new Emails(this);

constructor(readonly key?: string) {
constructor(
readonly key?: string,
options: Options = {},
) {
if (!key) {
this.key = process.env.RESEND_API_KEY;

Expand All @@ -33,29 +38,39 @@ export class Resend {
}
}

if (options.fetch) {
this.fetch = options.fetch;
}

this.headers = new Headers({
Authorization: `Bearer ${this.key}`,
'User-Agent': userAgent,
'Content-Type': 'application/json',
});

if (options.headers) {
for (const [key, value] of Object.entries(options.headers)) {
this.headers.set(key, value);
}
}
}

async fetchRequest<T>(
path: string,
options = {},
): Promise<{ data: T | null; error: ErrorResponse | null }> {
const response = await fetch(`${baseUrl}${path}`, options);
const response = await this.fetch(`${baseUrl}${path}`, options);

if (!response.ok) {
const error = await response.json();
const error = (await response.json()) as any;
if (isResendErrorResponse(error)) {
return { data: null, error };
}

return { data: null, error };
}

const data = await response.json();
const data = (await response.json()) as any;
return { data, error: null };
}

Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,13 @@ __metadata:
languageName: node
linkType: hard

"@fastify/busboy@npm:^2.0.0":
version: 2.1.0
resolution: "@fastify/busboy@npm:2.1.0"
checksum: 3233abd10f73e50668cb4bb278a79b7b3fadd30215ac6458299b0e5a09a29c3586ec07597aae6bd93f5cbedfcef43a8aeea51829cd28fc13850cdbcd324c28d5
languageName: node
linkType: hard

"@humanwhocodes/config-array@npm:^0.11.13":
version: 0.11.13
resolution: "@humanwhocodes/config-array@npm:0.11.13"
Expand Down Expand Up @@ -4318,6 +4325,7 @@ __metadata:
ts-jest: 29.1.1
ts-node: 10.9.1
typescript: 5.2.2
undici: ^5.27.2
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -4897,6 +4905,15 @@ __metadata:
languageName: node
linkType: hard

"undici@npm:^5.27.2":
version: 5.27.2
resolution: "undici@npm:5.27.2"
dependencies:
"@fastify/busboy": ^2.0.0
checksum: 22bbdd763798700979986546d70072b67223189353d2a811efa9c6e44476161a0d1781ffe24115221f69a1b344b95d5926bd39a6eb760a2cd8804781cec0c5eb
languageName: node
linkType: hard

"unique-filename@npm:^3.0.0":
version: 3.0.0
resolution: "unique-filename@npm:3.0.0"
Expand Down