From 93ef682813e6b12b944da0d41765066ecf997ced Mon Sep 17 00:00:00 2001 From: Jekri Preclaro Orlina Date: Tue, 5 Nov 2024 14:06:56 +0000 Subject: [PATCH] feat: create SDK interface for accommodation listing --- package.json | 2 +- src/Stays/Accommodation/Accommodation.spec.ts | 33 +++++++++++++++++++ src/Stays/Accommodation/Accommodation.ts | 10 ++++++ src/Stays/StaysTypes.ts | 13 +++++--- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index fa27424a..3478f2a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@duffel/api", - "version": "3.1.1", + "version": "3.2.0", "description": "Javascript client library for the Duffel API", "main": "dist/index.js", "module": "dist/index.es.js", diff --git a/src/Stays/Accommodation/Accommodation.spec.ts b/src/Stays/Accommodation/Accommodation.spec.ts index a85e833a..4685b796 100644 --- a/src/Stays/Accommodation/Accommodation.spec.ts +++ b/src/Stays/Accommodation/Accommodation.spec.ts @@ -62,4 +62,37 @@ describe('Stays/Accommodation', () => { const response = await duffel.stays.accommodation.get(id) expect(response.data).toEqual(mockResponse.data) }) + + it('should send GET to /stays/accommodation when `list` is called', async () => { + const mockResponse = { + data: [MOCK_ACCOMMODATION], + meta: { limit: 1, before: null, after: null }, + } + + const params = { + radius: 7, + latitude: 13.5276927, + longitude: 122.1160988, + } + + 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, mockResponse) + + const response = await duffel.stays.accommodation.list(params) + + expect(response.data).toEqual(mockResponse.data) + expect(response.meta).toEqual(mockResponse.meta) + }) }) diff --git a/src/Stays/Accommodation/Accommodation.ts b/src/Stays/Accommodation/Accommodation.ts index aa2ef17f..f455c84e 100644 --- a/src/Stays/Accommodation/Accommodation.ts +++ b/src/Stays/Accommodation/Accommodation.ts @@ -1,6 +1,7 @@ import { Client } from '../../Client' import { LocationParams, + ListAccommodationParams, StaysAccommodationSuggestion, StaysAccommodation, } from '../StaysTypes' @@ -46,4 +47,13 @@ export class Accommodation extends Resource { method: 'GET', path: `${this.path}/${id}`, }) + + public list = ( + params: ListAccommodationParams, + ): Promise> => + this.request({ + method: 'GET', + path: `${this.path}`, + params: params, + }) } diff --git a/src/Stays/StaysTypes.ts b/src/Stays/StaysTypes.ts index 162b2e6c..95b46720 100644 --- a/src/Stays/StaysTypes.ts +++ b/src/Stays/StaysTypes.ts @@ -1,3 +1,5 @@ +import { PaginationMeta } from './../types' + export type StaysBedType = 'single' | 'double' | 'queen' | 'king' | 'sofabed' export interface StaysBed { @@ -628,10 +630,7 @@ interface CommonStaysSearchParams { export type LocationParams = { radius: number - geographic_coordinates: { - latitude: number - longitude: number - } + geographic_coordinates: GeographicCoordinates } type LocationSearchParams = { @@ -647,6 +646,12 @@ type AccommodationSearchParams = { export type StaysSearchParams = LocationSearchParams | AccommodationSearchParams +export interface ListAccommodationParams extends PaginationMeta { + radius?: LocationParams['radius'] + latitude: GeographicCoordinates['latitude'] + longitude: GeographicCoordinates['longitude'] +} + export interface StaysSearchResult { id: string check_in_date: string