Skip to content
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
18 changes: 12 additions & 6 deletions src/contacts/contacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,12 @@ describe('Contacts', () => {
describe('remove', () => {
it('removes a contact by id', async () => {
const response: RemoveContactsResponseSuccess = {
contact: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
object: 'contact',
deleted: true,
contact: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
};
fetchMock.mockOnce(JSON.stringify(response), {
fetchMock.mockOnce(JSON.stringify({ ...response, contact: undefined }), {
status: 200,
headers: {
'content-type': 'application/json',
Expand All @@ -899,6 +900,7 @@ describe('Contacts', () => {
"data": {
"contact": "3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223",
"deleted": true,
"id": "3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223",
"object": "contact",
},
"error": null,
Expand All @@ -911,11 +913,12 @@ describe('Contacts', () => {

it('removes a contact by email', async () => {
const response: RemoveContactsResponseSuccess = {
contact: 'acme@example.com',
id: 'acme@example.com',
object: 'contact',
deleted: true,
contact: 'acme@example.com',
};
fetchMock.mockOnce(JSON.stringify(response), {
fetchMock.mockOnce(JSON.stringify({ ...response, contact: undefined }), {
status: 200,
headers: {
'content-type': 'application/json',
Expand All @@ -934,6 +937,7 @@ describe('Contacts', () => {
"data": {
"contact": "acme@example.com",
"deleted": true,
"id": "acme@example.com",
"object": "contact",
},
"error": null,
Expand All @@ -946,11 +950,12 @@ describe('Contacts', () => {

it('removes a contact by string id', async () => {
const response: RemoveContactsResponseSuccess = {
contact: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
object: 'contact',
deleted: true,
contact: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
};
fetchMock.mockOnce(JSON.stringify(response), {
fetchMock.mockOnce(JSON.stringify({ ...response, contact: undefined }), {
status: 200,
headers: {
'content-type': 'application/json',
Expand All @@ -965,6 +970,7 @@ describe('Contacts', () => {
"data": {
"contact": "3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223",
"deleted": true,
"id": "3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223",
"object": "contact",
},
"error": null,
Expand Down
38 changes: 28 additions & 10 deletions src/contacts/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import type {
import { ContactSegments } from './segments/contact-segments';
import { ContactTopics } from './topics/contact-topics';

function normalizeRemoveResponse(
response: RemoveContactsResponse,
): RemoveContactsResponse {
if (response.data && 'id' in response.data) {
return {
...response,
data: { ...response.data, contact: response.data.id },
};
}
return response;
}

export class Contacts {
readonly topics: ContactTopics;
readonly segments: ContactSegments;
Expand All @@ -40,11 +52,11 @@ export class Contacts {
}

async create(
paylaod: CreateContactOptions,
payload: CreateContactOptions,
options?: CreateContactRequestOptions,
): Promise<CreateContactResponse>;
async create(
paylaod: LegacyCreateContactOptions,
payload: LegacyCreateContactOptions,
options?: CreateContactRequestOptions,
): Promise<CreateContactResponse>;

Expand Down Expand Up @@ -183,8 +195,10 @@ export class Contacts {

async remove(payload: RemoveContactOptions): Promise<RemoveContactsResponse> {
if (typeof payload === 'string') {
return this.resend.delete<RemoveContactsResponseSuccess>(
`/contacts/${payload}`,
return normalizeRemoveResponse(
await this.resend.delete<RemoveContactsResponseSuccess>(
`/contacts/${payload}`,
),
);
}

Expand All @@ -201,15 +215,19 @@ export class Contacts {
}

if (!payload.audienceId) {
return this.resend.delete<RemoveContactsResponseSuccess>(
`/contacts/${payload?.email ? payload?.email : payload?.id}`,
return normalizeRemoveResponse(
await this.resend.delete<RemoveContactsResponseSuccess>(
`/contacts/${payload?.email ? payload?.email : payload?.id}`,
),
);
}

return this.resend.delete<RemoveContactsResponseSuccess>(
`/audiences/${payload.audienceId}/contacts/${
payload?.email ? payload?.email : payload?.id
}`,
return normalizeRemoveResponse(
await this.resend.delete<RemoveContactsResponseSuccess>(
`/audiences/${payload.audienceId}/contacts/${
payload?.email ? payload?.email : payload?.id
}`,
),
);
}
}
5 changes: 5 additions & 0 deletions src/contacts/interfaces/remove-contact.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import type { SelectingField } from './contact';
export type RemoveContactsResponseSuccess = {
object: 'contact';
deleted: boolean;
id: string;
/**
* Same as `id`. Kept for backward compatibility.
* @deprecated Use `id` instead. Will be removed in the next major version.
*/
contact: string;
};

Expand Down
12 changes: 6 additions & 6 deletions src/topics/topics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ describe('Topics', () => {
id: 'b6d24b8e-af0b-4c3c-be0c-359bbd97381e',
name: 'Newsletter',
description: 'Weekly newsletter updates',
defaultSubscription: 'opt_in',
default_subscription: 'opt_in',
created_at: '2023-04-07T23:13:52.669661+00:00',
},
{
id: 'ac7503ac-e027-4aea-94b3-b0acd46f65f9',
name: 'Product Updates',
description: 'Product announcements and updates',
defaultSubscription: 'opt_out',
default_subscription: 'opt_out',
created_at: '2023-04-07T23:13:20.417116+00:00',
},
],
Expand All @@ -149,14 +149,14 @@ describe('Topics', () => {
"data": [
{
"created_at": "2023-04-07T23:13:52.669661+00:00",
"defaultSubscription": "opt_in",
"default_subscription": "opt_in",
"description": "Weekly newsletter updates",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"name": "Newsletter",
},
{
"created_at": "2023-04-07T23:13:20.417116+00:00",
"defaultSubscription": "opt_out",
"default_subscription": "opt_out",
"description": "Product announcements and updates",
"id": "ac7503ac-e027-4aea-94b3-b0acd46f65f9",
"name": "Product Updates",
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('Topics', () => {
id: 'fd61172c-cafc-40f5-b049-b45947779a29',
name: 'Newsletter',
description: 'Weekly newsletter updates',
defaultSubscription: 'opt_in',
default_subscription: 'opt_in',
created_at: '2024-01-16T18:12:26.514Z',
};

Expand All @@ -225,7 +225,7 @@ describe('Topics', () => {
{
"data": {
"created_at": "2024-01-16T18:12:26.514Z",
"defaultSubscription": "opt_in",
"default_subscription": "opt_in",
"description": "Weekly newsletter updates",
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
"name": "Newsletter",
Expand Down
Loading