Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!(superfetch): merge URL with config object and change priori… #5

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor!(superfetch): merge URL with config object and change priori…
…ty for config json
  • Loading branch information
maxtsh committed Nov 21, 2023
commit 9032d7eca3739db85541d1fdc461e4c39c53b501
77 changes: 41 additions & 36 deletions src/libs/SuperFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,45 @@ import Interception from "./Interceptions";

export type URLType = RequestInfo | URL;

export type RequestProps = {
URL: URLType;
config?: RequestInit;
};
export type RequestConfig = {
url: URLType;
} & Partial<RequestInit>;

export type InterceptorRequestFn = (requestProps: RequestProps) => RequestProps;
export type InterceptorRequestFn = (
requestConfig: RequestConfig,
) => RequestConfig;
export type InterceptorResponseFn = (response: Response) => Response;

export type SuperFetchConfigs = {
timeout?: number;
baseURL?: string;
requestConfig?: RequestInit;
defaultReqInterceptors?: InterceptorRequestFn[];
defaultResInterceptors?: InterceptorResponseFn[];
};
export type SuperFetchConfigs = Partial<{
timeout: number;
baseURL: string;
requestConfig: Partial<RequestConfig>;
defaultReqInterceptors: InterceptorRequestFn[];
defaultResInterceptors: InterceptorResponseFn[];
}>;

let signalTimeoutId: ReturnType<typeof setTimeout> | number | null = null;

class SuperFetch {
defaultConfigs: SuperFetchConfigs = { timeout: 150000 };
defaultConfig: SuperFetchConfigs;
baseFetch = globalThis.fetch;
interceptors = {
request: new Interception<InterceptorRequestFn>(),
response: new Interception<InterceptorResponseFn>(),
};

constructor(defaultSuperFetchConfigs: SuperFetchConfigs = {}) {
constructor(
defaultSuperFetchConfig: SuperFetchConfigs = { timeout: 150000 },
) {
if (!this.baseFetch) throw new Error("Fetch API is not available!");

this.baseFetch = this.baseFetch.bind(globalThis);

this.defaultConfigs = defaultSuperFetchConfigs;
this.defaultConfig = defaultSuperFetchConfig;

this.loadDefaultInterceptors({
defaultReqInterceptors: defaultSuperFetchConfigs.defaultReqInterceptors,
defaultResInterceptors: defaultSuperFetchConfigs.defaultResInterceptors,
defaultReqInterceptors: defaultSuperFetchConfig.defaultReqInterceptors,
defaultResInterceptors: defaultSuperFetchConfig.defaultResInterceptors,
});
}

Expand All @@ -60,11 +63,11 @@ class SuperFetch {
}
}

private createRequestURL(URL: URLType) {
const newURL = this.defaultConfigs.baseURL
? `${this.defaultConfigs.baseURL}` + URL
: URL;
return newURL;
private createRequestURL(url: URLType) {
const newurl = this.defaultConfig?.baseURL
? this.defaultConfig.baseURL + url
: url;
return newurl;
}

private timeoutController() {
Expand All @@ -74,35 +77,37 @@ class SuperFetch {

signalTimeoutId = setTimeout(
() => controller.abort(),
this.defaultConfigs.timeout,
this.defaultConfig.timeout,
);

return controller;
}

async request({
URL,
config = this.defaultConfigs.requestConfig,
}: RequestProps) {
let reqURL = this.createRequestURL(URL);
let reqConfig = config;
async request(config: RequestConfig) {
let requestConfig: RequestConfig = {
...this.defaultConfig.requestConfig,
...config,
headers: {
...this.defaultConfig.requestConfig?.headers,
...config.headers,
},
url: this.createRequestURL(config.url),
};

const reqInterceptors = this.interceptors.request.handlers;
const resInterceptors = this.interceptors.response.handlers;

if (reqInterceptors.length > 0) {
reqInterceptors.forEach((reqInterceptor) => {
const newProps = reqInterceptor({ URL: reqURL, config: reqConfig });
reqURL = newProps.URL;
reqConfig = newProps.config;
requestConfig = reqInterceptor(requestConfig);
});
}

let response = await this.baseFetch(reqURL, {
...reqConfig,
signal: !reqConfig?.signal
let response = await this.baseFetch(requestConfig.url, {
...requestConfig,
signal: !requestConfig?.signal
? this.timeoutController().signal
: reqConfig.signal,
: requestConfig.signal,
});

if (signalTimeoutId) clearTimeout(signalTimeoutId);
Expand Down