Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import type { DFetchRequestConfig, DFetchResponseType, Data } from './types.ts';import { HttpMethods } from './types.ts';import { initURL, initFetchConfig, setFetchTimeout } from './utils.ts';
export default class DFetch {
constructor(private _config: DFetchRequestConfig = {}) {}
static async #request<T = unknown>(config: DFetchRequestConfig): Promise<DFetchResponseType<T>>{ const url = initURL(config); const fetchConfig = initFetchConfig(config); const timeoutId = setFetchTimeout(fetchConfig, config.timeout); const fetchResponse = await fetch(url, fetchConfig); timeoutId && clearInterval(timeoutId); let data: T | undefined = undefined; try { data = await fetchResponse.clone().json(); } catch (ex) { data = await fetchResponse.clone().text() as unknown as T; }
return { status: fetchResponse.status, statusText: fetchResponse.statusText, data, headers: fetchResponse.headers, config } } async request<T = unknown>(config: DFetchRequestConfig): Promise<DFetchResponseType<T>>{ return DFetch.#request({ ...this._config, ...config }); }
static async request<T = unknown>(config: DFetchRequestConfig): Promise<DFetchResponseType<T>>{ return DFetch.#request(config); } async get<T = unknown>(url: string, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config, method: HttpMethods.GET }); }
async post<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.POST, data }); }
async put<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.PUT, data }); }
async delete<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.DELETE, data }); }
async options<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.OPTIONS, data }); }
async head<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.HEAD, data }); }
async connect<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.CONNECT, data }); }
async trace<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.TRACE, data }); }
async patch<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.PATCH, data }); }
static async get<T = unknown>(url: string, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config, method: HttpMethods.GET }); }
static async post<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.POST, data }); }
static async put<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.PUT, data }); }
static async delete<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.DELETE, data }); }
static async options<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.OPTIONS, data }); }
static async head<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.HEAD, data }); }
static async connect<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.CONNECT, data }); }
static async trace<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.TRACE, data }); }
static async patch<T = unknown>(url: string, data?: Data, config: DFetchRequestConfig = {}): Promise<DFetchResponseType<T>> { return this.request({ url, ...config , method: HttpMethods.PATCH, data }); }}