diff --git a/package-lock.json b/package-lock.json index f9156423b..c482b89d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.10.3", + "version": "7.10.4", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 530c5a43c..3629d1d55 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.10.3", + "version": "7.10.4", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { diff --git a/src/controllers/Document.ts b/src/controllers/Document.ts index 558d6f86f..2c3cd294a 100644 --- a/src/controllers/Document.ts +++ b/src/controllers/Document.ts @@ -840,7 +840,7 @@ export interface ArgsDocumentControllerDelete extends ArgsDefault { } export interface ArgsDocumentControllerDeleteByQuery extends ArgsDefault { - refresh?: string; + refresh?: "wait_for" | "false"; silent?: boolean; lang?: string; } @@ -933,7 +933,7 @@ export interface ArgsDocumentControllerUpdateByQuery extends ArgsDefault { export interface ArgsDocumentControllerUpsert extends ArgsDefault { default?: Partial; - refresh?: string; + refresh?: "wait_for" | "false"; silent?: boolean; retryOnConflict?: boolean; source?: boolean; diff --git a/src/core/searchResult/SearchResultBase.ts b/src/core/searchResult/SearchResultBase.ts index 8d91ee55a..053a6b27f 100644 --- a/src/core/searchResult/SearchResultBase.ts +++ b/src/core/searchResult/SearchResultBase.ts @@ -164,14 +164,11 @@ export class SearchResultBase implements SearchResult { } return this._kuzzle - .query( - { - ...this._request, - action: this._searchAction, - from: this.fetched, - }, - this._options - ) + .query({ + ...this._request, + action: this._searchAction, + from: this.fetched, + }) .then(({ result }) => this._buildNextSearchResult(result)); } diff --git a/src/protocols/Http.ts b/src/protocols/Http.ts index 228ad6929..bcc86fed6 100644 --- a/src/protocols/Http.ts +++ b/src/protocols/Http.ts @@ -274,6 +274,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol { payload.headers.authorization = "Bearer " + value; } else if (key === "volatile") { payload.headers["x-kuzzle-volatile"] = JSON.stringify(value); + } else if (key === "index" || key === "collection") { + // If we're calling a non-native route that answer to a GET request + // we need to add the index and collection (if provided) to the query string + if (!staticHttpRoutes[request.controller] && method === "GET") { + queryArgs[key] = value; + } else { + payload[key] = value; + } } else if (Object.prototype.hasOwnProperty.call(payload, key)) { payload[key] = value; } else if (value !== undefined && value !== null) { diff --git a/src/protocols/abstract/Realtime.ts b/src/protocols/abstract/Realtime.ts index bebe2766c..929976568 100644 --- a/src/protocols/abstract/Realtime.ts +++ b/src/protocols/abstract/Realtime.ts @@ -83,11 +83,12 @@ export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol { this.retrying = true; if ( + window !== null && typeof window === "object" && - typeof window.navigator === "object" && - window.navigator.onLine === false + typeof window!.navigator === "object" && + window!.navigator.onLine === false ) { - window.addEventListener( + window!.addEventListener( "online", () => { this.retrying = false; diff --git a/src/utils/debug.js b/src/utils/debug.js index ed87a8de1..cb504a59a 100644 --- a/src/utils/debug.js +++ b/src/utils/debug.js @@ -3,19 +3,33 @@ let NODE_DEBUG; /* eslint no-undef: 0 */ function shouldDebug() { - if (typeof window === "undefined") { - // Avoid multiple calls to process.env - if (!NODE_DEBUG) { - NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk"); + /** + * Some framework like react-native or other might emulate the window object + * but when on plateforms like iOS / Android, the window.location is undefined. + * + * So we need to check if window.location is defined before using it otherwise + * we will get an error. + * + * If something went wrong, be sure to return false to avoid any error. + */ + try { + if (typeof window === "undefined") { + // Avoid multiple calls to process.env + if (!NODE_DEBUG) { + NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk"); + } + + return NODE_DEBUG; } - return NODE_DEBUG; + return ( + window.debugKuzzleSdk || + (window.location && + new URL(window.location).searchParams.get("debugKuzzleSdk") !== null) + ); + } catch (e) { + return false; } - - return ( - window.debugKuzzleSdk || - new URL(window.location).searchParams.get("debugKuzzleSdk") !== null - ); } /** diff --git a/test/core/searchResult/document.test.js b/test/core/searchResult/document.test.js index 4012505f4..e154ecc3a 100644 --- a/test/core/searchResult/document.test.js +++ b/test/core/searchResult/document.test.js @@ -332,18 +332,15 @@ describe("DocumentSearchResult", () => { return searchResult.next().then((nextSearchResult) => { should(kuzzle.query) .be.calledOnce() - .be.calledWith( - { - index: "index", - collection: "collection", - body: { query: { foo: "bar" } }, - controller: "document", - action: "search", - size: 2, - from: 2, - }, - options - ); + .be.calledWith({ + index: "index", + collection: "collection", + body: { query: { foo: "bar" } }, + controller: "document", + action: "search", + size: 2, + from: 2, + }); should(nextSearchResult).not.be.equal(searchResult); should(nextSearchResult).be.instanceOf(DocumentSearchResult); }); diff --git a/test/core/searchResult/profile.test.js b/test/core/searchResult/profile.test.js index c477fdf0d..b40b80d5d 100644 --- a/test/core/searchResult/profile.test.js +++ b/test/core/searchResult/profile.test.js @@ -352,16 +352,13 @@ describe("ProfileSearchResult", () => { return searchResult.next().then((nextSearchResult) => { should(kuzzle.query) .be.calledOnce() - .be.calledWith( - { - body: { query: { foo: "bar" } }, - controller: "security", - action: "searchProfiles", - size: 2, - from: 2, - }, - options - ); + .be.calledWith({ + body: { query: { foo: "bar" } }, + controller: "security", + action: "searchProfiles", + size: 2, + from: 2, + }); should(nextSearchResult).not.be.equal(searchResult); should(nextSearchResult).be.instanceOf(ProfileSearchResult); }); diff --git a/test/core/searchResult/role.test.js b/test/core/searchResult/role.test.js index 120c970e5..75f281320 100644 --- a/test/core/searchResult/role.test.js +++ b/test/core/searchResult/role.test.js @@ -160,16 +160,13 @@ describe("RoleSearchResult", () => { return searchResult.next().then((nextSearchResult) => { should(kuzzle.query) .be.calledOnce() - .be.calledWith( - { - body: { foo: "bar" }, - controller: "security", - action: "searchRoles", - size: 2, - from: 2, - }, - options - ); + .be.calledWith({ + body: { foo: "bar" }, + controller: "security", + action: "searchRoles", + size: 2, + from: 2, + }); should(nextSearchResult).not.be.equal(searchResult); should(nextSearchResult).be.instanceOf(RoleSearchResult); }); diff --git a/test/core/searchResult/specifications.test.js b/test/core/searchResult/specifications.test.js index f5a848fae..df1bdb639 100644 --- a/test/core/searchResult/specifications.test.js +++ b/test/core/searchResult/specifications.test.js @@ -331,16 +331,13 @@ describe("SpecificationsSearchResult", () => { return searchResult.next().then((nextSearchResult) => { should(kuzzle.query) .be.calledOnce() - .be.calledWith( - { - body: { query: { foo: "bar" } }, - controller: "collection", - action: "searchSpecifications", - size: 2, - from: 2, - }, - options - ); + .be.calledWith({ + body: { query: { foo: "bar" } }, + controller: "collection", + action: "searchSpecifications", + size: 2, + from: 2, + }); should(nextSearchResult).not.be.equal(searchResult); should(nextSearchResult).be.instanceOf(SpecificationsSearchResult); }); diff --git a/test/core/searchResult/user.test.js b/test/core/searchResult/user.test.js index 0d4b875bf..047fe81c0 100644 --- a/test/core/searchResult/user.test.js +++ b/test/core/searchResult/user.test.js @@ -392,16 +392,13 @@ describe("UserSearchResult", () => { return searchResult.next().then((nextSearchResult) => { should(kuzzle.query) .be.calledOnce() - .be.calledWith( - { - body: { query: { foo: "bar" } }, - controller: "security", - action: "searchUsers", - size: 2, - from: 2, - }, - options - ); + .be.calledWith({ + body: { query: { foo: "bar" } }, + controller: "security", + action: "searchUsers", + size: 2, + from: 2, + }); should(nextSearchResult).not.be.equal(searchResult); should(nextSearchResult).be.instanceOf(UserSearchResult); }); diff --git a/test/protocol/Http.test.js b/test/protocol/Http.test.js index 4bc21135a..bdad64c42 100644 --- a/test/protocol/Http.test.js +++ b/test/protocol/Http.test.js @@ -639,6 +639,33 @@ describe("HTTP networking module", () => { protocol.send(data); }); + + it("should add index and collection to the query args if they are defined when using a custom GET route", (done) => { + const data = { + requestId: "requestId", + controller: "foo", + action: "bar", + index: "index", + collection: "collection", + }; + + protocol._routes = { + foo: { bar: { verb: "GET", url: "/foo/bar" } }, + }; + + protocol.on("requestId", () => { + should(protocol._sendHttpRequest) + .be.calledOnce() + .and.be.calledWithMatch({ + method: "GET", + path: "/foo/bar?index=index&collection=collection", + }); + + done(); + }); + + protocol.send(data); + }); }); describe("#sendHttpRequest NodeJS", () => {