-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.d.ts
60 lines (52 loc) · 1.65 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as React from 'react';
export interface FetchRequestProps {
url: string;
options?: RequestInit;
}
export interface FetchUpdateOptions {
ignorePreviousData: boolean;
}
export interface FetchResult<TData = any, TError = Error> {
data?: TData;
loading: boolean | null;
error?: TError;
request: FetchRequestProps;
response: Response;
fetch(
url?: string | null,
options?: RequestInit,
updateOptions?: Partial<FetchUpdateOptions>
): Promise<any>;
clearData(): void;
}
export type BodyMethods = 'arrayBuffer' | 'blob' | 'formData' | 'json' | 'text';
export interface FetchProps<TData = any, TError = Error> {
url: string;
options?: RequestInit | (() => RequestInit);
manual?: boolean;
cache?: boolean | object;
as?:
| 'auto'
| BodyMethods
| ((response: Response) => void)
| { [type: string]: (res: Response) => Promise<any> };
fetchFunction?: (url: string, options: RequestInit) => Promise<any>;
onDataChange?: (newData: TData, data: TData) => any;
onResponseChange?: (response: Response) => any;
onChange?: (result: FetchResult<TData, TError>) => void;
deps?: React.DependencyList;
}
export function useFetch<TData = any, TError = Error>(
props: FetchProps<TData, TError>
): FetchResult<TData, TError>;
export default class Fetch<TData = any, TError = Error> extends React.Component<
FetchProps<TData, TError> & {
children?: (
result: FetchResult<TData, TError>
) => React.ReactNode | React.ReactNode;
}
> {
// Passing any to FetchResult as unable to use TData
static Consumer: React.Consumer<FetchResult<any>>;
}
export const FetchContext: React.Context<FetchResult<TData, TError>>;