diff --git a/README.md b/README.md index bc85c31..93ee4be 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ 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" }) ``` Creates a new `Ebay` instance. @@ -59,11 +60,14 @@ Creates a new `Ebay` instance. Join eBay developers program. Register your app here https://go.developer.ebay.com/quick-start-guide. +If you using Sandbox environment, make sure to provide `env` variable in options as mentioned above. + #### Options - `clientID` - Required(`String`) - Client Id key provided when you register in eBay developers program. - `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. ## Example diff --git a/package.json b/package.json index baef2df..2c18d87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ebay-node-api", - "version": "2.3.1", + "version": "2.5.1", "description": "Ebay node api client", "main": "./src/index.js", "homepage": "https://github.com/ajay2507/ebay-node-api", @@ -29,4 +29,4 @@ "nock": "^9.2.3", "sinon": "^4.4.5" } -} +} \ No newline at end of file diff --git a/src/buildURL.js b/src/buildURL.js index 05e0647..6b506b7 100644 --- a/src/buildURL.js +++ b/src/buildURL.js @@ -13,7 +13,8 @@ const buildURL = { * @private */ buildSearchUrl(options) { - let base_url = "http://svcs.ebay.com/services/search/FindingService/v1?"; + console.log(options); + let base_url = `http://${options.baseSvcUrl}/services/search/FindingService/v1?`; base_url += "SECURITY-APPNAME=" + options.clientID; base_url += "&OPERATION-NAME=" + options.operationName; base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON"; @@ -33,7 +34,7 @@ const buildURL = { * @private */ buildShoppingUrl(options) { - let base_url = "http://open.api.ebay.com/Shopping?"; + let base_url = `http://${options.baseUrl}/Shopping?`; base_url += "appid=" + options.clientID; base_url += "&callname=" + options.operationName; base_url += "&version=967&siteid=0&responseencoding=JSON&"; diff --git a/src/buy-api.js b/src/buy-api.js index 4656894..99abc2c 100644 --- a/src/buy-api.js +++ b/src/buy-api.js @@ -7,7 +7,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('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => { + return makeRequest(this.options.baseUrl, `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => { return JSON.parse(result); }); }; @@ -20,7 +20,7 @@ const getItemByLegacyId = function (legacyOptions) { let param = "legacy_item_id=" + legacyOptions.legacyItemId; param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : ''; - makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => { + makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => { return JSON.parse(result); }).then((error) => { console.log(error.errors); @@ -34,7 +34,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('api.ebay.com', `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => { + makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => { resolve(result); }).then((error) => { reject(error); @@ -55,7 +55,7 @@ const searchItems = function (searchConfig) { if (searchConfig.fieldgroups != undefined) queryParam = queryParam + "&fieldgroups=" + searchConfig.fieldgroups; if (searchConfig.filter != undefined) queryParam = queryParam + "&filter=" + encodeURIComponent(makeString(searchConfig.filter, { quotes: "no", braces: 'false' })); return new Promise((resolve, reject) => { - makeRequest('api.ebay.com', `/buy/browse/v1/item_summary/search?${queryParam}`, 'GET', this.options.body, auth).then((result) => { + makeRequest(this.options.baseUrl, `/buy/browse/v1/item_summary/search?${queryParam}`, 'GET', this.options.body, auth).then((result) => { resolve(result); }).then((error) => { reject(error); diff --git a/src/constants.js b/src/constants.js index c434f75..1c87a4e 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,6 +1,9 @@ 'use strict'; module.exports = { + PROD_BASE_URL: "api.ebay.com", + SANDBOX_BASE_URL: "api.sandbox.ebay.com", BASE_SVC_URL: "svcs.ebay.com", + BASE_SANDBX_SVC_URL: "svcs.sandbox.ebay.com", MERCH_SRVC_NAME: "MerchandisingService" } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 4b6a8b5..e24f761 100644 --- a/src/index.js +++ b/src/index.js @@ -10,13 +10,23 @@ const { getDefaultCategoryTreeId, getCategorySuggestions, getItemAspectsForCategory } = require('./taxonomy-api'); const { getSimilarItems, getMostWatchedItems } = require('./merchandising'); +const { PROD_BASE_URL, SANDBOX_BASE_URL, BASE_SANDBX_SVC_URL, BASE_SVC_URL } = require('./constants'); const urlObject = require('./buildURL'); - +const PROD_ENV = "PROD"; +const SANDBOX_ENV = "SANDBOX"; function Ebay(options) { if (!options) throw new Error("Options is missing, please provide the input"); if (!options.clientID) throw Error("Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/"); if (!(this instanceof Ebay)) return new Ebay(options); + if (!options.env) options.env = PROD_ENV; + options.baseUrl = PROD_BASE_URL; + options.baseSvcUrl = BASE_SVC_URL; + // handle sandbox env. + if (options.env === SANDBOX_ENV) { + options.baseUrl = SANDBOX_BASE_URL; + options.baseSvcUrl = BASE_SANDBX_SVC_URL; + } this.options = options; this.options.globalID = options.countryCode || "EBAY-US"; } @@ -64,6 +74,7 @@ Ebay.prototype = { getVersion: function () { this.options.operationName = "getVersion"; const url = urlObject.buildSearchUrl(this.options); + console.log(url); return getRequest(url).then((data) => { return JSON.parse(data)["getVersionResponse"][0]; }, console.error @@ -94,7 +105,7 @@ Ebay.prototype = { const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret); const self = this; const auth = "Basic " + encodedStr; - return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => { + return makeRequest(this.options.baseUrl, '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => { const resultJSON = JSON.parse(result); self.setAccessToken(resultJSON.access_token); return resultJSON; diff --git a/src/merchandising.js b/src/merchandising.js index 1a1c4c4..f0dc666 100644 --- a/src/merchandising.js +++ b/src/merchandising.js @@ -1,7 +1,7 @@ 'use strict'; const { getRequest } = require('./request'); -const { BASE_SVC_URL, MERCH_SRVC_NAME } = require('./constants'); +const { MERCH_SRVC_NAME } = require('./constants'); //https://developer.ebay.com/devzone/merchandising/docs/CallRef/getMostWatchedItems.html#Samples @@ -11,12 +11,11 @@ const { BASE_SVC_URL, MERCH_SRVC_NAME } = require('./constants'); * @param {String} categoryId (optional) */ const getMostWatchedItems = function (merchOptions) { - console.log(this); if (!this.options.clientID) throw new Error("Missing App id or client id"); let url = ''; if (merchOptions && merchOptions.categoryId != undefined) url = `&categoryId=${merchOptions.categoryId}`; if (merchOptions && merchOptions.maxResults) url = `&maxResults=${merchOptions.maxResults}`; - return getRequest(`http://${BASE_SVC_URL}/${MERCH_SRVC_NAME}?OPERATION-NAME=getMostWatchedItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => { + return getRequest(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getMostWatchedItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => { return JSON.parse(result); }).catch((error) => { console.log(error); @@ -34,7 +33,7 @@ const getSimilarItems = function (merchOptions) { let url = ''; if (merchOptions && merchOptions.itemId) url = `&itemId=${merchOptions.itemId}`; if (merchOptions && merchOptions.maxResults) url = `${url}&maxResults=${merchOptions.maxResults}`; - return getRequest(`http://${BASE_SVC_URL}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => { + return getRequest(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => { return JSON.parse(result); }).catch((error) => { console.log(error); diff --git a/src/taxonomy-api.js b/src/taxonomy-api.js index 5da5b8f..d372a4f 100644 --- a/src/taxonomy-api.js +++ b/src/taxonomy-api.js @@ -12,7 +12,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('api.ebay.com', `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', this.options.body, auth).then((result) => { + 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 JSON.parse(result); }); }; @@ -26,7 +26,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('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', this.options.body, auth).then((result) => { + return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', this.options.body, auth).then((result) => { return JSON.parse(result); }); }; @@ -42,7 +42,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('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_subtree?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => { + 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 JSON.parse(result); }); }; @@ -58,7 +58,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('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', this.options.body, auth).then((result) => { + 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 JSON.parse(result); }); }; @@ -73,7 +73,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('api.ebay.com', `/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.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_item_aspects_for_category?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => { return JSON.parse(result); }); }; diff --git a/test/buildURL.test.js b/test/buildURL.test.js index 1e40727..3fa379f 100644 --- a/test/buildURL.test.js +++ b/test/buildURL.test.js @@ -14,7 +14,8 @@ describe("test building url methods", () => { param: "keywords", clientID: "testID", limit: 6, - globalID: "EBAY-US" + globalID: "EBAY-US", + baseSvcUrl: 'svcs.ebay.com' } expect(buildURL.buildSearchUrl(options)).to.be.equal(expected_search_url); }); @@ -26,6 +27,7 @@ describe("test building url methods", () => { operationName: "demoShoppingName", param: "keywords", clientID: "testID", + baseUrl: 'open.api.ebay.com' } expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url); }); @@ -37,7 +39,8 @@ describe("test building url methods", () => { operationName: "demoShoppingName", param: "keywords", clientID: "testID", - includeSelector: true + includeSelector: true, + baseUrl: 'open.api.ebay.com' } expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url); });