diff --git a/src/common-utils/index.js b/src/common-utils/index.js index 78eea25..92fb906 100644 --- a/src/common-utils/index.js +++ b/src/common-utils/index.js @@ -1,5 +1,4 @@ 'use strict'; -const { makeRequest } = require('../request'); const currency = require('./currency.json'); const base64Encode = encodeData => { @@ -22,31 +21,39 @@ function constructAdditionalParams(options){ let currencyKey = this ? this.options.globalID : 'EBAY-US'; 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}&`; + if (!options.hasOwnProperty(key)) { + continue; + } + const value = options[key]; + if (['entriesPerPage', 'pageNumber'].includes(key)) { + params += `paginationInput.${key}=${value}&`; + } + else if (['keywords', 'categoryId', 'productId', 'sortOrder', 'storeName'].includes(key)) { + const encodeParam = encodeURIComponent(value); + params += `${key}=${encodeParam}&`; + } + else if (key === 'affiliate') { + for (let innerKey in value) { + params += `${key}.${innerKey}=${value[innerKey]}&`; } - else if (key === 'affiliate') { - const innerParams = options[key]; - for (let innerKey in innerParams) { - params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`; + } + else { + params += `itemFilter(${count}).name=${key}&`; + if (!Array.isArray(value)) { + params += `itemFilter(${count}).value=${value}&`; + } else { + for (let innerKey in value) { + params += `itemFilter(${count}).value(${innerKey})=${value[innerKey]}&`; } } - else { - params = `${params}itemFilter(${count}).name=${key}& - itemFilter(${count}).value=${options[key]}&`; - if(key === "MinPrice" || key === "MaxPrice"){ - params = `${params}itemFilter(${count}).paramName=Currency& - itemFilter(${count}).paramValue=${currency[currencyKey]}&`; - } - - count += 1; + if(key === "MinPrice" || key === "MaxPrice"){ + params += `itemFilter(${count}).paramName=Currency& + itemFilter(${count}).paramValue=${currency[currencyKey]}&`; } + + count++; } + } // replace extra space params = params.replace(/\s/g, ''); diff --git a/test/common.test.js b/test/common.test.js index 7aeb49f..2d6c055 100644 --- a/test/common.test.js +++ b/test/common.test.js @@ -19,11 +19,12 @@ describe('test common util methods', () => { describe('test constructAdditionalParams', () => { it('test constructAdditionalParams with required params', () => { - const expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; + const expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=condition&itemFilter(0).value(0)=3000&itemFilter(0).value(1)=4000'; const options = { keywords: 'iphone', categoryId: '111', - sortOrder: 'PricePlusShippingLowest' + sortOrder: 'PricePlusShippingLowest', + condition: ['3000', '4000'] }; const emptyOptions = {}; expect(constructAdditionalParams(options)).to.be.equal(expectedParam);