You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/Service.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Middleware `Service` class
2
2
3
-
This is the heart of this plugin module. It works by leveraging axios' adapter to call its middleware stack at each relevant steps of a request lifecycle.
3
+
This is the heart of this plugin module. It works by leveraging axios' adapter to call the middleware stack at each relevant steps of a request lifecycle.
4
4
5
5
## `constructor(axios)`
6
6
@@ -41,4 +41,3 @@ Empty the middleware stack.
41
41
## `adapter(config)`
42
42
43
43
The adapter function that replaces the default axios adapter. It calls the default implementation and passes the resulting promise to the middleware stack's `onSync` method.
Copy file name to clipboardExpand all lines: docs/api/methods.md
+6-2Lines changed: 6 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ These will be called at different step of a request lifecycle. Each method can r
6
6
7
7
## `onRequest(config)`
8
8
9
-
Receives the configuration objects before the request is made. Useful to add headers, query parameters, validate data, etc.
9
+
Receives the configuration object before the request is made. Useful to add headers, query parameters, validate data, etc.
10
10
11
11
!> It must return the received `config` even if no changes were made to it. It can also return a promise that should resolve with the config to use for the request.
12
12
@@ -16,6 +16,8 @@ No internet connection right now? You might end up in this function. Do what you
16
16
17
17
You can return a promise, or throw another error to keep the middleware chain going.
18
18
19
+
!> Failing to return a rejecting promise or throw an error would mean that the error was dealt with and the chain would continue on a success path.
20
+
19
21
## `onSync(promise)`
20
22
21
23
The request is being made and its promise is being passed. Do what you want with it but be sure to **return a promise**, be it the one just received or a new one.
@@ -28,7 +30,7 @@ Parsing the response can be done here. Say all responses from your API are retur
28
30
29
31
```javascript
30
32
onResponse(response) {
31
-
returnresponse._data;
33
+
returnresponse._data;
32
34
}
33
35
```
34
36
@@ -39,3 +41,5 @@ onResponse(response) {
39
41
Not authenticated? Server problems? You might end up here. Do what you need with the error.
40
42
41
43
?> You can return a promise, which is useful when you want to retry failed requests.
44
+
45
+
!> Failing to return a rejecting promise or throw an error would mean that the error was dealt with and the chain would continue on a success path.
In a case where we'd like to retry a request if not authenticated, we could return a promise in the `onResponseError` method.
3
+
Every method of our middleware are promise callback functions, meaning that they can return either a value, a new promise or throw an error and the middleware chain will react accordingly.
4
4
5
5
```javascript
6
-
exportdefaultclassAuthMiddleware {
7
-
constructor(auth, http) {
8
-
this.auth= auth;
9
-
this.http= http;
10
-
}
11
-
12
-
onResponseError(err) {
13
-
if (err.response.status===401&&err.config&&!err.config.hasRetriedRequest) {
0 commit comments