Skip to content

Commit c78727c

Browse files
authored
Merge pull request #142 from mailtrap/MT-22678-nodejs-contact-list-search
MT-22678: Add search param to contact lists getList
2 parents acd1973 + b566f3f commit c78727c

5 files changed

Lines changed: 95 additions & 4 deletions

File tree

examples/contact-lists/everything.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ async function contactListsFlow() {
1818
const all = await client.contactLists.getList();
1919
console.log("All contact lists:", all);
2020

21+
// Filter contact lists by name (case-insensitive prefix match)
22+
const filtered = await client.contactLists.getList({ search: "news" });
23+
console.log("Filtered contact lists:", filtered);
24+
2125
// Get a specific contact list
2226
const one = await client.contactLists.get(all[0].id);
2327
console.log("One contact list:", one);

src/__tests__/lib/api/ContactLists.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import axios from "axios";
2+
import AxiosMockAdapter from "axios-mock-adapter";
23

34
import ContactLists from "../../../lib/api/ContactLists";
5+
import handleSendingError from "../../../lib/axios-logger";
6+
import { ContactList } from "../../../types/api/contactlist";
7+
8+
import CONFIG from "../../../config";
9+
10+
const { CLIENT_SETTINGS } = CONFIG;
11+
const { GENERAL_ENDPOINT } = CLIENT_SETTINGS;
412

513
describe("lib/api/ContactLists: ", () => {
14+
let mock: AxiosMockAdapter;
615
const accountId = 100;
716
const contactListsAPI = new ContactLists(axios, accountId);
817

@@ -17,4 +26,53 @@ describe("lib/api/ContactLists: ", () => {
1726
});
1827
});
1928
});
29+
30+
beforeAll(() => {
31+
axios.interceptors.response.use(
32+
(response) => response.data,
33+
handleSendingError
34+
);
35+
mock = new AxiosMockAdapter(axios);
36+
});
37+
38+
afterEach(() => {
39+
mock.reset();
40+
});
41+
42+
describe("getList(): ", () => {
43+
it("successfully gets all contact lists.", async () => {
44+
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
45+
const expectedResponseData: ContactList[] = [
46+
{ id: 1, name: "Test List 1" },
47+
{ id: 2, name: "Test List 2" },
48+
];
49+
50+
expect.assertions(2);
51+
52+
mock.onGet(endpoint).reply(200, expectedResponseData);
53+
const result = await contactListsAPI.getList();
54+
55+
expect(mock.history.get[0].url).toEqual(endpoint);
56+
expect(result).toEqual(expectedResponseData);
57+
});
58+
59+
it("passes the search param to the request.", async () => {
60+
const search = "news";
61+
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
62+
const expectedResponseData: ContactList[] = [
63+
{ id: 1, name: "Newsletter" },
64+
];
65+
66+
expect.assertions(3);
67+
68+
mock
69+
.onGet(endpoint, { params: { search } })
70+
.reply(200, expectedResponseData);
71+
const result = await contactListsAPI.getList({ search });
72+
73+
expect(mock.history.get[0].url).toEqual(endpoint);
74+
expect(mock.history.get[0].params).toEqual({ search });
75+
expect(result).toEqual(expectedResponseData);
76+
});
77+
});
2078
});

src/__tests__/lib/api/resources/ContactLists.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ describe("lib/api/resources/ContactLists: ", () => {
7575
expect(result).toEqual(expectedResponseData);
7676
});
7777

78+
it("successfully gets contact lists filtered by name.", async () => {
79+
const search = "news";
80+
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
81+
const expectedResponseData: ContactList[] = [
82+
{ id: 1, name: "Newsletter" },
83+
];
84+
85+
expect.assertions(3);
86+
87+
mock
88+
.onGet(endpoint, { params: { search } })
89+
.reply(200, expectedResponseData);
90+
const result = await contactListsAPI.getList({ search });
91+
92+
expect(mock.history.get[0].url).toEqual(endpoint);
93+
expect(mock.history.get[0].params).toEqual({ search });
94+
expect(result).toEqual(expectedResponseData);
95+
});
96+
7897
it("fails with error.", async () => {
7998
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`;
8099
const expectedErrorMessage = "Request failed with status code 400";

src/lib/api/resources/ContactLists.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import CONFIG from "../../../config";
44
import {
55
ContactList,
66
ContactListOptions,
7+
ContactListsListOptions,
78
} from "../../../types/api/contactlist";
89

910
const { CLIENT_SETTINGS } = CONFIG;
@@ -20,12 +21,17 @@ export default class ContactListsApi {
2021
}
2122

2223
/**
23-
* Get all contact lists.
24+
* Get all contact lists. Optionally filter by name via a case-insensitive
25+
* prefix match with `search`.
2426
*/
25-
public async getList() {
26-
const url = `${this.contactListsURL}`;
27+
public async getList(options?: ContactListsListOptions) {
28+
const params = {
29+
...(options?.search && { search: options.search }),
30+
};
2731

28-
return this.client.get<ContactList[], ContactList[]>(url);
32+
return this.client.get<ContactList[], ContactList[]>(this.contactListsURL, {
33+
params,
34+
});
2935
}
3036

3137
/**

src/types/api/contactlist.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export interface ContactList {
66
export interface ContactListOptions {
77
name: string;
88
}
9+
10+
export interface ContactListsListOptions {
11+
search?: string;
12+
}

0 commit comments

Comments
 (0)