feat: add standard Fetch API compatible interface#24
Open
Lqm1 wants to merge 4 commits into
Open
Conversation
The new `fetch()` function wraps impers' HTTP engine with a standard Fetch API interface, supporting `RequestInit` and impers-specific extensions like `impersonate`, `params`, `proxy`, `auth`, `timeout`, and more. Includes shared session management, an example file, and comprehensive tests.
…quest Each `fetch()` call now creates a new `Session` instance and closes it after the request completes, preventing cookies and headers from leaking across standalone calls. The underlying CurlMulti connection pool remains shared via `getSharedMulti()`. - Remove `shared-session.ts` module and inline its logic into `public.ts` - Delete the `getFetchSession()` helper from `fetch.ts` - Add test verifying cookies are not persisted between `fetch()` calls
…cause Remove the `typeErrorWithCause` helper and use the standard `TypeError` constructor with the `cause` option directly. This simplifies the code and aligns with modern JavaScript error handling patterns. To support the `cause` option, the TypeScript compilation target is bumped from ES2020 to ES2022 in `tsconfig.json`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Proposal-oriented PR adding a standard Fetch API compatible
fetch()on top of impers' HTTP engine, so impers can be plugged in as a custom fetch for clients TypeScript devs already use (ky,axios, etc.) while keeping impers' browser impersonation / HTTP/2-3 fingerprinting.Why
kyandaxiosaccept a custom fetch:Users keep familiar ergonomics and transparently gain fingerprint spoofing. Even standalone, a Fetch-compatible interface is something TypeScript users expect.
Changes
src/http/fetch.ts—fetch(input, init?):string | URL | Requestinput, standardRequestInit, returns a real globalResponse(.status/.ok/.headers/.text()/.json()/.arrayBuffer()/.clone()/bodyUsed). Network/SSL/timeout/abort errors →TypeErrorwith original error incause(Fetch semantics); 4xx/5xx returned, not rejected.redirect: follow|manual|errorhonored,maxRedirectsdefaults to 20. impers extensions viaImpersRequestInit; freshSessionper call (no cookie/state leakage, shared connection pool).src/public.ts— exportsfetch+ImpersRequestInit.tests/fetch.test.ts— GET/POST, body types,Requestinput, redirects, errors, body semantics, impers extensions.examples/fetch-api.ts— runnable sample.tsconfig.json—targetES2020→ES2022(required fornew TypeError(msg, { cause })).Limitations
Inherited from the public
Responseconstructor:response.url/.redirected/.typeare not populated. UnsupportedRequestInitfields silently ignored:credentials,mode,integrity,keepalive,window,duplex,referrerPolicy,referrer.Test plan
npm testnpm run lintnpm run buildNotes for review
fetchvs. named-only export?ImpersRequestInitextensions flat onRequestInitvs. animperssub-object?