diff --git a/package-lock.json b/package-lock.json index 1efaedd2b..cb2aea5ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.1.1", + "version": "7.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7f1cfd5c6..60f024f0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.1.1", + "version": "7.1.2", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { diff --git a/src/controllers/document.js b/src/controllers/document.js index 6baaccd64..02934b828 100644 --- a/src/controllers/document.js +++ b/src/controllers/document.js @@ -194,12 +194,8 @@ class DocumentController extends BaseController { delete options[opt]; } - if (request.size === undefined) { - request.size = 10; - } - - if (!request.scroll && !request.body.sort && !request.from) { - request.from = 0; + if (!options.verb) { + options.verb = 'POST'; } return this.query(request, options) diff --git a/src/protocols/http.js b/src/protocols/http.js index 57f69ec77..0b0a8fcf8 100644 --- a/src/protocols/http.js +++ b/src/protocols/http.js @@ -307,8 +307,19 @@ class HttpWrapper extends KuzzleAbstractProtocol { if (http && http.length === 1) { routes[controller][action] = http[0]; - } else if (http && http.length > 1) { - routes[controller][action] = getCorrectRoute(http); + } + else if (http && http.length > 1) { + // We need this ugly fix because the document:search route can also + // be accessed in GET with this url: "/:index/:collection" + // But to send a query, we need to pass it in the body so we need POST + // so we can change the verb but then POST on "/:index/:collection" + // is the collection:update method (document:search is "/:index/:collection/_search") + if (controller === 'document' && action === 'search') { + routes[controller][action] = getPostRoute(http); + } + else { + routes[controller][action] = getCorrectRoute(http); + } } } @@ -325,7 +336,10 @@ class HttpWrapper extends KuzzleAbstractProtocol { _warn (message) { console.warn(message); // eslint-disable-line no-console } +} +function getPostRoute (routes) { + return routes[0].verb === 'POST' ? routes[0] : routes[1]; } function getCorrectRoute (routes) { diff --git a/test/controllers/document.test.js b/test/controllers/document.test.js index d3447d0cf..08722d6a1 100644 --- a/test/controllers/document.test.js +++ b/test/controllers/document.test.js @@ -382,14 +382,14 @@ describe('Document Controller', () => { .then(res => { should(kuzzle.query) .be.calledOnce() - .be.calledWith({ + .be.calledWithMatch({ controller: 'document', action: 'search', index: 'index', collection: 'collection', body: {foo: 'bar'}, - from: 0, - size: 10, + from: undefined, + size: undefined, scroll: undefined }, options); @@ -416,7 +416,7 @@ describe('Document Controller', () => { .then(res => { should(kuzzle.query) .be.calledOnce() - .be.calledWith({ + .be.calledWithMatch({ controller: 'document', action: 'search', index: 'index', @@ -428,30 +428,13 @@ describe('Document Controller', () => { }, {}); should(res).be.an.instanceOf(DocumentSearchResult); - should(res._options).be.empty(); + should(res._options).match({ verb: 'POST' }); should(res._response).be.equal(result); should(res.fetched).be.equal(2); should(res.total).be.equal(3); }); }); - it('should set default value for from and size', () => { - const result = { - hits: [], - total: 0 - }; - kuzzle.document.query = sinon.stub().resolves({result}); - - return kuzzle.document.search('index', 'collection', {}) - .then(() => { - should(kuzzle.document.query).be.calledOnce(); - - const request = kuzzle.document.query.getCall(0).args[0]; - should(request.from).be.eql(0); - should(request.size).be.eql(10); - }); - }); - it('should allow to set value of 0 for size', () => { const result = { hits: [], @@ -464,7 +447,6 @@ describe('Document Controller', () => { should(kuzzle.document.query).be.calledOnce(); const request = kuzzle.document.query.getCall(0).args[0]; - should(request.from).be.eql(0); should(request.size).be.eql(0); }); }); diff --git a/test/protocol/http.test.js b/test/protocol/http.test.js index 15e594942..5f9ca09d1 100644 --- a/test/protocol/http.test.js +++ b/test/protocol/http.test.js @@ -721,6 +721,14 @@ describe('HTTP networking module', () => { describe('_constructRoutes', () => { it('should construct http routes from server:publicApi', () => { const publicApi = { + document: { + search: { + http: [ + { verb: 'GET', url: '/:index/:collection/' }, + { verb: 'POST', url: '/:index/:collection/_search' } + ] + } + }, foo: { login: { http: [ @@ -766,8 +774,10 @@ describe('HTTP networking module', () => { // will be in the query string should(routes.foo.create.url).be.eql('/:index/:collection/_create'); - should(routes.foo.subscribe).be.undefined(); + // we should choose the POST route for document:create + should(routes.document.search.url).be.eql('/:index/:collection/_search'); + should(routes.foo.subscribe).be.undefined(); }); it('should overwrite kuzzle routes with custom routes', () => {