Skip to content

Commit

Permalink
feat: introduce a generator for followups
Browse files Browse the repository at this point in the history
  • Loading branch information
jekku committed Nov 5, 2024
1 parent 93ef682 commit e4121fa
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ export class Client {
response = await this.request({
method: 'GET',
path,
params: { limit: response.meta.limit, after: response.meta.after },
params: {
...params,
...{ limit: response.meta.limit, after: response.meta.after },
},
})
for (const item of response.data) {
yield { data: item }
Expand Down
64 changes: 64 additions & 0 deletions src/Stays/Accommodation/Accommodation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,68 @@ describe('Stays/Accommodation', () => {
expect(response.data).toEqual(mockResponse.data)
expect(response.meta).toEqual(mockResponse.meta)
})

it('should send GET to /stays/accommodation when an individual item from `listWithGenerator` is executed', async () => {
const mockFirstPageResponse = {
data: [MOCK_ACCOMMODATION],
meta: { limit: 2, before: null, after: 'next_page_token' },
}

const mockLastPageResponse = {
data: [MOCK_ACCOMMODATION],
meta: { limit: 1, before: 'prev_page_token', after: null },
}

const params = {
radius: 7,
latitude: 13.5276927,
longitude: 122.1160988,
}

// First Page expectations
nock(/(.*)/)
.get(`/stays/accommodation`)
.query((queryObject) => {
expect(parseInt(queryObject.radius as string)).toEqual(params.radius)
expect(parseFloat(queryObject.latitude as string)).toEqual(
params.latitude,
)
expect(parseFloat(queryObject.longitude as string)).toEqual(
params.longitude,
)

return true
})
.reply(200, mockFirstPageResponse)

// Last Page expectations
nock(/(.*)/)
.get(`/stays/accommodation`)
.query((queryObject) => {
expect(parseInt(queryObject.radius as string)).toEqual(params.radius)
expect(parseFloat(queryObject.latitude as string)).toEqual(
params.latitude,
)
expect(parseFloat(queryObject.longitude as string)).toEqual(
params.longitude,
)

return true
})
.reply(200, mockLastPageResponse)

const generator = duffel.stays.accommodation.listWithGenerator(params)

const firstItem = await generator.next()
expect(firstItem.value.data).toEqual(MOCK_ACCOMMODATION)
expect(firstItem.done).toBe(false)

const lastItem = await generator.next()
expect(lastItem.value.data).toEqual(MOCK_ACCOMMODATION)
expect(lastItem.done).toBe(false)

const terminatingItem = await generator.next()
expect(terminatingItem.value).toBeUndefined()
expect(terminatingItem.done).toBe(true)
})
})
20 changes: 19 additions & 1 deletion src/Stays/Accommodation/Accommodation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,30 @@ export class Accommodation extends Resource {
path: `${this.path}/${id}`,
})

/**
* Retrieves a page of accommodation.
* @param {Object.<ListAccommodationParams>} params - Endpoint options (radius, latitude, longitude, before, after, limit)
* @link https://duffel.com/docs/api/v2/accommodation/list-accommodation
*/
public list = (
params: ListAccommodationParams,
): Promise<DuffelResponse<StaysAccommodation[]>> =>
this.request({
method: 'GET',
path: `${this.path}`,
path: this.path,
params: params,
})

/**
* Retrieves a generator of accommodation pages given the criteria in the params.
* @param {Object.<ListAccommodationParams>} params - Endpoint options (radius, latitude, longitude, before, after, limit)
* @link https://duffel.com/docs/api/v2/accommodation/list-accommodation
*/
public listWithGenerator = (
params: ListAccommodationParams,
): AsyncGenerator<DuffelResponse<StaysAccommodation[]>> =>
this.paginatedRequest({
path: this.path,
params: params,
})
}

0 comments on commit e4121fa

Please sign in to comment.