This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathContext.tsx
56 lines (51 loc) · 1.7 KB
/
Context.tsx
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
import noop from "lodash/noop";
import * as React from "react";
import { ResolveFunction } from "./Get";
export interface RestfulReactProviderProps<T = any> {
/** The backend URL where the RESTful resources live. */
base: string;
/**
* A function to resolve data return from the backend, most typically
* used when the backend response needs to be adapted in some way.
*/
resolve?: ResolveFunction<T>;
/**
* Options passed to the fetch request.
*/
requestOptions?: (() => Partial<RequestInit>) | Partial<RequestInit>;
/**
* Trigger on each error.
* For `Get` and `Mutation` calls, you can also call `retry` to retry the exact same request.
* Please note that it's quite hard to retrieve the response data after a retry mutation in this case.
* Depending of your case, it can be easier to add a `localErrorOnly` on your `Mutate` component
* to deal with your retry locally instead of in the provider scope.
*/
onError?: (err: any, retry?: () => Promise<T | null>) => void;
}
const { Provider, Consumer: RestfulReactConsumer } = React.createContext<Required<RestfulReactProviderProps>>({
base: "",
resolve: (data: any) => data,
requestOptions: {},
onError: noop,
});
export interface InjectedProps {
onError: RestfulReactProviderProps["onError"];
}
export default class RestfulReactProvider<T> extends React.Component<RestfulReactProviderProps<T>> {
public render() {
const { children, ...value } = this.props;
return (
<Provider
value={{
onError: noop,
resolve: (data: any) => data,
requestOptions: {},
...value,
}}
>
{children}
</Provider>
);
}
}
export { RestfulReactConsumer };