From 72d5fde7b130d27f0c403df40150bd3715646b0d Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Mon, 9 Sep 2019 01:10:25 -0700 Subject: [PATCH] Support for adding custom headers (#54) * create common utils across project * add support for custom headers * override eslint config --- .eslintrc | 9 +++++++++ .travis.yml | 4 +++- README.md | 7 +++++-- package.json | 2 +- src/buy-api.js | 9 ++++----- src/common-utils/index.js | 11 ++++------- src/findingApi.js | 1 - src/index.js | 1 + src/request.js | 10 +++++----- src/taxonomy-api.js | 10 +++++----- 10 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..a6f09d9 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,9 @@ +{ + "extends": [ + "eslint-config-ajay" + ], + "parserOptions": { + "ecmaVersion": 2018, + "sourceType": "module" + } +} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index b4856d6..6067467 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,6 @@ +sudo: false language: node_js +cache: npm node_js: - - '6' - '8.10.0' + - '10' diff --git a/README.md b/README.md index 3b2e2c0..e1cba92 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/package.json b/package.json index 430d677..4a7cb04 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/buy-api.js b/src/buy-api.js index 0c7d176..c44cfa7 100644 --- a/src/buy-api.js +++ b/src/buy-api.js @@ -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); }); }; @@ -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); @@ -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); @@ -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); diff --git a/src/common-utils/index.js b/src/common-utils/index.js index 0720527..55338fa 100644 --- a/src/common-utils/index.js +++ b/src/common-utils/index.js @@ -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; @@ -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(); diff --git a/src/findingApi.js b/src/findingApi.js index d508000..0845a1a 100644 --- a/src/findingApi.js +++ b/src/findingApi.js @@ -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; diff --git a/src/index.js b/src/index.js index cea330e..4f30511 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; } diff --git a/src/request.js b/src/request.js index 5aac47a..5cfa95f 100644 --- a/src/request.js +++ b/src/request.js @@ -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) { diff --git a/src/taxonomy-api.js b/src/taxonomy-api.js index 4d6c349..b36857f 100644 --- a/src/taxonomy-api.js +++ b/src/taxonomy-api.js @@ -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); }); }; @@ -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); }); }; @@ -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); }); }; @@ -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); }); }; @@ -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); }); };