| 1 |
import { PartialObserver } from '../types'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Valid Ajax direction types. Prefixes the event `type` in the |
| 5 |
* {@link AjaxResponse} object with "upload_" for events related |
| 6 |
* to uploading and "download_" for events related to downloading. |
| 7 |
*/ |
| 8 |
export type AjaxDirection = 'upload' | 'download'; |
| 9 |
|
| 10 |
export type ProgressEventType = 'loadstart' | 'progress' | 'load'; |
| 11 |
|
| 12 |
export type AjaxResponseType = `${AjaxDirection}_${ProgressEventType}`; |
| 13 |
|
| 14 |
/** |
| 15 |
* The object containing values RxJS used to make the HTTP request. |
| 16 |
* |
| 17 |
* This is provided in {@link AjaxError} instances as the `request` |
| 18 |
* object. |
| 19 |
*/ |
| 20 |
export interface AjaxRequest { |
| 21 |
/** |
| 22 |
* The URL requested. |
| 23 |
*/ |
| 24 |
url: string; |
| 25 |
|
| 26 |
/** |
| 27 |
* The body to send over the HTTP request. |
| 28 |
*/ |
| 29 |
body?: any; |
| 30 |
|
| 31 |
/** |
| 32 |
* The HTTP method used to make the HTTP request. |
| 33 |
*/ |
| 34 |
method: string; |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether or not the request was made asynchronously. |
| 38 |
*/ |
| 39 |
async: boolean; |
| 40 |
|
| 41 |
/** |
| 42 |
* The headers sent over the HTTP request. |
| 43 |
*/ |
| 44 |
headers: Readonly<Record<string, any>>; |
| 45 |
|
| 46 |
/** |
| 47 |
* The timeout value used for the HTTP request. |
| 48 |
* Note: this is only honored if the request is asynchronous (`async` is `true`). |
| 49 |
*/ |
| 50 |
timeout: number; |
| 51 |
|
| 52 |
/** |
| 53 |
* The user credentials user name sent with the HTTP request. |
| 54 |
*/ |
| 55 |
user?: string; |
| 56 |
|
| 57 |
/** |
| 58 |
* The user credentials password sent with the HTTP request. |
| 59 |
*/ |
| 60 |
password?: string; |
| 61 |
|
| 62 |
/** |
| 63 |
* Whether or not the request was a CORS request. |
| 64 |
*/ |
| 65 |
crossDomain: boolean; |
| 66 |
|
| 67 |
/** |
| 68 |
* Whether or not a CORS request was sent with credentials. |
| 69 |
* If `false`, will also ignore cookies in the CORS response. |
| 70 |
*/ |
| 71 |
withCredentials: boolean; |
| 72 |
|
| 73 |
/** |
| 74 |
* The [`responseType`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) set before sending the request. |
| 75 |
*/ |
| 76 |
responseType: XMLHttpRequestResponseType; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Configuration for the {@link ajax} creation function. |
| 81 |
*/ |
| 82 |
export interface AjaxConfig { |
| 83 |
/** The address of the resource to request via HTTP. */ |
| 84 |
url: string; |
| 85 |
|
| 86 |
/** |
| 87 |
* The body of the HTTP request to send. |
| 88 |
* |
| 89 |
* This is serialized, by default, based off of the value of the `"content-type"` header. |
| 90 |
* For example, if the `"content-type"` is `"application/json"`, the body will be serialized |
| 91 |
* as JSON. If the `"content-type"` is `"application/x-www-form-urlencoded"`, whatever object passed |
| 92 |
* to the body will be serialized as URL, using key-value pairs based off of the keys and values of the object. |
| 93 |
* In all other cases, the body will be passed directly. |
| 94 |
*/ |
| 95 |
body?: any; |
| 96 |
|
| 97 |
/** |
| 98 |
* Whether or not to send the request asynchronously. Defaults to `true`. |
| 99 |
* If set to `false`, this will block the thread until the AJAX request responds. |
| 100 |
*/ |
| 101 |
async?: boolean; |
| 102 |
|
| 103 |
/** |
| 104 |
* The HTTP Method to use for the request. Defaults to "GET". |
| 105 |
*/ |
| 106 |
method?: string; |
| 107 |
|
| 108 |
/** |
| 109 |
* The HTTP headers to apply. |
| 110 |
* |
| 111 |
* Note that, by default, RxJS will add the following headers under certain conditions: |
| 112 |
* |
| 113 |
* 1. If the `"content-type"` header is **NOT** set, and the `body` is [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData), |
| 114 |
* a `"content-type"` of `"application/x-www-form-urlencoded; charset=UTF-8"` will be set automatically. |
| 115 |
* 2. If the `"x-requested-with"` header is **NOT** set, and the `crossDomain` configuration property is **NOT** explicitly set to `true`, |
| 116 |
* (meaning it is not a CORS request), a `"x-requested-with"` header with a value of `"XMLHttpRequest"` will be set automatically. |
| 117 |
* This header is generally meaningless, and is set by libraries and frameworks using `XMLHttpRequest` to make HTTP requests. |
| 118 |
*/ |
| 119 |
headers?: Readonly<Record<string, any>>; |
| 120 |
|
| 121 |
/** |
| 122 |
* The time to wait before causing the underlying XMLHttpRequest to timeout. This is only honored if the |
| 123 |
* `async` configuration setting is unset or set to `true`. Defaults to `0`, which is idiomatic for "never timeout". |
| 124 |
*/ |
| 125 |
timeout?: number; |
| 126 |
|
| 127 |
/** The user credentials user name to send with the HTTP request */ |
| 128 |
user?: string; |
| 129 |
|
| 130 |
/** The user credentials password to send with the HTTP request*/ |
| 131 |
password?: string; |
| 132 |
|
| 133 |
/** |
| 134 |
* Whether or not to send the HTTP request as a CORS request. |
| 135 |
* Defaults to `false`. |
| 136 |
* |
| 137 |
* @deprecated Will be removed in version 8. Cross domain requests and what creates a cross |
| 138 |
* domain request, are dictated by the browser, and a boolean that forces it to be cross domain |
| 139 |
* does not make sense. If you need to force cross domain, make sure you're making a secure request, |
| 140 |
* then add a custom header to the request or use `withCredentials`. For more information on what |
| 141 |
* triggers a cross domain request, see the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials). |
| 142 |
* In particular, the section on [Simple Requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests) is useful |
| 143 |
* for understanding when CORS will not be used. |
| 144 |
*/ |
| 145 |
crossDomain?: boolean; |
| 146 |
|
| 147 |
/** |
| 148 |
* To send user credentials in a CORS request, set to `true`. To exclude user credentials from |
| 149 |
* a CORS request, _OR_ when cookies are to be ignored by the CORS response, set to `false`. |
| 150 |
* |
| 151 |
* Defaults to `false`. |
| 152 |
*/ |
| 153 |
withCredentials?: boolean; |
| 154 |
|
| 155 |
/** |
| 156 |
* The name of your site's XSRF cookie. |
| 157 |
*/ |
| 158 |
xsrfCookieName?: string; |
| 159 |
|
| 160 |
/** |
| 161 |
* The name of a custom header that you can use to send your XSRF cookie. |
| 162 |
*/ |
| 163 |
xsrfHeaderName?: string; |
| 164 |
|
| 165 |
/** |
| 166 |
* Can be set to change the response type. |
| 167 |
* Valid values are `"arraybuffer"`, `"blob"`, `"document"`, `"json"`, and `"text"`. |
| 168 |
* Note that the type of `"document"` (such as an XML document) is ignored if the global context is |
| 169 |
* not `Window`. |
| 170 |
* |
| 171 |
* Defaults to `"json"`. |
| 172 |
*/ |
| 173 |
responseType?: XMLHttpRequestResponseType; |
| 174 |
|
| 175 |
/** |
| 176 |
* An optional factory used to create the XMLHttpRequest object used to make the AJAX request. |
| 177 |
* This is useful in environments that lack `XMLHttpRequest`, or in situations where you |
| 178 |
* wish to override the default `XMLHttpRequest` for some reason. |
| 179 |
* |
| 180 |
* If not provided, the `XMLHttpRequest` in global scope will be used. |
| 181 |
* |
| 182 |
* NOTE: This AJAX implementation relies on the built-in serialization and setting |
| 183 |
* of Content-Type headers that is provided by standards-compliant XMLHttpRequest implementations, |
| 184 |
* be sure any implementation you use meets that standard. |
| 185 |
*/ |
| 186 |
createXHR?: () => XMLHttpRequest; |
| 187 |
|
| 188 |
/** |
| 189 |
* An observer for watching the upload progress of an HTTP request. Will |
| 190 |
* emit progress events, and completes on the final upload load event, will error for |
| 191 |
* any XHR error or timeout. |
| 192 |
* |
| 193 |
* This will **not** error for errored status codes. Rather, it will always _complete_ when |
| 194 |
* the HTTP response comes back. |
| 195 |
* |
| 196 |
* @deprecated If you're looking for progress events, use {@link includeDownloadProgress} and |
| 197 |
* {@link includeUploadProgress} instead. Will be removed in v8. |
| 198 |
*/ |
| 199 |
progressSubscriber?: PartialObserver<ProgressEvent>; |
| 200 |
|
| 201 |
/** |
| 202 |
* If `true`, will emit all download progress and load complete events as {@link AjaxResponse} |
| 203 |
* from the observable. The final download event will also be emitted as a {@link AjaxResponse}. |
| 204 |
* |
| 205 |
* If both this and {@link includeUploadProgress} are `false`, then only the {@link AjaxResponse} will |
| 206 |
* be emitted from the resulting observable. |
| 207 |
*/ |
| 208 |
includeDownloadProgress?: boolean; |
| 209 |
|
| 210 |
/** |
| 211 |
* If `true`, will emit all upload progress and load complete events as {@link AjaxResponse} |
| 212 |
* from the observable. The final download event will also be emitted as a {@link AjaxResponse}. |
| 213 |
* |
| 214 |
* If both this and {@link includeDownloadProgress} are `false`, then only the {@link AjaxResponse} will |
| 215 |
* be emitted from the resulting observable. |
| 216 |
*/ |
| 217 |
includeUploadProgress?: boolean; |
| 218 |
|
| 219 |
/** |
| 220 |
* Query string parameters to add to the URL in the request. |
| 221 |
* <em>This will require a polyfill for `URL` and `URLSearchParams` in Internet Explorer!</em> |
| 222 |
* |
| 223 |
* Accepts either a query string, a `URLSearchParams` object, a dictionary of key/value pairs, or an |
| 224 |
* array of key/value entry tuples. (Essentially, it takes anything that `new URLSearchParams` would normally take). |
| 225 |
* |
| 226 |
* If, for some reason you have a query string in the `url` argument, this will append to the query string in the url, |
| 227 |
* but it will also overwrite the value of any keys that are an exact match. In other words, a url of `/test?a=1&b=2`, |
| 228 |
* with queryParams of `{ b: 5, c: 6 }` will result in a url of roughly `/test?a=1&b=5&c=6`. |
| 229 |
*/ |
| 230 |
queryParams?: |
| 231 |
| string |
| 232 |
| URLSearchParams |
| 233 |
| Record<string, string | number | boolean | string[] | number[] | boolean[]> |
| 234 |
| [string, string | number | boolean | string[] | number[] | boolean[]][]; |
| 235 |
} |