Skip to content
Merged
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: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"url": "https://github.com/resend/resend-node/issues"
},
"homepage": "https://github.com/resend/resend-node#readme",
"dependencies": {
"svix": "1.76.1"
},
"peerDependencies": {
"@react-email/render": "*"
},
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/resend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Emails } from './emails/emails';
import type { ErrorResponse } from './interfaces';
import { Templates } from './templates/templates';
import { Topics } from './topics/topics';
import { Webhooks } from './webhooks/webhooks';

const defaultBaseUrl = 'https://api.resend.com';
const defaultUserAgent = `resend-node:${version}`;
Expand All @@ -38,6 +39,7 @@ export class Resend {
readonly emails = new Emails(this);
readonly templates = new Templates(this);
readonly topics = new Topics(this);
readonly webhooks = new Webhooks();

constructor(readonly key?: string) {
if (!key) {
Expand Down
51 changes: 51 additions & 0 deletions src/webhooks/webhooks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Webhook } from 'svix';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Webhooks } from './webhooks';

const mocks = vi.hoisted(() => {
const verify = vi.fn();
const webhookConstructor = vi.fn(() => ({
verify,
}));

return {
verify,
webhookConstructor,
};
});

vi.mock('svix', () => ({
Webhook: mocks.webhookConstructor,
}));

describe('Webhooks', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.verify.mockReset();
});

it('verifies payload using svix headers', () => {
const options = {
payload: '{"type":"email.sent"}',
headers: {
id: 'msg_123',
timestamp: '1713984875',
signature: 'v1,some-signature',
},
webhookSecret: 'whsec_123',
};

const expectedResult = { id: 'msg_123', status: 'verified' };
mocks.verify.mockReturnValue(expectedResult);

const result = new Webhooks().verify(options);

expect(Webhook).toHaveBeenCalledWith(options.webhookSecret);
expect(mocks.verify).toHaveBeenCalledWith(options.payload, {
'svix-id': options.headers.id,
'svix-timestamp': options.headers.timestamp,
'svix-signature': options.headers.signature,
});
expect(result).toBe(expectedResult);
});
});
24 changes: 24 additions & 0 deletions src/webhooks/webhooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Webhook } from 'svix';

interface Headers {
id: string;
timestamp: string;
signature: string;
}

interface VerifyWebhookOptions {
payload: string;
headers: Headers;
webhookSecret: string;
}

export class Webhooks {
verify(payload: VerifyWebhookOptions) {
const webhook = new Webhook(payload.webhookSecret);
return webhook.verify(payload.payload, {
'svix-id': payload.headers.id,
'svix-timestamp': payload.headers.timestamp,
'svix-signature': payload.headers.signature,
});
}
}
Loading