-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Request Headers Interceptor | ||
|
||
```js | ||
import { createAxle, requestHeadersInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useRequestInterceptor( | ||
requestHeadersInterceptor({ | ||
// additional headers carried by the request | ||
headers: { | ||
'Custom-Field': 'custom-field' | ||
}, | ||
|
||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` | ||
|
||
```js | ||
import { createAxle, requestHeadersInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useRequestInterceptor( | ||
requestHeadersInterceptor({ | ||
// headers can also be a getter | ||
headers: () => ({ | ||
'Authorization': localStorage.getItem('token'), | ||
'Custom-Field': 'custom-field', | ||
}), | ||
|
||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Request Mock Interceptor | ||
|
||
```js | ||
import { createAxle, requestMockInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useRequestInterceptor( | ||
requestMockInterceptor({ | ||
// url and handler mapping | ||
mapping: { | ||
'/user/getUsers': { | ||
// request delay time (ms) | ||
delay: 1000, | ||
handler: () => ({ | ||
data: { | ||
code: 200, | ||
data: [ | ||
{ | ||
id: 1, | ||
name: 'Mock Jack Ma', | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Mock Tom', | ||
}, | ||
], | ||
message: 'success', | ||
}, | ||
}), | ||
}, | ||
|
||
'/error/**': { | ||
delay: 1000, | ||
handler: () => ({ | ||
// status defaults 200 | ||
status: 500, | ||
data: null | ||
}), | ||
} | ||
}, | ||
|
||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Response Blob Interceptor | ||
|
||
```js | ||
import { createAxle, responseBlobInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useResponseInterceptor( | ||
responseBlobInterceptor({ | ||
// transform blob to normal response type | ||
onResponse: (response) => ({ | ||
...response, | ||
data: { | ||
code: 200, | ||
data: response.data, | ||
message: 'success', | ||
} | ||
}), | ||
|
||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Response Retry Interceptor | ||
|
||
```js | ||
import { createAxle, responseRetryInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useResponseInterceptor( | ||
responseRetryInterceptor({ | ||
// retry count | ||
count: 3, | ||
|
||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Response Timeout Interceptor | ||
|
||
```js | ||
import { createAxle, responseTimeoutInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useResponseInterceptor( | ||
// normalize the timeout error code to TIMEOUT | ||
responseTimeoutInterceptor({ | ||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` | ||
|
||
```js | ||
import { createAxle, responseTimeoutInterceptor } from '@varlet/axle' | ||
|
||
const axle = createAxle() | ||
|
||
axle.useResponseInterceptor( | ||
// custom error code | ||
responseTimeoutInterceptor({ | ||
normalizeErrorCode: 'CUSTOM', | ||
|
||
// optional filtering options that determine whether the interceptor intercepts | ||
include: ['method:get', 'method:post'], | ||
exclude: ['/user/**'], | ||
}) | ||
) | ||
``` |