Request.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @Class Request
  3. * @description luch-request http请求插件
  4. * @Author lu-ch
  5. * @Email webwork.s@qq.com
  6. * 文档: https://www.quanzhan.co/luch-request/
  7. * github: https://github.com/lei-mu/luch-request
  8. * DCloud: http://ext.dcloud.net.cn/plugin?id=392
  9. */
  10. import dispatchRequest from './dispatchRequest'
  11. import InterceptorManager from './InterceptorManager'
  12. import mergeConfig from './mergeConfig'
  13. import defaults from './defaults'
  14. import { isPlainObject } from '../utils'
  15. import clone from '../utils/clone'
  16. export default class Request {
  17. /**
  18. * @param {Object} arg - 全局配置
  19. * @param {String} arg.baseURL - 全局根路径
  20. * @param {Object} arg.header - 全局header
  21. * @param {String} arg.method = [GET|POST|PUT|DELETE|CONNECT|HEAD|OPTIONS|TRACE] - 全局默认请求方式
  22. * @param {String} arg.dataType = [json] - 全局默认的dataType
  23. * @param {String} arg.responseType = [text|arraybuffer] - 全局默认的responseType。支付宝小程序不支持
  24. * @param {Object} arg.custom - 全局默认的自定义参数
  25. * @param {Number} arg.timeout - 全局默认的超时时间,单位 ms。默认60000。H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序
  26. * @param {Boolean} arg.sslVerify - 全局默认的是否验证 ssl 证书。默认true.仅App安卓端支持(HBuilderX 2.3.3+)
  27. * @param {Boolean} arg.withCredentials - 全局默认的跨域请求时是否携带凭证(cookies)。默认false。仅H5支持(HBuilderX 2.6.15+)
  28. * @param {Boolean} arg.firstIpv4 - 全DNS解析时优先使用ipv4。默认false。仅 App-Android 支持 (HBuilderX 2.8.0+)
  29. * @param {Function(statusCode):Boolean} arg.validateStatus - 全局默认的自定义验证器。默认statusCode >= 200 && statusCode < 300
  30. */
  31. constructor(arg = {}) {
  32. if (!isPlainObject(arg)) {
  33. arg = {}
  34. console.warn('设置全局参数必须接收一个Object')
  35. }
  36. this.config = clone({...defaults, ...arg})
  37. this.interceptors = {
  38. request: new InterceptorManager(),
  39. response: new InterceptorManager()
  40. }
  41. }
  42. /**
  43. * @Function
  44. * @param {Request~setConfigCallback} f - 设置全局默认配置
  45. */
  46. setConfig(f) {
  47. this.config = f(this.config)
  48. }
  49. middleware(config) {
  50. config = mergeConfig(this.config, config)
  51. let chain = [dispatchRequest, undefined]
  52. let promise = Promise.resolve(config)
  53. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  54. chain.unshift(interceptor.fulfilled, interceptor.rejected)
  55. })
  56. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  57. chain.push(interceptor.fulfilled, interceptor.rejected)
  58. })
  59. while (chain.length) {
  60. promise = promise.then(chain.shift(), chain.shift())
  61. }
  62. return promise
  63. }
  64. /**
  65. * @Function
  66. * @param {Object} config - 请求配置项
  67. * @prop {String} options.url - 请求路径
  68. * @prop {Object} options.data - 请求参数
  69. * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
  70. * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对返回的数据做一次 JSON.parse
  71. * @prop {Object} [options.header = config.header] - 请求header
  72. * @prop {Object} [options.method = config.method] - 请求方法
  73. * @returns {Promise<unknown>}
  74. */
  75. request(config = {}) {
  76. return this.middleware(config)
  77. }
  78. get(url, options = {}) {
  79. return this.middleware({
  80. url,
  81. method: 'GET',
  82. ...options
  83. })
  84. }
  85. post(url, data, options = {}) {
  86. return this.middleware({
  87. url,
  88. data,
  89. method: 'POST',
  90. ...options
  91. })
  92. }
  93. // #ifndef MP-ALIPAY || MP-KUAISHOU || MP-JD
  94. put(url, data, options = {}) {
  95. return this.middleware({
  96. url,
  97. data,
  98. method: 'PUT',
  99. ...options
  100. })
  101. }
  102. // #endif
  103. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  104. delete(url, data, options = {}) {
  105. return this.middleware({
  106. url,
  107. data,
  108. method: 'DELETE',
  109. ...options
  110. })
  111. }
  112. // #endif
  113. // #ifdef H5 || MP-WEIXIN
  114. connect(url, data, options = {}) {
  115. return this.middleware({
  116. url,
  117. data,
  118. method: 'CONNECT',
  119. ...options
  120. })
  121. }
  122. // #endif
  123. // #ifdef H5 || MP-WEIXIN || MP-BAIDU
  124. head(url, data, options = {}) {
  125. return this.middleware({
  126. url,
  127. data,
  128. method: 'HEAD',
  129. ...options
  130. })
  131. }
  132. // #endif
  133. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  134. options(url, data, options = {}) {
  135. return this.middleware({
  136. url,
  137. data,
  138. method: 'OPTIONS',
  139. ...options
  140. })
  141. }
  142. // #endif
  143. // #ifdef H5 || MP-WEIXIN
  144. trace(url, data, options = {}) {
  145. return this.middleware({
  146. url,
  147. data,
  148. method: 'TRACE',
  149. ...options
  150. })
  151. }
  152. // #endif
  153. upload(url, config = {}) {
  154. config.url = url
  155. config.method = 'UPLOAD'
  156. return this.middleware(config)
  157. }
  158. download(url, config = {}) {
  159. config.url = url
  160. config.method = 'DOWNLOAD'
  161. return this.middleware(config)
  162. }
  163. get version () {
  164. return '3.1.0'
  165. }
  166. }
  167. /**
  168. * setConfig回调
  169. * @return {Object} - 返回操作后的config
  170. * @callback Request~setConfigCallback
  171. * @param {Object} config - 全局默认config
  172. */