AjaxObservable.d.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Observable } from '../../Observable';
  2. import { Subscriber } from '../../Subscriber';
  3. import { TeardownLogic } from '../../Subscription';
  4. export interface AjaxRequest {
  5. url?: string;
  6. body?: any;
  7. user?: string;
  8. async?: boolean;
  9. method?: string;
  10. headers?: Object;
  11. timeout?: number;
  12. password?: string;
  13. hasContent?: boolean;
  14. crossDomain?: boolean;
  15. withCredentials?: boolean;
  16. createXHR?: () => XMLHttpRequest;
  17. progressSubscriber?: Subscriber<any>;
  18. responseType?: string;
  19. }
  20. export interface AjaxCreationMethod {
  21. (urlOrRequest: string | AjaxRequest): Observable<AjaxResponse>;
  22. get(url: string, headers?: Object): Observable<AjaxResponse>;
  23. post(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  24. put(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  25. patch(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  26. delete(url: string, headers?: Object): Observable<AjaxResponse>;
  27. getJSON<T>(url: string, headers?: Object): Observable<T>;
  28. }
  29. export declare function ajaxGet(url: string, headers?: Object): AjaxObservable<AjaxResponse>;
  30. export declare function ajaxPost(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  31. export declare function ajaxDelete(url: string, headers?: Object): Observable<AjaxResponse>;
  32. export declare function ajaxPut(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  33. export declare function ajaxPatch(url: string, body?: any, headers?: Object): Observable<AjaxResponse>;
  34. export declare function ajaxGetJSON<T>(url: string, headers?: Object): Observable<T>;
  35. /**
  36. * We need this JSDoc comment for affecting ESDoc.
  37. * @extends {Ignored}
  38. * @hide true
  39. */
  40. export declare class AjaxObservable<T> extends Observable<T> {
  41. /**
  42. * Creates an observable for an Ajax request with either a request object with
  43. * url, headers, etc or a string for a URL.
  44. *
  45. * @example
  46. * source = Rx.Observable.ajax('/products');
  47. * source = Rx.Observable.ajax({ url: 'products', method: 'GET' });
  48. *
  49. * @param {string|Object} request Can be one of the following:
  50. * A string of the URL to make the Ajax call.
  51. * An object with the following properties
  52. * - url: URL of the request
  53. * - body: The body of the request
  54. * - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE
  55. * - async: Whether the request is async
  56. * - headers: Optional headers
  57. * - crossDomain: true if a cross domain request, else false
  58. * - createXHR: a function to override if you need to use an alternate
  59. * XMLHttpRequest implementation.
  60. * - resultSelector: a function to use to alter the output value type of
  61. * the Observable. Gets {@link AjaxResponse} as an argument.
  62. * @return {Observable} An observable sequence containing the XMLHttpRequest.
  63. * @static true
  64. * @name ajax
  65. * @owner Observable
  66. */
  67. static create: AjaxCreationMethod;
  68. private request;
  69. constructor(urlOrRequest: string | AjaxRequest);
  70. /** @deprecated internal use only */ _subscribe(subscriber: Subscriber<T>): TeardownLogic;
  71. }
  72. /**
  73. * We need this JSDoc comment for affecting ESDoc.
  74. * @ignore
  75. * @extends {Ignored}
  76. */
  77. export declare class AjaxSubscriber<T> extends Subscriber<Event> {
  78. request: AjaxRequest;
  79. private xhr;
  80. private done;
  81. constructor(destination: Subscriber<T>, request: AjaxRequest);
  82. next(e: Event): void;
  83. private send();
  84. private serializeBody(body, contentType?);
  85. private setHeaders(xhr, headers);
  86. private setupEvents(xhr, request);
  87. unsubscribe(): void;
  88. }
  89. /**
  90. * A normalized AJAX response.
  91. *
  92. * @see {@link ajax}
  93. *
  94. * @class AjaxResponse
  95. */
  96. export declare class AjaxResponse {
  97. originalEvent: Event;
  98. xhr: XMLHttpRequest;
  99. request: AjaxRequest;
  100. /** @type {number} The HTTP status code */
  101. status: number;
  102. /** @type {string|ArrayBuffer|Document|object|any} The response data */
  103. response: any;
  104. /** @type {string} The raw responseText */
  105. responseText: string;
  106. /** @type {string} The responseType (e.g. 'json', 'arraybuffer', or 'xml') */
  107. responseType: string;
  108. constructor(originalEvent: Event, xhr: XMLHttpRequest, request: AjaxRequest);
  109. }
  110. /**
  111. * A normalized AJAX error.
  112. *
  113. * @see {@link ajax}
  114. *
  115. * @class AjaxError
  116. */
  117. export declare class AjaxError extends Error {
  118. /** @type {XMLHttpRequest} The XHR instance associated with the error */
  119. xhr: XMLHttpRequest;
  120. /** @type {AjaxRequest} The AjaxRequest associated with the error */
  121. request: AjaxRequest;
  122. /** @type {number} The HTTP status code */
  123. status: number;
  124. /** @type {string} The responseType (e.g. 'json', 'arraybuffer', or 'xml') */
  125. responseType: string;
  126. /** @type {string|ArrayBuffer|Document|object|any} The response data */
  127. response: any;
  128. constructor(message: string, xhr: XMLHttpRequest, request: AjaxRequest);
  129. }
  130. /**
  131. * @see {@link ajax}
  132. *
  133. * @class AjaxTimeoutError
  134. */
  135. export declare class AjaxTimeoutError extends AjaxError {
  136. constructor(xhr: XMLHttpRequest, request: AjaxRequest);
  137. }