forked from contiamo/restful-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutate.tsx
192 lines (171 loc) · 5.56 KB
/
Mutate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as React from "react";
import url from "url";
import RestfulReactProvider, { InjectedProps, RestfulReactConsumer, RestfulReactProviderProps } from "./Context";
import { GetState } from "./Get";
import { processResponse } from "./util/processResponse";
/**
* An enumeration of states that a fetchable
* view could possibly have.
*/
export interface States<TData, TError> {
/** Is our view currently loading? */
loading: boolean;
/** Do we have an error in the view? */
error?: GetState<TData, TError>["error"];
}
/**
* Meta information returned to the fetchable
* view.
*/
export interface Meta {
/** The entire response object passed back from the request. */
response: Response | null;
/** The absolute path of this request. */
absolutePath: string;
}
/**
* Props for the <Mutate /> component.
*/
export interface MutateCommonProps {
/**
* The path at which to request data,
* typically composed by parents or the RestfulProvider.
*/
path: string;
/**
* What HTTP verb are we using?
*/
verb: "POST" | "PUT" | "PATCH" | "DELETE";
/**
* An escape hatch and an alternative to `path` when you'd like
* to fetch from an entirely different URL.
*
*/
base?: string;
/** Options passed into the fetch call. */
requestOptions?: RestfulReactProviderProps["requestOptions"];
/**
* Don't send the error to the Provider
*/
localErrorOnly?: boolean;
}
export interface MutateWithDeleteProps<TData, TError> extends MutateCommonProps {
verb: "DELETE";
/**
* A function that recieves a mutation function, along with
* some metadata.
*
* @param actions - a key/value map of HTTP verbs, aliasing destroy to DELETE.
*/
children: (
mutate: (resourceId?: string | {}) => Promise<TData>,
states: States<TData, TError>,
meta: Meta,
) => React.ReactNode;
}
export interface MutateWithOtherVerbProps<TData, TError> extends MutateCommonProps {
verb: "POST" | "PUT" | "PATCH";
/**
* A function that recieves a mutation function, along with
* some metadata.
*
* @param actions - a key/value map of HTTP verbs, aliasing destroy to DELETE.
*/
children: (
mutate: (body?: string | {}) => Promise<TData>,
states: States<TData, TError>,
meta: Meta,
) => React.ReactNode;
}
export type MutateProps<TData, TError> = MutateWithDeleteProps<TData, TError> | MutateWithOtherVerbProps<TData, TError>;
/**
* State for the <Mutate /> component. These
* are implementation details and should be
* hidden from any consumers.
*/
export interface MutateState<TData, TError> {
response: Response | null;
error: GetState<TData, TError>["error"];
loading: boolean;
}
/**
* The <Mutate /> component without Context. This
* is a named class because it is useful in
* debugging.
*/
class ContextlessMutate<TData, TError> extends React.Component<
MutateProps<TData, TError> & InjectedProps,
MutateState<TData, TError>
> {
public readonly state: Readonly<MutateState<TData, TError>> = {
response: null,
loading: false,
error: null,
};
public static defaultProps = {
base: "",
path: "",
};
public mutate = async (body?: string | {}, mutateRequestOptions?: RequestInit) => {
const { base, path, verb, requestOptions: providerRequestOptions } = this.props;
this.setState(() => ({ error: null, loading: true }));
const requestPath =
verb === "DELETE" && typeof body === "string"
? url.resolve(base!, url.resolve(path, body))
: url.resolve(base!, path);
const request = new Request(requestPath, {
method: verb,
body: typeof body === "object" ? JSON.stringify(body) : body,
...(typeof providerRequestOptions === "function" ? providerRequestOptions() : providerRequestOptions),
...mutateRequestOptions,
headers: {
"content-type": typeof body === "object" ? "application/json" : "text/plain",
...(typeof providerRequestOptions === "function"
? providerRequestOptions().headers
: (providerRequestOptions || {}).headers),
...(mutateRequestOptions ? mutateRequestOptions.headers : {}),
},
});
const response = await fetch(request);
const { data, responseError } = await processResponse(response);
if (!response.ok || responseError) {
const error = { data, message: `Failed to fetch: ${response.status} ${response.statusText}` };
this.setState({
loading: false,
});
if (!this.props.localErrorOnly && this.props.onError) {
this.props.onError(error, () => this.mutate(body, mutateRequestOptions));
}
throw error;
}
this.setState({ loading: false });
return data;
};
public render() {
const { children, path, base } = this.props;
const { error, loading, response } = this.state;
return children(this.mutate, { loading, error }, { response, absolutePath: `${base}${path}` });
}
}
/**
* The <Mutate /> component _with_ context.
* Context is used to compose path props,
* and to maintain the base property against
* which all requests will be made.
*
* We compose Consumers immediately with providers
* in order to provide new `base` props that contain
* a segment of the path, creating composable URLs.
*/
function Mutate<TError = any, TData = any>(props: MutateProps<TData, TError>) {
return (
<RestfulReactConsumer>
{contextProps => (
<RestfulReactProvider {...contextProps} base={`${contextProps.base}${props.path}`}>
<ContextlessMutate<TData, TError> {...contextProps} {...props} />
</RestfulReactProvider>
)}
</RestfulReactConsumer>
);
}
export default Mutate;