Skip to content

Commit 15b5851

Browse files
authored
fix(offer requests): ensure offers is inaccessible when return_offers=false (#358)
When an offer request is created with `return_offers` being false, the API won't return any offers. So we will reflect that in the type as well.
1 parent c1090ae commit 15b5851

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/booking/OfferRequests/OfferRequests.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('OfferRequests', () => {
106106
).toHaveLength(1)
107107
})
108108

109-
test('should create an offer request and no offers should return when requested', async () => {
109+
test('should create an offer request and return the offer request id when `return_offers` is false', async () => {
110110
const mockResponseWithoutOffer = { ...mockOfferRequest }
111111
delete mockResponseWithoutOffer.offers
112112
nock(/(.*)/)
@@ -120,7 +120,7 @@ describe('OfferRequests', () => {
120120
...mockCreateOfferRequest,
121121
return_offers: false,
122122
})
123-
expect(response.data?.offers).toBe(undefined)
123+
// In this case, `offers` won't be in the response, but the offer request's id will still be returned and can be used with the List Offers endpoint to retrieve the offers.
124124
expect(response.data?.id).toBe(mockOfferRequest.id)
125125
})
126126
})

src/booking/OfferRequests/OfferRequests.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ export class OfferRequests extends Resource {
6363
* If set to false, the offer request resource won't include any `offers`. To retrieve the associated offers later, use the List Offers endpoint, specifying the `offer_request_id`.
6464
* @link https://duffel.com/docs/api/offer-requests/create-offer-request
6565
*/
66-
public create = async (
67-
options: CreateOfferRequest & CreateOfferRequestQueryParameters
68-
): Promise<DuffelResponse<OfferRequest>> => {
66+
public create = async <QueryParams extends CreateOfferRequestQueryParameters>(
67+
options: CreateOfferRequest & QueryParams
68+
): Promise<
69+
DuffelResponse<
70+
// Ensure that the `offers` field can't be accessed if `return_offers` is false
71+
QueryParams extends { return_offers: false }
72+
? Omit<OfferRequest, 'offers'>
73+
: OfferRequest
74+
>
75+
> => {
6976
const { return_offers, ...data } = options
7077

7178
return this.request({

src/booking/OfferRequests/OfferRequestsTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export interface OfferRequest {
127127
/**
128128
* The offers returned by the airlines
129129
*/
130-
offers?: Omit<Offer, 'available_services'>[]
130+
offers: Omit<Offer, 'available_services'>[]
131131

132132
/**
133133
* The passengers who want to travel. A passenger will have only a type or an age.

0 commit comments

Comments
 (0)