index.d.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. export interface FlyRequestConfig extends Object {
  2. url?: string;
  3. method?: string;
  4. baseURL?: string;
  5. headers?: any;
  6. body?: any;
  7. timeout?: number;
  8. withCredentials?: boolean;
  9. parseJson?: boolean;
  10. responseType?: string;
  11. }
  12. export interface FlyError {
  13. status: number;
  14. message: string;
  15. engine: XMLHttpRequest;
  16. request?: FlyRequestConfig;
  17. response?: FlyErrResponse;
  18. }
  19. export interface FlyResponse<T = any> {
  20. data: T;
  21. request: FlyRequestConfig;
  22. engine: XMLHttpRequest;
  23. headers: Object;
  24. }
  25. export interface FlyErrResponse {
  26. data: any;
  27. headers: Object;
  28. status: number;
  29. statusText: string;
  30. }
  31. export interface FlyPromise<T = any> extends Promise<FlyResponse<T>> {
  32. }
  33. export interface FlyRequestInterceptor<V> {
  34. use(onSend?: (request: V) => any): void;
  35. lock(): void;
  36. unlock(): void;
  37. clear(): void;
  38. }
  39. export interface FlyResponseInterceptor<V> {
  40. use(onSucceed?: (response: V) => any, onError?: (err: Error) => any): void;
  41. lock(): void;
  42. unlock(): void;
  43. clear(): void;
  44. }
  45. export interface Fly {
  46. config: FlyRequestConfig;
  47. interceptors: {
  48. request: FlyRequestInterceptor<FlyRequestConfig>;
  49. response:FlyResponseInterceptor<FlyResponse>;
  50. };
  51. engine:any;
  52. request<T = any>(url: string, data?: any, config?: FlyRequestConfig): FlyPromise<T>;
  53. get<T = any>(url: string, data?:any, config?: FlyRequestConfig): FlyPromise<T>;
  54. delete(url: string, data?:any, config?: FlyRequestConfig): FlyPromise;
  55. head(url: string,data?:any, config?: FlyRequestConfig): FlyPromise;
  56. post<T = any>(url: string, data?: any, config?: FlyRequestConfig): FlyPromise<T>;
  57. put<T = any>(url: string, data?: any, config?: FlyRequestConfig): FlyPromise<T>;
  58. patch<T = any>(url: string, data?: any, config?: FlyRequestConfig): FlyPromise<T>;
  59. all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
  60. spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
  61. lock(): void;
  62. unlock(): void;
  63. clear(): void;
  64. }
  65. declare const fly:Fly;
  66. export default fly;