-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: more type and naming refactors
- Loading branch information
1 parent
891f1d0
commit b42d3f1
Showing
10 changed files
with
182 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import type { | ||
AxiosDefaults, | ||
AxiosInterceptorManager, | ||
AxiosRequestConfig, | ||
AxiosResponse | ||
} from 'axios'; | ||
import type { CacheInstance, CacheProperties } from './cache'; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body was | ||
*/ | ||
export type CacheAxiosResponse<R, D> = AxiosResponse<R, D> & { | ||
config: CacheRequestConfig<D>; | ||
|
||
/** | ||
* The id used for this request. if config specified an id, the id | ||
* will be returned | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* A simple boolean to check whether this request was cached or not | ||
*/ | ||
cached: boolean; | ||
}; | ||
|
||
/** | ||
* Options that can be overridden per request | ||
* | ||
* @template D The type for the request body | ||
*/ | ||
export type CacheRequestConfig<D> = AxiosRequestConfig<D> & { | ||
/** | ||
* An id for this request, if this request is used in cache, only | ||
* the last request made with this id will be returned. | ||
* | ||
* @default undefined | ||
*/ | ||
id?: string; | ||
|
||
/** | ||
* All cache options for the request. | ||
* | ||
* False means ignore everything about cache, for this request. | ||
*/ | ||
cache?: false | Partial<CacheProperties>; | ||
}; | ||
|
||
/** | ||
* Same as the AxiosInstance but with CacheRequestConfig as a config | ||
* type and CacheAxiosResponse as response type. | ||
* | ||
* @see Axios | ||
* @see CacheRequestConfig | ||
* @see CacheInstance | ||
*/ | ||
export interface AxiosCacheInstance extends CacheInstance { | ||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
<R = unknown, D = any>(config: CacheRequestConfig<D>): Promise< | ||
CacheAxiosResponse<R, D> | ||
>; | ||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
<R = unknown, D = any>(url: string, config?: CacheRequestConfig<D>): Promise< | ||
CacheAxiosResponse<R, D> | ||
>; | ||
|
||
defaults: AxiosDefaults<any> & { | ||
cache: CacheProperties; | ||
}; | ||
|
||
interceptors: { | ||
request: AxiosInterceptorManager<CacheRequestConfig<any>>; | ||
response: AxiosInterceptorManager<CacheAxiosResponse<never, any>>; | ||
}; | ||
|
||
/** | ||
* @template D The type that the request body use | ||
*/ | ||
getUri<D>(config?: CacheRequestConfig<D>): string; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
request<R = unknown, D = any>( | ||
config: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
get<R = unknown, D = any>( | ||
url: string, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
delete<R = unknown, D = any>( | ||
url: string, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
head<R = unknown, D = any>( | ||
url: string, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
options<R = unknown, D = any>( | ||
url: string, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
post<R = unknown, D = any>( | ||
url: string, | ||
data?: D, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
put<R = unknown, D = any>( | ||
url: string, | ||
data?: D, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
|
||
/** | ||
* @template R The type returned by this response | ||
* @template D The type that the request body use | ||
*/ | ||
patch<R = unknown, D = any>( | ||
url: string, | ||
data?: D, | ||
config?: CacheRequestConfig<D> | ||
): Promise<CacheAxiosResponse<R, D>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.