https://github.com/efureev/request-wrapper
https://github.com/efureev/request-wrapper
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/efureev/request-wrapper
- Owner: efureev
- Created: 2021-02-24T06:21:04.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-17T12:49:33.000Z (about 4 years ago)
- Last Synced: 2025-01-15T09:03:03.159Z (3 months ago)
- Language: JavaScript
- Size: 43 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# request-wrapper
## Basic usage
Response wrapper with no data-key. All response data is in native `response.data` scope.
It's used to get content of a response. Example: content of a file.
```js
request({ responseWrapper: { dataKey: null } })
.get('download', { params: { disk, path } })
.then(response=>{
console.log(response.data())
})
```Response wrapper with data-key. By default, data-key is `data`.
It's used to get data from json response. Example json:
```json
{
"data": {
"id": 1,
"name": "test"
}
}
```
```js
request().get('list')
.then(response=>{
console.log(response.data()
)})
```## Use in package
`/request/index.js`:
```js
const fmApiPath = process.env.VUE_APP_LFM_PATH || 'file-manager'const wrapRequest = axiosInstance => {
return axiosInstance.reconfigure(
instance => {
instance.config.baseURL += `/${fmApiPath}`
instance.registerRequestInterceptors(LoadingRequestInterceptor)
instance.registerResponseInterceptors(LoadingResponseInterceptor, ResponseNoticeInterceptor)
}
)
}const request = config => {
return wrapRequest(Vue.prototype.$request(config))
}export default request
```use package's request in application
`/request/index.js`:
```js
import buildBaseRequest from '@feugene/request'const basePath = process.env.VUE_APP_BASE_PATH
const request = store => (config = {}) => {
const headers = {
...(isObject(config.headers) ? config.headers : {}),
}const mergedConfig = {
baseURL: basePath,
...config,
headers,
}mergedConfig.store = store
return buildBaseRequest(mergedConfig)
}export default request
```
`/main.js`
```js
import Vue from 'vue'
import Vuex from 'vuex'
import request from './request'
Vue.use(Vuex)const store = new Vuex.Store()
Vue.prototype.$request = request(store)
```