@yy-web/request
v1.5.9
Published
yyweb request
Downloads
37
Readme
@yy-web/request
Features
- Plug-in mode
- Chain call
- restful specification
- Flexible configuration
Install
npm i @yy-web/request
// request.ts
import axios from 'axios'
import request, { setRequest } from '@yy-web/request'
const service = axios.create({
baseURL: '/api',
})
const yyRequest = request(service)
setRequest(yyRequest)
export default yyRequest
Feature
- get action
import { getRequest } from '@yy-web/request' // get instance
import request from './request.ts'
// simple
request.setPath('xxxx').get(params)
// cache get
request.setPath('xxxx').get(true)
request.setPath('xxxx').get(params, true)
- other
import { getRequest } from '@yy-web/request' // get instance
import request from './request.ts'
request.setPath('xxxx').post(data)
request.setPath('xxxx').put()
request.setPath('xxxx').upload()
request.setPath('xxxx').del()
result carry
const id = 1
request.setPath('xxxx/{id}').carry(id) // -> request.setPath('xxxx/1')
- downfile
import axios from 'axios'
import request, { fileInterceptorsResponseConfig, setRequest } from '@yy-web/request'
const service = axios.create({
baseURL: '/api',
})
service.interceptors.response.use((response: any) => {
const { isFile, value } = fileInterceptorsResponseConfig(response)
if (isFile)
return value
return response.data
})
interface RequestStoreConfig {
getStore: (key: string) => any
setStore: (key: string, data: any) => void
cancelRepeat?: boolean // cancel repeat action
}
const yyRequest = request(service)
const store = {}
function getStore(key: string) {
return store[key]
}
function setStore(key: string, data: any) {
store[key] = data
}
setRequest(yyRequest, { getStore, setStore, cancelRepeat: true })
request.setPath('xxxx').downFile(data)
type
interface IRequest {
setPath(url: string, loading?: boolean): IRequest
setConfig(config: IAxiosRequestConfig): IRequest
forceCancelRepeat(): IRequest
carry(key: string | number): IRequest
get<T, Callback = false>(params?: boolean | object, cache?: boolean, dataCallback?: (data: T) => Callback): Promise<Callback extends false ? T : Callback>
post<T>(data?: object | FormData): Promise<T>
put<T>(data?: object): Promise<T>
del<T>(params?: object): Promise<T>
upload<T>(file: File, data?: object): Promise<T>
downLoad(params?: object, methods?: 'post' | 'get', fileName?: string): Promise<unknown>
}
License
MIT