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