Commit 0fd6a61
Lee Richmond
Add fetch middleware; improve promise handling
This is for things like "on any unauthenticated request, redirect to the
login page". You can now add before/after fetch hooks:
```ts
Config.beforeFetch.push(function(url, options) {
options.headers['some-new-header'] = 'foo'
})
Config.afterFetch.push(function(response, json) {
if (response.status === 401) {
throw('abort')
}
})
```
Throwing `abort` will reject the promises as *all* promises are now
rejected: by throwing a RequestError or ResponseError class. These
classes have additional info (like the response object) on them for
additional introspection/handling.
```ts
author.save().catch((e) => {
console.log(e.response.status);
})
```
Finally, this behavior can be overridden at the model level:
```ts
static beforeFetch(url: RequestInfo, options: RequestInit) {
// your overrides here
// call super if you want the Config hooks to run as well
}
static afterFetch(response: Response, json: JSON) {
// your overrides here
// call super if you want the Config hooks to run as well
}
```1 parent 27d4f6e commit 0fd6a61
File tree
7 files changed
+465
-26
lines changed- src
- test
- integration
7 files changed
+465
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| |||
215 | 227 | | |
216 | 228 | | |
217 | 229 | | |
218 | | - | |
| 230 | + | |
219 | 231 | | |
220 | 232 | | |
221 | 233 | | |
| |||
281 | 293 | | |
282 | 294 | | |
283 | 295 | | |
284 | | - | |
| 296 | + | |
285 | 297 | | |
286 | 298 | | |
287 | 299 | | |
| |||
292 | 304 | | |
293 | 305 | | |
294 | 306 | | |
295 | | - | |
| 307 | + | |
296 | 308 | | |
297 | 309 | | |
298 | 310 | | |
| |||
304 | 316 | | |
305 | 317 | | |
306 | 318 | | |
307 | | - | |
308 | 319 | | |
309 | 320 | | |
310 | 321 | | |
| |||
320 | 331 | | |
321 | 332 | | |
322 | 333 | | |
323 | | - | |
324 | 334 | | |
325 | 335 | | |
326 | | - | |
| 336 | + | |
327 | 337 | | |
328 | 338 | | |
329 | 339 | | |
| |||
333 | 343 | | |
334 | 344 | | |
335 | 345 | | |
336 | | - | |
337 | | - | |
338 | 346 | | |
339 | 347 | | |
340 | 348 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
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 | + | |
3 | 30 | | |
4 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
5 | 38 | | |
6 | 39 | | |
7 | 40 | | |
| |||
39 | 72 | | |
40 | 73 | | |
41 | 74 | | |
42 | | - | |
| 75 | + | |
43 | 76 | | |
| 77 | + | |
44 | 78 | | |
45 | | - | |
46 | 79 | | |
47 | 80 | | |
48 | 81 | | |
49 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
50 | 89 | | |
51 | 90 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 91 | + | |
56 | 92 | | |
57 | 93 | | |
58 | | - | |
| 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 | + | |
59 | 123 | | |
60 | 124 | | |
61 | 125 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
218 | | - | |
| 218 | + | |
219 | 219 | | |
220 | | - | |
| 220 | + | |
221 | 221 | | |
222 | | - | |
| 222 | + | |
223 | 223 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
229 | 229 | | |
230 | 230 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| |||
0 commit comments