Skip to content

Commit

Permalink
Support for adding custom headers (#54)
Browse files Browse the repository at this point in the history
* create common utils across project

* add support for custom headers

* override eslint config
  • Loading branch information
pajaydev authored Sep 9, 2019
1 parent 64d3475 commit 72d5fde
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 27 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": [
"eslint-config-ajay"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
}
}
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
sudo: false
language: node_js
cache: npm
node_js:
- '6'
- '8.10.0'
- '10'
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ let eBay = require('ebay-node-api')

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

## Example

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ebay-node-api",
"version": "2.6.0",
"version": "2.7.0",
"description": "Ebay node api client",
"main": "./src/index.js",
"homepage": "https://github.com/pajaydev/ebay-node-api",
Expand Down
9 changes: 4 additions & 5 deletions src/buy-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getItem = function (itemId) {
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
const id = encodeURIComponent(itemId);
return makeRequest(this.options.baseUrl, `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options, `/buy/browse/v1/item/${id}`, 'GET', auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -21,7 +21,7 @@ const getItemByLegacyId = function (legacyOptions) {
let param = 'legacy_item_id=' + legacyOptions.legacyItemId;
param += legacyOptions.legacyVariationSku ? '&legacy_variation_sku=' + legacyOptions.legacyVariationSku : '';
return new Promise((resolve, reject) => {
makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
makeRequest(this.options, `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', auth).then((result) => {
return resolve(JSON.parse(result));
}).then((error) => {
return reject(error);
Expand All @@ -35,7 +35,7 @@ const getItemByItemGroup = function (itemGroupId) {
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
return new Promise((resolve, reject) => {
makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
makeRequest(this.options, `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', auth).then((result) => {
resolve(result);
}).then((error) => {
reject(error);
Expand All @@ -56,10 +56,9 @@ const searchItems = function (searchConfig) {
queryParam = queryParam + (searchConfig.sort ? '&sort=' + searchConfig.sort : '');
if (searchConfig.fieldgroups !== undefined) queryParam = queryParam + '&fieldgroups=' + searchConfig.fieldgroups;
if (searchConfig.filter !== undefined) queryParam = queryParam + '&filter=' + encodeURLQuery(makeString(searchConfig.filter, { quotes: 'no', braces: 'false' }));
console.log(this.options.baseUrl + `/buy/browse/v1/item_summary/search?${(queryParam)}`);
//this.options.baseUrl, `/buy/browse/v1/item_summary/search?${encodeURI(queryParam)}
return new Promise((resolve, reject) => {
makeRequest(this.options.baseUrl, `/buy/browse/v1/item_summary/search?${(queryParam)}`, 'GET', this.options.body, auth).then((result) => {
makeRequest(this.options, `/buy/browse/v1/item_summary/search?${(queryParam)}`, 'GET', auth).then((result) => {
resolve(result);
}).then((error) => {
reject(error);
Expand Down
11 changes: 4 additions & 7 deletions src/common-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function base64Encode(encodeData) {
const buff = Buffer.from(encodeData);
return buff.toString('base64');
}
let headers = {};

module.exports = {
setAccessToken: function (token) {
this.options.access_token = token;
Expand All @@ -17,17 +17,14 @@ module.exports = {
const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret);
const self = this;
const auth = 'Basic ' + encodedStr;
return makeRequest(this.options.baseUrl, '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then((result) => {
const resultJSON = JSON.parse(result);
self.setAccessToken(resultJSON.access_token);
return resultJSON;
});
},
setHeaders(headerObj) {
headers = { ...headers, ...headerObj };
},
getHeaders() {
return headers;
setHeaders(self, headerObj) {
self.headers = Object.assign({}, self.headers, headerObj);
},
upperCase(data) {
if (!isString(data)) data = data.toString();
Expand Down
1 change: 0 additions & 1 deletion src/findingApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const findCompletedItems = function (options) {
this.options.operationName = 'findCompletedItems';
this.options.additionalParam = constructAdditionalParams(options);
const url = urlObject.buildSearchUrl(this.options);
console.log(url);
return getRequest(url).then((data) => {
return JSON.parse(data).findCompletedItemsResponse;

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function Ebay(options) {
options.baseSvcUrl = BASE_SANDBX_SVC_URL;
}
this.options = options;
setHeaders(this, options.headers);
this.options.globalID = options.countryCode || 'EBAY-US';
}

Expand Down
10 changes: 5 additions & 5 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ const getRequest = (url) => {
});
};

const makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
const makeRequest = function postRequest(self, endpoint, methodName, token) {
let dataString = '';
methodName === 'POST' ? dataString = qs.stringify(data) : '';
// console.log(endpoint);
methodName === 'POST' ? dataString = qs.stringify(self.body) : '';
const options = {
'hostname': hostName,
'hostname': self.baseUrl,
'path': endpoint,
'method': methodName || 'GET',
'headers': {
'content-type': methodName === 'POST' ? 'application/x-www-form-urlencoded' : 'application/json',
'authorization': token,
'cache-control': 'no-cache'
'cache-control': 'no-cache',
...self.headers
}
};
return new Promise(function (resolve, reject) {
Expand Down
10 changes: 5 additions & 5 deletions src/taxonomy-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getDefaultCategoryTreeId = function (marketPlaceId) {
marketPlaceId = upperCase(marketPlaceId);
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -27,7 +27,7 @@ const getCategoryTree = function (categoryTreeId) {
if (!categoryTreeId) categoryTreeId = 0;
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -43,7 +43,7 @@ const getCategorySubtree = function (categoryTreeId, categoryId) {
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');
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
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) => {
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_subtree?category_id=${categoryId}`, 'GET', auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -59,7 +59,7 @@ const getCategorySuggestions = function (categoryTreeId, keyword) {
if (!keyword) throw new Error('Missing keyword \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getCategorySuggestions');
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -74,7 +74,7 @@ const getItemAspectsForCategory = function (categoryTreeId, categoryId) {
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');
if (!this.options.access_token) throw new Error('Missing Access token, Generate access token');
const auth = 'Bearer ' + this.options.access_token;
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) => {
return makeRequest(this.options, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_item_aspects_for_category?category_id=${categoryId}`, 'GET', auth).then((result) => {
return JSON.parse(result);
});
};
Expand Down

0 comments on commit 72d5fde

Please sign in to comment.