Skip to content

Commit

Permalink
stringified params while maintaining backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
darseen committed Oct 7, 2024
1 parent f6fb447 commit 7b2d050
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 36 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand All @@ -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'
});
Expand All @@ -59,15 +59,15 @@ 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).

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'
});
```
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions spec/amadeus/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});

Expand Down
24 changes: 12 additions & 12 deletions spec/amadeus/namespaces.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', {});
});

});
Expand Down
3 changes: 2 additions & 1 deletion src/amadeus/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class Client {
* @return {Promise.<Response,ResponseError>} 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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/booking/flight_orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/booking/hotel_bookings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/booking/hotel_orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/ordering/transfer_orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/shopping/flight_offers/pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Pricing {
url += '?' + queryString;
}

return this.client.post(url, JSON.stringify(params));
return this.client.post(url, params);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/shopping/flight_offers/upselling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/shopping/flight_offers_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/shopping/seatmaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/shopping/transfer_offers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 7b2d050

Please sign in to comment.