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
4 changes: 4 additions & 0 deletions examples/contact-lists/everything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ async function contactListsFlow() {
const all = await client.contactLists.getList();
console.log("All contact lists:", all);

// Filter contact lists by name (case-insensitive prefix match)
const filtered = await client.contactLists.getList({ search: "news" });
console.log("Filtered contact lists:", filtered);

// Get a specific contact list
const one = await client.contactLists.get(all[0].id);
console.log("One contact list:", one);
Expand Down
58 changes: 58 additions & 0 deletions src/__tests__/lib/api/ContactLists.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import axios from "axios";
import AxiosMockAdapter from "axios-mock-adapter";

import ContactLists from "../../../lib/api/ContactLists";
import handleSendingError from "../../../lib/axios-logger";
import { ContactList } from "../../../types/api/contactlist";

import CONFIG from "../../../config";

const { CLIENT_SETTINGS } = CONFIG;
const { GENERAL_ENDPOINT } = CLIENT_SETTINGS;

describe("lib/api/ContactLists: ", () => {
let mock: AxiosMockAdapter;
const accountId = 100;
const contactListsAPI = new ContactLists(axios, accountId);

Expand All @@ -17,4 +26,53 @@ describe("lib/api/ContactLists: ", () => {
});
});
});

beforeAll(() => {
axios.interceptors.response.use(
(response) => response.data,
handleSendingError
);
mock = new AxiosMockAdapter(axios);
});

afterEach(() => {
mock.reset();
});

describe("getList(): ", () => {
it("successfully gets all contact lists.", async () => {
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
const expectedResponseData: ContactList[] = [
{ id: 1, name: "Test List 1" },
{ id: 2, name: "Test List 2" },
];

expect.assertions(2);

mock.onGet(endpoint).reply(200, expectedResponseData);
const result = await contactListsAPI.getList();

expect(mock.history.get[0].url).toEqual(endpoint);
expect(result).toEqual(expectedResponseData);
});

it("passes the search param to the request.", async () => {
const search = "news";
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
const expectedResponseData: ContactList[] = [
{ id: 1, name: "Newsletter" },
];

expect.assertions(3);

mock
.onGet(endpoint, { params: { search } })
.reply(200, expectedResponseData);
const result = await contactListsAPI.getList({ search });

expect(mock.history.get[0].url).toEqual(endpoint);
expect(mock.history.get[0].params).toEqual({ search });
expect(result).toEqual(expectedResponseData);
});
});
});
19 changes: 19 additions & 0 deletions src/__tests__/lib/api/resources/ContactLists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ describe("lib/api/resources/ContactLists: ", () => {
expect(result).toEqual(expectedResponseData);
});

it("successfully gets contact lists filtered by name.", async () => {
const search = "news";
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
const expectedResponseData: ContactList[] = [
{ id: 1, name: "Newsletter" },
];

expect.assertions(3);

mock
.onGet(endpoint, { params: { search } })
.reply(200, expectedResponseData);
const result = await contactListsAPI.getList({ search });

expect(mock.history.get[0].url).toEqual(endpoint);
expect(mock.history.get[0].params).toEqual({ search });
expect(result).toEqual(expectedResponseData);
});

it("fails with error.", async () => {
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
const expectedErrorMessage = "Request failed with status code 400";
Expand Down
14 changes: 10 additions & 4 deletions src/lib/api/resources/ContactLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CONFIG from "../../../config";
import {
ContactList,
ContactListOptions,
ContactListsListOptions,
} from "../../../types/api/contactlist";

const { CLIENT_SETTINGS } = CONFIG;
Expand All @@ -20,12 +21,17 @@ export default class ContactListsApi {
}

/**
* Get all contact lists.
* Get all contact lists. Optionally filter by name via a case-insensitive
* prefix match with `search`.
*/
public async getList() {
const url = `${this.contactListsURL}`;
public async getList(options?: ContactListsListOptions) {
const params = {
...(options?.search && { search: options.search }),
};

return this.client.get<ContactList[], ContactList[]>(url);
return this.client.get<ContactList[], ContactList[]>(this.contactListsURL, {
params,
});
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/types/api/contactlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export interface ContactList {
export interface ContactListOptions {
name: string;
}

export interface ContactListsListOptions {
search?: string;
}
Loading