Skip to content

Commit

Permalink
Added fetch proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Sep 18, 2024
1 parent 7e80d01 commit f488dba
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Agent } from "http";

type IBuilder = (r: Request) => Request;

type IRequest = { url?: string, log?: (...a: any[]) => void, logError?: (...a: any[]) => void } & RequestInit;
type IRequest = { fetchProxy?: any, url?: string, log?: (...a: any[]) => void, logError?: (...a: any[]) => void } & RequestInit;

class FetchBuilder {

Expand Down Expand Up @@ -186,14 +186,18 @@ class FetchBuilder {
return this.append({ dispatcher });
}

public withFetchProxy(fetchProxy: any) {
return this.append({ fetchProxy });
}

public async execute<T>(ensureSuccess = true,
postProcessor: (r: Response) => T | Promise<T>): Promise<{ result: T, headers: any, status: number }> {

let { log, logError } = this.request;
try {

const { headers } = this.request;
const r = await fetch(this.request.url, this.request);
const { headers, fetchProxy } = this.request;
const r = await (fetchProxy ?? fetch)(this.request.url, this.request);
if (ensureSuccess) {
if (r.status > 300) {
log = logError;
Expand Down

0 comments on commit f488dba

Please sign in to comment.