Skip to content

Commit 01d33b2

Browse files
committed
actually add rateLimiting field to response
1 parent f6110da commit 01d33b2

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/contacts/contacts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class Contacts {
5757
if (!options.id && !options.email) {
5858
return {
5959
data: null,
60+
rateLimiting: null,
6061
error: {
6162
message: 'Missing `id` or `email` field.',
6263
name: 'missing_required_field',
@@ -74,6 +75,7 @@ export class Contacts {
7475
if (!payload.id && !payload.email) {
7576
return {
7677
data: null,
78+
rateLimiting: null,
7779
error: {
7880
message: 'Missing `id` or `email` field.',
7981
name: 'missing_required_field',
@@ -96,6 +98,7 @@ export class Contacts {
9698
if (!payload.id && !payload.email) {
9799
return {
98100
data: null,
101+
rateLimiting: null,
99102
error: {
100103
message: 'Missing `id` or `email` field.',
101104
name: 'missing_required_field',

src/interfaces.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RateLimit } from './rate-limiting';
1+
import type { RateLimit } from './rate-limiting';
22

33
export const RESEND_ERROR_CODES_BY_KEY = {
44
missing_required_field: 422,
@@ -26,17 +26,16 @@ export type ErrorResponse = {
2626
name: RESEND_ERROR_CODE_KEY;
2727
};
2828

29-
export type Response<Data> = (
29+
export type Response<Data> =
3030
| {
3131
data: Data;
32+
rateLimiting: RateLimit;
3233
error: null;
3334
}
3435
| {
3536
data: null;
37+
rateLimiting: RateLimit | null;
3638
error: ErrorResponse;
37-
}
38-
) & {
39-
rateLimiting: RateLimit;
40-
};
39+
};
4140

4241
export type Tag = { name: string; value: string };

src/rate-limiting.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ export type RateLimit = {
44
shouldResetAfter: number;
55
};
66

7-
export function parseRateLimit(headers: Headers): RateLimit | undefined {
7+
export function parseRateLimit(headers: Headers): RateLimit {
88
const limitHeader = headers.get('ratelimit-limit');
99
const remainingHeader = headers.get('ratelimit-remaining');
1010
const resetHeader = headers.get('ratelimit-reset');
1111

1212
if (!limitHeader || !remainingHeader || !resetHeader) {
13-
console.warn(
13+
throw new Error(
1414
"The rate limit headers are not present in the response, something must've gone wrong, please email us at support@resend.com",
1515
);
16-
return undefined;
1716
}
1817

1918
const limit = Number.parseInt(limitHeader, 10);

src/resend.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import type { PatchOptions } from './common/interfaces/patch-option.interface';
99
import { Contacts } from './contacts/contacts';
1010
import { Domains } from './domains/domains';
1111
import { Emails } from './emails/emails';
12-
import type { ErrorResponse } from './interfaces';
12+
import type { ErrorResponse, Response } from './interfaces';
13+
import { parseRateLimit } from './rate-limiting';
1314

1415
const defaultBaseUrl = 'https://api.resend.com';
1516
const defaultUserAgent = `resend-node:${version}`;
@@ -53,21 +54,21 @@ export class Resend {
5354
});
5455
}
5556

56-
async fetchRequest<T>(
57-
path: string,
58-
options = {},
59-
): Promise<{ data: T; error: null } | { data: null; error: ErrorResponse }> {
57+
async fetchRequest<T>(path: string, options = {}): Promise<Response<T>> {
6058
try {
6159
const response = await fetch(`${baseUrl}${path}`, options);
6260

61+
const rateLimiting = parseRateLimit(response.headers);
62+
6363
if (!response.ok) {
6464
try {
6565
const rawError = await response.text();
66-
return { data: null, error: JSON.parse(rawError) };
66+
return { data: null, rateLimiting, error: JSON.parse(rawError) };
6767
} catch (err) {
6868
if (err instanceof SyntaxError) {
6969
return {
7070
data: null,
71+
rateLimiting,
7172
error: {
7273
name: 'application_error',
7374
message:
@@ -82,18 +83,23 @@ export class Resend {
8283
};
8384

8485
if (err instanceof Error) {
85-
return { data: null, error: { ...error, message: err.message } };
86+
return {
87+
data: null,
88+
rateLimiting: rateLimiting,
89+
error: { ...error, message: err.message },
90+
};
8691
}
8792

88-
return { data: null, error };
93+
return { data: null, rateLimiting, error };
8994
}
9095
}
9196

9297
const data = await response.json();
93-
return { data, error: null };
98+
return { data, rateLimiting, error: null };
9499
} catch (error) {
95100
return {
96101
data: null,
102+
rateLimiting: null,
97103
error: {
98104
name: 'application_error',
99105
message: 'Unable to fetch data. The request could not be resolved.',

0 commit comments

Comments
 (0)