Skip to content

Commit

Permalink
Remove ofetch from constructor argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Denoder committed Dec 19, 2023
1 parent 31d4d9c commit 8ca0e02
Show file tree
Hide file tree
Showing 6 changed files with 1,836 additions and 3,726 deletions.
5,483 changes: 1,794 additions & 3,689 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@refactorjs/ofetch",
"version": "1.0.10",
"version": "1.0.13",
"description": "An extension to ofetch",
"repository": {
"type": "git",
Expand Down Expand Up @@ -32,9 +32,9 @@
"ofetch": "^1.3.3"
},
"devDependencies": {
"@types/node": "^18",
"@types/node": "^20",
"standard-version": "^9.5.0",
"typescript": "^5.2.2",
"typescript": "^5.3.3",
"unbuild": "^2.0.0"
},
"publishConfig": {
Expand All @@ -45,6 +45,7 @@
},
"homepage": "https://github.com/refactorjs/ofetch#readme",
"keywords": [
"fetch",
"http",
"ohmyfetch",
"ofetch"
Expand Down
22 changes: 11 additions & 11 deletions src/adapters/InterceptorManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FetchInterceptorOptions } from '../types'
import type { FetchInterceptorManager, FetchInterceptorOptions } from '../types'

export default class InterceptorManager<V> {
handlers: Array<any>
export default class InterceptorManager<V> implements FetchInterceptorManager<V> {
handlers: Array<FetchInterceptorOptions | null>;

constructor() {
this.handlers = []
Expand All @@ -10,17 +10,17 @@ export default class InterceptorManager<V> {
/**
* Add a new interceptor to the stack
*
* @param { (value: V) => T | Promise<T> } fulfilled The function to handle `then` for a `Promise`
* @param { (error: any) => any } rejected The function to handle `reject` for a `Promise`
* @param { (value: V) => T | Promise<T> } onFulfilled The function to handle `then` for a `Promise`
* @param { (error: any) => any } onRejected The function to handle `reject` for a `Promise`
*
* @return { number } An ID used to remove interceptor later
*/
use<T = V>(fulfilled: (value: V) => T | Promise<T>, rejected: (error: any) => any, options: FetchInterceptorOptions): number {
use<T = V>(onFulfilled: (value: any) => T | Promise<T>, onRejected: (error: any) => any, options: Omit<FetchInterceptorOptions, 'onFulfilled' | 'onRejected'>): number {
this.handlers.push({
fulfilled,
rejected,
onFulfilled,
onRejected,
synchronous: options ? options.synchronous : false,
runWhen: options ? options.runWhen : null
runWhen: options ? options.runWhen : undefined
});

return this.handlers.length - 1;
Expand Down Expand Up @@ -60,8 +60,8 @@ export default class InterceptorManager<V> {
*
* @returns { void }
*/
forEach(fn: (handler: any) => void): void {
this.handlers.forEach((handler: any) => {
forEach(fn: (handler: FetchInterceptorOptions) => void): void {
this.handlers.forEach((handler) => {
if (handler !== null) {
fn(handler);
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './ofetch'
export * from './types'
export * from './ofetch';
export * from './types';
36 changes: 19 additions & 17 deletions src/ofetch.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { FetchConfig, FetchInterceptorManager, RequestInfo, MakeRequired } from './types'
import { $fetch as ofetch, type $Fetch, type SearchParameters, type FetchResponse } from 'ofetch'
import InterceptorManager from './adapters/InterceptorManager'
import { getCookie, getCookies } from './utils'
import { defu } from 'defu'
import type { FetchConfig, FetchInterceptorManager, RequestInfo, MakeRequired } from './types';
import { $fetch as ofetch, type $Fetch, type SearchParameters, type FetchResponse } from 'ofetch';
import InterceptorManager from './adapters/InterceptorManager';
import { getCookie, getCookies } from './utils';
import { defu } from 'defu';

export class FetchInstance {
#$fetch: $Fetch;
#configDefaults: FetchConfig;

interceptors: {
request: FetchInterceptorManager<FetchConfig>
request: FetchInterceptorManager<FetchConfig>;
response: FetchInterceptorManager<Promise<any>>;
};

constructor(config: FetchConfig = {}, instance = ofetch) {
constructor(config: FetchConfig = {}) {
this.#configDefaults = {
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
Expand All @@ -25,7 +25,7 @@ export class FetchInstance {
response: new InterceptorManager()
}

this.#$fetch = instance
this.#$fetch = ofetch
this.#createMethods()
}

Expand Down Expand Up @@ -70,19 +70,19 @@ export class FetchInstance {
const requestInterceptorChain: Array<any> = [];
let synchronousRequestInterceptors = true;

this.interceptors.request.forEach((interceptor: any) => {
if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
this.interceptors.request.forEach((interceptor) => {
if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config as FetchConfig) === false) {
return;
}

synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous as boolean;
requestInterceptorChain.unshift(interceptor.onFulfilled, interceptor.onRejected);
});

const responseInterceptorChain: Array<any> = [];
this.interceptors.response.forEach((interceptor: any) => {
this.interceptors.response.forEach((interceptor) => {

responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
responseInterceptorChain.push(interceptor.onFulfilled, interceptor.onRejected);
});

let promise: Promise<any>;
Expand Down Expand Up @@ -137,7 +137,7 @@ export class FetchInstance {
return promise;
}

#dispatchRequest(config: FetchConfig): Promise<FetchResponse<any> | any> {
async #dispatchRequest(config: FetchConfig): Promise<FetchResponse<any> | any> {
const controller = new AbortController();
const timeoutSignal = setTimeout(() => controller.abort(), config.timeout);
const $ofetch = this.getFetch()
Expand All @@ -159,13 +159,15 @@ export class FetchInstance {
clearTimeout(timeoutSignal);

if (config.native) {
delete config.native
return fetch(config.url, {
signal: controller.signal,
...config as RequestInit
})
}

if (config.raw) {
delete config.raw
return $ofetch.raw(config.url, {
signal: controller.signal,
...config
Expand Down Expand Up @@ -283,9 +285,9 @@ function serializeQuery(params: SearchParameters) {
return {}
}

export function createInstance(config?: FetchConfig, instance?: $Fetch): FetchInstance {
export function createInstance(config?: FetchConfig): FetchInstance {
// Create new Fetch instance
return new FetchInstance(config, instance)
return new FetchInstance(config)
}

export const $fetch = createInstance
10 changes: 6 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import type { FetchOptions as Options } from 'ofetch'
import type { FetchOptions as Options } from 'ofetch';

export type RequestInfo = string | FetchConfig
export type RequestInfo = string | FetchConfig;

export type MakeRequired<Type, Key extends keyof Type> = Omit<Type, Key> & Required<Pick<Type, Key>>;
export type MakeOptional<Type, Key extends keyof Type> = Omit<Type, Key> & Partial<Pick<Type, Key>>;

export interface FetchInterceptorOptions {
onFulfilled?: (value: any) => any | Promise<any>;
onRejected?: (error: any) => any;
synchronous?: boolean;
runWhen?: (config: FetchConfig) => boolean;
runWhen?: (config: FetchConfig) => boolean | null;
}

export interface FetchInterceptorManager<V> {
use<T = V>(onFulfilled?: (value: V) => T | Promise<T>, onRejected?: (error: any) => any, options?: FetchInterceptorOptions): number;
eject(id: number): void;
forEach(fn: (handler: any) => void): void;
forEach(fn: (handler: FetchInterceptorOptions) => void): void;
}

export interface FetchConfig extends Options {
Expand Down

0 comments on commit 8ca0e02

Please sign in to comment.