index.d.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. export type HttpTask = UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask;
  2. export type HttpRequestTask = UniApp.RequestTask;
  3. export type HttpUploadTask = UniApp.UploadTask;
  4. export type HttpDownloadTask = UniApp.DownloadTask;
  5. export type HttpMethod =
  6. "GET"
  7. | "POST"
  8. | "PUT"
  9. | "DELETE"
  10. | "CONNECT"
  11. | "HEAD"
  12. | "OPTIONS"
  13. | "TRACE"
  14. | "UPLOAD"
  15. | "DOWNLOAD";
  16. export type HttpRequestHeader = Record<string, string>;
  17. export type HttpParams = Record<string, any>;
  18. export type HttpData = Record<string, any>;
  19. export type HttpResponseType = 'arraybuffer' | 'text';
  20. export type HttpCustom = Record<string, any>;
  21. export type HttpFileType = 'image' | 'video' | 'audio';
  22. export type HttpFormData = Record<string, any>;
  23. export type HttpResponseHeader = Record<string, string> & {
  24. "set-cookie"?: string[]
  25. };
  26. export interface HttpRequestConfig<T = HttpTask> {
  27. /** @desc 请求服务器接口地址 */
  28. url?: string;
  29. /** @desc 请求方式,默认为 GET */
  30. method?: HttpMethod;
  31. /** @desc 请求基地址 */
  32. baseURL?: string;
  33. /** @desc 请求头信息,不能设置 Referer,App、H5 端会自动带上 cookie,且 H5 端不可手动修改 */
  34. header?: HttpRequestHeader;
  35. /** @desc 请求查询参数,自动拼接为查询字符串 */
  36. params?: HttpParams;
  37. /** @desc 请求体参数 */
  38. data?: HttpData;
  39. /** @desc 超时时间,单位 ms,默认为 60000,仅 H5 (HBuilderX 2.9.9+)、APP (HBuilderX 2.9.9+)、微信小程序 (2.10.0)、支付宝小程序支持 */
  40. timeout?: number;
  41. /** @desc 跨域请求时是否携带凭证 (cookies),默认为 false,仅 H5 (HBuilderX 2.6.15+) 支持 */
  42. withCredentials?: boolean;
  43. /** @desc 设置响应的数据类型,支付宝小程序不支持 */
  44. responseType?: HttpResponseType;
  45. /** @desc 全局自定义验证器 */
  46. validateStatus?: ((statusCode: number) => boolean) | null;
  47. /** params 参数自定义处理 */
  48. paramsSerializer?: (params: AnyObject) => string | void;
  49. /** @desc 默认为 json,如果设为 json,会尝试对返回的数据做一次 JSON.parse */
  50. dataType?: string;
  51. /** @desc DNS 解析时是否优先使用 ipv4,默认为 false,仅 App-Android (HBuilderX 2.8.0+) 支持 */
  52. firstIpv4?: boolean;
  53. /** @desc 是否验证 SSL 证书,默认为 true,仅 App-Android (HBuilderX 2.3.3+) 支持 */
  54. sslVerify?: boolean;
  55. /** @desc 开启 http2;微信小程序 */
  56. enableHttp2?: boolean;
  57. /** @desc 开启 quic;微信小程序 */
  58. enableQuic?: boolean;
  59. /** @desc 开启 cache;微信小程序、字节跳动小程序 2.31.0+ */
  60. enableCache?: boolean;
  61. /** @desc 开启 httpDNS;微信小程序 */
  62. enableHttpDNS?: boolean;
  63. /** @desc httpDNS 服务商;微信小程序 */
  64. httpDNSServiceId?: string;
  65. /** @desc 开启 transfer-encoding chunked;微信小程序 */
  66. enableChunked?: boolean;
  67. /** @desc wifi下使用移动网络发送请求;微信小程序 */
  68. forceCellularNetwork?: boolean;
  69. /** @desc 开启后可在headers中编辑cookie;支付宝小程序 10.2.33+ */
  70. enableCookie?: boolean;
  71. /** @desc 是否开启云加速;百度小程序 3.310.11+ */
  72. cloudCache?: boolean | object;
  73. /** @desc 控制当前请求是否延时至首屏内容渲染后发送;百度小程序 3.310.11+ */
  74. defer?: boolean;
  75. /** @desc 自定义参数 */
  76. custom?: HttpCustom;
  77. /** @desc 返回当前请求的 task 和 options,不要在这里修改 options */
  78. getTask?: (task: T, options: HttpRequestConfig<T>) => void;
  79. /** @desc 需要上传的文件列表,使用 files 时,filePath 和 name 不生效,仅支持 App、H5 (2.6.15+) */
  80. files?: { name?: string; file?: File; uri: string; }[];
  81. /** @desc 文件类型,仅支付宝小程序支持且为必填项 */
  82. fileType?: HttpFileType;
  83. /** @desc 要上传的文件对象,仅 H5 (2.6.15+) 支持 */
  84. file?: File;
  85. /** @desc 要上传文件资源的路径,使用 files 时,filePath 和 name 不生效 */
  86. filePath?: string;
  87. /** @desc 文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容,使用 files 时,filePath 和 name 不生效 */
  88. name?: string;
  89. /** @desc 请求中其他额外的 form data */
  90. formData?: HttpFormData;
  91. }
  92. export interface HttpResponse<T = any, D = HttpTask> {
  93. data: T;
  94. statusCode: number;
  95. header: HttpResponseHeader;
  96. config: HttpRequestConfig<D>;
  97. cookies: string[];
  98. errMsg: string;
  99. rawData: any;
  100. }
  101. export interface HttpUploadResponse<T = any, D = HttpTask> {
  102. data: T;
  103. statusCode: number;
  104. config: HttpRequestConfig<D>;
  105. errMsg: string;
  106. rawData: any;
  107. }
  108. export interface HttpDownloadResponse extends HttpResponse {
  109. tempFilePath: string;
  110. apFilePath?: string;
  111. filePath?: string;
  112. fileContent?: string;
  113. }
  114. export interface HttpError<T = any, D = HttpTask> {
  115. data?: T;
  116. statusCode?: number;
  117. header?: HttpResponseHeader;
  118. config: HttpRequestConfig<D>;
  119. cookies?: string[];
  120. errMsg: string;
  121. }
  122. export interface HttpPromise<T = any> extends Promise<HttpResponse<T>> {
  123. }
  124. export interface HttpInterceptorManager<V, E = V> {
  125. use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: E) => T | Promise<E>): void;
  126. eject(id: number): void;
  127. }
  128. export abstract class HttpRequestAbstract {
  129. constructor(config?: HttpRequestConfig);
  130. interceptors: {
  131. request: HttpInterceptorManager<HttpRequestConfig>;
  132. response: HttpInterceptorManager<HttpResponse, HttpError>;
  133. }
  134. request<T = any, R = HttpResponse<T>, D = HttpRequestTask>(config: HttpRequestConfig<D>): Promise<R>;
  135. get<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, config?: HttpRequestConfig<D>): Promise<R>;
  136. delete<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  137. head<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  138. options<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  139. post<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  140. put<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  141. config: HttpRequestConfig;
  142. setConfig<D = HttpTask>(onSend: (config: HttpRequestConfig<D>) => HttpRequestConfig<D>): void;
  143. connect<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  144. trace<T = any, R = HttpResponse<T>, D = HttpRequestTask>(url: string, data?: HttpData, config?: HttpRequestConfig<D>): Promise<R>;
  145. upload<T = any, R = HttpUploadResponse<T>, D = HttpUploadTask>(url: string, config?: HttpRequestConfig<D>): Promise<R>;
  146. download<T = any, R = HttpDownloadResponse<T>, D = HttpDownloadTask>(url: string, config?: HttpRequestConfig<D>): Promise<R>;
  147. middleware<T = any, R = HttpResponse<T>, D = HttpTask>(config: HttpRequestConfig<D>): Promise<R>;
  148. }
  149. declare class HttpRequest extends HttpRequestAbstract {
  150. }
  151. export default HttpRequest;