Skip to content

Commit

Permalink
Feature: multiple filter values (#137)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Davey Brown <>
  • Loading branch information
EarthlingDavey and DaveyBrown authored Mar 12, 2021
1 parent c04df6d commit cce21ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
49 changes: 28 additions & 21 deletions src/common-utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const { makeRequest } = require('../request');
const currency = require('./currency.json');

const base64Encode = encodeData => {
Expand All @@ -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, '');
Expand Down
5 changes: 3 additions & 2 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cce21ff

Please sign in to comment.