-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
Showing
5 changed files
with
136 additions
and
112 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
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 |
---|---|---|
@@ -1,4 +1,127 @@ | ||
// -------------------------- | ||
// $fetch API | ||
// -------------------------- | ||
|
||
export interface $Fetch { | ||
<T = any, R extends ResponseType = "json">( | ||
request: FetchRequest, | ||
options?: FetchOptions<R> | ||
): Promise<MappedResponseType<R, T>>; | ||
raw<T = any, R extends ResponseType = "json">( | ||
request: FetchRequest, | ||
options?: FetchOptions<R> | ||
): Promise<FetchResponse<MappedResponseType<R, T>>>; | ||
native: Fetch; | ||
create(defaults: FetchOptions): $Fetch; | ||
} | ||
|
||
// -------------------------- | ||
// Context | ||
// -------------------------- | ||
|
||
export interface FetchContext<T = any, R extends ResponseType = ResponseType> { | ||
request: FetchRequest; | ||
// eslint-disable-next-line no-use-before-define | ||
options: FetchOptions<R>; | ||
response?: FetchResponse<T>; | ||
error?: Error; | ||
} | ||
|
||
// -------------------------- | ||
// Options | ||
// -------------------------- | ||
|
||
export interface FetchOptions<R extends ResponseType = ResponseType> | ||
extends Omit<RequestInit, "body"> { | ||
baseURL?: string; | ||
body?: RequestInit["body"] | Record<string, any>; | ||
ignoreResponseError?: boolean; | ||
params?: Record<string, any>; | ||
query?: Record<string, any>; | ||
parseResponse?: (responseText: string) => any; | ||
responseType?: R; | ||
|
||
/** | ||
* @experimental Set to "half" to enable duplex streaming. | ||
* Will be automatically set to "half" when using a ReadableStream as body. | ||
* https://fetch.spec.whatwg.org/#enumdef-requestduplex | ||
*/ | ||
duplex?: "half" | undefined; | ||
|
||
/** timeout in milliseconds */ | ||
timeout?: number; | ||
|
||
retry?: number | false; | ||
/** Delay between retries in milliseconds. */ | ||
retryDelay?: number; | ||
/** Default is [408, 409, 425, 429, 500, 502, 503, 504] */ | ||
retryStatusCodes?: number[]; | ||
|
||
onRequest?(context: FetchContext): Promise<void> | void; | ||
onRequestError?( | ||
context: FetchContext & { error: Error } | ||
): Promise<void> | void; | ||
onResponse?( | ||
context: FetchContext & { response: FetchResponse<R> } | ||
): Promise<void> | void; | ||
onResponseError?( | ||
context: FetchContext & { response: FetchResponse<R> } | ||
): Promise<void> | void; | ||
} | ||
|
||
export interface CreateFetchOptions { | ||
// eslint-disable-next-line no-use-before-define | ||
defaults?: FetchOptions; | ||
fetch?: Fetch; | ||
Headers?: typeof Headers; | ||
AbortController?: typeof AbortController; | ||
} | ||
|
||
// -------------------------- | ||
// Response Types | ||
// -------------------------- | ||
|
||
export interface ResponseMap { | ||
blob: Blob; | ||
text: string; | ||
arrayBuffer: ArrayBuffer; | ||
stream: ReadableStream<Uint8Array>; | ||
} | ||
|
||
export type ResponseType = keyof ResponseMap | "json"; | ||
|
||
export type MappedResponseType< | ||
R extends ResponseType, | ||
JsonType = any, | ||
> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType; | ||
|
||
export interface FetchResponse<T> extends Response { | ||
_data?: T; | ||
} | ||
|
||
// -------------------------- | ||
// Error | ||
// -------------------------- | ||
|
||
export interface IFetchError<T = any> extends Error { | ||
request?: FetchRequest; | ||
options?: FetchOptions; | ||
response?: FetchResponse<T>; | ||
data?: T; | ||
status?: number; | ||
statusText?: string; | ||
statusCode?: number; | ||
statusMessage?: string; | ||
} | ||
|
||
// -------------------------- | ||
// Other types | ||
// -------------------------- | ||
|
||
export type Fetch = typeof globalThis.fetch; | ||
export type RequestInfo = globalThis.RequestInfo; | ||
export type RequestInit = globalThis.RequestInit; | ||
export type Response = globalThis.Response; | ||
|
||
export type FetchRequest = RequestInfo; | ||
|
||
export interface SearchParameters { | ||
[key: string]: any; | ||
} |
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