Skip to content

Commit 8b75799

Browse files
authored
fix affiliate in finding api (#68)
1 parent ceffff9 commit 8b75799

File tree

7 files changed

+71
-9
lines changed

7 files changed

+71
-9
lines changed

demo/findingApi.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-console */
2+
'use strict';
13
const Ebay = require('../src/index');
24
const { clientId } = require('./credentials/index');
35

@@ -17,7 +19,11 @@ ebay.findItemsByKeywords({
1719
keywords: 'iphone',
1820
sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html
1921
Condition: 3000,
20-
SoldItemsOnly: false
22+
SoldItemsOnly: false,
23+
affiliate: {
24+
networkId: 9,
25+
trackingId: 1234567890
26+
}
2127
}).then((data) => {
2228
console.log(data);
2329
}, (error) => {
@@ -28,7 +34,6 @@ ebay.findItemsByKeywords({
2834
/* This call searches for items whose listings are completed and are no longer available for
2935
sale by category (using categoryId), by keywords (using keywords), or a combination of the two.
3036
Keyword queries search the title and subtitle of the item; they do not search descriptions. */
31-
3237
ebay.findCompletedItems({
3338
keywords: 'Garmin nuvi 1300 Automotive GPS Receiver',
3439
categoryId: '156955',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ebay-node-api",
3-
"version": "2.7.4",
3+
"version": "2.7.5",
44
"description": "Ebay node api client",
55
"main": "./src/index.js",
66
"homepage": "https://github.com/pajaydev/ebay-node-api",

src/common-utils/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,15 @@ module.exports = {
5050
encodeURLQuery(url) {
5151
return encodeURIComponent(url).replace(/'/g, '%27').replace(/"/g, '%22');
5252
},
53+
54+
// parses the object and converts it into query params.
55+
parseObj(options, url = '') {
56+
if (options) {
57+
for (let key in options) {
58+
url = `${url}&${key}=${options[key]}`
59+
}
60+
}
61+
return url;
62+
},
5363
base64Encode
5464
};

src/findingApi.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ const constructAdditionalParams = (options) => {
102102
else if (key === 'keywords' || key === 'categoryId' || key === 'productId' || key === 'sortOrder') {
103103
params = `${params}${key}=${options[key]}&`;
104104
}
105+
else if (key === 'affiliate') {
106+
const innerParams = options[key];
107+
for (let innerKey in innerParams) {
108+
params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`;
109+
}
110+
}
105111
else {
106112
params = `${params}itemFilter(${count}).name=${key}&
107113
itemFilter(${count}).value=${options[key]}&`;

src/merchandising.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { getRequest } = require('./request');
4+
const { parseObj } = require('./common-utils/index');
45
const { MERCH_SRVC_NAME } = require('./constants');
56

67
//https://developer.ebay.com/devzone/merchandising/docs/CallRef/getMostWatchedItems.html#Samples
@@ -12,9 +13,7 @@ const { MERCH_SRVC_NAME } = require('./constants');
1213
*/
1314
const getMostWatchedItems = function (merchOptions) {
1415
if (!this.options.clientID) throw new Error('Missing App id or client id');
15-
let url = '';
16-
if (merchOptions && merchOptions.categoryId !== undefined) url = `&categoryId=${merchOptions.categoryId}`;
17-
if (merchOptions && merchOptions.maxResults) url = `&maxResults=${merchOptions.maxResults}`;
16+
const url = parseObj(merchOptions);
1817
return getRequest(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getMostWatchedItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
1918
return JSON.parse(result);
2019
}).catch((error) => {
@@ -30,9 +29,8 @@ const getMostWatchedItems = function (merchOptions) {
3029
*/
3130
const getSimilarItems = function (merchOptions) {
3231
if (!this.options.clientID) throw new Error('Missing App id or client id');
33-
let url = '';
34-
if (merchOptions && merchOptions.itemId) url = `&itemId=${merchOptions.itemId}`;
35-
if (merchOptions && merchOptions.maxResults) url = `${url}&maxResults=${merchOptions.maxResults}`;
32+
const url = parseObj(merchOptions);
33+
console.log(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`);
3634
return getRequest(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
3735
return JSON.parse(result);
3836
}).catch((error) => {

test/common.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const expect = require('chai').expect;
3+
const should = require('chai').should();
4+
const { parseObj } = require('../src/common-utils/index');
5+
6+
describe('test common util methods', () => {
7+
it('test parse object to query params', () => {
8+
const expected_param = '&keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
9+
const options = {
10+
keywords: 'iphone',
11+
categoryId: '111',
12+
sortOrder: 'PricePlusShippingLowest'
13+
};
14+
const emptyOptions = {};
15+
expect(parseObj(options)).to.be.equal(expected_param);
16+
expect(parseObj(emptyOptions)).to.be.equal('');
17+
expect(parseObj(options, 'userName=ebay')).to.be.equal(`userName=ebay${expected_param}`);
18+
});
19+
});

test/findingApi.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ describe('test ebay finding Api', () => {
3535
expect(constructAdditionalParams(emptyOptions)).to.be.equal('');
3636
});
3737

38+
it('test constructAdditionalParams with affiliate params', () => {
39+
let expected_param_with_affiliate = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&affiliate.trackingId=1234567899&affiliate.networkId=123';
40+
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
41+
const options = {
42+
keywords: 'iphone',
43+
categoryId: '111',
44+
sortOrder: 'PricePlusShippingLowest',
45+
affiliate: {
46+
trackingId: 1234567899,
47+
networkId: 123
48+
}
49+
};
50+
51+
const optionsWithNoAffiliate = {
52+
keywords: 'iphone',
53+
categoryId: '111',
54+
sortOrder: 'PricePlusShippingLowest'
55+
};
56+
const emptyOptions = {};
57+
expect(constructAdditionalParams(options)).to.be.equal(expected_param_with_affiliate);
58+
expect(constructAdditionalParams(optionsWithNoAffiliate)).to.be.equal(expected_param);
59+
expect(constructAdditionalParams(emptyOptions)).to.be.equal('');
60+
});
61+
3862
it('test constructAdditionalParams with additional params', () => {
3963
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true';
4064
let expected_pag_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true&paginationInput.entriesPerPage=2';

0 commit comments

Comments
 (0)