index.d.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. export type BodyCallback = (
  2. error: Error,
  3. body: any
  4. ) => void;
  5. export type HttpResponseHandler = (
  6. callback: BodyCallback,
  7. decodeResponseBody: boolean
  8. ) => XhrCallback;
  9. export type XhrCallback = (
  10. error: Error,
  11. response: XhrResponse,
  12. body: any
  13. ) => void;
  14. export interface XhrResponse {
  15. body: Object | string;
  16. statusCode: number;
  17. method: string;
  18. headers: XhrHeaders;
  19. url: string;
  20. rawRequest: XMLHttpRequest;
  21. }
  22. export interface XhrHeaders {
  23. [key: string]: string;
  24. }
  25. export interface XhrBaseConfig {
  26. useXDR?: boolean;
  27. sync?: boolean;
  28. method?: 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH';
  29. timeout?: number;
  30. headers?: XhrHeaders;
  31. body?: string | any;
  32. json?: boolean;
  33. username?: string;
  34. password?: string;
  35. withCredentials?: boolean;
  36. responseType?: '' | 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
  37. beforeSend?: (xhrObject: XMLHttpRequest) => void;
  38. xhr?: XMLHttpRequest;
  39. }
  40. export interface XhrUriConfig extends XhrBaseConfig {
  41. uri: string;
  42. }
  43. export interface XhrUrlConfig extends XhrBaseConfig {
  44. url: string;
  45. }
  46. export interface XhrInstance {
  47. (options: XhrUriConfig | XhrUrlConfig, callback: XhrCallback): any;
  48. (url: string, callback: XhrCallback): any;
  49. (url: string, options: XhrBaseConfig, callback: XhrCallback): any;
  50. }
  51. export interface XhrStatic extends XhrInstance {
  52. del: XhrInstance;
  53. get: XhrInstance;
  54. head: XhrInstance;
  55. patch: XhrInstance;
  56. post: XhrInstance;
  57. put: XhrInstance;
  58. }
  59. declare const Xhr: XhrStatic;
  60. export default Xhr;