From f2754081e5a66f8c9220870b6829922f34677d67 Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Thu, 21 May 2020 00:49:03 -0700 Subject: [PATCH] Added findItemsineBayStores support (#94) * v2.7.7 * simple * add find items in ebay stores api --- demo/{findingApi.js => finding.js} | 9 +++++ demo/getAccessToken.js | 41 +++++++++++++++++----- package.json | 4 +-- src/common-utils/index.js | 40 +++++++++++++++++++-- src/{findingApi.js => finding.js} | 56 ++++++++++-------------------- src/index.js | 27 ++++---------- test/finding.test.js | 2 +- 7 files changed, 108 insertions(+), 71 deletions(-) rename demo/{findingApi.js => finding.js} (89%) rename src/{findingApi.js => finding.js} (71%) diff --git a/demo/findingApi.js b/demo/finding.js similarity index 89% rename from demo/findingApi.js rename to demo/finding.js index fb1ef59..4d8817f 100644 --- a/demo/findingApi.js +++ b/demo/finding.js @@ -74,3 +74,12 @@ ebay.getVersion().then((data) => { }, (error) => { console.log(error); }); + + +// Find ebay stores here https://www.ebay.com/sns +// https://developer.ebay.com/devzone/finding/callref/findItemsIneBayStores.html +ebay.findItemsIneBayStores({storeName: 'Battery Gallery'}).then((data) => { + console.log(data); +}, (error) => { + console.log(error); +}); diff --git a/demo/getAccessToken.js b/demo/getAccessToken.js index 7d2a81c..759003b 100644 --- a/demo/getAccessToken.js +++ b/demo/getAccessToken.js @@ -1,13 +1,38 @@ -const EbayToken = require('oauth-ebay'); +const Ebay = require('../src/index'); const { clientId, clientSecret } = require('./credentials/index'); -let ebay = new EbayToken({ +let ebay = new Ebay({ clientID: clientId, clientSecret: clientSecret, - grantType: 'client_credentials' + body: { + grant_type: 'client_credentials', + scope: 'https://api.ebay.com/oauth/api_scope' + + } }); -ebay.getAccessToken().then((data) => { - console.log(data); -}, (error) => { - console.log(error); -}); \ No newline at end of file + +//console.log(ebay.getAccessToken()); + +// // //Search for Items by Keyword. +ebay.getAccessToken() + .then((data) => { + console.log("generate tokensss"); + console.log(data); + }); + +console.log("++++++++++++++++++++"); + +ebay.getAccessToken() + .then((data) => { + console.log("generate tokensss"); + console.log(data); + }); + + +setTimeout(() => { + ebay.getAccessToken() + .then((data) => { + console.log("generate tokensss"); + console.log(data); + }); +}, 7200); \ No newline at end of file diff --git a/package.json b/package.json index 22ea9f4..2decded 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ebay-node-api", - "version": "2.8.4", + "version": "2.8.5", "description": "Ebay node api client", "main": "./src/index.js", "homepage": "https://github.com/pajaydev/ebay-node-api", @@ -9,7 +9,7 @@ "test": "mocha && npm run lint", "docs": "docsify init ./docs", "serve-docs": "docsify serve docs", - "publish": "gh-pages --dist docs --dotfiles --message 'chore: Publish docs'", + "docs-publish": "gh-pages --dist docs --dotfiles --message 'chore: Publish docs'", "prepublish": "npm run test" }, "author": "Ajaykumar prathap", diff --git a/src/common-utils/index.js b/src/common-utils/index.js index 27f3be7..cd517af 100644 --- a/src/common-utils/index.js +++ b/src/common-utils/index.js @@ -4,7 +4,42 @@ const { makeRequest } = require('../request'); const base64Encode = (encodeData) => { const buff = Buffer.from(encodeData);; return buff.toString('base64'); -} +}; + +/** + * Constructs query param based on some logic to support filter and aspect_filter params. + * output will be keywords=iphone&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=FreeShippingOnly&itemFilter(1).value=true&itemFilter(2).name=SoldItemsOnly&itemFilter(2).value=true + * @param {Object} options + */ +const constructAdditionalParams = (options) => { + let params = ''; + let count = 0; + for (let key in options) { + if (options.hasOwnProperty(key)) { + if (key === 'entriesPerPage' || key === 'pageNumber') { + params = `${params}paginationInput.${key}=${options[key]}&`; + } + else if (key === 'keywords' || key === 'categoryId' || key === 'productId' || key === 'sortOrder' || key === 'storeName') { + const encodeParam = encodeURIComponent(options[key]); + params = `${params}${key}=${encodeParam}&`; + } + else if (key === 'affiliate') { + const innerParams = options[key]; + for (let innerKey in innerParams) { + params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`; + } + } + else { + params = `${params}itemFilter(${count}).name=${key}& + itemFilter(${count}).value=${options[key]}&`; + count += 1; + } + } + } + // replace extra space + params = params.replace(/\s/g, ''); + return params.substring(0, params.length - 1); +}; module.exports = { setAccessToken: function (token) { @@ -63,5 +98,6 @@ module.exports = { } return url; }, - base64Encode + base64Encode, + constructAdditionalParams }; \ No newline at end of file diff --git a/src/findingApi.js b/src/finding.js similarity index 71% rename from src/findingApi.js rename to src/finding.js index ea4f66e..22a1f33 100644 --- a/src/findingApi.js +++ b/src/finding.js @@ -2,10 +2,12 @@ const urlObject = require('./buildURL'); const { getRequest } = require('./request'); +const utils = require('./common-utils/index'); const FIND_ITEMS_BY_KEYWORD = 'findItemsByKeywords'; const FIND_ITEMS_BY_CATEGORY = 'findItemsByCategory'; const FIND_COMPLETED_ITEMS = 'findCompletedItems'; const FIND_ITEMS_ADV = 'findItemsAdvanced'; +const FIND_EBAY_STORES = 'findItemsIneBayStores'; const findItemsByKeywords = function (options) { if (!options) { @@ -16,7 +18,7 @@ const findItemsByKeywords = function (options) { // support only keyword string. if (!options.keywords) options = { keywords: options }; options.keywords = encodeURIComponent(options.keywords); - this.options.additionalParam = constructAdditionalParams(options); + this.options.additionalParam = utils.constructAdditionalParams(options); const url = urlObject.buildSearchUrl(this.options); return getRequest(url).then((data) => { return JSON.parse(data).findItemsByKeywordsResponse; @@ -48,7 +50,7 @@ const findCompletedItems = function (options) { options.keywords = encodeURIComponent(options.keywords); } this.options.operationName = FIND_COMPLETED_ITEMS; - this.options.additionalParam = constructAdditionalParams(options); + this.options.additionalParam = utils.constructAdditionalParams(options); const url = urlObject.buildSearchUrl(this.options); return getRequest(url).then((data) => { return JSON.parse(data).findCompletedItemsResponse; @@ -69,8 +71,9 @@ const findItemsAdvanced = function (options) { options.keywords = encodeURIComponent(options.keywords); } this.options.operationName = FIND_ITEMS_ADV; - this.options.additionalParam = constructAdditionalParams(options); + this.options.additionalParam = utils.constructAdditionalParams(options); const url = urlObject.buildSearchUrl(this.options); + console.log(url); return getRequest(url).then((data) => { return JSON.parse(data).findItemsAdvancedResponse; }, console.error // eslint-disable-line no-console @@ -96,9 +99,10 @@ const findItemsByProduct = function (options) { if (!options.productId) throw new Error('INVALID_REQUEST_PARMS --> Product ID is required.'); let type = options.type ? options.type : 'ReferenceID'; this.options.operationName = 'findItemsByProduct'; - this.options.additionalParam = constructAdditionalParams(options); + this.options.additionalParam = utils.constructAdditionalParams(options); let url = urlObject.buildSearchUrl(this.options); url = `${url}&productId.@type=${type}`; + console.log(url); return getRequest(url).then((data) => { return JSON.parse(data).findItemsByProductResponse; @@ -106,47 +110,25 @@ const findItemsByProduct = function (options) { ); }; +const findItemsIneBayStores = function (options) { + if (!options) throw new Error('INVALID_REQUEST_PARMS --> Please enter the Valid input.'); + if (!options.storeName) throw new Error('INVALID_REQUEST_PARMS --> Store name is required.'); + this.options.operationName = FIND_EBAY_STORES; + this.options.additionalParam = utils.constructAdditionalParams(options); + console.log(urlObject.buildSearchUrl(this.options)); + return getRequest(urlObject.buildSearchUrl(this.options)).then((data) => { + return JSON.parse(data).findItemsIneBayStoresResponse; -/** - * Constructs query param based on some logic to support filter and aspect_filter params. - * output will be keywords=iphone&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=FreeShippingOnly&itemFilter(1).value=true&itemFilter(2).name=SoldItemsOnly&itemFilter(2).value=true - * @param {Object} options - */ -const constructAdditionalParams = (options) => { - let params = ''; - let count = 0; - for (let key in options) { - if (options.hasOwnProperty(key)) { - if (key === 'entriesPerPage' || key === 'pageNumber') { - params = `${params}paginationInput.${key}=${options[key]}&`; - } - else if (key === 'keywords' || key === 'categoryId' || key === 'productId' || key === 'sortOrder') { - params = `${params}${key}=${options[key]}&`; - } - else if (key === 'affiliate') { - const innerParams = options[key]; - for (let innerKey in innerParams) { - params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`; - } - } - else { - params = `${params}itemFilter(${count}).name=${key}& - itemFilter(${count}).value=${options[key]}&`; - count += 1; - } - } - } - // replace extra space - params = params.replace(/\s/g, ''); - return params.substring(0, params.length - 1); + }, console.error // eslint-disable-line no-console + ); }; module.exports = { findItemsByKeywords, findItemsByCategory, findCompletedItems, - constructAdditionalParams, findItemsByProduct, findItemsAdvanced, + findItemsIneBayStores, getVersion }; diff --git a/src/index.js b/src/index.js index bab315e..b01d660 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,9 @@ 'use strict'; const ebayBuyApi = require('./buy-api'); const shoppingApi = require('./shopping'); -const { getDefaultCategoryTreeId, - getCategoryTree, - getCategorySubtree, - getCategorySuggestions, - getItemAspectsForCategory } = require('./taxonomy-api'); -const ebayFindingApi = require('./findingApi'); -const { setAccessToken, - getAccessToken, - setHeaders, - getHeaders -} = require('./common-utils'); +const taxonomyApi = require('./taxonomy-api'); +const ebayFindingApi = require('./finding'); +const commonUtils = require('./common-utils'); const { getSimilarItems, getMostWatchedItems } = require('./merchandising'); const { PROD_BASE_URL, SANDBOX_BASE_URL, BASE_SANDBX_SVC_URL, BASE_SVC_URL } = require('./constants'); const PROD_ENV = 'PROD'; @@ -42,25 +34,18 @@ function Ebay(options) { options.baseSvcUrl = BASE_SANDBX_SVC_URL; } this.options = options; - setHeaders(this, options.headers); + commonUtils.setHeaders(this, options.headers); this.options.globalID = options.countryCode || 'EBAY-US'; this.options.siteId = options.siteId || '0'; } Ebay.prototype = { - setAccessToken, - getAccessToken, - setHeaders, - getHeaders, - getDefaultCategoryTreeId, - getCategoryTree, - getCategorySubtree, - getCategorySuggestions, - getItemAspectsForCategory, getMostWatchedItems, getSimilarItems, + ...commonUtils, ...shoppingApi, ...ebayBuyApi, + ...taxonomyApi, ...ebayFindingApi }; diff --git a/test/finding.test.js b/test/finding.test.js index 7b57c81..8f9da16 100644 --- a/test/finding.test.js +++ b/test/finding.test.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const should = require('chai').should(); const nock = require('nock'); const Ebay = require('../src/index'); -const { constructAdditionalParams } = require('../src/findingApi'); +const { constructAdditionalParams } = require('../src/common-utils/index'); const nockFindingApi = nock('https://svcs.ebay.com/'); describe('test ebay finding Api', () => {