Skip to content

Commit

Permalink
feat: polyfill fetch for http client (#56)
Browse files Browse the repository at this point in the history
close #55
  • Loading branch information
andogq authored Jun 11, 2024
2 parents d9129ca + f8ff07b commit d45f9fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/polyfill-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@qubit-rs/client": patch:feat
---

feat: allow for polyfilling `fetch` for `http` transport (close #55)
10 changes: 8 additions & 2 deletions packages/client/src/transport/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { build_client } from "../client";
import { parse_response } from "../jsonrpc";

export function http<Server>(host: string): Server {
export type HttpOptions = {
fetch: typeof fetch;
};

export function http<Server>(host: string, http_options?: HttpOptions): Server {
const fetch_impl = http_options?.fetch || fetch;

return build_client({
request: async (_id, payload) => {
const res = await fetch(host, {
const res = await fetch_impl(host, {
method: "POST",
mode: "cors",
headers: { "Content-Type": "application/json" },
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/transport/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { ws } from "./ws";
export { http } from "./http";
export { http, type HttpOptions } from "./http";
export type { SocketOptions } from "../util";

export type ClientBuilder<Server> = (host: string) => Server;

0 comments on commit d45f9fc

Please sign in to comment.