https://github.com/efureev/request-interceptors
Interceptors for Axios
https://github.com/efureev/request-interceptors
Last synced: about 2 months ago
JSON representation
Interceptors for Axios
- Host: GitHub
- URL: https://github.com/efureev/request-interceptors
- Owner: efureev
- Created: 2021-03-22T05:03:06.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-11-13T01:43:40.000Z (over 2 years ago)
- Last Synced: 2024-10-30T00:32:58.279Z (6 months ago)
- Language: TypeScript
- Size: 246 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# interceptors for the Axios
## List of request interceptor
- AuthInterceptor
## List of response interceptor
- WrapperInterceptor
- ActionInterceptor### WrapperInterceptor
Wraps axios-response to `ResponseWrapper` wrapper. Wraps success and error responses.
### ActionInterceptor
Execute some action by attributes from response.
Actions:
- Redirect
- Download file by link
- Blob downloadResponse must have following structure:
For redirect
```json
{
"status": {
"type": "redirect",
"url": "https://google.com"
}
}
```For download by url
```json
{
"status": {
"type": "download",
"url": "https://google.com/file.pdf",
"name": "ExampleFile.pdf"
}
}
```
For download blob-file
headers:
```json
{
"Access-Control-Expose-Headers" : "Content-Disposition",
"x-filename": "test2.pdf"
}
```Sample for Server side (Laravel Controler):
```php
return Storage::disk('public')->download(
$user->avatar,
"avater.{$ext}",
[
'Access-Control-Expose-Headers' => 'Content-Disposition',
'x-filename' => "test2.{$ext}",
]
);
```## Basic usage
```js
import buildRequest from '@feugene/layer-request'const apiHost = process.env.VUE_APP_API_HOST || ''
export const createRequest = (store, config) => {
const request = buildRequest({ extra: { store } })request.manager.addLayer((cm) => {
return cm.new({
requestConfig: {
headers: {
...(isObject(config.headers) ? config.headers : {}),
'X-Requested-With': 'XMLHttpRequest',
},
baseURL: `${apiHost}/api`,
},
interceptors: {
request: [(config) => (rConfig) => {
console.info(`\t🌐 ${rConfig.baseURL}/${rConfig.url}`)
return rConfig
}],
response: [
(config) => (response) => {
console.info(`\t✅ ${response.request.responseURL}`)
return response
},
Interceptor(),
ActionInterceptorBuild({
actionAttributeName: 'status',
}),
],
},
})
}, 'api')return request
}
```