From cce21ff9dc6ca57bcc6be438673303797fccd619 Mon Sep 17 00:00:00 2001 From: EarthlingDavey <15802017+EarthlingDavey@users.noreply.github.com> Date: Fri, 12 Mar 2021 04:00:08 +0000 Subject: [PATCH] Feature: multiple filter values (#137) * Don't encode filter= The query string for the buy api should not encode the = symbol. The `filter=` part of the query string has been removed outside of the `encodeURLQuery` function. * constructAdditionalParams function Allow for array of options for multiple filter values eBay reference: Filters with Multiple Values - https://developer.ebay.com/devzone/finding/Concepts/FindingAPIGuide.html#useitemfilters * fix conflict from unrelated merge conflict. * test constructAdditionalParams now includes test for array of values for multiple filter values. * Slightly improve readability of constructAdditionalParams Co-authored-by: daveybrown Co-authored-by: Davey Brown <> --- src/common-utils/index.js | 49 ++++++++++++++++++++++----------------- test/common.test.js | 5 ++-- 2 files changed, 31 insertions(+), 23 deletions(-) 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);