Skip to content

Commit 1cfb81e

Browse files
committed
Merge branch '7-dev' into window-object-i-hate-you
2 parents ef0aece + e814df2 commit 1cfb81e

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

src/protocols/Http.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
274274
payload.headers.authorization = "Bearer " + value;
275275
} else if (key === "volatile") {
276276
payload.headers["x-kuzzle-volatile"] = JSON.stringify(value);
277+
} else if (key === "index" || key === "collection") {
278+
// If we're calling a non-native route that answer to a GET request
279+
// we need to add the index and collection (if provided) to the query string
280+
if (!staticHttpRoutes[request.controller] && method === "GET") {
281+
queryArgs[key] = value;
282+
} else {
283+
payload[key] = value;
284+
}
277285
} else if (Object.prototype.hasOwnProperty.call(payload, key)) {
278286
payload[key] = value;
279287
} else if (value !== undefined && value !== null) {

src/utils/debug.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ let NODE_DEBUG;
33
/* eslint no-undef: 0 */
44

55
function shouldDebug() {
6-
if (typeof window === "undefined") {
7-
// Avoid multiple calls to process.env
8-
if (!NODE_DEBUG) {
9-
NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk");
6+
/**
7+
* Some framework like react-native or other might emulate the window object
8+
* but when on plateforms like iOS / Android, the window.location is undefined.
9+
*
10+
* So we need to check if window.location is defined before using it otherwise
11+
* we will get an error.
12+
*
13+
* If something went wrong, be sure to return false to avoid any error.
14+
*/
15+
try {
16+
if (typeof window === "undefined") {
17+
// Avoid multiple calls to process.env
18+
if (!NODE_DEBUG) {
19+
NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk");
20+
}
21+
22+
return NODE_DEBUG;
1023
}
1124

12-
return NODE_DEBUG;
25+
return (
26+
window.debugKuzzleSdk ||
27+
(window.location &&
28+
new URL(window.location).searchParams.get("debugKuzzleSdk") !== null)
29+
);
30+
} catch (e) {
31+
return false;
1332
}
14-
15-
return (
16-
window.debugKuzzleSdk ||
17-
new URL(window.location).searchParams.get("debugKuzzleSdk") !== null
18-
);
1933
}
2034

2135
/**

test/protocol/Http.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,33 @@ describe("HTTP networking module", () => {
639639

640640
protocol.send(data);
641641
});
642+
643+
it("should add index and collection to the query args if they are defined when using a custom GET route", (done) => {
644+
const data = {
645+
requestId: "requestId",
646+
controller: "foo",
647+
action: "bar",
648+
index: "index",
649+
collection: "collection",
650+
};
651+
652+
protocol._routes = {
653+
foo: { bar: { verb: "GET", url: "/foo/bar" } },
654+
};
655+
656+
protocol.on("requestId", () => {
657+
should(protocol._sendHttpRequest)
658+
.be.calledOnce()
659+
.and.be.calledWithMatch({
660+
method: "GET",
661+
path: "/foo/bar?index=index&collection=collection",
662+
});
663+
664+
done();
665+
});
666+
667+
protocol.send(data);
668+
});
642669
});
643670

644671
describe("#sendHttpRequest NodeJS", () => {

0 commit comments

Comments
 (0)