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
22 changes: 21 additions & 1 deletion src/booking/Offers/OfferTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
PlaceType,
Aircraft,
Airline,
Airport
Airport,
PaginationMeta
} from '../../types'

/**
Expand Down Expand Up @@ -427,3 +428,22 @@ export interface OfferSliceSegmentPassengerBaggage {
*/
quantity: number
}

export interface ListOffersParams extends PaginationMeta {
/**
* Duffel's unique identifier for the offer request, returned when it was created
*/
offer_request_id: string

/**
* Allows to filter the offers list by the maximum number of connections in a given offer. e.g. a return flight with three flights outbound and a direct inbound flight would be filtered out if `max_connections=1` was passed.
*/
max_connections?: number

/**
* By default, the offers will be returned sorted by ID in ascending order.
* This parameter allows you to sort the list of offers by `total_amount` or `total_duration`.
* By default the sorting order will be ascending, if you wish to sort in descending order a - will need to be prepended to the sorting attribute (i.e: `-total_amount`).
*/
sort?: 'total_amount' | 'total_duration'
}
20 changes: 17 additions & 3 deletions src/booking/Offers/Offers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,34 @@ describe('offers', () => {

test('should get a page of offers', async () => {
nock(/(.*)/)
.get(`/air/offers?limit=1`)
.get(`/air/offers`)
.query((queryObject) => {
expect(queryObject?.offer_request_id).toBe(mockOffer.id)
expect(queryObject?.limit).toEqual('1')
return true
})
.reply(200, { data: [mockOffer], meta: { limit: 1, before: null, after: null } })

const response = await new Offers(new Client({ token: 'mockToken' })).list({ limit: 1 })
const response = await new Offers(new Client({ token: 'mockToken' })).list({
offer_request_id: mockOffer.id,
limit: 1
})
expect(response.data).toHaveLength(1)
expect(response.data[0].id).toBe(mockOffer.id)
})

test('should get all offers paginated', async () => {
nock(/(.*)/)
.get(`/air/offers`)
.query((queryObject) => {
expect(queryObject?.offer_request_id).toBe(mockOffer.id)
return true
})
.reply(200, { data: [mockOffer], meta: { limit: 1, before: null, after: null } })

const response = new Offers(new Client({ token: 'mockToken' })).listWithGenerator()
const response = new Offers(new Client({ token: 'mockToken' })).listWithGenerator({
offer_request_id: mockOffer.id
})
for await (const page of response) {
expect(page.data.id).toBe(mockOffer.id)
}
Expand Down
31 changes: 25 additions & 6 deletions src/booking/Offers/Offers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Resource } from '../../Resource'
import { DuffelResponse, Offer, PaginationMeta } from '../../types'
import { DuffelResponse, ListOffersParams, Offer } from '../../types'

/**
* Each offer represents flights you can buy from an airline at a particular price that meet your search criteria.
Expand Down Expand Up @@ -28,16 +28,35 @@ export class Offers extends Resource {

/**
* Retrieves a page of offers. The results may be returned in any order.
* @param {Object} [options] - Pagination options (optional: limit, after, before)
* @param {Object.<ListOffersParams>} params - Endpoint options (optional: limit, after, before, max_connections, sort)
* @param {string} params.offer_request_id - Duffel's unique identifier for the offer request, returned when it was created
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param {string} params.offer_request_id - Duffel's unique identifier for the offer request, returned when it was created
* @param {string} offer_request_id - Duffel's unique identifier for the offer request, returned when it was created

* @link https://duffel.com/docs/api/offers/get-offers
*/
public list = (options?: PaginationMeta): Promise<DuffelResponse<Offer[]>> =>
this.request({ method: 'GET', path: this.path, params: options })
public list = ({ offer_request_id, ...params }: ListOffersParams): Promise<DuffelResponse<Offer[]>> =>
this.request({
method: 'GET',
path: this.path,
params: {
...params,
offer_request_id
}
})

/**
* Retrieves a generator of all offers. The results may be returned in any order.
* @param {Object.<ListOffersParams>} params - Endpoint options (optional: limit, after, before, max_connections, sort)
* @param {string} params.offer_request_id - Duffel's unique identifier for the offer request, returned when it was created
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param {string} params.offer_request_id - Duffel's unique identifier for the offer request, returned when it was created
* @param {string} offer_request_id - Duffel's unique identifier for the offer request, returned when it was created

Since it shouldn't actually be nested inside "params"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was reading about it and because it's nested inside params we need to define the previous object since we are deconstructing the object. Makes sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

aah ok, I read that as you need to have it as {params: {offer_request_id: ...}}

* @link https://duffel.com/docs/api/offers/get-offers
*/
public listWithGenerator = (): AsyncGenerator<DuffelResponse<Offer>, void, unknown> =>
this.paginatedRequest({ path: this.path })
public listWithGenerator = ({
offer_request_id,
...params
}: ListOffersParams): AsyncGenerator<DuffelResponse<Offer>, void, unknown> =>
this.paginatedRequest({
path: this.path,
params: {
...params,
offer_request_id
}
})
}