Skip to content

Commit

Permalink
fix: transform anyEmail array into comma-delimited any_email paramete…
Browse files Browse the repository at this point in the history
…r in threads list
  • Loading branch information
AaronDDM committed Jan 17, 2025
1 parent 5a0800b commit 55b04e7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
* Remove `createdAt` field from message model.
* Change `latestMessageReceivedDate` & `latestMessageSentDate` to be optional on threads model.
* Fix issue where query params with array values were not being transformed into comma-delimited strings

### 7.7.2 / 2024-12-02
* Fix `credentials` resource to use correct endpoint.
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export default class APIClient {
}
url.searchParams.set('metadata_pair', metadataPair.join(','));
} else if (Array.isArray(value)) {
url.searchParams.append(snakeCaseKey, value.join(','));
for (const item of value) {
url.searchParams.append(snakeCaseKey, item as string);
}
} else if (typeof value === 'object') {
for (const item in value) {
url.searchParams.append(
Expand Down
12 changes: 11 additions & 1 deletion src/resources/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ export class Threads extends Resource {
}: ListThreadsParams & Overrides): AsyncListResponse<
NylasListResponse<Thread>
> {
const modifiedQueryParams: Record<string, unknown> | undefined = queryParams ? { ...queryParams } : undefined;

// Transform some query params that are arrays into comma-delimited strings
if (modifiedQueryParams && queryParams) {
if (Array.isArray(queryParams?.anyEmail)) {
delete modifiedQueryParams.anyEmail;
modifiedQueryParams['any_email'] = queryParams.anyEmail.join(',');
}
}

return super._list<NylasListResponse<Thread>>({
queryParams,
queryParams: modifiedQueryParams,
overrides,
path: `/v3/grants/${identifier}/threads`,
});
Expand Down
18 changes: 17 additions & 1 deletion tests/apiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,23 @@ describe('APIClient', () => {

expect(options.url).toEqual(
new URL(
'https://api.us.nylas.com/test?foo=bar&list=a%2Cb%2Cc&map=key1%3Avalue1&map=key2%3Avalue2'
'https://api.us.nylas.com/test?foo=bar&list=a&list=b&list=c&map=key1%3Avalue1&map=key2%3Avalue2'
)
);
});

it('should handle repeated query parameters', () => {
const options = client.requestOptions({
path: '/test',
method: 'GET',
queryParams: {
eventType: ['default', 'outOfOffice', 'focusTime'],
},
});

expect(options.url).toEqual(
new URL(
'https://api.us.nylas.com/test?event_type=default&event_type=outOfOffice&event_type=focusTime'
)
);
});
Expand Down
23 changes: 21 additions & 2 deletions tests/resources/threads.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import APIClient from '../../src/apiClient';
import { Threads } from '../../src/resources/threads';
jest.mock('../src/apiClient');
jest.mock('../../src/apiClient');

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

beforeAll(() => {
beforeEach(() => {
apiClient = new APIClient({
apiKey: 'apiKey',
apiUri: 'https://test.api.nylas.com',
Expand Down Expand Up @@ -37,6 +37,25 @@ describe('Threads', () => {
},
});
});

it('should transform anyEmail array into comma-delimited any_email parameter', async () => {
const mockEmails = ['[email protected]', '[email protected]'];
await threads.list({
identifier: 'id123',
queryParams: {
anyEmail: mockEmails
}
});

expect(apiClient.request).toHaveBeenCalledWith({
method: 'GET',
path: '/v3/grants/id123/threads',
overrides: undefined,
queryParams: {
any_email: mockEmails.join(',')
}
});
});
});

describe('find', () => {
Expand Down

0 comments on commit 55b04e7

Please sign in to comment.