Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Add option to client constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jul 30, 2021
1 parent dfd3527 commit 15836d7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 47 deletions.
6 changes: 4 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface KintoClientOptions {
requestMode?: RequestMode;
timeout?: number;
batch?: boolean;
fetchFunc?: Function;
}

export interface PaginatedParams {
Expand Down Expand Up @@ -107,6 +108,7 @@ export default class KintoClientBase {
* @param {String} [options.bucket="default"] The default bucket to use.
* @param {String} [options.requestMode="cors"] The HTTP request mode (from ES6 fetch spec).
* @param {Number} [options.timeout=null] The request timeout in ms, if any.
* @param {Function} [options.fetchFunc=fetch] The function to be used to execute HTTP requests.
*/
constructor(remote: string, options: KintoClientOptions) {
if (typeof remote !== "string" || !remote.length) {
Expand Down Expand Up @@ -145,13 +147,13 @@ export default class KintoClientBase {

this.endpoints = endpoints;

const { requestMode, timeout } = options;
const { fetchFunc, requestMode, timeout } = options;
/**
* The HTTP instance.
* @ignore
* @type {HTTP}
*/
this.http = new HTTP(this.events, { requestMode, timeout });
this.http = new HTTP(this.events, { fetchFunc, requestMode, timeout });
this._registerHTTPEvents();
}

Expand Down
10 changes: 9 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Emitter } from "./types";
interface HttpOptions {
timeout?: number | null;
requestMode?: RequestMode;
fetchFunc?: Function;
}

interface RequestOptions {
Expand Down Expand Up @@ -51,6 +52,7 @@ export default class HTTP {
public events?: Emitter;
public requestMode: RequestMode;
public timeout: number;
public fetchFunc: Function;

/**
* Constructor.
Expand Down Expand Up @@ -80,6 +82,12 @@ export default class HTTP {
* @type {Number}
*/
this.timeout = options.timeout || HTTP.defaultOptions.timeout!;

/**
* The fetch() function.
* @type {Function}
*/
this.fetchFunc = options.fetchFunc || fetch;
}

/**
Expand Down Expand Up @@ -112,7 +120,7 @@ export default class HTTP {
}
};
}
fetch(url, options)
this.fetchFunc(url, options)
.then(proceedWithHandler(resolve))
.catch(proceedWithHandler(reject));
});
Expand Down
55 changes: 34 additions & 21 deletions test/api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ describe("KintoClient", () => {
const api = new KintoClient(sampleRemote, { safe: true });
expect(api["_safe"]).eql(true);
});

it("should use fetchFunc option", async () => {
let called = false;
async function fetchFunc() {
called = true;
return fakeServerResponse(200, {}, {});
}
const api = new KintoClient(sampleRemote, { fetchFunc });

await api.fetchServerInfo();

expect(called).eql(true);
});
});

/** @test {KintoClient#setHeaders} */
Expand All @@ -145,7 +158,7 @@ describe("KintoClient", () => {
// Make Date#getTime always returning 1000000, for predictability
sandbox.stub(Date.prototype, "getTime").returns(1000 * 1000);
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, {}, { Backoff: "1000" }));

await api.listBuckets();
Expand All @@ -154,7 +167,7 @@ describe("KintoClient", () => {

it("should provide no remaining backoff time when none is set", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, {}, {}));

await api.listBuckets();
Expand Down Expand Up @@ -197,7 +210,7 @@ describe("KintoClient", () => {

it("should retrieve server settings on first request made", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, fakeServerInfo));

(await api.fetchServerInfo()).should.deep.equal(fakeServerInfo);
Expand All @@ -206,7 +219,7 @@ describe("KintoClient", () => {
it("should store server settings into the serverSettings property", async () => {
// api.serverSettings = { a: 1 };
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, fakeServerInfo));

await api.fetchServerInfo();
Expand All @@ -215,7 +228,7 @@ describe("KintoClient", () => {

it("should not fetch server settings if they're cached already", () => {
api.serverInfo = fakeServerInfo;
const fetchStub = sandbox.stub(globalThis as any, "fetch");
const fetchStub = sandbox.stub(api.http as any, "fetchFunc");

api.fetchServerInfo();
sinon.assert.notCalled(fetchStub);
Expand All @@ -236,7 +249,7 @@ describe("KintoClient", () => {

it("should retrieve server settings", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, fakeServerInfo));

(await api.fetchServerSettings()).should.have.property("fake").eql(true);
Expand All @@ -249,7 +262,7 @@ describe("KintoClient", () => {

it("should retrieve server capabilities", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, fakeServerInfo));

(await api.fetchServerCapabilities()).should.have
Expand All @@ -264,7 +277,7 @@ describe("KintoClient", () => {

it("should retrieve user information", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, fakeServerInfo));

(await api.fetchUser())!.should.have.property("fake").eql(true);
Expand All @@ -277,7 +290,7 @@ describe("KintoClient", () => {

it("should retrieve current API version", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, fakeServerInfo));

(await api.fetchHTTPApiVersion()).should.have.property("fake").eql(true);
Expand Down Expand Up @@ -320,7 +333,7 @@ describe("KintoClient", () => {
let requestBody: any, requestHeaders: any, fetch: sinon.SinonStub;

beforeEach(() => {
fetch = sandbox.stub(globalThis as any, "fetch");
fetch = sandbox.stub(api.http as any, "fetchFunc");
fetch.returns(fakeServerResponse(200, { responses: [] }));
});

Expand Down Expand Up @@ -448,7 +461,7 @@ describe("KintoClient", () => {
];

it("should reject on HTTP 400", async () => {
sandbox.stub(globalThis as any, "fetch").returns(
sandbox.stub(api.http as any, "fetchFunc").returns(
fakeServerResponse(400, {
error: true,
errno: 117,
Expand All @@ -460,7 +473,7 @@ describe("KintoClient", () => {
});

it("should reject on HTTP error status code", async () => {
sandbox.stub(globalThis as any, "fetch").returns(
sandbox.stub(api.http as any, "fetchFunc").returns(
fakeServerResponse(500, {
error: true,
message: "http 500",
Expand All @@ -484,7 +497,7 @@ describe("KintoClient", () => {
},
];
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, { responses }));

(await executeBatch(fixtures)).should.deep.equal(responses);
Expand All @@ -500,7 +513,7 @@ describe("KintoClient", () => {
},
];
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, { responses }));

(await executeBatch(fixtures)).should.deep.equal(responses);
Expand All @@ -515,7 +528,7 @@ describe("KintoClient", () => {
},
];
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, { responses }));

(await executeBatch(fixtures)).should.deep.equal(responses);
Expand All @@ -530,7 +543,7 @@ describe("KintoClient", () => {
},
];
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, { responses }));

(await executeBatch(fixtures)).should.deep.equal(responses);
Expand All @@ -548,7 +561,7 @@ describe("KintoClient", () => {

it("should chunk batch requests", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.onFirstCall()
.returns(
fakeServerResponse(200, {
Expand Down Expand Up @@ -578,7 +591,7 @@ describe("KintoClient", () => {
})
);
sandbox.stub(api, "fetchServerSettings").get(() => fetchServerSettings);
const fetchStub = sandbox.stub(globalThis as any, "fetch").returns(
const fetchStub = sandbox.stub(api.http as any, "fetchFunc").returns(
fakeServerResponse(200, {
responses: [],
})
Expand All @@ -589,7 +602,7 @@ describe("KintoClient", () => {

it("should map initial records to conflict objects", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.onFirstCall()
.returns(
fakeServerResponse(200, {
Expand Down Expand Up @@ -617,7 +630,7 @@ describe("KintoClient", () => {

it("should chunk batch requests concurrently", async () => {
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.onFirstCall()
.returns(
new Promise((resolve) => {
Expand Down Expand Up @@ -679,7 +692,7 @@ describe("KintoClient", () => {
},
];
sandbox
.stub(globalThis as any, "fetch")
.stub(api.http as any, "fetchFunc")
.returns(fakeServerResponse(200, { responses }));

const aggregateResponse = (await executeBatch(fixtures, {
Expand Down
2 changes: 1 addition & 1 deletion test/collection_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ describe("Collection", () => {

beforeEach(() => {
sandbox.restore();
const fetchStub = sandbox.stub(globalThis as any, "fetch");
const fetchStub = sandbox.stub(client.http as any, "fetchFunc");
fetchStub
.onCall(0)
.returns(fakeServerResponse(503, {}, { "Retry-After": "1" }));
Expand Down
Loading

0 comments on commit 15836d7

Please sign in to comment.