Created
June 7, 2018 13:24
-
-
Save msokk/2ba91ebead1bfdd83639a02c9f998601 to your computer and use it in GitHub Desktop.
react-request definitions
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
declare module 'react-request' { | |
interface FetchOptions extends RequestInit { | |
url: string; | |
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD'; | |
} | |
type ResponseType = | |
| 'json' | |
| 'text' | |
| 'blob' | |
| 'arrayBuffer' | |
| 'formData' | |
| ((response: Response) => string); | |
/** | |
* Fetch render prop props | |
*/ | |
export interface FetchRenderProps<D = any> { | |
fetching: boolean; | |
failed: boolean; | |
error: Error | null; | |
response: Response; | |
data: D | null; | |
doFetch: (options?: FetchOptions) => void; | |
url: string; | |
requestName: string; | |
requestKey: string; | |
} | |
/** | |
* Props for <Fetch /> | |
*/ | |
export interface FetchProps<D> extends FetchOptions { | |
children?: ((props: FetchRenderProps<D>) => React.ReactNode); | |
lazy?: boolean; | |
beforeFetch?: (params: { url: string; init: RequestInit; requestKey: string }) => void; | |
afterFetch?: ( | |
params: { | |
url: string; | |
init: RequestInit; | |
requestKey: string; | |
response: Response; | |
data: D | null; | |
error: Error | null; | |
didUnmount: boolean; | |
} | |
) => void; | |
onResponse?: (error: Error, response: Response) => void; | |
transformData?: (data: D | null) => D; | |
responseType?: ResponseType; | |
requestName?: string; | |
fetchPolicy?: 'cache-first' | 'cache-and-network' | 'network-only' | 'cache-only'; | |
cacheResponse?: boolean; | |
dedupe?: boolean; | |
requestKey?: string; | |
} | |
/** | |
* Helper to type "fetch components" | |
*/ | |
export interface FetchSFCProps<D> extends FetchProps<D> { | |
url?: string; | |
} | |
/** | |
* A component for making a single HTTP request. It accepts every value of init and input from the fetch() API as a prop, in addition to a few other things. | |
*/ | |
export class Fetch extends React.Component<FetchProps> {} | |
interface DedupeOptions { | |
responseType?: ResponseType; | |
requestKey?: string; | |
dedupe?: boolean; | |
} | |
/** | |
* This is the fetchDedupe export from the Fetch Dedupe library. Fetch Dedupe powers the request deduplication in React Request. | |
* | |
* If, for whatever reason, you need to make a standalone HTTP request outside of the <Fetch /> component, then you can use this with confidence that you won't send a duplicate request. | |
* | |
* For more, refer to the documentation of fetch-dedupe. | |
*/ | |
export function fetchDedupe( | |
input?: Request | string, | |
init?: RequestInit, | |
dedupeOptions?: DedupeOptions | |
): Promise<Response>; | |
/** | |
* Generates a request key. All of the values are optional. | |
* | |
* This method comes from fetch-dedupe. | |
*/ | |
export function getRequestKey(props?: { | |
url?: string; | |
method?: string; | |
responseType?: string; | |
body?: string; | |
}): string; | |
/** | |
* Return a Boolean representing if a request for requestKey is in flight or not. | |
* | |
* This method comes from fetch-dedupe. | |
* @param requestKey | |
*/ | |
export function isRequestInFlight(requestKey: string): boolean; | |
/** | |
* Wipes the cache of deduped requests. Mostly useful for testing. | |
* This method comes from fetch-dedupe. | |
* | |
* Note: this method is not safe to use in application code. | |
*/ | |
export function clearRequestCache(): void; | |
/** | |
* Wipes the cache of cached responses. Mostly useful for testing. | |
* | |
* Note: this method is not safe to use in application code. | |
*/ | |
export function clearResponseCache(): void; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment