EventRequest.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import EventEmitter from './EventEmitter'
  2. import merge from 'ts-deepmerge'
  3. import { getSign } from './util'
  4. const DEFAULT_CONFIG: EventRequestConfig = {
  5. maxConcurrent: 10,
  6. timeout: 0,
  7. header: {},
  8. dataType: 'json',
  9. baseUrl: '',
  10. }
  11. type HttpHeader = Record<string, string>
  12. interface EventRequestConfig {
  13. maxConcurrent: number
  14. /** 过期时间(毫秒) */
  15. timeout: number
  16. header: HttpHeader
  17. dataType: 'json' | '其他'
  18. baseUrl: string
  19. }
  20. interface HttpRequestConfig {
  21. url: string
  22. method: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT' | undefined
  23. data: Object | string
  24. header: HttpHeader
  25. dataType: 'json' | '其他'
  26. }
  27. interface RequestJob {
  28. config: HttpRequestConfig
  29. resolve(data: any): void
  30. reject(data: any): void
  31. response?: any
  32. }
  33. interface HttpRequestCallBack {
  34. success(data: HttpResponse): void
  35. fail(err: HttpRequestErr): void
  36. complete(): void
  37. }
  38. interface HttpRequestErr {
  39. /** 错误信息 */
  40. errMsg: string
  41. /** 需要基础库: `2.24.0`
  42. *
  43. * errno 错误码,错误码的详细说明参考 [Errno错误码](https://developers.weixin.qq.com/miniprogram/dev/framework/usability/PublicErrno.html) */
  44. errno: number
  45. }
  46. interface HttpResponse<T = any> {
  47. timeout?: string
  48. data: T
  49. errMsg: string
  50. statusCode: number
  51. header: Record<string, string>
  52. }
  53. type RequestInterceptorCallback = (config: HttpRequestConfig) => boolean
  54. type ResponseInterceptorCallback = (
  55. config: HttpRequestConfig,
  56. response: HttpResponse,
  57. ) => Promise<any>
  58. class EventRequest extends EventEmitter {
  59. private config: EventRequestConfig
  60. private queue: RequestJob[] = []
  61. private runningTask: number = 0
  62. private maxConcurrent = DEFAULT_CONFIG.maxConcurrent
  63. private requestInterceptor: RequestInterceptorCallback = () => true
  64. private responseInterceptor: ResponseInterceptorCallback = (_, rsp) =>
  65. new Promise((resolve) => resolve(rsp.data))
  66. constructor(_config?: Partial<EventRequestConfig>) {
  67. super()
  68. if (_config) {
  69. this.config = merge({}, DEFAULT_CONFIG, _config)
  70. } else {
  71. this.config = DEFAULT_CONFIG
  72. }
  73. this.maxConcurrent = this.config.maxConcurrent
  74. }
  75. private next(): void {
  76. const queue = this.queue
  77. if (!queue.length || this.runningTask >= this.maxConcurrent) return
  78. const entity = queue.shift()!
  79. const httpConfig = entity.config
  80. const { requestInterceptor, responseInterceptor } = this
  81. if (requestInterceptor.call(this, httpConfig) !== true) {
  82. let response: HttpResponse = {
  83. data: null,
  84. errMsg: `Request Interceptor: Request can\'t pass the Interceptor`,
  85. statusCode: 0,
  86. header: {},
  87. }
  88. entity.reject(response)
  89. return
  90. }
  91. this.emit('request', httpConfig)
  92. this.runningTask = this.runningTask + 1
  93. let timer = -1
  94. let aborted: boolean = false
  95. let finished: boolean = false
  96. const callBack: HttpRequestCallBack = {
  97. success: (res: HttpResponse): void => {
  98. if (aborted) return
  99. finished = true
  100. timer && clearTimeout(timer)
  101. entity.response = res
  102. this.emit('success', httpConfig, res)
  103. responseInterceptor
  104. .call(this, httpConfig, res)
  105. .then((rsp) => {
  106. entity.resolve(rsp)
  107. })
  108. .catch((err) => {
  109. entity.reject(err)
  110. })
  111. },
  112. fail: (err: HttpRequestErr): void => {
  113. if (aborted) return
  114. finished = true
  115. timer && clearTimeout(timer)
  116. entity.response = err
  117. this.emit('fail', httpConfig, err)
  118. entity.reject(err)
  119. },
  120. complete: () => {
  121. if (aborted) return
  122. this.emit('complete', httpConfig, entity.response)
  123. this.next()
  124. this.runningTask = this.runningTask - 1
  125. },
  126. }
  127. const requestConfig = Object.assign(httpConfig, callBack)
  128. const task = wx.request(requestConfig)
  129. if (this.config.timeout > 0) {
  130. timer = setTimeout(() => {
  131. if (!finished) {
  132. aborted = true
  133. entity.reject({
  134. timeout: '访问超时',
  135. })
  136. task && task.abort()
  137. this.next()
  138. }
  139. }, this.config.timeout)
  140. }
  141. }
  142. request<T = any>(
  143. method: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT',
  144. url: string,
  145. data: Object | string = '',
  146. header: HttpHeader = {},
  147. dataType?: 'json' | '其他',
  148. ): Promise<T> {
  149. const token = getSign()
  150. const language = 'zh-Hans'
  151. let authHeader = {}
  152. if (token != null) {
  153. authHeader = {
  154. Authorization: `Bearer ${token}`,
  155. '.AspNetCore.Culture': `c=${language}|uic=${language}`,
  156. Accept: 'application/json',
  157. } as Record<string, any>
  158. }
  159. const config: HttpRequestConfig = {
  160. method,
  161. url: this.config.baseUrl + url,
  162. data,
  163. header: { ...header, ...this.config.header, ...authHeader },
  164. dataType: dataType || this.config.dataType,
  165. }
  166. return new Promise((resolve, reject) => {
  167. const entity: RequestJob = { config, resolve, reject, response: null }
  168. this.queue.push(entity)
  169. this.next()
  170. })
  171. }
  172. head<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  173. return this.request<T>('HEAD', url, data, header, dataType)
  174. }
  175. options<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  176. return this.request<T>('OPTIONS', url, data, header, dataType)
  177. }
  178. get<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  179. return this.request<T>('GET', url, data, header, dataType)
  180. }
  181. post<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  182. return this.request<T>('POST', url, data, header, dataType)
  183. }
  184. put<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  185. return this.request<T>('PUT', url, data, header, dataType)
  186. }
  187. ['delete']<T>(
  188. url: string,
  189. data?: Object | string,
  190. header?: HttpHeader,
  191. dataType?: 'json' | '其他',
  192. ) {
  193. return this.request<T>('DELETE', url, data, header, dataType)
  194. }
  195. trace<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  196. return this.request<T>('TRACE', url, data, header, dataType)
  197. }
  198. connect<T>(url: string, data?: Object | string, header?: HttpHeader, dataType?: 'json' | '其他') {
  199. return this.request<T>('CONNECT', url, data, header, dataType)
  200. }
  201. setRequestInterceptor(interceptor: (config: HttpRequestConfig) => boolean) {
  202. this.requestInterceptor = interceptor
  203. return this
  204. }
  205. setResponseInterceptor(
  206. interceptor: (config: HttpRequestConfig, response: HttpResponse) => Promise<any>,
  207. ) {
  208. this.responseInterceptor = interceptor
  209. return this
  210. }
  211. clean(): void {
  212. this.queue = []
  213. }
  214. }
  215. export default EventRequest