@autobest-ui/ajax
TypeScript icon, indicating that this package has built-in type declarations

3.0.0-alpha.0 • Public • Published

@autobest-ui/ajax 说明

  • 支持客户端和服务端
  • 支持abort,cache,timeout,retry,interceptor等
  • 扩展更加方便,通过中间件,可自定义扩展(当前扩展例子有:cache, retry, 文件上传)

Ajax 方法

Ajax.get(url: string, params?: ParamsData, options?: AjaxOptions)
Ajax.delete(url: string, params?: ParamsData, options?: AjaxOptions)
Ajax.post(url: string, body?: BodyData, params?: ParamsData, options?: AjaxOptions)
Ajax.put(url: string, body?: BodyData, params?: ParamsData, options?: AjaxOptions)
// 总入口, 可自定义请求如:options,head等, retry就是interceptor + request实现
Ajax.request(requestArgs: RequestArgs);

params 参数解析

支持url变量替换,其中变量使用方式是 :变量明

Ajax.get('/api/:id/find-name', {id: 'aaaaa', name: 'autobest'})
// 等价于
Ajax.get('/api/aaaaa/find-name', {name: 'autobest'})
// 等价于
Ajax.get('/api/aaaaa/find-name?name=autobest')

配置默认值

export const defaultConfig: AjaxGlobalConfig = {
  timeout: 0,
  responseType: 'json',
  requestConfig: {
    cache: 'no-store', // 解决IE11下缓存问题
    credentials: 'same-origin',
    referrerPolicy: 'no-referrer-when-downgrade',
    headers: {
      'If-Modified-Since': '0' // 解决IE11下缓存问题
    },
    mode: 'cors',
    redirect: 'follow'
  }
};

// 非formData, file, 默认是Content-Type
headers['Content-Type'] = 'application/json';

其他方法可参考具体说明

Readme

Keywords

none

Package Sidebar

Install

npm i @autobest-ui/ajax

Weekly Downloads

44

Version

3.0.0-alpha.0

License

ISC

Unpacked Size

94.8 kB

Total Files

54

Last publish

Collaborators

  • zhang123456