index.d.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. type AnyObject = Record<string | number | symbol, any>
  2. type HttpPromise<T> = Promise<HttpResponse<T>>;
  3. type Tasks = UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask
  4. export interface RequestTask {
  5. abort: () => void;
  6. offHeadersReceived: () => void;
  7. onHeadersReceived: () => void;
  8. }
  9. export interface HttpRequestConfig<T = Tasks> {
  10. /** 请求基地址 */
  11. baseURL?: string;
  12. /** 请求服务器接口地址 */
  13. url?: string;
  14. /** 请求查询参数,自动拼接为查询字符串 */
  15. params?: AnyObject;
  16. /** 请求体参数 */
  17. data?: AnyObject;
  18. /** 文件对应的 key */
  19. name?: string;
  20. /** HTTP 请求中其他额外的 form data */
  21. formData?: AnyObject;
  22. /** 要上传文件资源的路径。 */
  23. filePath?: string;
  24. /** 需要上传的文件列表。使用 files 时,filePath 和 name 不生效,App、H5( 2.6.15+) */
  25. files?: Array<{
  26. name?: string;
  27. file?: File;
  28. uri: string;
  29. }>;
  30. /** 要上传的文件对象,仅H5(2.6.15+)支持 */
  31. file?: File;
  32. /** 请求头信息 */
  33. header?: AnyObject;
  34. /** 请求方式 */
  35. method?: "GET" | "POST" | "PUT" | "DELETE" | "CONNECT" | "HEAD" | "OPTIONS" | "TRACE" | "UPLOAD" | "DOWNLOAD";
  36. /** 如果设为 json,会尝试对返回的数据做一次 JSON.parse */
  37. dataType?: string;
  38. /** 设置响应的数据类型,支付宝小程序不支持 */
  39. responseType?: "text" | "arraybuffer";
  40. /** 自定义参数 */
  41. custom?: AnyObject;
  42. /** 超时时间,仅微信小程序(2.10.0)、支付宝小程序支持 */
  43. timeout?: number;
  44. /** DNS解析时优先使用ipv4,仅 App-Android 支持 (HBuilderX 2.8.0+) */
  45. firstIpv4?: boolean;
  46. /** 验证 ssl 证书 仅5+App安卓端支持(HBuilderX 2.3.3+) */
  47. sslVerify?: boolean;
  48. /** 跨域请求时是否携带凭证(cookies)仅H5支持(HBuilderX 2.6.15+) */
  49. withCredentials?: boolean;
  50. /** 返回当前请求的task, options。请勿在此处修改options。 */
  51. getTask?: (task: T, options: HttpRequestConfig<T>) => void;
  52. /** 全局自定义验证器 */
  53. validateStatus?: (statusCode: number) => boolean | void;
  54. }
  55. export interface HttpResponse<T = any> {
  56. config: HttpRequestConfig;
  57. statusCode: number;
  58. cookies: Array<string>;
  59. data: T;
  60. errMsg: string;
  61. header: AnyObject;
  62. }
  63. export interface HttpUploadResponse<T = any> {
  64. config: HttpRequestConfig;
  65. statusCode: number;
  66. data: T;
  67. errMsg: string;
  68. }
  69. export interface HttpDownloadResponse extends HttpResponse {
  70. tempFilePath: string;
  71. }
  72. export interface HttpError {
  73. config: HttpRequestConfig;
  74. statusCode?: number;
  75. cookies?: Array<string>;
  76. data?: any;
  77. errMsg: string;
  78. header?: AnyObject;
  79. }
  80. export interface HttpInterceptorManager<V, E = V> {
  81. use(
  82. onFulfilled?: (config: V) => Promise<V> | V,
  83. onRejected?: (config: E) => Promise<E> | E
  84. ): void;
  85. eject(id: number): void;
  86. }
  87. export abstract class HttpRequestAbstract {
  88. constructor(config?: HttpRequestConfig);
  89. config: HttpRequestConfig;
  90. interceptors: {
  91. request: HttpInterceptorManager<HttpRequestConfig, HttpRequestConfig>;
  92. response: HttpInterceptorManager<HttpResponse, HttpError>;
  93. }
  94. middleware<T = any>(config: HttpRequestConfig): HttpPromise<T>;
  95. request<T = any>(config: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  96. get<T = any>(url: string, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  97. upload<T = any>(url: string, config?: HttpRequestConfig<UniApp.UploadTask>): HttpPromise<T>;
  98. delete<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  99. head<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  100. post<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  101. put<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  102. connect<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  103. options<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  104. trace<T = any>(url: string, data?: AnyObject, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
  105. download(url: string, config?: HttpRequestConfig<UniApp.DownloadTask>): Promise<HttpDownloadResponse>;
  106. setConfig(onSend: (config: HttpRequestConfig) => HttpRequestConfig): void;
  107. }
  108. declare class HttpRequest extends HttpRequestAbstract { }
  109. export default HttpRequest;