diff --git a/src/Client.ts b/src/Client.ts index d9718e14..87b9ae9d 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -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 } diff --git a/src/Stays/Accommodation/Accommodation.spec.ts b/src/Stays/Accommodation/Accommodation.spec.ts index 4685b796..74564218 100644 --- a/src/Stays/Accommodation/Accommodation.spec.ts +++ b/src/Stays/Accommodation/Accommodation.spec.ts @@ -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) + }) }) diff --git a/src/Stays/Accommodation/Accommodation.ts b/src/Stays/Accommodation/Accommodation.ts index f455c84e..2402fdff 100644 --- a/src/Stays/Accommodation/Accommodation.ts +++ b/src/Stays/Accommodation/Accommodation.ts @@ -48,12 +48,30 @@ export class Accommodation extends Resource { path: `${this.path}/${id}`, }) + /** + * Retrieves a page of accommodation. + * @param {Object.} params - Endpoint options (radius, latitude, longitude, before, after, limit) + * @link https://duffel.com/docs/api/v2/accommodation/list-accommodation + */ public list = ( params: ListAccommodationParams, ): Promise> => 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.} params - Endpoint options (radius, latitude, longitude, before, after, limit) + * @link https://duffel.com/docs/api/v2/accommodation/list-accommodation + */ + public listWithGenerator = ( + params: ListAccommodationParams, + ): AsyncGenerator> => + this.paginatedRequest({ + path: this.path, params: params, }) }