Skip to content

Commit 1bf1cf3

Browse files
authored
Add support for listImportEvents method to import events from calendars (#623)
* Add support for listImportEvents method to import events from calendars This adds support for the new beta endpoint GET /v3/grants/{grant_id}/events/import, which allows importing events from a calendar within a specified time frame. This is useful for applications that need to synchronize events with their own calendaring solution or enrich events with custom data. * Rename ImportEventQueryParams to ListImportEventQueryParams for consistency * Renamed max_results to limit for listImportEvents * Ran prettier
1 parent b555cde commit 1bf1cf3

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
### Unreleased
4+
* Add support for `listImportEvents` method to import events from a specified calendar within a given time frame
5+
36
### 7.7.4 / 2025-01-23
47
* Fix: any_email was not transformed to a comma delimited list for messages.list
58

@@ -91,7 +94,7 @@
9194
* **BREAKING CHANGE**: Dropped the use of 'Collections' in favor of 'Resources'
9295
* **BREAKING CHANGE**: Removed all REST calls from models and moved them directly into resources
9396
* **REMOVED**: Local Webhook development support is removed due to incompatibility
94-
* Rewrote the majority of SDK to be more modular and efficient
97+
* Rewritten the majority of SDK to be more modular and efficient
9598
* Removed the use of custom strings for serialization and deserialization, now automatically converting to camelCase and from the API's snake_case
9699
* Added support for both ES6 and CommonJS module systems
97100
* Created models for all API resources and endpoints, for all HTTP methods to reduce confusion on which fields are available for each endpoint
@@ -416,7 +419,7 @@ Note: version 4.2.1 was not released.
416419
* Added `upgrade()` and `downgrade()` for account management
417420
* Added `getRaw()` for retrieving raw messages
418421
* **BREAKING CHANGE**: Changed API for sending raw messages to use `draft.send()` instead of `Message.sendRaw()`
419-
* Changed `list()` to override default `offset` with users
422+
* Changed `list()` to override default `offset` with user's
420423
* **BREAKING CHANGE**: Changed models for `Contact`, `Draft`, `Event`, `File`, `Folder`, `Message`, and `Thread` to accurately reflect the attribute that the API returns
421424
* Return headers correctly for `expanded` view for `Message` objects
422425
* **BREAKING CHANGE**: Return `Message` object instead of `Draft` object after send

src/models/events.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,27 @@ export type DestroyEventQueryParams = CreateEventQueryParams;
333333
*/
334334
export type SendRsvpQueryParams = FindEventQueryParams;
335335

336+
/**
337+
* Interface representing the query parameters for importing events.
338+
*/
339+
export interface ListImportEventQueryParams extends ListQueryParams {
340+
/**
341+
* (Required) Calendar ID to import events from. "primary" is a supported value indicating the user's primary calendar.
342+
* Note: "primary" is not supported for iCloud.
343+
*/
344+
calendarId: string;
345+
/**
346+
* Filter for events that start at or after the specified time, in Unix timestamp format.
347+
* Defaults to the time of the request.
348+
*/
349+
start?: number;
350+
/**
351+
* Filter for events that end at or before the specified time, in Unix timestamp format.
352+
* Defaults to one month from the time of the request.
353+
*/
354+
end?: number;
355+
}
356+
336357
/**
337358
* Enum representing the status of an event.
338359
*/

src/resources/events.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DestroyEventQueryParams,
66
Event,
77
FindEventQueryParams,
8+
ListImportEventQueryParams,
89
ListEventQueryParams,
910
SendRsvpQueryParams,
1011
SendRsvpRequest,
@@ -74,6 +75,15 @@ export interface DestroyEventParams {
7475
queryParams: DestroyEventQueryParams;
7576
}
7677

78+
/**
79+
* @property identifier The identifier of the grant to act upon
80+
* @property queryParams The query parameters to include in the request
81+
*/
82+
export interface ListImportEventParams {
83+
identifier: string;
84+
queryParams: ListImportEventQueryParams;
85+
}
86+
7787
/**
7888
* @property identifier The identifier of the grant to act upon
7989
* @property eventId The id of the Event to update.
@@ -109,6 +119,25 @@ export class Events extends Resource {
109119
});
110120
}
111121

122+
/**
123+
* (Beta) Import events from a calendar within a given time frame
124+
* This is useful when you want to import, store, and synchronize events from the time frame to your application
125+
* @return The list of imported Events
126+
*/
127+
public listImportEvents({
128+
identifier,
129+
queryParams,
130+
overrides,
131+
}: ListImportEventParams & Overrides): AsyncListResponse<
132+
NylasListResponse<Event>
133+
> {
134+
return super._list({
135+
queryParams,
136+
path: `/v3/grants/${identifier}/events/import`,
137+
overrides,
138+
});
139+
}
140+
112141
/**
113142
* Return an Event
114143
* @return The Event

tests/resources/events.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,88 @@ describe('Events', () => {
128128
//TODO::More iterator tests
129129
});
130130

131+
describe('listImportEvents', () => {
132+
it('should call apiClient.request with the correct params', async () => {
133+
await events.listImportEvents({
134+
identifier: 'id123',
135+
queryParams: {
136+
calendarId: 'calendar123',
137+
start: 1617235200,
138+
end: 1619827200,
139+
limit: 100,
140+
select: 'id,name',
141+
},
142+
overrides: {
143+
apiUri: 'https://test.api.nylas.com',
144+
headers: { override: 'bar' },
145+
},
146+
});
147+
148+
expect(apiClient.request).toHaveBeenCalledWith({
149+
method: 'GET',
150+
path: '/v3/grants/id123/events/import',
151+
queryParams: {
152+
calendarId: 'calendar123',
153+
start: 1617235200,
154+
end: 1619827200,
155+
limit: 100,
156+
select: 'id,name',
157+
},
158+
overrides: {
159+
apiUri: 'https://test.api.nylas.com',
160+
headers: { override: 'bar' },
161+
},
162+
});
163+
});
164+
165+
it('should paginate correctly with page_token for import events', async () => {
166+
apiClient.request.mockResolvedValueOnce({
167+
requestId: 'request123',
168+
data: [
169+
{
170+
id: 'id',
171+
name: 'name',
172+
},
173+
],
174+
nextCursor: 'cursor123',
175+
});
176+
const eventList = await events.listImportEvents({
177+
identifier: 'id123',
178+
queryParams: {
179+
calendarId: 'calendar123',
180+
},
181+
overrides: {
182+
apiUri: 'https://test.api.nylas.com',
183+
headers: { override: 'bar' },
184+
},
185+
});
186+
apiClient.request.mockResolvedValueOnce({
187+
requestId: 'request123',
188+
data: [
189+
{
190+
id: 'id',
191+
name: 'name',
192+
},
193+
],
194+
});
195+
await eventList.next();
196+
197+
expect(apiClient.request).toBeCalledTimes(2);
198+
expect(apiClient.request).toHaveBeenLastCalledWith({
199+
method: 'GET',
200+
path: '/v3/grants/id123/events/import',
201+
queryParams: {
202+
calendarId: 'calendar123',
203+
pageToken: 'cursor123',
204+
},
205+
overrides: {
206+
apiUri: 'https://test.api.nylas.com',
207+
headers: { override: 'bar' },
208+
},
209+
});
210+
});
211+
});
212+
131213
describe('find', () => {
132214
it('should call apiClient.request with the correct params', async () => {
133215
await events.find({

0 commit comments

Comments
 (0)