diff --git a/.changes/polyfill-fetch.md b/.changes/polyfill-fetch.md new file mode 100644 index 0000000..fe9094b --- /dev/null +++ b/.changes/polyfill-fetch.md @@ -0,0 +1,5 @@ +--- +"@qubit-rs/client": patch:feat +--- + +feat: allow for polyfilling `fetch` for `http` transport (close #55) diff --git a/packages/client/src/transport/http.ts b/packages/client/src/transport/http.ts index 39719fd..6fe431e 100644 --- a/packages/client/src/transport/http.ts +++ b/packages/client/src/transport/http.ts @@ -1,10 +1,16 @@ import { build_client } from "../client"; import { parse_response } from "../jsonrpc"; -export function http(host: string): Server { +export type HttpOptions = { + fetch: typeof fetch; +}; + +export function http(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" }, diff --git a/packages/client/src/transport/index.ts b/packages/client/src/transport/index.ts index bb8531e..849d7f6 100644 --- a/packages/client/src/transport/index.ts +++ b/packages/client/src/transport/index.ts @@ -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 = (host: string) => Server;