Skip to content

Commit cce21ff

Browse files
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 <daveybrown@users.noreply.github.com> Co-authored-by: Davey Brown <>
1 parent c04df6d commit cce21ff

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

src/common-utils/index.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const { makeRequest } = require('../request');
32
const currency = require('./currency.json');
43

54
const base64Encode = encodeData => {
@@ -22,31 +21,39 @@ function constructAdditionalParams(options){
2221
let currencyKey = this ? this.options.globalID : 'EBAY-US';
2322

2423
for (let key in options) {
25-
if (options.hasOwnProperty(key)) {
26-
if (key === 'entriesPerPage' || key === 'pageNumber') {
27-
params = `${params}paginationInput.${key}=${options[key]}&`;
28-
}
29-
else if (key === 'keywords' || key === 'categoryId' || key === 'productId' || key === 'sortOrder' || key === 'storeName') {
30-
const encodeParam = encodeURIComponent(options[key]);
31-
params = `${params}${key}=${encodeParam}&`;
24+
if (!options.hasOwnProperty(key)) {
25+
continue;
26+
}
27+
const value = options[key];
28+
if (['entriesPerPage', 'pageNumber'].includes(key)) {
29+
params += `paginationInput.${key}=${value}&`;
30+
}
31+
else if (['keywords', 'categoryId', 'productId', 'sortOrder', 'storeName'].includes(key)) {
32+
const encodeParam = encodeURIComponent(value);
33+
params += `${key}=${encodeParam}&`;
34+
}
35+
else if (key === 'affiliate') {
36+
for (let innerKey in value) {
37+
params += `${key}.${innerKey}=${value[innerKey]}&`;
3238
}
33-
else if (key === 'affiliate') {
34-
const innerParams = options[key];
35-
for (let innerKey in innerParams) {
36-
params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`;
39+
}
40+
else {
41+
params += `itemFilter(${count}).name=${key}&`;
42+
if (!Array.isArray(value)) {
43+
params += `itemFilter(${count}).value=${value}&`;
44+
} else {
45+
for (let innerKey in value) {
46+
params += `itemFilter(${count}).value(${innerKey})=${value[innerKey]}&`;
3747
}
3848
}
39-
else {
40-
params = `${params}itemFilter(${count}).name=${key}&
41-
itemFilter(${count}).value=${options[key]}&`;
42-
if(key === "MinPrice" || key === "MaxPrice"){
43-
params = `${params}itemFilter(${count}).paramName=Currency&
44-
itemFilter(${count}).paramValue=${currency[currencyKey]}&`;
45-
}
46-
47-
count += 1;
49+
if(key === "MinPrice" || key === "MaxPrice"){
50+
params += `itemFilter(${count}).paramName=Currency&
51+
itemFilter(${count}).paramValue=${currency[currencyKey]}&`;
4852
}
53+
54+
count++;
4955
}
56+
5057
}
5158
// replace extra space
5259
params = params.replace(/\s/g, '');

test/common.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ describe('test common util methods', () => {
1919

2020
describe('test constructAdditionalParams', () => {
2121
it('test constructAdditionalParams with required params', () => {
22-
const expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
22+
const expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=condition&itemFilter(0).value(0)=3000&itemFilter(0).value(1)=4000';
2323
const options = {
2424
keywords: 'iphone',
2525
categoryId: '111',
26-
sortOrder: 'PricePlusShippingLowest'
26+
sortOrder: 'PricePlusShippingLowest',
27+
condition: ['3000', '4000']
2728
};
2829
const emptyOptions = {};
2930
expect(constructAdditionalParams(options)).to.be.equal(expectedParam);

0 commit comments

Comments
 (0)