1.5.9 • Published 6 months ago

@yy-web/request v1.5.9

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@yy-web/request

NPM version

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

1.5.5

8 months ago

1.5.4

9 months ago

1.5.3

9 months ago

1.5.2

10 months ago

1.5.0

10 months ago

1.5.3-beta.2

9 months ago

1.5.3-beta.3

9 months ago

1.5.9

6 months ago

1.5.8

6 months ago

1.5.7

6 months ago

1.5.6

7 months ago

1.4.3

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.1-beta.0

1 year ago

1.0.0

1 year ago