Skip to content

Commit

Permalink
feat: make proxies work with new Retcher instance for each request
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed Jan 3, 2025
1 parent 3697bf5 commit d28960c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/retch-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ export { Browser } from 'retch-http';
* A HTTP client implementation based on the `retch-http` library.
*/
export class RetchHttpClient implements BaseHttpClient {
private retcher: Retcher;
private retcherOptions: RetcherOptions;
private maxRedirects: number;
private followRedirects: boolean;

constructor(options?: RetcherOptions & { maxRedirects?: number }) {
this.retcher = new Retcher({
...(options ?? {}),
followRedirects: false, // Disable the redirection handling from `retch-http`
});
constructor(options?: Omit<RetcherOptions, 'proxyUrl'> & { maxRedirects?: number }) {
this.retcherOptions = options ?? {};

this.followRedirects = options?.followRedirects ?? true;
this.maxRedirects = options?.maxRedirects ?? 10;
this.followRedirects = options?.followRedirects ?? true;
}

/**
Expand Down Expand Up @@ -96,11 +93,16 @@ export class RetchHttpClient implements BaseHttpClient {
const headers = request.headers !== undefined ? this.flattenHeaders(request.headers) : undefined;
const body = request.body !== undefined ? await this.intoRetcherBody(request.body) : undefined;

const response = await this.retcher.fetch(url, {
const retcher = new Retcher({
...this.retcherOptions,
proxyUrl: request.proxyUrl,
followRedirects: false,
});

const response = await retcher.fetch(url, {
method: request.method as HttpMethod,
headers,
body: body as string,
// fix - respect the proxy url!
});

if (this.followRedirects && response.status >= 300 && response.status < 400) {
Expand Down

0 comments on commit d28960c

Please sign in to comment.