axios-miniprogram.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // global.d.ts
  2. type AnyObject<T = any> = Record<string, T>;
  3. // global.d.ts end
  4. interface CancelAction {
  5. /**
  6. * 取消信息
  7. */
  8. (message?: string): void;
  9. }
  10. interface CancelExecutor {
  11. (cancel: CancelAction): void;
  12. }
  13. interface CancelTokenSource {
  14. /**
  15. * 取消令牌
  16. */
  17. token: CancelToken;
  18. /**
  19. * 取消函数
  20. */
  21. cancel: CancelAction;
  22. }
  23. interface CancelTokenConstructor {
  24. new (executor: CancelExecutor): CancelToken;
  25. source(): CancelTokenSource;
  26. }
  27. declare class Cancel {
  28. message?: string;
  29. constructor(message?: string);
  30. toString(): string;
  31. }
  32. declare function isCancel(value: unknown): value is Cancel;
  33. declare class CancelToken {
  34. #private;
  35. constructor(executor: CancelExecutor);
  36. static source(): CancelTokenSource;
  37. }
  38. interface AxiosTransformCallback<TData = unknown> {
  39. (
  40. /**
  41. * 数据
  42. */
  43. data?: TData,
  44. /**
  45. * 头信息
  46. */
  47. headers?: AnyObject): TData | undefined;
  48. }
  49. type AxiosTransformer<TData = unknown> = AxiosTransformCallback<TData> | AxiosTransformCallback<TData>[];
  50. interface InterceptorResolved<T = unknown> {
  51. (value: T): T | Promise<T>;
  52. }
  53. interface InterceptorRejected<T = unknown> {
  54. (error: unknown): T | Promise<T>;
  55. }
  56. /**
  57. * 拦截器管理器
  58. */
  59. declare class InterceptorManager<T = unknown> {
  60. #private;
  61. /**
  62. * 添加拦截器
  63. *
  64. * @param resolved 成功的回调
  65. * @param rejected 失败的回调
  66. * @returns 拦截器标识符(可用于移除拦截器)
  67. */
  68. use(resolved: InterceptorResolved<T>, rejected?: InterceptorRejected<T>): number;
  69. /**
  70. * 移除拦截器
  71. *
  72. * @param id 拦截器标识符
  73. */
  74. eject(id: number): boolean;
  75. /**
  76. * 清空拦截器
  77. */
  78. clear(): void;
  79. }
  80. interface MiddlewareNext {
  81. (): Promise<void>;
  82. }
  83. /**
  84. * 中间件上下文
  85. */
  86. interface MiddlewareContext {
  87. /**
  88. * 请求体
  89. *
  90. * 同于请求配置
  91. */
  92. req: AxiosRequestConfig;
  93. /**
  94. * 响应体
  95. */
  96. res: null | AxiosResponse;
  97. }
  98. /**
  99. * 中间件
  100. */
  101. interface MiddlewareCallback {
  102. (ctx: MiddlewareContext, next: MiddlewareNext): Promise<void>;
  103. }
  104. /**
  105. * 请求方法
  106. */
  107. type AxiosRequestMethod = AxiosAdapterRequestMethod | 'options' | 'get' | 'head' | 'post' | 'put' | 'patch' | 'delete' | 'trace' | 'connect';
  108. /**
  109. * 请求头
  110. */
  111. interface AxiosRequestHeaders extends AnyObject {
  112. /**
  113. * 通用请求头
  114. */
  115. common?: AnyObject;
  116. /**
  117. * options 请求头
  118. */
  119. options?: AnyObject;
  120. /**
  121. * get 请求头
  122. */
  123. get?: AnyObject;
  124. /**
  125. * head 请求头
  126. */
  127. head?: AnyObject;
  128. /**
  129. * post 请求头
  130. */
  131. post?: AnyObject;
  132. /**
  133. * put 请求头
  134. */
  135. put?: AnyObject;
  136. /**
  137. * delete 请求头
  138. */
  139. delete?: AnyObject;
  140. /**
  141. * trace 请求头
  142. */
  143. trace?: AnyObject;
  144. /**
  145. * connect 请求头
  146. */
  147. connect?: AnyObject;
  148. }
  149. /**
  150. * 表单数据(上传会用到)
  151. */
  152. interface AxiosRequestFormData extends AnyObject {
  153. /**
  154. * 文件名
  155. */
  156. name: string;
  157. /**
  158. * 文件路径
  159. */
  160. filePath: string;
  161. }
  162. /**
  163. * 请求数据
  164. */
  165. type AxiosRequestData = string | AnyObject | ArrayBuffer | AxiosRequestFormData;
  166. /**
  167. * 响应数据
  168. */
  169. type AxiosResponseData = number | AxiosAdapterResponseData;
  170. /**
  171. * 进度对象
  172. */
  173. interface AxiosProgressEvent extends AnyObject {
  174. /**
  175. * 上传进度百分比
  176. */
  177. progress: number;
  178. }
  179. /**
  180. * 下载进度对象
  181. */
  182. interface AxiosDownloadProgressEvent extends AxiosProgressEvent {
  183. /**
  184. * 已经下载的数据长度,单位 Bytes
  185. */
  186. totalBytesWritten: number;
  187. /**
  188. * 预预期需要下载的数据总长度,单位 Bytes
  189. */
  190. totalBytesExpectedToWrite: number;
  191. }
  192. /**
  193. * 监听下载进度
  194. */
  195. interface AxiosDownloadProgressCallback {
  196. (event: AxiosDownloadProgressEvent): void;
  197. }
  198. /**
  199. * 上传进度对象
  200. */
  201. interface AxiosUploadProgressEvent extends AxiosProgressEvent {
  202. /**
  203. * 已经上传的数据长度,单位 Bytes
  204. */
  205. totalBytesSent: number;
  206. /**
  207. * 预期需要上传的数据总长度,单位 Bytes
  208. */
  209. totalBytesExpectedToSend: number;
  210. }
  211. /**
  212. * 监听上传进度
  213. */
  214. interface AxiosUploadProgressCallback {
  215. (event: AxiosUploadProgressEvent): void;
  216. }
  217. /**
  218. * 请求配置
  219. */
  220. interface AxiosRequestConfig extends Partial<Omit<AxiosAdapterRequestConfig, 'type' | 'success' | 'fail'>> {
  221. /**
  222. * 请求适配器
  223. */
  224. adapter?: AxiosAdapter;
  225. /**
  226. * 基础路径
  227. */
  228. baseURL?: string;
  229. /**
  230. * 请求的 URL
  231. */
  232. url?: string;
  233. /**
  234. * 请求参数
  235. */
  236. params?: AnyObject;
  237. /**
  238. * 请求数据
  239. */
  240. data?: AxiosRequestData;
  241. /**
  242. * 请求头
  243. */
  244. headers?: AxiosRequestHeaders;
  245. /**
  246. * 请求方法
  247. */
  248. method?: AxiosRequestMethod;
  249. /**
  250. * 取消令牌
  251. */
  252. cancelToken?: CancelToken;
  253. /**
  254. * 下载文件
  255. */
  256. download?: boolean;
  257. /**
  258. * 上传文件
  259. */
  260. upload?: boolean;
  261. /**
  262. * 请求参数系列化函数
  263. */
  264. paramsSerializer?: (params?: AnyObject) => string;
  265. /**
  266. * 校验状态码
  267. */
  268. validateStatus?: (status: number) => boolean;
  269. /**
  270. * 转换请求数据
  271. */
  272. transformRequest?: AxiosTransformer<AxiosRequestData>;
  273. /**
  274. * 转换响应数据
  275. */
  276. transformResponse?: AxiosTransformer<AxiosResponseData>;
  277. /**
  278. * 错误处理
  279. */
  280. errorHandler?: (error: unknown) => Promise<AxiosResponse>;
  281. /**
  282. * 监听下载进度
  283. */
  284. onDownloadProgress?: AxiosUploadProgressCallback;
  285. /**
  286. * 监听上传进度
  287. */
  288. onUploadProgress?: AxiosUploadProgressCallback;
  289. }
  290. /**
  291. * 响应体
  292. */
  293. interface AxiosResponse<TData extends AxiosResponseData = AxiosResponseData> extends AnyObject {
  294. /**
  295. * 状态码
  296. */
  297. status: number;
  298. /**
  299. * 状态字符
  300. */
  301. statusText: string;
  302. /**
  303. * 响应头
  304. */
  305. headers: AnyObject;
  306. /**
  307. * 响应数据
  308. */
  309. data: TData;
  310. /**
  311. * 请求配置
  312. */
  313. config: AxiosRequestConfig;
  314. /**
  315. * 请求任务
  316. */
  317. request?: AxiosAdapterPlatformTask;
  318. }
  319. /**
  320. * 错误体
  321. */
  322. interface AxiosResponseError extends AnyObject {
  323. /**
  324. * 状态码
  325. */
  326. status: number;
  327. /**
  328. * 状态字符
  329. */
  330. statusText: string;
  331. /**
  332. * 响应头
  333. */
  334. headers: AnyObject;
  335. /**
  336. * 错误数据
  337. */
  338. data: AnyObject;
  339. /**
  340. * 失败的请求,指没能够成功响应的请求
  341. */
  342. isFail: true;
  343. /**
  344. * 请求配置
  345. */
  346. config: AxiosRequestConfig;
  347. /**
  348. * 请求任务
  349. */
  350. request?: AxiosAdapterPlatformTask;
  351. }
  352. interface AxiosRequest {
  353. <TData extends AxiosResponseData>(config: AxiosRequestConfig): Promise<AxiosResponse<TData>>;
  354. <TData extends AxiosResponseData>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<TData>>;
  355. }
  356. /**
  357. * 普通的请求方法
  358. */
  359. type AxiosRequestMethodFn = <TData extends AxiosResponseData>(url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<TData>>;
  360. /**
  361. * 带参数的请求方法
  362. */
  363. type AxiosRequestMethodFnWithParams = <TData extends AxiosResponseData>(url: string, params?: AnyObject, config?: AxiosRequestConfig) => Promise<AxiosResponse<TData>>;
  364. /**
  365. * 带数据的请求方法
  366. */
  367. type AxiosRequestMethodFnWithData = <TData extends AxiosResponseData>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig) => Promise<AxiosResponse<TData>>;
  368. /**
  369. * Axios 构造函数
  370. */
  371. interface AxiosConstructor {
  372. new (config: AxiosRequestConfig): Axios;
  373. }
  374. declare class Axios {
  375. #private;
  376. /**
  377. * 默认请求配置
  378. */
  379. defaults: AxiosRequestConfig;
  380. /**
  381. * 拦截器
  382. */
  383. interceptors: {
  384. /**
  385. * 请求拦截器
  386. */
  387. request: InterceptorManager<AxiosRequestConfig>;
  388. /**
  389. * 响应拦截器
  390. */
  391. response: InterceptorManager<AxiosResponse<AxiosResponseData>>;
  392. };
  393. /**
  394. * 发送 options 请求
  395. */
  396. options: AxiosRequestMethodFn;
  397. /**
  398. * 发送 get 请求
  399. */
  400. get: AxiosRequestMethodFnWithParams;
  401. /**
  402. * 发送 head 请求
  403. */
  404. head: AxiosRequestMethodFnWithParams;
  405. /**
  406. * 发送 post 请求
  407. */
  408. post: AxiosRequestMethodFnWithData;
  409. /**
  410. * 发送 put 请求
  411. */
  412. put: AxiosRequestMethodFnWithData;
  413. /**
  414. * 发送 patch 请求
  415. */
  416. patch: AxiosRequestMethodFnWithData;
  417. /**
  418. * 发送 delete 请求
  419. */
  420. delete: AxiosRequestMethodFnWithParams;
  421. /**
  422. * 发送 trace 请求
  423. */
  424. trace: AxiosRequestMethodFn;
  425. /**
  426. * 发送 connect 请求
  427. */
  428. connect: AxiosRequestMethodFn;
  429. /**
  430. *
  431. * @param config 默认配置
  432. * @param parent 父级实例
  433. */
  434. constructor(config: AxiosRequestConfig, parent?: Axios);
  435. /**
  436. * 发送请求
  437. */
  438. request: AxiosRequest;
  439. /**
  440. * 注册中间件
  441. *
  442. * 示例1:注册一个中间件
  443. * ```ts
  444. * axios.use(async function middleware(ctx, next) {
  445. * console.log(ctx.req);
  446. * await next();
  447. * console.log(ctx.res);
  448. * });
  449. * ```
  450. *
  451. * 示例2:链式注册多个中间件
  452. * ```ts
  453. * axios
  454. * .use(async function middleware1(ctx, next) {
  455. * console.log(ctx.req);
  456. * await next();
  457. * console.log(ctx.res);
  458. * })
  459. * .use(async function middleware2(ctx, next) {
  460. * console.log(ctx.req);
  461. * await next();
  462. * console.log(ctx.res);
  463. * });
  464. * ```
  465. */
  466. use: (middleware: MiddlewareCallback) => this;
  467. }
  468. /**
  469. * 适配器请求方法
  470. */
  471. type AxiosAdapterRequestMethod = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'TRACE' | 'CONNECT';
  472. /**
  473. * 适配器请求数据
  474. */
  475. type AxiosAdapterRequestData = string | AnyObject | ArrayBuffer;
  476. /**
  477. * 适配器响应数据
  478. */
  479. type AxiosAdapterResponseData = string | ArrayBuffer | AnyObject;
  480. /**
  481. * 适配器响应体
  482. */
  483. interface AxiosAdapterResponse extends AnyObject {
  484. /**
  485. * 状态码
  486. */
  487. status?: number;
  488. /**
  489. * 状态字符
  490. */
  491. statusText?: string;
  492. /**
  493. * 响应头
  494. */
  495. headers?: AnyObject;
  496. /**
  497. * 响应数据
  498. */
  499. data: AxiosAdapterResponseData;
  500. }
  501. /**
  502. * 适配器错误体
  503. */
  504. interface AxiosAdapterResponseError extends AnyObject {
  505. /**
  506. * 状态码
  507. */
  508. status?: number;
  509. /**
  510. * 状态字符
  511. */
  512. statusText?: string;
  513. /**
  514. * 响应头
  515. */
  516. headers?: AnyObject;
  517. /**
  518. * 错误数据
  519. */
  520. data?: AnyObject;
  521. }
  522. /**
  523. * 适配器请求配置
  524. */
  525. interface AxiosAdapterRequestConfig extends AnyObject {
  526. /**
  527. * 请求类型
  528. */
  529. type: 'request' | 'upload' | 'download';
  530. /**
  531. * 开发者服务器接口地址
  532. */
  533. url: string;
  534. /**
  535. * HTTP 请求方法
  536. */
  537. method: AxiosAdapterRequestMethod;
  538. /**
  539. * 请求参数
  540. */
  541. params?: AnyObject;
  542. /**
  543. * 请求数据
  544. */
  545. data?: AxiosAdapterRequestData;
  546. /**
  547. * 请求头
  548. */
  549. headers?: AnyObject;
  550. /**
  551. * 返回的数据格式
  552. */
  553. dataType?: 'json' | '其他';
  554. /**
  555. * 响应的数据类型
  556. */
  557. responseType?: 'text' | 'arraybuffer';
  558. /**
  559. * 超时时间,单位为毫秒。默认值为 60000
  560. */
  561. timeout?: number;
  562. /**
  563. * 成功的回调
  564. */
  565. success(response: AxiosAdapterResponse): void;
  566. /**
  567. * 失败的回调
  568. */
  569. fail(error: AxiosAdapterResponseError): void;
  570. }
  571. /**
  572. * 请求函数基本选项
  573. */
  574. interface AxiosAdapterBaseOptions extends AxiosAdapterRequestConfig {
  575. /**
  576. * 请求头,同 headers
  577. */
  578. header?: AxiosRequestHeaders;
  579. /**
  580. * 成功的回调
  581. */
  582. success(response: AnyObject): void;
  583. /**
  584. * 失败的回调
  585. */
  586. fail(error: AnyObject): void;
  587. }
  588. /**
  589. * 请求函数选项
  590. */
  591. type AxiosAdapterRequestOptions = AxiosAdapterBaseOptions;
  592. /**
  593. * 下载函数选项
  594. */
  595. interface AxiosAdapterDownloadOptions extends AxiosAdapterBaseOptions {
  596. /**
  597. * 文件下载后存储的路径
  598. */
  599. filePath?: string;
  600. }
  601. /**
  602. * 上传函数选项
  603. */
  604. interface AxiosAdapterUploadOptions extends AxiosAdapterBaseOptions, AxiosRequestFormData {
  605. /**
  606. * [钉钉小程序用 fileName 代替 name](https://open.dingtalk.com/document/orgapp/dd-upload-objects#title-ngk-rr1-eow)
  607. */
  608. fileName: string;
  609. /**
  610. * 钉钉小程序|支付宝小程序特有参数
  611. */
  612. fileType?: 'image' | 'video' | 'audie';
  613. /**
  614. * 额外的数据
  615. */
  616. formData?: AnyObject;
  617. }
  618. /**
  619. * 请求函数
  620. */
  621. interface AxiosAdapterRequest {
  622. (config: AxiosAdapterRequestOptions): AxiosAdapterPlatformTask;
  623. }
  624. /**
  625. * 下载函数
  626. */
  627. interface AxiosAdapterDownload {
  628. (config: AxiosAdapterDownloadOptions): AxiosAdapterPlatformTask;
  629. }
  630. /**
  631. * 上传函数
  632. */
  633. interface AxiosAdapterUpload {
  634. (config: AxiosAdapterUploadOptions): AxiosAdapterPlatformTask;
  635. }
  636. /**
  637. * 适配器平台
  638. */
  639. interface AxiosAdapterPlatform {
  640. /**
  641. * 发送请求
  642. */
  643. request: AxiosAdapterRequest;
  644. /**
  645. * 下载文件
  646. */
  647. download: AxiosAdapterDownload;
  648. /**
  649. * 上传文件
  650. */
  651. upload: AxiosAdapterUpload;
  652. }
  653. /**
  654. * 适配器平台请求任务
  655. */
  656. type AxiosAdapterPlatformTask = undefined | void | {
  657. abort?(): void;
  658. onProgressUpdate?(callback: (event: AxiosProgressEvent) => void): void;
  659. offProgressUpdate?(callback: (event: AxiosProgressEvent) => void): void;
  660. };
  661. /**
  662. * 适配器函数
  663. */
  664. interface AxiosAdapter {
  665. (config: AxiosAdapterRequestConfig): AxiosAdapterPlatformTask;
  666. }
  667. /**
  668. * 创建适配器
  669. *
  670. * @param platform 平台 API 对象
  671. */
  672. declare function createAdapter(platform: AxiosAdapterPlatform): (config: AxiosAdapterRequestConfig) => AxiosAdapterPlatformTask;
  673. type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
  674. declare class AxiosError extends Error {
  675. config: AxiosRequestConfig;
  676. request: AxiosAdapterPlatformTask;
  677. response: AxiosErrorResponse;
  678. constructor(message: string, config: AxiosRequestConfig, response: AxiosErrorResponse, request: AxiosAdapterPlatformTask);
  679. }
  680. declare function isAxiosError(value: unknown): value is AxiosError;
  681. /**
  682. * axios 实例默认配置
  683. */
  684. interface AxiosInstanceDefaults extends AxiosRequestConfig {
  685. /**
  686. * 请求头
  687. */
  688. headers: Required<AxiosRequestHeaders>;
  689. }
  690. /**
  691. * axios 实例
  692. */
  693. interface AxiosInstance extends AxiosRequest, Axios {
  694. /**
  695. * 默认请求配置
  696. */
  697. defaults: AxiosInstanceDefaults;
  698. /**
  699. * 获取 URI
  700. *
  701. * @param config 配置
  702. */
  703. getUri(config: AxiosRequestConfig): string;
  704. /**
  705. * 创建实例
  706. *
  707. * @param config 默认配置
  708. */
  709. create(config?: AxiosRequestConfig): AxiosInstance;
  710. /**
  711. * 扩展实例
  712. *
  713. * @param config 默认配置
  714. */
  715. extend(config: AxiosRequestConfig): AxiosInstance;
  716. /**
  717. * 派生领域
  718. *
  719. * @param config 默认配置
  720. *
  721. * @deprecated 请使用 extend 替换 fork
  722. */
  723. fork(config: AxiosRequestConfig): AxiosInstance;
  724. }
  725. /**
  726. * axios 静态对象
  727. */
  728. interface AxiosStatic extends AxiosInstance {
  729. /**
  730. * 版本号
  731. */
  732. version: string;
  733. /**
  734. * Axios 类
  735. */
  736. Axios: AxiosConstructor;
  737. /**
  738. * 取消令牌
  739. */
  740. CancelToken: CancelTokenConstructor;
  741. /**
  742. * 创建适配器
  743. */
  744. createAdapter: typeof createAdapter;
  745. /**
  746. * 传入取消请求错误返回 true
  747. */
  748. isCancel: typeof isCancel;
  749. /**
  750. * 传入响应错误返回 true
  751. */
  752. isAxiosError: typeof isAxiosError;
  753. }
  754. declare const axios: AxiosStatic;
  755. declare const version = "2.5.0";
  756. export { Axios, AxiosAdapter, AxiosAdapterDownload, AxiosAdapterDownloadOptions, AxiosAdapterPlatform, AxiosAdapterPlatformTask, AxiosAdapterRequest, AxiosAdapterRequestConfig, AxiosAdapterRequestMethod, AxiosAdapterRequestOptions, AxiosAdapterResponse, AxiosAdapterResponseData, AxiosAdapterResponseError, AxiosAdapterUpload, AxiosAdapterUploadOptions, AxiosDownloadProgressCallback, AxiosDownloadProgressEvent, AxiosInstance, AxiosInstanceDefaults, AxiosRequestConfig, AxiosRequestData, AxiosRequestFormData, AxiosRequestHeaders, AxiosRequestMethod, AxiosResponse, AxiosResponseData, AxiosResponseError, AxiosStatic, AxiosUploadProgressCallback, AxiosUploadProgressEvent, CancelToken, MiddlewareCallback, MiddlewareContext, MiddlewareNext, createAdapter, axios as default, isAxiosError, isCancel, version };