microsoft/typed-rest-client

Discussion: use with fetch API

jamesarosen opened this issue · 5 comments

Environment

Node version: 18.x
typed-rest-client version: 1.8.11

Issue Description

Popular libraries like make-fetch-happen and cache-control-fetch rely on the fetch API. This API is new in Node 18, but has good polyfill support through node-fetch or minipass-fetch.

It is currently possible to override RestClient.client.requestRawWithCallback to use fetch, but the method is complex and the interface between fetch/Request/Response and http/ClientRequest/IncomingMessage is nuanced. I'd love to see a standard (and well-tested!) community solution for this so we can all get the implementation right.

In the meantime, scarlett is a possible alternative for folks who want a typed fetch-based REST library.

Hi @jamesarosen, thank's for the suggestions, we will take a look what is the best way to implement the solution when we will have enough capacity, but any thoughts, discussions and PRs are welcome!

I think the simplest thing would be to add an optional FetchBasedHttpClient to this package. Clients would opt into it with:

import { RestClient, FetchBasedHttpClient } from "typed-rest-client";

const myApi = new RestClient({ client: new FetchBasedHttpClient() });

That would require adding an optional package dependency on either node-fetch or minipass-fetch. The RestClient constructor doesn't currently accept a client, but it certainly could, especially since client is part of the class's public API.

Another alternative would be a separate package like microsoft/typed-fetch-client. That seems like more overhead than is necessary.

This issue has had no activity in 90 days. Please comment if it is not actually stale

This issue has had no activity in 90 days. Please comment if it is not actually stale