Skip to content

Commit

Permalink
Merge pull request #66 from libsql/custom-fetch
Browse files Browse the repository at this point in the history
Allow the user to provide custom fetch() function
  • Loading branch information
honzasp authored Jul 20, 2023
2 parents e863f45 + 1c394c0 commit 2b26a56
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"typescript": "^4.9.4"
},
"dependencies": {
"@libsql/hrana-client": "^0.4.2",
"@libsql/hrana-client": "^0.4.3",
"better-sqlite3": "^8.0.1",
"js-base64": "^3.7.5"
}
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from "@jest/globals";
import type { MatcherFunction } from "expect";
import { fetch, Request, Response } from "@libsql/isomorphic-fetch";

import "./helpers.js";

Expand Down Expand Up @@ -874,3 +875,21 @@ const hasNetworkErrors = isWs && (server == "test_v1" || server == "test_v2");
}));
}
});

(isHttp ? test : test.skip)("custom fetch", async () => {
let fetchCalledCount = 0;
function customFetch(request: Request): Promise<Response> {
expect(request).toBeInstanceOf(Request);
fetchCalledCount += 1;
return fetch(request);
}

const c = createClient({...config, fetch: customFetch});
try {
const rs = await c.execute("SELECT 42");
expect(rs.rows[0][0]).toStrictEqual(42);
expect(fetchCalledCount).toStrictEqual(1);
} finally {
c.close();
}
});
9 changes: 9 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export interface Config {
* - `"string"`: returns SQLite integers as strings.
*/
intMode?: IntMode;

/** Custom `fetch` function to use for the HTTP client.
*
* By default, the HTTP client uses `fetch` from the `@libsql/isomorphic-fetch` package, but you can pass
* your own function here. The argument to this function will be `Request` from
* `@libsql/isomorphic-fetch`, and it must return a promise that resolves to an object that is compatible
* with the Web `Response`.
*/
fetch?: Function;
}

/** Representation of integers from database as JavaScript values. See {@link Config.intMode}. */
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ExpandedConfig {
path: string;
authToken: string | undefined;
intMode: IntMode;
fetch: Function | undefined;
}

export type ExpandedScheme = "wss" | "ws" | "https" | "http" | "file";
Expand Down Expand Up @@ -99,5 +100,6 @@ export function expandConfig(config: Config, preferHttp: boolean): ExpandedConfi
path: uri.path,
authToken,
intMode,
fetch: config.fetch,
};
}
11 changes: 8 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function _createClient(config: ExpandedConfig): Client {
}

const url = encodeBaseUrl(config.scheme, config.authority, config.path);
return new HttpClient(url, config.authToken, config.intMode);
return new HttpClient(url, config.authToken, config.intMode, config.fetch);
}

const sqlCacheCapacity = 30;
Expand All @@ -46,8 +46,13 @@ export class HttpClient implements Client {
protocol: "http";

/** @private */
constructor(url: URL, authToken: string | undefined, intMode: IntMode) {
this.#client = hrana.openHttp(url, authToken);
constructor(
url: URL,
authToken: string | undefined,
intMode: IntMode,
customFetch: Function | undefined,
) {
this.#client = hrana.openHttp(url, authToken, customFetch);
this.#client.intMode = intMode;
this.protocol = "http";
}
Expand Down

0 comments on commit 2b26a56

Please sign in to comment.