diff --git a/README.md b/README.md index fea7aa6..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' @@ -204,7 +204,7 @@ amadeus.shopping.flightOffersSearch.get({ // Flight Offers Search POST // A full example can be found at https://github.com/amadeus4dev/amadeus-code-examples -amadeus.shopping.flightOffersSearch.post(JSON.stringify(body)) +amadeus.shopping.flightOffersSearch.post(body) // Flight Offers Price amadeus.shopping.flightOffersSearch.get({ @@ -214,12 +214,12 @@ amadeus.shopping.flightOffersSearch.get({ adults: '1' }).then(function(response){ return amadeus.shopping.flightOffers.pricing.post( - JSON.stringify({ + { 'data': { 'type': 'flight-offers-pricing', 'flightOffers': [response.data[0]] } - }) + } ) }).then(function(response){ console.log(response.data); @@ -229,19 +229,19 @@ amadeus.shopping.flightOffersSearch.get({ // Flight Offers Price with additional parameters // for example: check additional baggage options -amadeus.shopping.flightOffers.pricing.post(JSON.stringify(body),{include: 'bags'}) +amadeus.shopping.flightOffers.pricing.post(body ,{include: 'bags'}); // Flight Create Orders // To book the flight-offer(s) returned by the Flight Offers Price // and create a flight-order with travelers' information. // A full example can be found at https://git.io/JtnYo amadeus.booking.flightOrders.post( - JSON.stringify({ + { 'type': 'flight-order', 'flightOffers': [priced-offers], 'travelers': [] - }) -) + } +); // Retrieve flight order with ID 'XXX'. This ID comes from the // Flight Create Orders API, which is a temporary ID in test environment. @@ -261,9 +261,9 @@ amadeus.shopping.flightOffersSearch.get({ adults: '1' }).then(function(response){ return amadeus.shopping.seatmaps.post( - JSON.stringify({ + { 'data': [response.data[0]] - }) + } ); }).then(function(response){ console.log(response.data); @@ -276,10 +276,10 @@ amadeus.shopping.seatmaps.get({ }); // Flight Availabilities Search -amadeus.shopping.availability.flightAvailabilities.post(JSON.stringify((body)); +amadeus.shopping.availability.flightAvailabilities.post(body); // Branded Fares Upsell -amadeus.shopping.flightOffers.upselling.post(JSON.stringify(body)); +amadeus.shopping.flightOffers.upselling.post(body); // Flight Choice Prediction amadeus.shopping.flightOffersSearch.get({ @@ -288,9 +288,7 @@ amadeus.shopping.flightOffersSearch.get({ departureDate: '2022-11-01', adults: '2' }).then(function(response){ - return amadeus.shopping.flightOffers.prediction.post( - JSON.stringify(response) - ); + return amadeus.shopping.flightOffers.prediction.post(response); }).then(function(response){ console.log(response.data); }).catch(function(responseError){ @@ -384,26 +382,28 @@ amadeus.shopping.hotelOfferSearch('XXX').get() // Hotel Booking API v2 amadeus.booking.hotelOrders.post( - JSON.stringfy({ + { 'data': { 'type': 'hotel-order', 'guests': [], 'travelAgent': {}, 'roomAssociations': [], 'payment': {} - }}) + } + } ) // Hotel Booking API v1 amadeus.booking.hotelBookings.post( - JSON.stringify({ + { 'data': { 'offerId': 'XXXX', 'guests': [], 'payments': [], 'rooms': [] - }}) + } + } ) // On-Demand Flight Status @@ -510,13 +510,13 @@ amadeus.analytics.itineraryPriceMetrics.get({ //Cars & Transfers APIs // Transfer Search API: Search Transfer -amadeus.shopping.transferOffers.post(JSON.stringify(body)); +amadeus.shopping.transferOffers.post(body); // Transfer Book API: Book a transfer based on the offer id -amadeus.ordering.transferOrders.post(JSON.stringify(body),offerId='2094123123'); +amadeus.ordering.transferOrders.post(body, offerId='2094123123'); // Transfer Management API: Cancel a transfer based on the order id & confirmation number -amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), confirmNbr='12345'); +amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, confirmNbr='12345'); ``` 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/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/hotel_bookings.js b/src/amadeus/namespaces/booking/hotel_bookings.js index 7a46a15..be813f4 100644 --- a/src/amadeus/namespaces/booking/hotel_bookings.js +++ b/src/amadeus/namespaces/booking/hotel_bookings.js @@ -26,13 +26,13 @@ class HotelBookings { * * ```js * amadeus.booking.hotelBookings.post( - * JSON.stringify({ + * { * 'data': { * 'offerId': 'XXXX', * 'guests': [], * 'payments': [], * 'rooms': [] - * }}) + * }} * ) * ``` */ diff --git a/src/amadeus/namespaces/booking/hotel_orders.js b/src/amadeus/namespaces/booking/hotel_orders.js index 8bc1ff5..d132764 100644 --- a/src/amadeus/namespaces/booking/hotel_orders.js +++ b/src/amadeus/namespaces/booking/hotel_orders.js @@ -26,19 +26,19 @@ class HotelOrders { * * ```js * amadeus.booking.hotelOrders.post( - * JSON.stringfy({ + * { * 'data': { * 'type': 'hotel-order', * 'guests': [], * 'travelAgent': {}, * 'roomAssociations': [], * 'payment': {} - * }}) - *) + * } + * }) * ``` + */ post(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 ff4871f..91e872f 100644 --- a/src/amadeus/namespaces/ordering/transfer_orders.js +++ b/src/amadeus/namespaces/ordering/transfer_orders.js @@ -25,7 +25,7 @@ class TransferOrders { * To book the transfer-offer(s) suggested by transferOffers and create a transfer-order * * ```js - * amadeus.ordering.transferOrders.post(body, '2094123123');; + * amadeus.ordering.transferOrders.post(body, '2094123123'); * ``` */ post(body, offerId) { diff --git a/src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js b/src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js index 64a1952..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 @@ -24,7 +24,7 @@ class Cancellation { * To cancel a transfer order with ID 'XXX' and confirmation number '12345' * * ```js - * amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), '12345');; + * amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, '12345');; * ``` */ post(body, confirmNbr) { 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 68b1a1a..3d358b7 100644 --- a/src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js +++ b/src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js @@ -31,9 +31,7 @@ class FlightChoicePrediction { * departureDate: '2020-08-01', * adults: '2' * }).then(function(response){ - * return amadeus.shopping.flightOffers.prediction.post( - * JSON.stringify(response) - * ); + * return amadeus.shopping.flightOffers.prediction.post(response); * }).then(function(response){ * console.log(response.data); * }).catch(function(responseError){ diff --git a/src/amadeus/namespaces/shopping/flight_offers_search.js b/src/amadeus/namespaces/shopping/flight_offers_search.js index d2198d6..9f00b21 100644 --- a/src/amadeus/namespaces/shopping/flight_offers_search.js +++ b/src/amadeus/namespaces/shopping/flight_offers_search.js @@ -52,7 +52,7 @@ class FlightOffersSearch { * To do a customized search with given options. * * ```js - * amadeus.shopping.flightOffersSearch.post (JSON.stringify({ + * amadeus.shopping.flightOffersSearch.post({ "currencyCode": "USD", "originDestinations": [ { @@ -114,7 +114,7 @@ class FlightOffersSearch { } } } - })) + }); * ``` */ post(params = {}) { diff --git a/src/amadeus/namespaces/shopping/seatmaps.js b/src/amadeus/namespaces/shopping/seatmaps.js index 749d180..ce50f64 100644 --- a/src/amadeus/namespaces/shopping/seatmaps.js +++ b/src/amadeus/namespaces/shopping/seatmaps.js @@ -51,9 +51,9 @@ class Seatmaps { * departureDate: '2020-08-01' * }).then(function(response){ * return amadeus.shopping.flightOffers.seatmaps.post( - * JSON.stringify({ - * 'data': response.data - * }) + * { + * data: response.data + * } * ); * }); * ```