249cca20a883933b445e965d64f5cf28b995c263e2555304a564b3e1c38817aa3c4bebbbf0c08ed806e275a965f4d8994029f91bccbd5edf744d3bc277e452 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { Observable } from '../Observable';
  2. import { AjaxConfig } from './types';
  3. import { AjaxResponse } from './AjaxResponse';
  4. export interface AjaxCreationMethod {
  5. /**
  6. * Creates an observable that will perform an AJAX request using the
  7. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  8. * global scope by default.
  9. *
  10. * This is the most configurable option, and the basis for all other AJAX calls in the library.
  11. *
  12. * ## Example
  13. *
  14. * ```ts
  15. * import { ajax } from 'rxjs/ajax';
  16. * import { map, catchError, of } from 'rxjs';
  17. *
  18. * const obs$ = ajax({
  19. * method: 'GET',
  20. * url: 'https://api.github.com/users?per_page=5',
  21. * responseType: 'json'
  22. * }).pipe(
  23. * map(userResponse => console.log('users: ', userResponse)),
  24. * catchError(error => {
  25. * console.log('error: ', error);
  26. * return of(error);
  27. * })
  28. * );
  29. * ```
  30. */
  31. <T>(config: AjaxConfig): Observable<AjaxResponse<T>>;
  32. /**
  33. * Perform an HTTP GET using the
  34. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  35. * global scope. Defaults to a `responseType` of `"json"`.
  36. *
  37. * ## Example
  38. *
  39. * ```ts
  40. * import { ajax } from 'rxjs/ajax';
  41. * import { map, catchError, of } from 'rxjs';
  42. *
  43. * const obs$ = ajax('https://api.github.com/users?per_page=5').pipe(
  44. * map(userResponse => console.log('users: ', userResponse)),
  45. * catchError(error => {
  46. * console.log('error: ', error);
  47. * return of(error);
  48. * })
  49. * );
  50. * ```
  51. */
  52. <T>(url: string): Observable<AjaxResponse<T>>;
  53. /**
  54. * Performs an HTTP GET using the
  55. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  56. * global scope by default, and a `responseType` of `"json"`.
  57. *
  58. * @param url The URL to get the resource from
  59. * @param headers Optional headers. Case-Insensitive.
  60. */
  61. get<T>(url: string, headers?: Record<string, string>): Observable<AjaxResponse<T>>;
  62. /**
  63. * Performs an HTTP POST using the
  64. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  65. * global scope by default, and a `responseType` of `"json"`.
  66. *
  67. * Before sending the value passed to the `body` argument, it is automatically serialized
  68. * based on the specified `responseType`. By default, a JavaScript object will be serialized
  69. * to JSON. A `responseType` of `application/x-www-form-urlencoded` will flatten any provided
  70. * dictionary object to a url-encoded string.
  71. *
  72. * @param url The URL to get the resource from
  73. * @param body The content to send. The body is automatically serialized.
  74. * @param headers Optional headers. Case-Insensitive.
  75. */
  76. post<T>(url: string, body?: any, headers?: Record<string, string>): Observable<AjaxResponse<T>>;
  77. /**
  78. * Performs an HTTP PUT using the
  79. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  80. * global scope by default, and a `responseType` of `"json"`.
  81. *
  82. * Before sending the value passed to the `body` argument, it is automatically serialized
  83. * based on the specified `responseType`. By default, a JavaScript object will be serialized
  84. * to JSON. A `responseType` of `application/x-www-form-urlencoded` will flatten any provided
  85. * dictionary object to a url-encoded string.
  86. *
  87. * @param url The URL to get the resource from
  88. * @param body The content to send. The body is automatically serialized.
  89. * @param headers Optional headers. Case-Insensitive.
  90. */
  91. put<T>(url: string, body?: any, headers?: Record<string, string>): Observable<AjaxResponse<T>>;
  92. /**
  93. * Performs an HTTP PATCH using the
  94. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  95. * global scope by default, and a `responseType` of `"json"`.
  96. *
  97. * Before sending the value passed to the `body` argument, it is automatically serialized
  98. * based on the specified `responseType`. By default, a JavaScript object will be serialized
  99. * to JSON. A `responseType` of `application/x-www-form-urlencoded` will flatten any provided
  100. * dictionary object to a url-encoded string.
  101. *
  102. * @param url The URL to get the resource from
  103. * @param body The content to send. The body is automatically serialized.
  104. * @param headers Optional headers. Case-Insensitive.
  105. */
  106. patch<T>(url: string, body?: any, headers?: Record<string, string>): Observable<AjaxResponse<T>>;
  107. /**
  108. * Performs an HTTP DELETE using the
  109. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  110. * global scope by default, and a `responseType` of `"json"`.
  111. *
  112. * @param url The URL to get the resource from
  113. * @param headers Optional headers. Case-Insensitive.
  114. */
  115. delete<T>(url: string, headers?: Record<string, string>): Observable<AjaxResponse<T>>;
  116. /**
  117. * Performs an HTTP GET using the
  118. * [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) in
  119. * global scope by default, and returns the hydrated JavaScript object from the
  120. * response.
  121. *
  122. * @param url The URL to get the resource from
  123. * @param headers Optional headers. Case-Insensitive.
  124. */
  125. getJSON<T>(url: string, headers?: Record<string, string>): Observable<T>;
  126. }
  127. /**
  128. * There is an ajax operator on the Rx object.
  129. *
  130. * It creates an observable for an Ajax request with either a request object with
  131. * url, headers, etc or a string for a URL.
  132. *
  133. * ## Examples
  134. *
  135. * Using `ajax()` to fetch the response object that is being returned from API
  136. *
  137. * ```ts
  138. * import { ajax } from 'rxjs/ajax';
  139. * import { map, catchError, of } from 'rxjs';
  140. *
  141. * const obs$ = ajax('https://api.github.com/users?per_page=5').pipe(
  142. * map(userResponse => console.log('users: ', userResponse)),
  143. * catchError(error => {
  144. * console.log('error: ', error);
  145. * return of(error);
  146. * })
  147. * );
  148. *
  149. * obs$.subscribe({
  150. * next: value => console.log(value),
  151. * error: err => console.log(err)
  152. * });
  153. * ```
  154. *
  155. * Using `ajax.getJSON()` to fetch data from API
  156. *
  157. * ```ts
  158. * import { ajax } from 'rxjs/ajax';
  159. * import { map, catchError, of } from 'rxjs';
  160. *
  161. * const obs$ = ajax.getJSON('https://api.github.com/users?per_page=5').pipe(
  162. * map(userResponse => console.log('users: ', userResponse)),
  163. * catchError(error => {
  164. * console.log('error: ', error);
  165. * return of(error);
  166. * })
  167. * );
  168. *
  169. * obs$.subscribe({
  170. * next: value => console.log(value),
  171. * error: err => console.log(err)
  172. * });
  173. * ```
  174. *
  175. * Using `ajax()` with object as argument and method POST with a two seconds delay
  176. *
  177. * ```ts
  178. * import { ajax } from 'rxjs/ajax';
  179. * import { map, catchError, of } from 'rxjs';
  180. *
  181. * const users = ajax({
  182. * url: 'https://httpbin.org/delay/2',
  183. * method: 'POST',
  184. * headers: {
  185. * 'Content-Type': 'application/json',
  186. * 'rxjs-custom-header': 'Rxjs'
  187. * },
  188. * body: {
  189. * rxjs: 'Hello World!'
  190. * }
  191. * }).pipe(
  192. * map(response => console.log('response: ', response)),
  193. * catchError(error => {
  194. * console.log('error: ', error);
  195. * return of(error);
  196. * })
  197. * );
  198. *
  199. * users.subscribe({
  200. * next: value => console.log(value),
  201. * error: err => console.log(err)
  202. * });
  203. * ```
  204. *
  205. * Using `ajax()` to fetch. An error object that is being returned from the request
  206. *
  207. * ```ts
  208. * import { ajax } from 'rxjs/ajax';
  209. * import { map, catchError, of } from 'rxjs';
  210. *
  211. * const obs$ = ajax('https://api.github.com/404').pipe(
  212. * map(userResponse => console.log('users: ', userResponse)),
  213. * catchError(error => {
  214. * console.log('error: ', error);
  215. * return of(error);
  216. * })
  217. * );
  218. *
  219. * obs$.subscribe({
  220. * next: value => console.log(value),
  221. * error: err => console.log(err)
  222. * });
  223. * ```
  224. */
  225. export declare const ajax: AjaxCreationMethod;
  226. export declare function fromAjax<T>(init: AjaxConfig): Observable<AjaxResponse<T>>;
  227. //# sourceMappingURL=ajax.d.ts.map