Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Stays/Bookings/Bookings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ describe('Stays/Bookings', () => {
expect(response.data).toEqual(mockResponse.data)
})

it('should get a page of bookings when `list` is called with pagination params', async () => {
const mockResponse = { data: [MOCK_BOOKING] }

nock(/(.*)/)
.get('/stays/bookings')
.query((queryObject) => {
expect(queryObject.limit).toEqual('1')
return true
})
.reply(200, mockResponse)

const response = await duffel.stays.bookings.list({ limit: 1 })
expect(response.data).toEqual(mockResponse.data)
})

it('should get all bookings paginated', async () => {
nock(/(.*)/)
.get(`/stays/bookings`)
.reply(200, {
data: [MOCK_BOOKING],
meta: { limit: 1, before: null, after: null },
})

const response = duffel.stays.bookings.listWithGenerator()

for await (const page of response) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

witty

expect(page.data.id).toBe(MOCK_BOOKING.id)
}
})

it('should get to /stays/bookings/{id} when `get` is called', async () => {
const mockResponse = { data: MOCK_BOOKING }

Expand Down
23 changes: 17 additions & 6 deletions src/Stays/Bookings/Bookings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client } from '../../Client'
import { StaysBooking } from '../StaysTypes'
import { Resource } from '../../Resource'
import { DuffelResponse } from '../../types'
import { DuffelResponse, PaginationMeta } from '../../types'

export interface StaysBookingPayload {
quote_id: string
Expand Down Expand Up @@ -54,12 +54,23 @@ export class Bookings extends Resource {

/**
* List bookings
* @param {Object} [options] - Pagination options (optional: limit, after, before)
* @link https://duffel.com/docs/api/bookings/list-bookings
*/
public list = async (): Promise<DuffelResponse<StaysBooking[]>> =>
this.request({
method: 'GET',
path: this.path,
})
public list = async (
options?: PaginationMeta,
): Promise<DuffelResponse<StaysBooking[]>> =>
this.request({ method: 'GET', path: this.path, params: options })

/**
* Retrieves a generator of all bookings. The results may be returned in any order.
* @link https://duffel.com/docs/api/bookings/list-bookings
*/
public listWithGenerator = (): AsyncGenerator<
DuffelResponse<StaysBooking>,
void,
unknown
> => this.paginatedRequest({ path: this.path })

/**
* Cancel a booking
Expand Down
Loading