Request.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // console.info('初始化luch-request')
  36. if (!isPlainObject(arg)) {
  37. arg = {}
  38. console.warn('设置全局参数必须接收一个Object')
  39. }
  40. this.config = clone({ ...defaults, ...arg })
  41. this.interceptors = {
  42. request: new InterceptorManager(),
  43. response: new InterceptorManager()
  44. }
  45. }
  46. /**
  47. * @Function
  48. * @param {Request~setConfigCallback} f - 设置全局默认配置
  49. */
  50. setConfig(f) {
  51. this.config = f(this.config)
  52. }
  53. middleware(config) {
  54. config = mergeConfig(this.config, config)
  55. const chain = [dispatchRequest, undefined]
  56. let promise = Promise.resolve(config)
  57. this.interceptors.request.forEach((interceptor) => {
  58. chain.unshift(interceptor.fulfilled, interceptor.rejected)
  59. })
  60. this.interceptors.response.forEach((interceptor) => {
  61. chain.push(interceptor.fulfilled, interceptor.rejected)
  62. })
  63. while (chain.length) {
  64. promise = promise.then(chain.shift(), chain.shift())
  65. }
  66. return promise
  67. }
  68. /**
  69. * @Function
  70. * @param {Object} config - 请求配置项
  71. * @prop {String} options.url - 请求路径
  72. * @prop {Object} options.data - 请求参数
  73. * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
  74. * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对返回的数据做一次 JSON.parse
  75. * @prop {Object} [options.header = config.header] - 请求header
  76. * @prop {Object} [options.method = config.method] - 请求方法
  77. * @returns {Promise<unknown>}
  78. */
  79. request(config = {}) {
  80. return this.middleware(config)
  81. }
  82. get(url, options = {}) {
  83. return this.middleware({
  84. url,
  85. method: 'GET',
  86. ...options
  87. })
  88. }
  89. post(url, data, options = {}) {
  90. return this.middleware({
  91. url,
  92. data,
  93. method: 'POST',
  94. ...options
  95. })
  96. }
  97. // #ifndef MP-ALIPAY
  98. put(url, data, options = {}) {
  99. return this.middleware({
  100. url,
  101. data,
  102. method: 'PUT',
  103. ...options
  104. })
  105. }
  106. // #endif
  107. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  108. delete(url, data, options = {}) {
  109. return this.middleware({
  110. url,
  111. data,
  112. method: 'DELETE',
  113. ...options
  114. })
  115. }
  116. // #endif
  117. // #ifdef H5 || MP-WEIXIN
  118. connect(url, data, options = {}) {
  119. return this.middleware({
  120. url,
  121. data,
  122. method: 'CONNECT',
  123. ...options
  124. })
  125. }
  126. // #endif
  127. // #ifdef H5 || MP-WEIXIN || MP-BAIDU
  128. head(url, data, options = {}) {
  129. return this.middleware({
  130. url,
  131. data,
  132. method: 'HEAD',
  133. ...options
  134. })
  135. }
  136. // #endif
  137. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  138. options(url, data, options = {}) {
  139. return this.middleware({
  140. url,
  141. data,
  142. method: 'OPTIONS',
  143. ...options
  144. })
  145. }
  146. // #endif
  147. // #ifdef H5 || MP-WEIXIN
  148. trace(url, data, options = {}) {
  149. return this.middleware({
  150. url,
  151. data,
  152. method: 'TRACE',
  153. ...options
  154. })
  155. }
  156. // #endif
  157. upload(url, config = {}) {
  158. config.url = url
  159. config.method = 'UPLOAD'
  160. return this.middleware(config)
  161. }
  162. download(url, config = {}) {
  163. config.url = url
  164. config.method = 'DOWNLOAD'
  165. return this.middleware(config)
  166. }
  167. }
  168. /**
  169. * setConfig回调
  170. * @return {Object} - 返回操作后的config
  171. * @callback Request~setConfigCallback
  172. * @param {Object} config - 全局默认config
  173. */