From 6ef574b9fc2feb68206143a70d55eb1a0c0883b8 Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Sat, 29 Jun 2019 10:01:26 -0700 Subject: [PATCH] Fix keyword space in search api (#40) * fix keyword space * remove lru-cache --- package.json | 2 +- src/buy-api.js | 9 ++++++--- src/utils.js | 7 ++++++- yarn.lock | 12 ++++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 9426265..61f48ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ebay-node-api", - "version": "2.5.5", + "version": "2.5.6", "description": "Ebay node api client", "main": "./src/index.js", "homepage": "https://github.com/ajay2507/ebay-node-api", diff --git a/src/buy-api.js b/src/buy-api.js index 20b2540..5fac11d 100644 --- a/src/buy-api.js +++ b/src/buy-api.js @@ -1,6 +1,7 @@ 'use strict'; const makeString = require('make-string'); const { makeRequest } = require('./request'); +const { encodeURLQuery } = require('./utils'); const getItem = function (itemId) { if (!itemId) throw new Error('Item Id is required'); @@ -47,15 +48,17 @@ const searchItems = function (searchConfig) { if (!searchConfig.keyword && !searchConfig.categoryId && !searchConfig.gtin) throw new Error('Error --> Keyword or category id is required in query param'); if (!this.options.access_token) throw new Error('Error -->Missing Access token, Generate access token'); const auth = 'Bearer ' + this.options.access_token; - let queryParam = searchConfig.keyword ? 'q=' + searchConfig.keyword : ''; + let queryParam = searchConfig.keyword ? 'q=' + encodeURIComponent(searchConfig.keyword) : ''; queryParam = queryParam + (searchConfig.gtin ? '>in=' + searchConfig.gtin : ''); queryParam = queryParam + (searchConfig.categoryId ? '&category_ids=' + searchConfig.categoryId : ''); queryParam = queryParam + (searchConfig.limit ? '&limit=' + searchConfig.limit : ''); queryParam = queryParam + (searchConfig.sort ? '&sort=' + searchConfig.sort : ''); if (searchConfig.fieldgroups !== undefined) queryParam = queryParam + '&fieldgroups=' + searchConfig.fieldgroups; - if (searchConfig.filter !== undefined) queryParam = queryParam + '&filter=' + encodeURIComponent(makeString(searchConfig.filter, { quotes: 'no', braces: 'false' })); + if (searchConfig.filter !== undefined) queryParam = queryParam + '&' + encodeURLQuery('filter=' + makeString(searchConfig.filter, { quotes: 'no', braces: 'false' })); + console.log(this.options.baseUrl + `/buy/browse/v1/item_summary/search?${(queryParam)}`); + //this.options.baseUrl, `/buy/browse/v1/item_summary/search?${encodeURI(queryParam)} return new Promise((resolve, reject) => { - makeRequest(this.options.baseUrl, `/buy/browse/v1/item_summary/search?${queryParam}`, 'GET', this.options.body, auth).then((result) => { + makeRequest(this.options.baseUrl, `/buy/browse/v1/item_summary/search?${(queryParam)}`, 'GET', this.options.body, auth).then((result) => { resolve(result); }).then((error) => { reject(error); diff --git a/src/utils.js b/src/utils.js index b8df05b..6b34fe7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -10,6 +10,11 @@ function isString(value) { return typeof value === 'string' || value instanceof String; }; +function encodeURLQuery(url) { + return encodeURIComponent(url).replace(/'/g, '%27').replace(/"/g, '%22'); +}; + module.exports = { - upperCase + upperCase, + encodeURLQuery }; diff --git a/yarn.lock b/yarn.lock index 9b155e8..5809681 100644 --- a/yarn.lock +++ b/yarn.lock @@ -534,6 +534,13 @@ lolex@^2.2.0, lolex@^2.3.2: version "2.7.5" resolved "https://registry.yarnpkg.com/lolex/-/lolex-2.7.5.tgz#113001d56bfc7e02d56e36291cc5c413d1aa0733" +lru-cache@^5.1.1: + version "5.1.1" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920" + integrity sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w== + dependencies: + yallist "^3.0.2" + make-string@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/make-string/-/make-string-1.0.2.tgz#e88970b637e09e9e98ceb71b6298fa942b3a753c" @@ -891,3 +898,8 @@ write@1.0.3: resolved "https://registry.yarnpkg.com/write/-/write-1.0.3.tgz#0800e14523b923a387e415123c865616aae0f5c3" dependencies: mkdirp "^0.5.1" + +yallist@^3.0.2: + version "3.0.3" + resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9" + integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==