Request.js 5.8 KB

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