From 7b2d050b0661bfa311fc98ecadc83d05ae6e212b Mon Sep 17 00:00:00 2001 From: darseen Date: Mon, 7 Oct 2024 23:22:17 +0300 Subject: [PATCH] stringified params while maintaining backwards compatibility --- README.md | 16 ++++++------- spec/amadeus/client.test.js | 4 ++-- spec/amadeus/namespaces.test.js | 24 +++++++++---------- src/amadeus/client.js | 3 ++- .../namespaces/booking/flight_orders.js | 2 +- .../namespaces/booking/hotel_bookings.js | 2 +- .../namespaces/booking/hotel_orders.js | 2 +- .../namespaces/ordering/transfer_orders.js | 2 +- .../transfer_orders/transfers/cancellation.js | 4 ++-- .../availability/flight_availabilities.js | 2 +- .../flight_offers/flight_choice_prediction.js | 2 +- .../shopping/flight_offers/pricing.js | 2 +- .../shopping/flight_offers/upselling.js | 2 +- .../shopping/flight_offers_search.js | 2 +- src/amadeus/namespaces/shopping/seatmaps.js | 2 +- .../namespaces/shopping/transfer_offers.js | 2 +- 16 files changed, 37 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 8caddc7..bdcc39f 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ npm install amadeus --save To make your first API call, you will need to [register](https://developers.amadeus.com/register) for an Amadeus Developer Account and [set up your first application](https://developers.amadeus.com/my-apps). ```js -var Amadeus = require('amadeus'); +const Amadeus = require('amadeus'); -var amadeus = new Amadeus({ +const amadeus = new Amadeus({ clientId: 'REPLACE_BY_YOUR_API_KEY', clientSecret: 'REPLACE_BY_YOUR_API_SECRET' }); @@ -50,7 +50,7 @@ The client can be initialized directly. ```js // Initialize using parameters -var amadeus = new Amadeus({ +const amadeus = new Amadeus({ clientId: 'REPLACE_BY_YOUR_API_KEY', clientSecret: 'REPLACE_BY_YOUR_API_SECRET' }); @@ -59,7 +59,7 @@ var amadeus = new Amadeus({ Alternatively, it can be initialized without any parameters if the environment variables `AMADEUS_CLIENT_ID` and `AMADEUS_CLIENT_SECRET` are present. ```js -var amadeus = new Amadeus(); +const amadeus = new Amadeus(); ``` Your credentials can be found on the [Amadeus dashboard](https://developers.amadeus.com/my-apps). @@ -67,7 +67,7 @@ Your credentials can be found on the [Amadeus dashboard](https://developers.amad By default, the SDK environment is set to `test` environment. To switch to a `production` (pay-as-you-go) environment, please switch the hostname as follows: ```js -var amadeus = new Amadeus({ +const amadeus = new Amadeus({ hostname: 'production' }); ``` @@ -104,7 +104,7 @@ amadeus.client.get('/v2/reference-data/urls/checkin-links', { airlineCode: 'BA' Or, with a `POST` using `.client.post` method: ```js -amadeus.client.post('/v1/shopping/flight-offers/pricing', JSON.stringify({ data })); +amadeus.client.post('/v1/shopping/flight-offers/pricing', { data }); ``` ## Promises @@ -152,7 +152,7 @@ If a page is not available, the response will resolve to `null`. The SDK makes it easy to add your own logger that is compatible with the default `console`. ```js -var amadeus = new Amadeus({ +const amadeus = new Amadeus({ clientId: 'REPLACE_BY_YOUR_API_KEY', clientSecret: 'REPLACE_BY_YOUR_API_SECRET', logger: new MyConsole() @@ -162,7 +162,7 @@ var amadeus = new Amadeus({ Additionally, to enable more verbose logging, you can set the appropriate level on your own logger. The easiest way would be to enable debugging via a parameter during initialization, or using the `AMADEUS_LOG_LEVEL` environment variable. The available options are `silent` (default), `warn`, and `debug`. ```js -var amadeus = new Amadeus({ +const amadeus = new Amadeus({ clientId: 'REPLACE_BY_YOUR_API_KEY', clientSecret: 'REPLACE_BY_YOUR_API_SECRET', logLevel: 'debug' diff --git a/spec/amadeus/client.test.js b/spec/amadeus/client.test.js index 0221418..4a2b894 100644 --- a/spec/amadeus/client.test.js +++ b/spec/amadeus/client.test.js @@ -110,7 +110,7 @@ describe('Client', () => { // make an authenticated POST call client.post(path, params); // ensure Client.call() was called with the right parameters - expect(call).toHaveBeenCalledWith('POST', path, params, 'token'); + expect(call).toHaveBeenCalledWith('POST', path, JSON.stringify(params), 'token'); }); it('should work without params', () => { @@ -119,7 +119,7 @@ describe('Client', () => { return { then: resolve => resolve('token') }; }}; client.post(path); - expect(call).toHaveBeenCalledWith('POST', path, {}, 'token'); + expect(call).toHaveBeenCalledWith('POST', path, JSON.stringify({}), 'token'); }); }); diff --git a/spec/amadeus/namespaces.test.js b/spec/amadeus/namespaces.test.js index 23f0750..f579498 100644 --- a/spec/amadeus/namespaces.test.js +++ b/spec/amadeus/namespaces.test.js @@ -322,21 +322,21 @@ describe('Namespaces', () => { amadeus.client.post = jest.fn(); amadeus.shopping.flightOffers.prediction.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v2/shopping/flight-offers/prediction', JSON.stringify({})); + .toHaveBeenCalledWith('/v2/shopping/flight-offers/prediction', {}); }); it('.amadeus.booking.flightOrders.post', () => { amadeus.client.post = jest.fn(); amadeus.booking.flightOrders.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/booking/flight-orders', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/booking/flight-orders', {}); }); it('.amadeus.shopping.flightOffers.pricing.post', () => { amadeus.client.post = jest.fn(); amadeus.shopping.flightOffers.pricing.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/shopping/flight-offers/pricing', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/shopping/flight-offers/pricing', {}); }); it('.amadeus.shopping.flightOffersSearch.get', () => { @@ -350,7 +350,7 @@ describe('Namespaces', () => { amadeus.client.post = jest.fn(); amadeus.shopping.flightOffersSearch.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v2/shopping/flight-offers', JSON.stringify({})); + .toHaveBeenCalledWith('/v2/shopping/flight-offers', {}); }); it('.amadeus.shopping.seatmaps.get', () => { @@ -364,7 +364,7 @@ describe('Namespaces', () => { amadeus.client.post = jest.fn(); amadeus.shopping.seatmaps.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/shopping/seatmaps', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/shopping/seatmaps', {}); }); it('.amadeus.shopping.hotelOfferSearch().get', () => { @@ -430,14 +430,14 @@ describe('Namespaces', () => { amadeus.client.post = jest.fn(); amadeus.booking.hotelBookings.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/booking/hotel-bookings', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/booking/hotel-bookings', {}); }); it('.amadeus.booking.hotelOrders.post', () => { amadeus.client.post = jest.fn(); amadeus.booking.hotelOrders.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v2/booking/hotel-orders', JSON.stringify({})); + .toHaveBeenCalledWith('/v2/booking/hotel-orders', {}); }); it('.amadeus.eReputation.hotelSentiments.get', () => { @@ -479,14 +479,14 @@ describe('Namespaces', () => { amadeus.client.post = jest.fn(); amadeus.shopping.availability.flightAvailabilities.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/shopping/availability/flight-availabilities', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/shopping/availability/flight-availabilities', {}); }); it('.amadeus.shopping.flight_offers.upselling.post', () => { amadeus.client.post = jest.fn(); amadeus.shopping.flightOffers.upselling.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/shopping/flight-offers/upselling', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/shopping/flight-offers/upselling', {}); }); it('.amadeus.airline.destinations.get', () => { @@ -500,21 +500,21 @@ describe('Namespaces', () => { amadeus.client.post = jest.fn(); amadeus.shopping.transferOffers.post(); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/shopping/transfer-offers', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/shopping/transfer-offers', {}); }); it('.amadeus.ordering.transferOrders.post', () => { amadeus.client.post = jest.fn(); amadeus.ordering.transferOrders.post({}, '1234123123'); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/ordering/transfer-orders?offerId=1234123123', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/ordering/transfer-orders?offerId=1234123123', {}); }); it('.amadeus.ordering.transferOrders().transfers.cancellation.post', () => { amadeus.client.post = jest.fn(); amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, 12345); expect(amadeus.client.post) - .toHaveBeenCalledWith('/v1/ordering/transfer-orders/XXX/transfers/cancellation?confirmNbr=12345', JSON.stringify({})); + .toHaveBeenCalledWith('/v1/ordering/transfer-orders/XXX/transfers/cancellation?confirmNbr=12345', {}); }); }); diff --git a/src/amadeus/client.js b/src/amadeus/client.js index 9d06277..957af49 100644 --- a/src/amadeus/client.js +++ b/src/amadeus/client.js @@ -73,7 +73,8 @@ class Client { * @return {Promise.} a Promise */ post(path, params = {}) { - return this.request('POST', path, params); + const stringifiedParams = typeof params === 'string' ? params : JSON.stringify(params); + return this.request('POST', path, stringifiedParams); } /** diff --git a/src/amadeus/namespaces/booking/flight_orders.js b/src/amadeus/namespaces/booking/flight_orders.js index b13d01e..886883c 100644 --- a/src/amadeus/namespaces/booking/flight_orders.js +++ b/src/amadeus/namespaces/booking/flight_orders.js @@ -33,7 +33,7 @@ class FlightOrders { * ``` */ post(params = {}) { - return this.client.post('/v1/booking/flight-orders', JSON.stringify(params)); + return this.client.post('/v1/booking/flight-orders', params); } } diff --git a/src/amadeus/namespaces/booking/hotel_bookings.js b/src/amadeus/namespaces/booking/hotel_bookings.js index 0ebac11..be813f4 100644 --- a/src/amadeus/namespaces/booking/hotel_bookings.js +++ b/src/amadeus/namespaces/booking/hotel_bookings.js @@ -37,7 +37,7 @@ class HotelBookings { * ``` */ post(params = {}) { - return this.client.post('/v1/booking/hotel-bookings', JSON.stringify(params)); + return this.client.post('/v1/booking/hotel-bookings', params); } } diff --git a/src/amadeus/namespaces/booking/hotel_orders.js b/src/amadeus/namespaces/booking/hotel_orders.js index 64e0c65..d132764 100644 --- a/src/amadeus/namespaces/booking/hotel_orders.js +++ b/src/amadeus/namespaces/booking/hotel_orders.js @@ -39,7 +39,7 @@ class HotelOrders { */ post(params = {}) { - return this.client.post('/v2/booking/hotel-orders', JSON.stringify(params)); + return this.client.post('/v2/booking/hotel-orders', params); } } diff --git a/src/amadeus/namespaces/ordering/transfer_orders.js b/src/amadeus/namespaces/ordering/transfer_orders.js index 9d73c1f..91e872f 100644 --- a/src/amadeus/namespaces/ordering/transfer_orders.js +++ b/src/amadeus/namespaces/ordering/transfer_orders.js @@ -29,7 +29,7 @@ class TransferOrders { * ``` */ post(body, offerId) { - return this.client.post(`/v1/ordering/transfer-orders?offerId=${offerId}`, JSON.stringify(body)); + return this.client.post(`/v1/ordering/transfer-orders?offerId=${offerId}`, body); } } diff --git a/src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js b/src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js index 73776e6..305c995 100644 --- a/src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js +++ b/src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js @@ -6,7 +6,7 @@ * * ```js * let amadeus = new Amadeus(); - * amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), '12345');; + * amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, '12345');; * ``` * * @param {Client} client @@ -29,7 +29,7 @@ class Cancellation { */ post(body, confirmNbr) { return this.client.post( - `/v1/ordering/transfer-orders/${this.orderId}/transfers/cancellation?confirmNbr=${confirmNbr}`, JSON.stringify(body)); + `/v1/ordering/transfer-orders/${this.orderId}/transfers/cancellation?confirmNbr=${confirmNbr}`, body); } } diff --git a/src/amadeus/namespaces/shopping/availability/flight_availabilities.js b/src/amadeus/namespaces/shopping/availability/flight_availabilities.js index 59e6da5..8b41300 100644 --- a/src/amadeus/namespaces/shopping/availability/flight_availabilities.js +++ b/src/amadeus/namespaces/shopping/availability/flight_availabilities.js @@ -27,7 +27,7 @@ class FlightAvailabilities { * ``` */ post(params = {}) { - return this.client.post('/v1/shopping/availability/flight-availabilities', JSON.stringify(params)); + return this.client.post('/v1/shopping/availability/flight-availabilities', params); } } diff --git a/src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js b/src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js index b5f0e70..3d358b7 100644 --- a/src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js +++ b/src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js @@ -40,7 +40,7 @@ class FlightChoicePrediction { * ``` */ post(params = {}) { - return this.client.post('/v2/shopping/flight-offers/prediction', JSON.stringify(params)); + return this.client.post('/v2/shopping/flight-offers/prediction', params); } } diff --git a/src/amadeus/namespaces/shopping/flight_offers/pricing.js b/src/amadeus/namespaces/shopping/flight_offers/pricing.js index 68b47bc..3d8db6e 100644 --- a/src/amadeus/namespaces/shopping/flight_offers/pricing.js +++ b/src/amadeus/namespaces/shopping/flight_offers/pricing.js @@ -48,7 +48,7 @@ class Pricing { url += '?' + queryString; } - return this.client.post(url, JSON.stringify(params)); + return this.client.post(url, params); } } diff --git a/src/amadeus/namespaces/shopping/flight_offers/upselling.js b/src/amadeus/namespaces/shopping/flight_offers/upselling.js index 393f993..258816b 100644 --- a/src/amadeus/namespaces/shopping/flight_offers/upselling.js +++ b/src/amadeus/namespaces/shopping/flight_offers/upselling.js @@ -27,7 +27,7 @@ class Upselling { * ``` */ post(params = {}) { - return this.client.post('/v1/shopping/flight-offers/upselling', JSON.stringify(params)); + return this.client.post('/v1/shopping/flight-offers/upselling', params); } } diff --git a/src/amadeus/namespaces/shopping/flight_offers_search.js b/src/amadeus/namespaces/shopping/flight_offers_search.js index 0aaf2ef..9f00b21 100644 --- a/src/amadeus/namespaces/shopping/flight_offers_search.js +++ b/src/amadeus/namespaces/shopping/flight_offers_search.js @@ -118,7 +118,7 @@ class FlightOffersSearch { * ``` */ post(params = {}) { - return this.client.post('/v2/shopping/flight-offers', JSON.stringify(params)); + return this.client.post('/v2/shopping/flight-offers', params); } } diff --git a/src/amadeus/namespaces/shopping/seatmaps.js b/src/amadeus/namespaces/shopping/seatmaps.js index a176c8f..ce50f64 100644 --- a/src/amadeus/namespaces/shopping/seatmaps.js +++ b/src/amadeus/namespaces/shopping/seatmaps.js @@ -59,7 +59,7 @@ class Seatmaps { * ``` */ post(params = {}) { - return this.client.post('/v1/shopping/seatmaps', JSON.stringify(params)); + return this.client.post('/v1/shopping/seatmaps', params); } } diff --git a/src/amadeus/namespaces/shopping/transfer_offers.js b/src/amadeus/namespaces/shopping/transfer_offers.js index f3d3c55..951a462 100644 --- a/src/amadeus/namespaces/shopping/transfer_offers.js +++ b/src/amadeus/namespaces/shopping/transfer_offers.js @@ -31,7 +31,7 @@ class TransferOffers { * ``` */ post(params = {}) { - return this.client.post('/v1/shopping/transfer-offers', JSON.stringify(params)); + return this.client.post('/v1/shopping/transfer-offers', params); } }