@fetchx/fetcher-core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (244) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +58 -0
  4. package/dist/cjs/const/index.js +13 -0
  5. package/dist/cjs/enum/index.js +65 -0
  6. package/dist/cjs/factory/create-fn-get-with-abort.js +33 -0
  7. package/dist/cjs/factory/create-fn-get.js +26 -0
  8. package/dist/cjs/factory/create-fn-post-with-abort.js +35 -0
  9. package/dist/cjs/factory/create-fn-post.js +28 -0
  10. package/dist/cjs/factory/fetcher-core.js +261 -0
  11. package/dist/cjs/factory/index.js +44 -0
  12. package/dist/cjs/factory/intercept-request-final.js +24 -0
  13. package/dist/cjs/factory/intercept-request-first.js +23 -0
  14. package/dist/cjs/factory/intercept-response-download.js +21 -0
  15. package/dist/cjs/helper/clone-response-data.js +17 -0
  16. package/dist/cjs/helper/create-fetcher-error-skip-network.js +23 -0
  17. package/dist/cjs/helper/delete-config-headers.js +27 -0
  18. package/dist/cjs/helper/index.js +82 -0
  19. package/dist/cjs/index.js +54 -0
  20. package/dist/cjs/types/common.js +5 -0
  21. package/dist/cjs/types/config-body.js +5 -0
  22. package/dist/cjs/types/config-params.js +5 -0
  23. package/dist/cjs/types/config.js +5 -0
  24. package/dist/cjs/types/fetcher-error.js +5 -0
  25. package/dist/cjs/types/fetcher-fn.js +5 -0
  26. package/dist/cjs/types/fetcher-interceptor.js +5 -0
  27. package/dist/cjs/types/fetcher-response.js +5 -0
  28. package/dist/cjs/types/fetcher.js +5 -0
  29. package/dist/cjs/types/index.js +104 -0
  30. package/dist/cjs/util/build-url-with-params.js +25 -0
  31. package/dist/cjs/util/build-url.js +26 -0
  32. package/dist/cjs/util/clone-type-form-data.js +23 -0
  33. package/dist/cjs/util/clone-type-url-search-params.js +22 -0
  34. package/dist/cjs/util/create-fetcher-error.js +32 -0
  35. package/dist/cjs/util/deserialize-body.js +13 -0
  36. package/dist/cjs/util/deserialize-params.js +13 -0
  37. package/dist/cjs/util/ensure-fetcher-error.js +32 -0
  38. package/dist/cjs/util/get-download-filename.js +15 -0
  39. package/dist/cjs/util/get-hash-from-config.js +29 -0
  40. package/dist/cjs/util/get-headers-and-body-from-config.js +94 -0
  41. package/dist/cjs/util/get-url-origin.js +22 -0
  42. package/dist/cjs/util/index.js +132 -0
  43. package/dist/cjs/util/is-config-allow-body.js +15 -0
  44. package/dist/cjs/util/is-config-cors.js +14 -0
  45. package/dist/cjs/util/is-config-jsonp.js +10 -0
  46. package/dist/cjs/util/is-instanceof-array-buffer.js +9 -0
  47. package/dist/cjs/util/is-instanceof-blob.js +9 -0
  48. package/dist/cjs/util/is-instanceof-form-data.js +9 -0
  49. package/dist/cjs/util/is-instanceof-url-search-params.js +9 -0
  50. package/dist/cjs/util/merge-body.js +28 -0
  51. package/dist/cjs/util/merge-config-body.js +28 -0
  52. package/dist/cjs/util/merge-config-headers.js +17 -0
  53. package/dist/cjs/util/merge-config-params.js +25 -0
  54. package/dist/cjs/util/merge-config.js +43 -0
  55. package/dist/cjs/util/merge-params.js +17 -0
  56. package/dist/cjs/util/merge-type-form-data.js +12 -0
  57. package/dist/cjs/util/merge-type-search-params.js +12 -0
  58. package/dist/cjs/util/parse-args-get.js +26 -0
  59. package/dist/cjs/util/parse-args-post.js +29 -0
  60. package/dist/cjs/util/queue-interceptor.js +18 -0
  61. package/dist/cjs/util/serialize-body.js +26 -0
  62. package/dist/cjs/util/serialize-params.js +23 -0
  63. package/dist/cjs/util/sort-interceptors.js +20 -0
  64. package/dist/esm/const/index.js +8 -0
  65. package/dist/esm/const/index.js.map +1 -0
  66. package/dist/esm/enum/index.js +61 -0
  67. package/dist/esm/enum/index.js.map +1 -0
  68. package/dist/esm/factory/create-fn-get-with-abort.js +27 -0
  69. package/dist/esm/factory/create-fn-get-with-abort.js.map +1 -0
  70. package/dist/esm/factory/create-fn-get.js +20 -0
  71. package/dist/esm/factory/create-fn-get.js.map +1 -0
  72. package/dist/esm/factory/create-fn-post-with-abort.js +29 -0
  73. package/dist/esm/factory/create-fn-post-with-abort.js.map +1 -0
  74. package/dist/esm/factory/create-fn-post.js +22 -0
  75. package/dist/esm/factory/create-fn-post.js.map +1 -0
  76. package/dist/esm/factory/fetcher-core.js +257 -0
  77. package/dist/esm/factory/fetcher-core.js.map +1 -0
  78. package/dist/esm/factory/index.js +39 -0
  79. package/dist/esm/factory/index.js.map +1 -0
  80. package/dist/esm/factory/intercept-request-final.js +20 -0
  81. package/dist/esm/factory/intercept-request-final.js.map +1 -0
  82. package/dist/esm/factory/intercept-request-first.js +19 -0
  83. package/dist/esm/factory/intercept-request-first.js.map +1 -0
  84. package/dist/esm/factory/intercept-response-download.js +16 -0
  85. package/dist/esm/factory/intercept-response-download.js.map +1 -0
  86. package/dist/esm/helper/clone-response-data.js +12 -0
  87. package/dist/esm/helper/clone-response-data.js.map +1 -0
  88. package/dist/esm/helper/create-fetcher-error-skip-network.js +18 -0
  89. package/dist/esm/helper/create-fetcher-error-skip-network.js.map +1 -0
  90. package/dist/esm/helper/delete-config-headers.js +22 -0
  91. package/dist/esm/helper/delete-config-headers.js.map +1 -0
  92. package/dist/esm/helper/index.js +10 -0
  93. package/dist/esm/helper/index.js.map +1 -0
  94. package/dist/esm/index.js +5 -0
  95. package/dist/esm/index.js.map +1 -0
  96. package/dist/esm/types/common.js +2 -0
  97. package/dist/esm/types/common.js.map +1 -0
  98. package/dist/esm/types/config-body.js +2 -0
  99. package/dist/esm/types/config-body.js.map +1 -0
  100. package/dist/esm/types/config-params.js +2 -0
  101. package/dist/esm/types/config-params.js.map +1 -0
  102. package/dist/esm/types/config.js +2 -0
  103. package/dist/esm/types/config.js.map +1 -0
  104. package/dist/esm/types/fetcher-error.js +2 -0
  105. package/dist/esm/types/fetcher-error.js.map +1 -0
  106. package/dist/esm/types/fetcher-fn.js +2 -0
  107. package/dist/esm/types/fetcher-fn.js.map +1 -0
  108. package/dist/esm/types/fetcher-interceptor.js +2 -0
  109. package/dist/esm/types/fetcher-interceptor.js.map +1 -0
  110. package/dist/esm/types/fetcher-response.js +2 -0
  111. package/dist/esm/types/fetcher-response.js.map +1 -0
  112. package/dist/esm/types/fetcher.js +2 -0
  113. package/dist/esm/types/fetcher.js.map +1 -0
  114. package/dist/esm/types/index.js +10 -0
  115. package/dist/esm/types/index.js.map +1 -0
  116. package/dist/esm/util/build-url-with-params.js +19 -0
  117. package/dist/esm/util/build-url-with-params.js.map +1 -0
  118. package/dist/esm/util/build-url.js +21 -0
  119. package/dist/esm/util/build-url.js.map +1 -0
  120. package/dist/esm/util/clone-type-form-data.js +17 -0
  121. package/dist/esm/util/clone-type-form-data.js.map +1 -0
  122. package/dist/esm/util/clone-type-url-search-params.js +16 -0
  123. package/dist/esm/util/clone-type-url-search-params.js.map +1 -0
  124. package/dist/esm/util/create-fetcher-error.js +26 -0
  125. package/dist/esm/util/create-fetcher-error.js.map +1 -0
  126. package/dist/esm/util/deserialize-body.js +7 -0
  127. package/dist/esm/util/deserialize-body.js.map +1 -0
  128. package/dist/esm/util/deserialize-params.js +7 -0
  129. package/dist/esm/util/deserialize-params.js.map +1 -0
  130. package/dist/esm/util/ensure-fetcher-error.js +26 -0
  131. package/dist/esm/util/ensure-fetcher-error.js.map +1 -0
  132. package/dist/esm/util/get-download-filename.js +10 -0
  133. package/dist/esm/util/get-download-filename.js.map +1 -0
  134. package/dist/esm/util/get-hash-from-config.js +24 -0
  135. package/dist/esm/util/get-hash-from-config.js.map +1 -0
  136. package/dist/esm/util/get-headers-and-body-from-config.js +89 -0
  137. package/dist/esm/util/get-headers-and-body-from-config.js.map +1 -0
  138. package/dist/esm/util/get-url-origin.js +17 -0
  139. package/dist/esm/util/get-url-origin.js.map +1 -0
  140. package/dist/esm/util/index.js +19 -0
  141. package/dist/esm/util/index.js.map +1 -0
  142. package/dist/esm/util/is-config-allow-body.js +10 -0
  143. package/dist/esm/util/is-config-allow-body.js.map +1 -0
  144. package/dist/esm/util/is-config-cors.js +8 -0
  145. package/dist/esm/util/is-config-cors.js.map +1 -0
  146. package/dist/esm/util/is-config-jsonp.js +5 -0
  147. package/dist/esm/util/is-config-jsonp.js.map +1 -0
  148. package/dist/esm/util/is-instanceof-array-buffer.js +4 -0
  149. package/dist/esm/util/is-instanceof-array-buffer.js.map +1 -0
  150. package/dist/esm/util/is-instanceof-blob.js +4 -0
  151. package/dist/esm/util/is-instanceof-blob.js.map +1 -0
  152. package/dist/esm/util/is-instanceof-form-data.js +4 -0
  153. package/dist/esm/util/is-instanceof-form-data.js.map +1 -0
  154. package/dist/esm/util/is-instanceof-url-search-params.js +4 -0
  155. package/dist/esm/util/is-instanceof-url-search-params.js.map +1 -0
  156. package/dist/esm/util/merge-body.js +23 -0
  157. package/dist/esm/util/merge-body.js.map +1 -0
  158. package/dist/esm/util/merge-config-body.js +22 -0
  159. package/dist/esm/util/merge-config-body.js.map +1 -0
  160. package/dist/esm/util/merge-config-headers.js +12 -0
  161. package/dist/esm/util/merge-config-headers.js.map +1 -0
  162. package/dist/esm/util/merge-config-params.js +19 -0
  163. package/dist/esm/util/merge-config-params.js.map +1 -0
  164. package/dist/esm/util/merge-config.js +38 -0
  165. package/dist/esm/util/merge-config.js.map +1 -0
  166. package/dist/esm/util/merge-params.js +11 -0
  167. package/dist/esm/util/merge-params.js.map +1 -0
  168. package/dist/esm/util/merge-type-form-data.js +7 -0
  169. package/dist/esm/util/merge-type-form-data.js.map +1 -0
  170. package/dist/esm/util/merge-type-search-params.js +7 -0
  171. package/dist/esm/util/merge-type-search-params.js.map +1 -0
  172. package/dist/esm/util/parse-args-get.js +20 -0
  173. package/dist/esm/util/parse-args-get.js.map +1 -0
  174. package/dist/esm/util/parse-args-post.js +23 -0
  175. package/dist/esm/util/parse-args-post.js.map +1 -0
  176. package/dist/esm/util/queue-interceptor.js +13 -0
  177. package/dist/esm/util/queue-interceptor.js.map +1 -0
  178. package/dist/esm/util/serialize-body.js +21 -0
  179. package/dist/esm/util/serialize-body.js.map +1 -0
  180. package/dist/esm/util/serialize-params.js +17 -0
  181. package/dist/esm/util/serialize-params.js.map +1 -0
  182. package/dist/esm/util/sort-interceptors.js +15 -0
  183. package/dist/esm/util/sort-interceptors.js.map +1 -0
  184. package/dist/types/const/index.d.ts +3 -0
  185. package/dist/types/enum/index.d.ts +55 -0
  186. package/dist/types/factory/create-fn-get-with-abort.d.ts +2 -0
  187. package/dist/types/factory/create-fn-get.d.ts +2 -0
  188. package/dist/types/factory/create-fn-post-with-abort.d.ts +2 -0
  189. package/dist/types/factory/create-fn-post.d.ts +2 -0
  190. package/dist/types/factory/fetcher-core.d.ts +53 -0
  191. package/dist/types/factory/index.d.ts +5 -0
  192. package/dist/types/factory/intercept-request-final.d.ts +5 -0
  193. package/dist/types/factory/intercept-request-first.d.ts +8 -0
  194. package/dist/types/factory/intercept-response-download.d.ts +2 -0
  195. package/dist/types/helper/clone-response-data.d.ts +5 -0
  196. package/dist/types/helper/create-fetcher-error-skip-network.d.ts +12 -0
  197. package/dist/types/helper/delete-config-headers.d.ts +7 -0
  198. package/dist/types/helper/index.d.ts +7 -0
  199. package/dist/types/index.d.ts +5 -0
  200. package/dist/types/types/common.d.ts +3 -0
  201. package/dist/types/types/config-body.d.ts +11 -0
  202. package/dist/types/types/config-params.d.ts +8 -0
  203. package/dist/types/types/config.d.ts +114 -0
  204. package/dist/types/types/fetcher-error.d.ts +38 -0
  205. package/dist/types/types/fetcher-fn.d.ts +92 -0
  206. package/dist/types/types/fetcher-interceptor.d.ts +30 -0
  207. package/dist/types/types/fetcher-response.d.ts +10 -0
  208. package/dist/types/types/fetcher.d.ts +44 -0
  209. package/dist/types/types/index.d.ts +9 -0
  210. package/dist/types/util/build-url-with-params.d.ts +2 -0
  211. package/dist/types/util/build-url.d.ts +10 -0
  212. package/dist/types/util/clone-type-form-data.d.ts +1 -0
  213. package/dist/types/util/clone-type-url-search-params.d.ts +1 -0
  214. package/dist/types/util/create-fetcher-error.d.ts +8 -0
  215. package/dist/types/util/deserialize-body.d.ts +2 -0
  216. package/dist/types/util/deserialize-params.d.ts +2 -0
  217. package/dist/types/util/ensure-fetcher-error.d.ts +2 -0
  218. package/dist/types/util/get-download-filename.d.ts +2 -0
  219. package/dist/types/util/get-hash-from-config.d.ts +5 -0
  220. package/dist/types/util/get-headers-and-body-from-config.d.ts +55 -0
  221. package/dist/types/util/get-url-origin.d.ts +4 -0
  222. package/dist/types/util/index.d.ts +18 -0
  223. package/dist/types/util/is-config-allow-body.d.ts +5 -0
  224. package/dist/types/util/is-config-cors.d.ts +2 -0
  225. package/dist/types/util/is-config-jsonp.d.ts +2 -0
  226. package/dist/types/util/is-instanceof-array-buffer.d.ts +1 -0
  227. package/dist/types/util/is-instanceof-blob.d.ts +1 -0
  228. package/dist/types/util/is-instanceof-form-data.d.ts +1 -0
  229. package/dist/types/util/is-instanceof-url-search-params.d.ts +1 -0
  230. package/dist/types/util/merge-body.d.ts +7 -0
  231. package/dist/types/util/merge-config-body.d.ts +2 -0
  232. package/dist/types/util/merge-config-headers.d.ts +3 -0
  233. package/dist/types/util/merge-config-params.d.ts +2 -0
  234. package/dist/types/util/merge-config.d.ts +5 -0
  235. package/dist/types/util/merge-params.d.ts +2 -0
  236. package/dist/types/util/merge-type-form-data.d.ts +1 -0
  237. package/dist/types/util/merge-type-search-params.d.ts +1 -0
  238. package/dist/types/util/parse-args-get.d.ts +2 -0
  239. package/dist/types/util/parse-args-post.d.ts +2 -0
  240. package/dist/types/util/queue-interceptor.d.ts +5 -0
  241. package/dist/types/util/serialize-body.d.ts +6 -0
  242. package/dist/types/util/serialize-params.d.ts +2 -0
  243. package/dist/types/util/sort-interceptors.d.ts +7 -0
  244. package/package.json +56 -0
@@ -0,0 +1,53 @@
1
+ import { TErrorNameNormalizer, TInterceptorEject, IFetcherClass, IFetcherConfig, IFetcherConfigDefault, TFetcherAdapter, TFetcherInterceptRequest, TFetcherInterceptResponseFulfilled, TFetcherInterceptResponseRejected } from '../types';
2
+ /**
3
+ * 一个允许添加 Request 和 Response 拦截器的 Fetcher 类,有些类似 axios,但有所不同:
4
+ *
5
+ * 1. 拦截方法更直接:`interceptRequest|Response`,而不是 `axios.interceptors.request|response.use()`
6
+ * 2. 解除拦截只需要记住以上两个方法返回的无参方法即可(在 React 的 useEffect hooks 下特别方便),而不是 `axios.interceptors.request|response.eject()`
7
+ * 3. `interceptRequest` 仅接受一个方法,而 `interceptResponse` 可以接受两个(跟 axios 类似)
8
+ * 4. `interceptRequest` 的顺序和最终调用的顺序一致,而 axios 的顺序是倒着来的
9
+ * 5. `interceptRequest` 如果抛错,不会触发真实的 API 请求(axios 一样),也不会触发任何 response interceptors(axios 会触发)
10
+ * 6. `interceptRequest` 可以不必返回全的 `FetcherConfig`,会自动进行 merge,axios 要求返回全的
11
+ */
12
+ export default class FetcherCore implements IFetcherClass {
13
+ private readonly adapter;
14
+ private readonly normalizeErrorName;
15
+ private readonly defaultConfig?;
16
+ private readonly interceptorQueueRequest;
17
+ private readonly interceptorQueueResponse;
18
+ private frozen;
19
+ /**
20
+ * 传递给 Interceptor,在 Interceptor 内部有需要可以重新请求
21
+ */
22
+ private requestByInterceptor;
23
+ constructor(adapter: TFetcherAdapter, normalizeErrorName?: TErrorNameNormalizer | null, defaultConfig?: IFetcherConfigDefault);
24
+ /**
25
+ * 获取此次调用需要用到的所有请求拦截器,且拦截器的顺序按指定顺序
26
+ */
27
+ private getInterceptorRequestQueue;
28
+ private getInterceptorResponseQueue;
29
+ /**
30
+ * 逐个调用请求拦截器,每个拦截器可以返回部分期望修改的 config(也可以不返回任何东西),最终得到的是合并后完整的 FetcherConfig 对象。
31
+ *
32
+ * 注意,Request 拦截器是一条不会反转 Reject 的 Promise 链,即只要其中任何一环 `throw`,即表明接口失败,并且不会进行真正的接口调用,也不会
33
+ * 进入响应拦截流程。
34
+ */
35
+ private invokeInterceptorQueueRequest;
36
+ /**
37
+ * 逐个调用响应拦截器,你可以在 `onFulfilled` 里转换数据或者将结果转成错误;也可以在 `onReject` 中将结果反转成成功。
38
+ */
39
+ private invokeInterceptorQueueResponse;
40
+ interceptRequest(onFulfilled: TFetcherInterceptRequest, priority?: number): TInterceptorEject;
41
+ /**
42
+ * 添加「预设」响应拦截器,返回解除拦截的无参方法
43
+ */
44
+ interceptResponse(onFulfilled?: TFetcherInterceptResponseFulfilled, onRejected?: TFetcherInterceptResponseRejected, priority?: number): TInterceptorEject;
45
+ /**
46
+ * 对于「开箱即用」的 Fetcher 实例,由于是会被复用的单例,一般不希望它被扩展和修改,此操作不可逆
47
+ */
48
+ freeze(): void;
49
+ /**
50
+ * 发送请求:前置请求拦截器 → 网络请求 → 后置响应拦截器
51
+ */
52
+ request<T = unknown>(config: IFetcherConfig): Promise<T>;
53
+ }
@@ -0,0 +1,5 @@
1
+ import { IFetcher, IFetcherConfigDefault, TFetcherAdapter, TErrorNameNormalizer } from '../types';
2
+ /**
3
+ * Fetcher 工厂
4
+ */
5
+ export default function factory(fetcherAdapter: TFetcherAdapter, normalizeErrorName?: TErrorNameNormalizer): <X = object>(defaultConfig?: IFetcherConfigDefault) => IFetcher<X>;
@@ -0,0 +1,5 @@
1
+ import { IFetcherConfig } from '../types';
2
+ /**
3
+ * Request 最后一个拦截器,发送请求前一刻执行
4
+ */
5
+ export default function interceptRequestFinal(config: IFetcherConfig): Partial<IFetcherConfig>;
@@ -0,0 +1,8 @@
1
+ import { IFetcherConfig } from '../types';
2
+ /**
3
+ * 默认的第一个 Request 拦截器
4
+ *
5
+ * - 创建 `_hash`(可以被后续拦截器用于缓存、合并等骚操作)
6
+ * - 保证 `method` 存在且大写
7
+ */
8
+ export default function interceptRequestFirst(config: IFetcherConfig): Partial<IFetcherConfig>;
@@ -0,0 +1,2 @@
1
+ import { IFetcherConfig, IFetcherResponse } from '../types';
2
+ export default function interceptResponseDownload(o: unknown, config: IFetcherConfig, fetcherResponse?: IFetcherResponse): unknown;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 有些场景或拦截器需要对响应数据进行克隆,但如果 clone 了 Blob 会成为一个空的 plain 对象,所以封一个简单的
3
+ * 帮助方法,对且仅对普通对象或数组进行克隆(简单数据不需要)
4
+ */
5
+ export default function cloneResponseData<T = unknown>(responseData: T): T;
@@ -0,0 +1,12 @@
1
+ import { IFetcherConfig, IFetcherErrorSkipNetwork } from '../types';
2
+ /**
3
+ * 创建一个特殊的错误,它将被用于请求拦截器中,如果请求拦截器抛出此错误,则请求不会被执行,余下的拦截器将**都**不会被执行。
4
+ * 跟其他的错误不同的是,Fetcher 会从这个错误中获取 result 并以它作为最终的返回结果。
5
+ *
6
+ * 它的作用场景:
7
+ *
8
+ * 1. 有缓存好已经获得到的结果,则直接返回结果
9
+ * 2. 合并接口请求,则直接返回合并前的临时 Promise
10
+ * 3. 请求被代理到新的 Promise
11
+ */
12
+ export default function createFetcherErrorSkipNetwork<T = void>(result: T | Promise<T>, config: IFetcherConfig): IFetcherErrorSkipNetwork<T>;
@@ -0,0 +1,7 @@
1
+ import { IFetcherConfig } from '../types';
2
+ /**
3
+ * Fetcher 遵循传入值的优先级高于拦截器注入,因此在利用 headers 进行登录判断的情况下,
4
+ * 若利用拦截器进行二次调用,可能会让前一次拦截器的结果成为后一次的「传入值」,
5
+ * 从而导致新的从拦截器进入的值无法生效,可用此帮助方法清空前一次的结果
6
+ */
7
+ export default function deleteConfigHeaders(config: IFetcherConfig, keys: string[]): void;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 仅供外部是要用,不在此包中使用的帮助方法
3
+ */
4
+ export { default as deleteConfigHeaders } from './delete-config-headers';
5
+ export { default as cloneResponseData } from './clone-response-data';
6
+ export { default as createFetcherErrorSkipNetwork } from './create-fetcher-error-skip-network';
7
+ export { isInstanceofBlob, isInstanceofArrayBuffer, isInstanceofFormData, isInstanceofUrlSearchParams, isConfigJsonp, cloneTypeFormData, cloneTypeUrlSearchParams, createFetcherError, buildUrl } from '../util';
@@ -0,0 +1,5 @@
1
+ export { default } from './factory';
2
+ export * from './helper';
3
+ export { EFetcherErrorName as FetcherErrorName, EFetcherResponseType as FetcherResponseType } from './enum';
4
+ export * from '@fetchx/fetcher-helper-headers';
5
+ export type { IFetcher as Fetcher, IPromiseWithAbort as PromiseWithAbort, TFetcherParams as FetcherParams, TFetcherBody as FetcherBody, TFetcherBodyNormalized as FetcherBodyNormalized, IFetcherConfig as FetcherConfig, IFetcherConfigDefault as FetcherConfigDefault, TFetcherConfigQuickJsonp as FetcherConfigQuickJsonp, TFetcherConfigQuick as FetcherConfigQuick, TFetcherFnRequest as FetcherCallRequest, IFetcherFnJsonp as FetcherCallJsonp, IFetcherFnGet as FetcherCallGet, IFetcherFnPost as FetcherCallPost, TFetcherInterceptRequest as FetcherInterceptRequest, TFetcherInterceptRequestReturn as FetcherInterceptRequestReturn, TFetcherInterceptResponseFulfilled as FetcherInterceptResponseFulfilled, TFetcherInterceptResponseRejected as FetcherInterceptResponseRejected, IFetcherResponse as FetcherResponse, IFetcherError as FetcherError } from './types';
@@ -0,0 +1,3 @@
1
+ export interface IPromiseWithAbort<T> extends Promise<T> {
2
+ abort(): void;
3
+ }
@@ -0,0 +1,11 @@
1
+ import { IStringifyOptions } from 'qs';
2
+ export type TFetcherBodyMergeable = Record<string, unknown> | URLSearchParams | FormData;
3
+ /**
4
+ * 能够接受 body 类型,是 `BodyInit` 的子集 + `Record<string, unknown>`
5
+ *
6
+ * `BodyInit = ReadableStream | Blob | BufferSource | FormData | URLSearchParams | string`
7
+ */
8
+ export type TFetcherBody = TFetcherBodyMergeable | Blob | string | null;
9
+ export type TFetcherBodyNormalized = null | string | URLSearchParams | FormData | Blob;
10
+ export interface IFetcherBodySerializeOptions extends IStringifyOptions {
11
+ }
@@ -0,0 +1,8 @@
1
+ import { IStringifyOptions } from 'qs';
2
+ export type TFetcherParamsMergeable = URLSearchParams | Record<string, unknown>;
3
+ /**
4
+ * 能够接受的 URL 参数类型
5
+ */
6
+ export type TFetcherParams = TFetcherParamsMergeable | string | null;
7
+ export interface IFetcherParamsSerializeOptions extends IStringifyOptions {
8
+ }
@@ -0,0 +1,114 @@
1
+ import { FetcherHeaders } from '@fetchx/fetcher-helper-headers';
2
+ import { EFetcherResponseType } from '../enum';
3
+ import { TFetcherParams, IFetcherParamsSerializeOptions } from './config-params';
4
+ import { TFetcherBody, IFetcherBodySerializeOptions } from './config-body';
5
+ type TFetcherConfigInternalFields = '_config' | '_hash' | '_timeStarted' | '_byInterceptor';
6
+ /**
7
+ * interceptor 的 config 参数,也是 Fetcher.prototype.request 的参数
8
+ *
9
+ * `_` 打头的是 fetcher 自己填入的,不要在调用时传入
10
+ */
11
+ export interface IFetcherConfig {
12
+ /**
13
+ * 调用 `request` 时传入的原初 `config`,可在「重试」拦截中使用以避免污染
14
+ */
15
+ _config?: Omit<IFetcherConfig, TFetcherConfigInternalFields>;
16
+ /**
17
+ * 根据 `method`、`url`、`urlBase`、`params`、`body` 计算得到的 MD5,可以被拦截器使用,比如缓存、合并时需要
18
+ */
19
+ _hash?: string;
20
+ /**
21
+ * 真正开始请求的时间,由最末一个 interceptor 计入,便于需要记录耗时的场景
22
+ */
23
+ _timeStarted?: number;
24
+ /**
25
+ * 标注在 Fetcher 内部由拦截器发起的二次请求
26
+ */
27
+ _byInterceptor?: boolean;
28
+ /**
29
+ * 超时时间
30
+ */
31
+ timeout?: number;
32
+ /**
33
+ * 接口 URL,若非绝对地址,将与 urlBase 结合使用
34
+ */
35
+ url?: string;
36
+ /**
37
+ * 如果指定 `urlBase` 且 `url` 不是绝对路径,会将两者拼接起来
38
+ *
39
+ * 注意 `urlBase` 和 `url` 之间不会被自动拼上 `/`,可认为它只是一个前缀
40
+ */
41
+ urlBase?: string;
42
+ /**
43
+ * 是否在 url 上拼接 `_cache_busting_` 参数,值用的是时间戳,一般来说不需要
44
+ */
45
+ urlCacheBusting?: boolean;
46
+ /**
47
+ * - 支持除了标准 HTTP 请求的 GET/POST/DELETE/PUT/PATCH + JSONP
48
+ * - 大小写无关,但内部一开始就会转成大写,建议统一用大写
49
+ * - 不要手动调用 HEAD/OPTIONS
50
+ *
51
+ * https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
52
+ */
53
+ method?: string;
54
+ /**
55
+ * URL search 参数,纯的 `fetch/jsonp` 的 url 要求是已经拼接好参数的 url
56
+ *
57
+ * `{ url: '/url', params: { a: 1, b: 2} }` 等价于 `{ url: '/url', params: 'a=1&b=2' }` 等价于 `{ url: '/url?a=1&b=2' }`
58
+ */
59
+ params?: TFetcherParams;
60
+ /**
61
+ * POST/PUT/DELETE 等请求体
62
+ */
63
+ body?: TFetcherBody;
64
+ /**
65
+ * 请求头
66
+ */
67
+ headers?: FetcherHeaders;
68
+ /**
69
+ * 如果传入的 `params` 是对象,用 `qs` 来序列化它的参数
70
+ */
71
+ serializeParams?: IFetcherParamsSerializeOptions;
72
+ /**
73
+ * 自定义 `body` 的 serialize
74
+ */
75
+ serializeBody?: IFetcherBodySerializeOptions;
76
+ /**
77
+ * 同 fetch 的 credentials
78
+ */
79
+ credentials?: RequestCredentials;
80
+ /**
81
+ * 对返回数据的处理,默认 `'json'`
82
+ */
83
+ responseType?: EFetcherResponseType | `${EFetcherResponseType}`;
84
+ /**
85
+ * 当 responseType 为 `blob-download`、`array-buffer-download` 时,可用它指定下载的文件名(无后缀)
86
+ */
87
+ downloadName?: string;
88
+ /**
89
+ * 允许 abort
90
+ */
91
+ signal?: AbortSignal | null;
92
+ /**
93
+ * 上传文件的情况,底层只能用 xhr 来获取进度
94
+ */
95
+ onProgress?(progress: number): void;
96
+ }
97
+ /**
98
+ * 允许拦截器对 FetcherConfig 进行扩展,在便捷方法和拦截器上可以安全使用扩展后的属性
99
+ */
100
+ export type TFetcherConfigX<X = object> = IFetcherConfig & X;
101
+ /**
102
+ * `new Fetcher` 时的 config,用于定义默认值,在执行请求时,将被传入的 config 混合
103
+ */
104
+ export interface IFetcherConfigDefault extends Pick<IFetcherConfig, 'timeout' | 'urlBase' | 'headers' | 'serializeParams' | 'serializeBody' | 'responseType'> {
105
+ }
106
+ /**
107
+ * 便捷 JSONP 方法,如果第一个参数为对象,则为 config
108
+ */
109
+ export type TFetcherConfigQuickJsonp<X = object> = Omit<IFetcherConfig, 'url' | 'method' | TFetcherConfigInternalFields> & X;
110
+ /**
111
+ * 其他便捷方法,如果第一个参数为对象,则为 config
112
+ */
113
+ export type TFetcherConfigQuick<X = object> = Omit<IFetcherConfig, 'url' | 'method' | TFetcherConfigInternalFields> & X;
114
+ export {};
@@ -0,0 +1,38 @@
1
+ import { FetcherHeadersFallbackNormalized } from '@fetchx/fetcher-helper-headers';
2
+ import { EFetcherErrorName } from '../enum';
3
+ import { IFetcherConfig } from './config';
4
+ /**
5
+ * Adapter 可能有或没有自己的错误名称定义,又需要的时候,应尽可能标准化为 Fetcher 定义好的名称
6
+ */
7
+ export type TErrorNameNormalizer = (errName: string) => EFetcherErrorName | undefined;
8
+ export interface IErrorExtendedInfo {
9
+ /**
10
+ * 预留扩展字段 - 错误码
11
+ */
12
+ code?: string;
13
+ /**
14
+ * 预留扩展字段 - 错误标题
15
+ */
16
+ title?: string;
17
+ /**
18
+ * 预留扩展字段 - 原始 response 中的 header
19
+ */
20
+ responseHeaders?: Headers | FetcherHeadersFallbackNormalized | null;
21
+ /**
22
+ * 预留扩展字段 - 原始 response 中的数据;强行把返回变成出错时需要
23
+ */
24
+ responseData?: unknown;
25
+ }
26
+ /**
27
+ * 错误
28
+ */
29
+ export interface IFetcherError extends Error, IErrorExtendedInfo {
30
+ config?: IFetcherConfig;
31
+ }
32
+ /**
33
+ * 特殊错误,用于绕过网络请求,直接返回 `result`,在某些拦截器中可以用到
34
+ */
35
+ export interface IFetcherErrorSkipNetwork<T = void> extends IFetcherError {
36
+ name: 'FetcherSkipNetwork';
37
+ result: T | Promise<T>;
38
+ }
@@ -0,0 +1,92 @@
1
+ import { IPromiseWithAbort } from './common';
2
+ import { TFetcherConfigX, TFetcherConfigQuick, TFetcherConfigQuickJsonp } from './config';
3
+ export type TFetcherArgsJsonp<P = void> = [string, P?] | [TFetcherConfigQuickJsonp, string, P?];
4
+ export type TFetcherArgsGet<P = void> = [string, P?] | [TFetcherConfigQuick, string, P?];
5
+ export type TFetcherArgsPost<B = void, P = void> = [string, B?, P?] | [TFetcherConfigQuick, string, B?, P?];
6
+ /**
7
+ * 执行请求的方法定义
8
+ */
9
+ export type TFetcherFnRequest<X = object> = <T = unknown>(config: TFetcherConfigX<X>) => Promise<T>;
10
+ /**
11
+ * 快捷方法定义 - JSONP
12
+ *
13
+ * ```js
14
+ * jsonp(url); // 无参调用
15
+ * jsonp(url, params); // 有参调用
16
+ * jsonp({ // 自定义额外配置,但无法覆盖 method
17
+ * timeout,
18
+ * charset,
19
+ * jsonpCallback,
20
+ * jsonpCallbackFunction
21
+ * }, url, params);
22
+ * ```
23
+ */
24
+ export interface IFetcherFnJsonp<X = object> {
25
+ <T = unknown>(url: string): Promise<T>;
26
+ <T = unknown, P = unknown>(url: string, params: P): Promise<T>;
27
+ <T = unknown>(config: TFetcherConfigQuickJsonp<X>, url: string): Promise<T>;
28
+ <T = unknown, P = unknown>(config: TFetcherConfigQuickJsonp<X>, url: string, params: P): Promise<T>;
29
+ }
30
+ export interface IFetcherFnJsonpWithAbort<X = object> {
31
+ <T = unknown>(url: string): IPromiseWithAbort<T>;
32
+ <T = unknown, P = unknown>(url: string, params: P): IPromiseWithAbort<T>;
33
+ <T = unknown>(config: TFetcherConfigQuickJsonp<X>, url: string): IPromiseWithAbort<T>;
34
+ <T = unknown, P = unknown>(config: TFetcherConfigQuickJsonp<X>, url: string, params: P): IPromiseWithAbort<T>;
35
+ }
36
+ /**
37
+ * 快捷方法定义 - 类 GET
38
+ *
39
+ * @example
40
+ *
41
+ * ```js
42
+ * get(url); // 无参调用
43
+ * get(url, params); // 有参调用
44
+ * get({ // 自定义额外配置,但无法覆盖 method
45
+ * headers: { ... },
46
+ * timeout: 1234
47
+ * }, url, params);
48
+ * ```
49
+ */
50
+ export interface IFetcherFnGet<X = object> {
51
+ <T = unknown>(url: string): Promise<T>;
52
+ <T = unknown, P = unknown>(url: string, params: P): Promise<T>;
53
+ <T = unknown>(config: TFetcherConfigQuick<X>, url: string): Promise<T>;
54
+ <T = unknown, P = unknown>(config: TFetcherConfigQuick<X>, url: string, params: P): Promise<T>;
55
+ }
56
+ export interface IFetcherFnGetWithAbort<X = object> {
57
+ <T = unknown>(url: string): IPromiseWithAbort<T>;
58
+ <T = unknown, P = unknown>(url: string, params: P): IPromiseWithAbort<T>;
59
+ <T = unknown>(config: TFetcherConfigQuick<X>, url: string): IPromiseWithAbort<T>;
60
+ <T = unknown, P = unknown>(config: TFetcherConfigQuick<X>, url: string, params: P): IPromiseWithAbort<T>;
61
+ }
62
+ /**
63
+ * 快捷方法定义 - 类 POST
64
+ *
65
+ * @example
66
+ *
67
+ * ```js
68
+ * post(url); // 无参调用
69
+ * post(url, body); // 有参调用(仅 body)
70
+ * post(url, body, params); // 有参调用,body 和 url 参数
71
+ * post({ // 自定义额外配置,但无法覆盖 method
72
+ * headers: { ... },
73
+ * timeout: 1234
74
+ * }, url, body, params);
75
+ * ```
76
+ */
77
+ export interface IFetcherFnPost<X = object> {
78
+ <T = unknown>(url: string): Promise<T>;
79
+ <T = unknown, B = unknown>(url: string, body: B): Promise<T>;
80
+ <T = unknown, B = unknown, P = unknown>(url: string, body: B, params: P): Promise<T>;
81
+ <T = unknown>(config: TFetcherConfigQuick<X>, url: string): Promise<T>;
82
+ <T = unknown, B = unknown>(config: TFetcherConfigQuick<X>, url: string, body: B): Promise<T>;
83
+ <T = unknown, B = unknown, P = unknown>(config: TFetcherConfigQuick<X>, url: string, body: B, params: P): Promise<T>;
84
+ }
85
+ export interface IFetcherFnPostWithAbort<X = object> {
86
+ <T = unknown>(url: string): Promise<T>;
87
+ <T = unknown, B = unknown>(url: string, body: B): IPromiseWithAbort<T>;
88
+ <T = unknown, B = unknown, P = unknown>(url: string, body: B, params: P): IPromiseWithAbort<T>;
89
+ <T = unknown>(config: TFetcherConfigQuick<X>, url: string): IPromiseWithAbort<T>;
90
+ <T = unknown, B = unknown>(config: TFetcherConfigQuick<X>, url: string, body: B): IPromiseWithAbort<T>;
91
+ <T = unknown, B = unknown, P = unknown>(config: TFetcherConfigQuick<X>, url: string, body: B, params: P): IPromiseWithAbort<T>;
92
+ }
@@ -0,0 +1,30 @@
1
+ import { IFetcherConfig, TFetcherConfigX } from './config';
2
+ import { IFetcherResponse } from './fetcher-response';
3
+ import { IFetcherError } from './fetcher-error';
4
+ import { TFetcherFnRequest } from './fetcher-fn';
5
+ export type TInterceptorEject = () => void;
6
+ export type TFetcherInterceptRequestReturn<X = object> = undefined | TFetcherConfigX<X> | Promise<undefined | TFetcherConfigX<X>>;
7
+ /**
8
+ * Request interceptor 方法类型
9
+ */
10
+ export type TFetcherInterceptRequest<X = object> = (config: TFetcherConfigX<X>, callRequest: TFetcherFnRequest<X>) => TFetcherInterceptRequestReturn<X>;
11
+ /**
12
+ * Response success interceptor 方法类型
13
+ * - T - 最终需要返回的 Promise 类型
14
+ * - D - 接口实际返回的 Promise 类型
15
+ */
16
+ export type TFetcherInterceptResponseFulfilled<T = unknown, D = T> = (data: D, config: IFetcherConfig, fetcherResponse: IFetcherResponse<T> | undefined, fetcherRequest: TFetcherFnRequest) => T | never;
17
+ /**
18
+ * Response error interceptor 方法类型
19
+ */
20
+ export type TFetcherInterceptResponseRejected<T = unknown> = (error: IFetcherError, config: IFetcherConfig, fetcherResponse: IFetcherResponse<T> | undefined, fetcherRequest: TFetcherFnRequest) => T | never;
21
+ export interface IInterceptorQueueItemBase {
22
+ priority?: number;
23
+ }
24
+ export interface IInterceptorQueueItemRequest<X = object> extends IInterceptorQueueItemBase {
25
+ onFulfilled?: TFetcherInterceptRequest<X>;
26
+ }
27
+ export interface IInterceptorQueueItemResponse extends IInterceptorQueueItemBase {
28
+ onFulfilled?: TFetcherInterceptResponseFulfilled;
29
+ onRejected?: TFetcherInterceptResponseRejected;
30
+ }
@@ -0,0 +1,10 @@
1
+ import { FetcherHeadersFallbackNormalized } from '@fetchx/fetcher-helper-headers';
2
+ /**
3
+ * 将 fetch 和 jsonp 各自的 response 同化后的类型,剔除 fetch Response 中不关心的部分(body、bodyUsed、ok、
4
+ * redirected、status、statusText、type 等),提取 `.json()` 后 resolve 的数据
5
+ */
6
+ export interface IFetcherResponse<T = unknown> {
7
+ readonly url: string;
8
+ readonly headers: Headers | FetcherHeadersFallbackNormalized;
9
+ readonly data: T;
10
+ }
@@ -0,0 +1,44 @@
1
+ import { FetcherHeadersNormalized } from '@fetchx/fetcher-helper-headers';
2
+ import { TFetcherConfigX } from './config';
3
+ import { TFetcherBodyNormalized } from './config-body';
4
+ import { IFetcherResponse } from './fetcher-response';
5
+ import { TInterceptorEject, TFetcherInterceptRequest, TFetcherInterceptResponseFulfilled, TFetcherInterceptResponseRejected } from './fetcher-interceptor';
6
+ import { IFetcherFnJsonp, IFetcherFnJsonpWithAbort, IFetcherFnGet, IFetcherFnGetWithAbort, IFetcherFnPost, IFetcherFnPostWithAbort } from './fetcher-fn';
7
+ /**
8
+ * 真正调用 adapter 执行网络请求前,Fetcher 会处理好完整的请求地址、标准的 Headers 和标准的 body(有的话),adapter 只需要安心使用即可
9
+ */
10
+ export type TFetcherAdapter = <T>(url: string, headers: FetcherHeadersNormalized, body: TFetcherBodyNormalized, config: TFetcherConfigX) => Promise<IFetcherResponse<T>>;
11
+ export interface IFetcherClass<X = object> {
12
+ /**
13
+ * 发送请求:前置请求拦截器 → 网络请求 → 后置响应拦截器
14
+ */
15
+ request<T = unknown>(config: TFetcherConfigX<X>): Promise<T>;
16
+ /**
17
+ * 添加「预设」请求拦截器,返回解除拦截的无参方法
18
+ */
19
+ interceptRequest(onFulfilled: TFetcherInterceptRequest, priority?: number): TInterceptorEject;
20
+ /**
21
+ * 添加「预设」响应拦截器,返回解除拦截的无参方法
22
+ */
23
+ interceptResponse(onFulfilled?: TFetcherInterceptResponseFulfilled, onRejected?: TFetcherInterceptResponseRejected, priority?: number): TInterceptorEject;
24
+ /**
25
+ * 对于「开箱即用」的 Fetcher 实例,由于是会被复用的单例,一般不希望它被扩展和修改,此操作不可逆
26
+ */
27
+ freeze(): void;
28
+ }
29
+ export interface IFetcher<X = object> extends Pick<IFetcherClass<X>, 'interceptRequest' | 'interceptResponse' | 'freeze' | 'request'> {
30
+ jsonp: IFetcherFnJsonp<X>;
31
+ get: IFetcherFnGet<X>;
32
+ post: IFetcherFnPost<X>;
33
+ put: IFetcherFnPost<X>;
34
+ patch: IFetcherFnPost<X>;
35
+ delete: IFetcherFnPost<X>;
36
+ withAbort: {
37
+ jsonp: IFetcherFnJsonpWithAbort<X>;
38
+ get: IFetcherFnGetWithAbort<X>;
39
+ post: IFetcherFnPostWithAbort<X>;
40
+ put: IFetcherFnPostWithAbort<X>;
41
+ patch: IFetcherFnPostWithAbort<X>;
42
+ delete: IFetcherFnPostWithAbort<X>;
43
+ };
44
+ }
@@ -0,0 +1,9 @@
1
+ export * from './common';
2
+ export * from './config-params';
3
+ export * from './config-body';
4
+ export * from './config';
5
+ export * from './fetcher-interceptor';
6
+ export * from './fetcher-response';
7
+ export * from './fetcher-error';
8
+ export * from './fetcher-fn';
9
+ export * from './fetcher';
@@ -0,0 +1,2 @@
1
+ import { IFetcherParamsSerializeOptions, TFetcherParams } from '../types';
2
+ export default function buildUrlWithParams(url: string, params?: TFetcherParams, options?: IFetcherParamsSerializeOptions): string;
@@ -0,0 +1,10 @@
1
+ import { IFetcherConfig } from '../types';
2
+ /**
3
+ * GET 类参数需要放到 URL 的 search 部分,这里参数 **可能** 由以下组成:
4
+ *
5
+ * - URL 中本身的 search,这部分是字符串
6
+ * - `params` 传入,一般是对象,但也有可能是字符串
7
+ *
8
+ * 这里,将以上参数混合在一起,生成一个新 URL
9
+ */
10
+ export default function buildUrl(config: IFetcherConfig): string;
@@ -0,0 +1 @@
1
+ export default function cloneTypeFormData(o: Record<string, unknown> | URLSearchParams | FormData): FormData;
@@ -0,0 +1 @@
1
+ export default function cloneTypeUrlSearchParams(o: Record<string, unknown> | URLSearchParams): URLSearchParams;
@@ -0,0 +1,8 @@
1
+ import { IErrorExtendedInfo, IFetcherConfig, IFetcherError } from '../types';
2
+ interface IOptions extends IErrorExtendedInfo {
3
+ originalError?: unknown;
4
+ name?: string;
5
+ message?: string;
6
+ }
7
+ export default function createFetcherError(config: IFetcherConfig, options?: IOptions): IFetcherError;
8
+ export {};
@@ -0,0 +1,2 @@
1
+ import { IFetcherBodySerializeOptions } from '../types';
2
+ export default function deserializeBody(body: string, options?: IFetcherBodySerializeOptions): Record<string, unknown>;
@@ -0,0 +1,2 @@
1
+ import { IFetcherBodySerializeOptions } from '../types';
2
+ export default function deserializeParams(params: string, options?: IFetcherBodySerializeOptions): Record<string, unknown>;
@@ -0,0 +1,2 @@
1
+ import { IFetcherError } from '../types';
2
+ export default function ensureFetcherError(originalError: unknown): IFetcherError;
@@ -0,0 +1,2 @@
1
+ import { IFetcherConfig, IFetcherResponse } from '../types';
2
+ export default function getDownloadFilename(config: IFetcherConfig, fetcherResponse?: IFetcherResponse): string;
@@ -0,0 +1,5 @@
1
+ import { IFetcherConfig } from '../types';
2
+ /**
3
+ * 根据 config 中的相关数据创建 Hash,可用于判断两个请求是否等价,带上 URL 是为了能够更具辨识度,且能够允许移除
4
+ */
5
+ export default function getHashFromConfig(config: IFetcherConfig): string;
@@ -0,0 +1,55 @@
1
+ import { FetcherHeadersNormalized } from '@fetchx/fetcher-helper-headers';
2
+ import { IFetcherConfig, TFetcherBodyNormalized } from '../types';
3
+ /**
4
+ * 处理 headers 和 body
5
+ *
6
+ * 如果 fetch 对 GET/HEAD 请求传入 body,哪怕只是一个空字符串,
7
+ * 浏览器就会直接拒绝并报错「HEAD or GET Request cannot have a body.」
8
+ *
9
+ * 当 `request.headers['Content-Type']` 不存在时,会自动设置 `Content-Type` 的:
10
+ *
11
+ * ### URLSearchParams
12
+ *
13
+ * 自动设置 `Content-Type` 为 `application/x-www-form-urlencoded;charset=UTF-8`,其他类型虽能发送,但可能没什么用
14
+ *
15
+ * → 删除 `headers` 中可能有的 `Content-Type`
16
+ *
17
+ * ```js
18
+ * fetch('/URLSearchParams', {
19
+ * method: 'POST',
20
+ * body: new URLSearchParams({ a, 1, b: 2 })
21
+ * });
22
+ * ```
23
+ *
24
+ * ### FormData
25
+ *
26
+ * 自动设置 `Content-Type` 为 `multipart/form-data; boundary---...`,其他类型浏览器会拒绝,因此需要踢掉设置的值
27
+ *
28
+ * → 删除 `headers` 中可能有的 `Content-Type`
29
+ *
30
+ * ```js
31
+ * fetch('/FormData', {
32
+ * method: 'POST',
33
+ * body: new FormData()
34
+ * });
35
+ * ```
36
+ *
37
+ * ### Blob
38
+ *
39
+ * 跟 `new Blob(['...'], { type: 'text/plain' })` 的 `type` 有关
40
+ *
41
+ * → 删除 `headers` 中可能有的 `Content-Type`
42
+ *
43
+ * ```js
44
+ * fetch('/Blob', {
45
+ * method: 'POST',
46
+ * body: new Blob(['hello'], { type: 'text/plain' })
47
+ * });
48
+ *
49
+ * fetch('/Blob', {
50
+ * method: 'POST',
51
+ * body: new Blob(['{"hello": "world"}'], { type: 'application/json' })
52
+ * });
53
+ * ```
54
+ */
55
+ export default function getHeadersAndBodyFromConfig(config: IFetcherConfig): [FetcherHeadersNormalized, TFetcherBodyNormalized];
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 提取 protocol 和 host,如果能够提取到则说明 url 是绝对地址
3
+ */
4
+ export default function getUrlOrigin(url?: string): string;
@@ -0,0 +1,18 @@
1
+ export { default as isInstanceofBlob } from './is-instanceof-blob';
2
+ export { default as isInstanceofArrayBuffer } from './is-instanceof-array-buffer';
3
+ export { default as isInstanceofFormData } from './is-instanceof-form-data';
4
+ export { default as isInstanceofUrlSearchParams } from './is-instanceof-url-search-params';
5
+ export { default as isConfigCors } from './is-config-cors';
6
+ export { default as isConfigJsonp } from './is-config-jsonp';
7
+ export { default as getHeadersAndBodyFromConfig } from './get-headers-and-body-from-config';
8
+ export { default as getHashFromConfig } from './get-hash-from-config';
9
+ export { default as getDownloadFilename } from './get-download-filename';
10
+ export { default as cloneTypeFormData } from './clone-type-form-data';
11
+ export { default as cloneTypeUrlSearchParams } from './clone-type-url-search-params';
12
+ export { default as parseArgsGet } from './parse-args-get';
13
+ export { default as parseArgsPost } from './parse-args-post';
14
+ export { default as buildUrl } from './build-url';
15
+ export { default as mergeConfig } from './merge-config';
16
+ export { default as createFetcherError } from './create-fetcher-error';
17
+ export { default as queueInterceptor } from './queue-interceptor';
18
+ export { default as sortInterceptors } from './sort-interceptors';
@@ -0,0 +1,5 @@
1
+ import { IFetcherConfig } from '../types';
2
+ /**
3
+ * 是否可以包含 body
4
+ */
5
+ export default function isConfigAllowBody(config: IFetcherConfig): boolean;