Skip to content

Commit 72d5fde

Browse files
authored
Support for adding custom headers (#54)
* create common utils across project * add support for custom headers * override eslint config
1 parent 64d3475 commit 72d5fde

File tree

10 files changed

+37
-27
lines changed

10 files changed

+37
-27
lines changed

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": [
3+
"eslint-config-ajay"
4+
],
5+
"parserOptions": {
6+
"ecmaVersion": 2018,
7+
"sourceType": "module"
8+
}
9+
}

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
sudo: false
12
language: node_js
3+
cache: npm
24
node_js:
3-
- '6'
45
- '8.10.0'
6+
- '10'

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ let eBay = require('ebay-node-api')
5151

5252
let ebay = new eBay({
5353
clientID: '-- Client APP ID ----',
54-
// options - optional HTTP request timeout to apply to all requests.
55-
env: 'SANDBOX' // optional default = 'PRODUCTION'
54+
env: 'SANDBOX', // optional default = 'PRODUCTION'
55+
headers:{ // optional
56+
'X-EBAY-C-MARKETPLACE-ID': 'EBAY_GB' // For Great Britain https://www.ebay.co.uk
57+
}
5658
})
5759
```
5860
Creates a new `Ebay` instance.
@@ -70,6 +72,7 @@ If you using Sandbox environment, make sure to provide `env` variable in options
7072
- `limit` - optional(`Number`) - fetch items functionality - Number that limits the number of data you need in response.
7173
- `details` - optional(`Boolean`) - Get User Details functionality - true, if you need details about the user.
7274
- `env` - optional(`String`) - Environment, default value is PRODUCTION.
75+
- `headers` - optional(`Object`) - Add custom request headers. For reference [Header Section](https://developer.ebay.com/api-docs/static/rest-request-components.html#HTTP)
7376

7477
## Example
7578

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ebay-node-api",
3-
"version": "2.6.0",
3+
"version": "2.7.0",
44
"description": "Ebay node api client",
55
"main": "./src/index.js",
66
"homepage": "https://github.com/pajaydev/ebay-node-api",

src/buy-api.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getItem = function (itemId) {
88
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
99
const auth = 'Bearer ' + this.options.access_token;
1010
const id = encodeURIComponent(itemId);
11-
return makeRequest(this.options.baseUrl, `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
11+
return makeRequest(this.options, `/buy/browse/v1/item/${id}`, 'GET', auth).then((result) => {
1212
return JSON.parse(result);
1313
});
1414
};
@@ -21,7 +21,7 @@ const getItemByLegacyId = function (legacyOptions) {
2121
let param = 'legacy_item_id=' + legacyOptions.legacyItemId;
2222
param += legacyOptions.legacyVariationSku ? '&legacy_variation_sku=' + legacyOptions.legacyVariationSku : '';
2323
return new Promise((resolve, reject) => {
24-
makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
24+
makeRequest(this.options, `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', auth).then((result) => {
2525
return resolve(JSON.parse(result));
2626
}).then((error) => {
2727
return reject(error);
@@ -35,7 +35,7 @@ const getItemByItemGroup = function (itemGroupId) {
3535
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
3636
const auth = 'Bearer ' + this.options.access_token;
3737
return new Promise((resolve, reject) => {
38-
makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
38+
makeRequest(this.options, `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', auth).then((result) => {
3939
resolve(result);
4040
}).then((error) => {
4141
reject(error);
@@ -56,10 +56,9 @@ const searchItems = function (searchConfig) {
5656
queryParam = queryParam + (searchConfig.sort ? '&sort=' + searchConfig.sort : '');
5757
if (searchConfig.fieldgroups !== undefined) queryParam = queryParam + '&fieldgroups=' + searchConfig.fieldgroups;
5858
if (searchConfig.filter !== undefined) queryParam = queryParam + '&filter=' + encodeURLQuery(makeString(searchConfig.filter, { quotes: 'no', braces: 'false' }));
59-
console.log(this.options.baseUrl + `/buy/browse/v1/item_summary/search?${(queryParam)}`);
6059
//this.options.baseUrl, `/buy/browse/v1/item_summary/search?${encodeURI(queryParam)}
6160
return new Promise((resolve, reject) => {
62-
makeRequest(this.options.baseUrl, `/buy/browse/v1/item_summary/search?${(queryParam)}`, 'GET', this.options.body, auth).then((result) => {
61+
makeRequest(this.options, `/buy/browse/v1/item_summary/search?${(queryParam)}`, 'GET', auth).then((result) => {
6362
resolve(result);
6463
}).then((error) => {
6564
reject(error);

src/common-utils/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function base64Encode(encodeData) {
55
const buff = Buffer.from(encodeData);
66
return buff.toString('base64');
77
}
8-
let headers = {};
8+
99
module.exports = {
1010
setAccessToken: function (token) {
1111
this.options.access_token = token;
@@ -17,17 +17,14 @@ module.exports = {
1717
const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret);
1818
const self = this;
1919
const auth = 'Basic ' + encodedStr;
20-
return makeRequest(this.options.baseUrl, '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
20+
return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then((result) => {
2121
const resultJSON = JSON.parse(result);
2222
self.setAccessToken(resultJSON.access_token);
2323
return resultJSON;
2424
});
2525
},
26-
setHeaders(headerObj) {
27-
headers = { ...headers, ...headerObj };
28-
},
29-
getHeaders() {
30-
return headers;
26+
setHeaders(self, headerObj) {
27+
self.headers = Object.assign({}, self.headers, headerObj);
3128
},
3229
upperCase(data) {
3330
if (!isString(data)) data = data.toString();

src/findingApi.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const findCompletedItems = function (options) {
3838
this.options.operationName = 'findCompletedItems';
3939
this.options.additionalParam = constructAdditionalParams(options);
4040
const url = urlObject.buildSearchUrl(this.options);
41-
console.log(url);
4241
return getRequest(url).then((data) => {
4342
return JSON.parse(data).findCompletedItemsResponse;
4443

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function Ebay(options) {
5151
options.baseSvcUrl = BASE_SANDBX_SVC_URL;
5252
}
5353
this.options = options;
54+
setHeaders(this, options.headers);
5455
this.options.globalID = options.countryCode || 'EBAY-US';
5556
}
5657

src/request.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ const getRequest = (url) => {
2525
});
2626
};
2727

28-
const makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
28+
const makeRequest = function postRequest(self, endpoint, methodName, token) {
2929
let dataString = '';
30-
methodName === 'POST' ? dataString = qs.stringify(data) : '';
31-
// console.log(endpoint);
30+
methodName === 'POST' ? dataString = qs.stringify(self.body) : '';
3231
const options = {
33-
'hostname': hostName,
32+
'hostname': self.baseUrl,
3433
'path': endpoint,
3534
'method': methodName || 'GET',
3635
'headers': {
3736
'content-type': methodName === 'POST' ? 'application/x-www-form-urlencoded' : 'application/json',
3837
'authorization': token,
39-
'cache-control': 'no-cache'
38+
'cache-control': 'no-cache',
39+
...self.headers
4040
}
4141
};
4242
return new Promise(function (resolve, reject) {

src/taxonomy-api.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const getDefaultCategoryTreeId = function (marketPlaceId) {
1313
marketPlaceId = upperCase(marketPlaceId);
1414
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
1515
const auth = 'Bearer ' + this.options.access_token;
16-
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', this.options.body, auth).then((result) => {
16+
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', auth).then((result) => {
1717
return JSON.parse(result);
1818
});
1919
};
@@ -27,7 +27,7 @@ const getCategoryTree = function (categoryTreeId) {
2727
if (!categoryTreeId) categoryTreeId = 0;
2828
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
2929
const auth = 'Bearer ' + this.options.access_token;
30-
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', this.options.body, auth).then((result) => {
30+
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', auth).then((result) => {
3131
return JSON.parse(result);
3232
});
3333
};
@@ -43,7 +43,7 @@ const getCategorySubtree = function (categoryTreeId, categoryId) {
4343
if (!categoryId) throw new Error('Missing Categor id \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getCategorySubtree#h2-samples');
4444
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
4545
const auth = 'Bearer ' + this.options.access_token;
46-
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_subtree?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => {
46+
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_subtree?category_id=${categoryId}`, 'GET', auth).then((result) => {
4747
return JSON.parse(result);
4848
});
4949
};
@@ -59,7 +59,7 @@ const getCategorySuggestions = function (categoryTreeId, keyword) {
5959
if (!keyword) throw new Error('Missing keyword \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getCategorySuggestions');
6060
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
6161
const auth = 'Bearer ' + this.options.access_token;
62-
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', this.options.body, auth).then((result) => {
62+
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', auth).then((result) => {
6363
return JSON.parse(result);
6464
});
6565
};
@@ -74,7 +74,7 @@ const getItemAspectsForCategory = function (categoryTreeId, categoryId) {
7474
if (!categoryId) throw new Error('Missing Category id \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getItemAspectsForCategory#h2-samples');
7575
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
7676
const auth = 'Bearer ' + this.options.access_token;
77-
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_item_aspects_for_category?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => {
77+
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_item_aspects_for_category?category_id=${categoryId}`, 'GET', auth).then((result) => {
7878
return JSON.parse(result);
7979
});
8080
};

0 commit comments

Comments
 (0)