1 | import { AjaxObservable, AjaxCreationMethod } from './AjaxObservable';
|
---|
2 | /**
|
---|
3 | * There is an ajax operator on the Rx object.
|
---|
4 | *
|
---|
5 | * It creates an observable for an Ajax request with either a request object with
|
---|
6 | * url, headers, etc or a string for a URL.
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * ## Using ajax() to fetch the response object that is being returned from API.
|
---|
10 | * ```ts
|
---|
11 | * import { ajax } from 'rxjs/ajax';
|
---|
12 | * import { map, catchError } from 'rxjs/operators';
|
---|
13 | * import { of } from 'rxjs';
|
---|
14 | *
|
---|
15 | * const obs$ = ajax(`https://api.github.com/users?per_page=5`).pipe(
|
---|
16 | * map(userResponse => console.log('users: ', userResponse)),
|
---|
17 | * catchError(error => {
|
---|
18 | * console.log('error: ', error);
|
---|
19 | * return of(error);
|
---|
20 | * })
|
---|
21 | * );
|
---|
22 | *
|
---|
23 | * ```
|
---|
24 | *
|
---|
25 | * ## Using ajax.getJSON() to fetch data from API.
|
---|
26 | * ```ts
|
---|
27 | * import { ajax } from 'rxjs/ajax';
|
---|
28 | * import { map, catchError } from 'rxjs/operators';
|
---|
29 | * import { of } from 'rxjs';
|
---|
30 | *
|
---|
31 | * const obs$ = ajax.getJSON(`https://api.github.com/users?per_page=5`).pipe(
|
---|
32 | * map(userResponse => console.log('users: ', userResponse)),
|
---|
33 | * catchError(error => {
|
---|
34 | * console.log('error: ', error);
|
---|
35 | * return of(error);
|
---|
36 | * })
|
---|
37 | * );
|
---|
38 | *
|
---|
39 | * ```
|
---|
40 | *
|
---|
41 | * ## Using ajax() with object as argument and method POST with a two seconds delay.
|
---|
42 | * ```ts
|
---|
43 | * import { ajax } from 'rxjs/ajax';
|
---|
44 | * import { of } from 'rxjs';
|
---|
45 | *
|
---|
46 | * const users = ajax({
|
---|
47 | * url: 'https://httpbin.org/delay/2',
|
---|
48 | * method: 'POST',
|
---|
49 | * headers: {
|
---|
50 | * 'Content-Type': 'application/json',
|
---|
51 | * 'rxjs-custom-header': 'Rxjs'
|
---|
52 | * },
|
---|
53 | * body: {
|
---|
54 | * rxjs: 'Hello World!'
|
---|
55 | * }
|
---|
56 | * }).pipe(
|
---|
57 | * map(response => console.log('response: ', response)),
|
---|
58 | * catchError(error => {
|
---|
59 | * console.log('error: ', error);
|
---|
60 | * return of(error);
|
---|
61 | * })
|
---|
62 | * );
|
---|
63 | *
|
---|
64 | * ```
|
---|
65 | *
|
---|
66 | * ## Using ajax() to fetch. An error object that is being returned from the request.
|
---|
67 | * ```ts
|
---|
68 | * import { ajax } from 'rxjs/ajax';
|
---|
69 | * import { map, catchError } from 'rxjs/operators';
|
---|
70 | * import { of } from 'rxjs';
|
---|
71 | *
|
---|
72 | * const obs$ = ajax(`https://api.github.com/404`).pipe(
|
---|
73 | * map(userResponse => console.log('users: ', userResponse)),
|
---|
74 | * catchError(error => {
|
---|
75 | * console.log('error: ', error);
|
---|
76 | * return of(error);
|
---|
77 | * })
|
---|
78 | * );
|
---|
79 | *
|
---|
80 | * ```
|
---|
81 | */
|
---|
82 | export const ajax: AjaxCreationMethod = (() => AjaxObservable.create)();
|
---|