Skip to content

Commit 7f3964e

Browse files
committed
fix: transform anyEmail array into comma-delimited any_email parameter in threads list
1 parent 5a0800b commit 7f3964e

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
* Remove `createdAt` field from message model.
55
* Change `latestMessageReceivedDate` & `latestMessageSentDate` to be optional on threads model.
6+
* Fix issue where query params with array values were not being transformed into comma-delimited strings
67

78
### 7.7.2 / 2024-12-02
89
* Fix `credentials` resource to use correct endpoint.

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/apiClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export default class APIClient {
106106
}
107107
url.searchParams.set('metadata_pair', metadataPair.join(','));
108108
} else if (Array.isArray(value)) {
109-
url.searchParams.append(snakeCaseKey, value.join(','));
109+
for (const item of value) {
110+
url.searchParams.append(snakeCaseKey, item as string);
111+
}
110112
} else if (typeof value === 'object') {
111113
for (const item in value) {
112114
url.searchParams.append(

src/resources/threads.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,20 @@ export class Threads extends Resource {
7171
}: ListThreadsParams & Overrides): AsyncListResponse<
7272
NylasListResponse<Thread>
7373
> {
74+
const modifiedQueryParams: Record<string, unknown> | undefined = queryParams
75+
? { ...queryParams }
76+
: undefined;
77+
78+
// Transform some query params that are arrays into comma-delimited strings
79+
if (modifiedQueryParams && queryParams) {
80+
if (Array.isArray(queryParams?.anyEmail)) {
81+
delete modifiedQueryParams.anyEmail;
82+
modifiedQueryParams['any_email'] = queryParams.anyEmail.join(',');
83+
}
84+
}
85+
7486
return super._list<NylasListResponse<Thread>>({
75-
queryParams,
87+
queryParams: modifiedQueryParams,
7688
overrides,
7789
path: `/v3/grants/${identifier}/threads`,
7890
});

tests/apiClient.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,23 @@ describe('APIClient', () => {
114114

115115
expect(options.url).toEqual(
116116
new URL(
117-
'https://api.us.nylas.com/test?foo=bar&list=a%2Cb%2Cc&map=key1%3Avalue1&map=key2%3Avalue2'
117+
'https://api.us.nylas.com/test?foo=bar&list=a&list=b&list=c&map=key1%3Avalue1&map=key2%3Avalue2'
118+
)
119+
);
120+
});
121+
122+
it('should handle repeated query parameters', () => {
123+
const options = client.requestOptions({
124+
path: '/test',
125+
method: 'GET',
126+
queryParams: {
127+
eventType: ['default', 'outOfOffice', 'focusTime'],
128+
},
129+
});
130+
131+
expect(options.url).toEqual(
132+
new URL(
133+
'https://api.us.nylas.com/test?event_type=default&event_type=outOfOffice&event_type=focusTime'
118134
)
119135
);
120136
});

tests/resources/threads.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import APIClient from '../../src/apiClient';
22
import { Threads } from '../../src/resources/threads';
3-
jest.mock('../src/apiClient');
3+
jest.mock('../../src/apiClient');
44

55
describe('Threads', () => {
66
let apiClient: jest.Mocked<APIClient>;
77
let threads: Threads;
88

9-
beforeAll(() => {
9+
beforeEach(() => {
1010
apiClient = new APIClient({
1111
apiKey: 'apiKey',
1212
apiUri: 'https://test.api.nylas.com',
@@ -37,6 +37,25 @@ describe('Threads', () => {
3737
},
3838
});
3939
});
40+
41+
it('should transform anyEmail array into comma-delimited any_email parameter', async () => {
42+
const mockEmails = ['test1@example.com', 'test2@example.com'];
43+
await threads.list({
44+
identifier: 'id123',
45+
queryParams: {
46+
anyEmail: mockEmails,
47+
},
48+
});
49+
50+
expect(apiClient.request).toHaveBeenCalledWith({
51+
method: 'GET',
52+
path: '/v3/grants/id123/threads',
53+
overrides: undefined,
54+
queryParams: {
55+
any_email: mockEmails.join(','),
56+
},
57+
});
58+
});
4059
});
4160

4261
describe('find', () => {

0 commit comments

Comments
 (0)