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: Add contacts methods #268

Merged
merged 2 commits into from
Nov 16, 2023
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
8 changes: 4 additions & 4 deletions src/audiences/audiences.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('Audiences', () => {
);

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
await expect(resend.audiences.create({ name: 'resend.com' })).resolves.
toMatchInlineSnapshot(`
await expect(resend.audiences.create({ name: 'resend.com' })).resolves
.toMatchInlineSnapshot(`
{
"data": {
"created_at": "2023-04-07T22:48:33.420498+00:00",
Expand Down Expand Up @@ -169,8 +169,8 @@ toMatchInlineSnapshot(`

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

await expect(resend.audiences.get('1234')).resolves.
toMatchInlineSnapshot(`
await expect(resend.audiences.get('1234')).resolves
.toMatchInlineSnapshot(`
{
"data": {
"created_at": "2023-06-21T06:10:36.144Z",
Expand Down
227 changes: 227 additions & 0 deletions src/contacts/contacts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import { enableFetchMocks } from 'jest-fetch-mock';
import { Resend } from '../resend';
import { GetContactResponseSuccess } from './interfaces';
import { ErrorResponse } from '../interfaces';

enableFetchMocks();

describe('Contacts', () => {
afterEach(() => fetchMock.resetMocks());

describe('create', () => {
it('creates a contact', async () => {
fetchMock.mockOnce(
JSON.stringify({
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
name: 'Resend',
created_at: '2023-04-07T22:48:33.420498+00:00',
}),
{
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_924b3rjh2387fbewf823',
},
},
);

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');
await expect(
resend.contacts.create({
email: 'team@resend.com',
audience_id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
}),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"created_at": "2023-04-07T22:48:33.420498+00:00",
"id": "3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222",
"name": "Resend",
},
"error": null,
}
`);
});

it('throws error when missing name', async () => {
const errorResponse: ErrorResponse = {
name: 'missing_required_field',
message: 'Missing `email` field.',
};

fetchMock.mockOnce(JSON.stringify(errorResponse), {
status: 422,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_924b3rjh2387fbewf823',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

const result = resend.contacts.create({ email: '', audience_id: '' });

await expect(result).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "Missing \`email\` field.",
"name": "missing_required_field",
},
}
`);
});
});

describe('list', () => {
it('lists contacts', async () => {
fetchMock.mockOnce(
JSON.stringify({
data: [
{
id: 'b6d24b8e-af0b-4c3c-be0c-359bbd97381e',
name: 'resend.com',
created_at: '2023-04-07T23:13:52.669661+00:00',
},
{
id: 'ac7503ac-e027-4aea-94b3-b0acd46f65f9',
name: 'react.email',
created_at: '2023-04-07T23:13:20.417116+00:00',
},
],
}),
{
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_924b3rjh2387fbewf823',
},
},
);

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

await expect(
resend.contacts.list({
audience_id: 'b6d24b8e-af0b-4c3c-be0c-359bbd97381a',
}),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"data": [
{
"created_at": "2023-04-07T23:13:52.669661+00:00",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"name": "resend.com",
},
{
"created_at": "2023-04-07T23:13:20.417116+00:00",
"id": "ac7503ac-e027-4aea-94b3-b0acd46f65f9",
"name": "react.email",
},
],
},
"error": null,
}
`);
});
});

describe('get', () => {
describe('when contact not found', () => {
it('returns error', async () => {
const errorResponse: ErrorResponse = {
name: 'not_found',
message: 'Contact not found',
};

fetchMock.mockOnce(JSON.stringify(errorResponse), {
status: 404,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_924b3rjh2387fbewf823',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

const result = resend.contacts.get({
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
audience_id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
});

await expect(result).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "Contact not found",
"name": "not_found",
},
}
`);
});
});

it('get contact', async () => {
const contact: GetContactResponseSuccess = {
object: 'contact',
id: 'fd61172c-cafc-40f5-b049-b45947779a29',
name: 'resend.com',
created_at: '2023-06-21T06:10:36.144Z',
};

fetchMock.mockOnce(JSON.stringify(contact), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_924b3rjh2387fbewf823',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

await expect(
resend.contacts.get({
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
audience_id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
}),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"created_at": "2023-06-21T06:10:36.144Z",
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
"name": "resend.com",
"object": "contact",
},
"error": null,
}
`);
});
});

describe('remove', () => {
it('removes a contact', async () => {
fetchMock.mockOnce(JSON.stringify({}), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_924b3rjh2387fbewf823',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

await expect(
resend.contacts.remove({
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
audience_id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
}),
).resolves.toMatchInlineSnapshot(`
{
"data": {},
"error": null,
}
`);
});
});
});
66 changes: 66 additions & 0 deletions src/contacts/contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Resend } from '../resend';
import {
CreateContactOptions,
CreateContactRequestOptions,
CreateContactResponse,
CreateContactResponseSuccess,
GetContactResponse,
GetContactResponseSuccess,
ListContactsResponse,
ListContactsResponseSuccess,
RemoveContactsResponse,
RemoveContactsResponseSuccess,
} from './interfaces';

export class Contacts {
constructor(private readonly resend: Resend) {}

async create(
payload: CreateContactOptions,
options: CreateContactRequestOptions = {},
): Promise<CreateContactResponse> {
const data = await this.resend.post<CreateContactResponseSuccess>(
`/audiences/${payload.audience_id}/contacts`,
payload,
options,
);
return data;
}

async list({
audience_id,
}: {
audience_id: string;
}): Promise<ListContactsResponse> {
const data = await this.resend.get<ListContactsResponseSuccess>(
`/audiences/${audience_id}/contacts`,
);
return data;
}

async get({
audience_id,
id,
}: {
audience_id: string;
id: string;
}): Promise<GetContactResponse> {
const data = await this.resend.get<GetContactResponseSuccess>(
`/audiences/${audience_id}/contacts/${id}`,
);
return data;
}

async remove({
audience_id,
id,
}: {
audience_id: string;
id: string;
}): Promise<RemoveContactsResponse> {
const data = await this.resend.delete<RemoveContactsResponseSuccess>(
`/audiences/${audience_id}/contacts/${id}`,
);
return data;
}
}
5 changes: 5 additions & 0 deletions src/contacts/interfaces/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Contact {
created_at: string;
id: string;
name: string;
}
21 changes: 21 additions & 0 deletions src/contacts/interfaces/create-contact-options.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PostOptions } from '../../common/interfaces';
import { ErrorResponse } from '../../interfaces';
import { Contact } from './contact';

export interface CreateContactOptions {
audience_id: string;
email: string;
unsubscribed?: boolean;
first_name?: string;
last_name?: string;
}

export interface CreateContactRequestOptions extends PostOptions {}

export interface CreateContactResponseSuccess
extends Pick<Contact, 'name' | 'id' | 'created_at'> {}

export interface CreateContactResponse {
data: CreateContactResponseSuccess | null;
error: ErrorResponse | null;
}
12 changes: 12 additions & 0 deletions src/contacts/interfaces/get-contact.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ErrorResponse } from '../../interfaces';
import { Contact } from './contact';

export interface GetContactResponseSuccess
extends Pick<Contact, 'id' | 'name' | 'created_at'> {
object: 'contact';
}

export interface GetContactResponse {
data: GetContactResponseSuccess | null;
error: ErrorResponse | null;
}
4 changes: 4 additions & 0 deletions src/contacts/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './create-contact-options.interface';
export * from './list-contacts.interface';
export * from './get-contact.interface';
export * from './remove-contact.interface';
9 changes: 9 additions & 0 deletions src/contacts/interfaces/list-contacts.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ErrorResponse } from '../../interfaces';
import { Contact } from './contact';

export type ListContactsResponseSuccess = Contact[];

export interface ListContactsResponse {
data: ListContactsResponseSuccess | null;
error: ErrorResponse | null;
}
9 changes: 9 additions & 0 deletions src/contacts/interfaces/remove-contact.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ErrorResponse } from '../../interfaces';
import { Contact } from './contact';

export type RemoveContactsResponseSuccess = Pick<Contact, 'id'>;

export interface RemoveContactsResponse {
data: RemoveContactsResponseSuccess | null;
error: ErrorResponse | null;
}
Loading