| 1 |
import { AjaxRequest, AjaxResponseType } from './types'; |
| 2 |
import { getXHRResponse } from './getXHRResponse'; |
| 3 |
|
| 4 |
/** |
| 5 |
* A normalized response from an AJAX request. To get the data from the response, |
| 6 |
* you will want to read the `response` property. |
| 7 |
* |
| 8 |
* - DO NOT create instances of this class directly. |
| 9 |
* - DO NOT subclass this class. |
| 10 |
* |
| 11 |
* It is advised not to hold this object in memory, as it has a reference to |
| 12 |
* the original XHR used to make the request, as well as properties containing |
| 13 |
* request and response data. |
| 14 |
* |
| 15 |
* @see {@link ajax} |
| 16 |
* @see {@link AjaxConfig} |
| 17 |
*/ |
| 18 |
export class AjaxResponse<T> { |
| 19 |
/** The HTTP status code */ |
| 20 |
readonly status: number; |
| 21 |
|
| 22 |
/** |
| 23 |
* The response data, if any. Note that this will automatically be converted to the proper type |
| 24 |
*/ |
| 25 |
readonly response: T; |
| 26 |
|
| 27 |
/** |
| 28 |
* The responseType set on the request. (For example: `""`, `"arraybuffer"`, `"blob"`, `"document"`, `"json"`, or `"text"`) |
| 29 |
* @deprecated There isn't much reason to examine this. It's the same responseType set (or defaulted) on the ajax config. |
| 30 |
* If you really need to examine this value, you can check it on the `request` or the `xhr`. Will be removed in v8. |
| 31 |
*/ |
| 32 |
readonly responseType: XMLHttpRequestResponseType; |
| 33 |
|
| 34 |
/** |
| 35 |
* The total number of bytes loaded so far. To be used with {@link total} while |
| 36 |
* calculating progress. (You will want to set {@link includeDownloadProgress} or |
| 37 |
* {@link includeDownloadProgress}) |
| 38 |
*/ |
| 39 |
readonly loaded: number; |
| 40 |
|
| 41 |
/** |
| 42 |
* The total number of bytes to be loaded. To be used with {@link loaded} while |
| 43 |
* calculating progress. (You will want to set {@link includeDownloadProgress} or |
| 44 |
* {@link includeDownloadProgress}) |
| 45 |
*/ |
| 46 |
readonly total: number; |
| 47 |
|
| 48 |
/** |
| 49 |
* A dictionary of the response headers. |
| 50 |
*/ |
| 51 |
readonly responseHeaders: Record<string, string>; |
| 52 |
|
| 53 |
/** |
| 54 |
* A normalized response from an AJAX request. To get the data from the response, |
| 55 |
* you will want to read the `response` property. |
| 56 |
* |
| 57 |
* - DO NOT create instances of this class directly. |
| 58 |
* - DO NOT subclass this class. |
| 59 |
* |
| 60 |
* @param originalEvent The original event object from the XHR `onload` event. |
| 61 |
* @param xhr The `XMLHttpRequest` object used to make the request. This is useful for examining status code, etc. |
| 62 |
* @param request The request settings used to make the HTTP request. |
| 63 |
* @param type The type of the event emitted by the {@link ajax} Observable |
| 64 |
*/ |
| 65 |
constructor( |
| 66 |
/** |
| 67 |
* The original event object from the raw XHR event. |
| 68 |
*/ |
| 69 |
public readonly originalEvent: ProgressEvent, |
| 70 |
/** |
| 71 |
* The XMLHttpRequest object used to make the request. |
| 72 |
* NOTE: It is advised not to hold this in memory, as it will retain references to all of it's event handlers |
| 73 |
* and many other things related to the request. |
| 74 |
*/ |
| 75 |
public readonly xhr: XMLHttpRequest, |
| 76 |
/** |
| 77 |
* The request parameters used to make the HTTP request. |
| 78 |
*/ |
| 79 |
public readonly request: AjaxRequest, |
| 80 |
/** |
| 81 |
* The event type. This can be used to discern between different events |
| 82 |
* if you're using progress events with {@link includeDownloadProgress} or |
| 83 |
* {@link includeUploadProgress} settings in {@link AjaxConfig}. |
| 84 |
* |
| 85 |
* The event type consists of two parts: the {@link AjaxDirection} and the |
| 86 |
* the event type. Merged with `_`, they form the `type` string. The |
| 87 |
* direction can be an `upload` or a `download` direction, while an event can |
| 88 |
* be `loadstart`, `progress` or `load`. |
| 89 |
* |
| 90 |
* `download_load` is the type of event when download has finished and the |
| 91 |
* response is available. |
| 92 |
*/ |
| 93 |
public readonly type: AjaxResponseType = 'download_load' |
| 94 |
) { |
| 95 |
const { status, responseType } = xhr; |
| 96 |
this.status = status ?? 0; |
| 97 |
this.responseType = responseType ?? ''; |
| 98 |
|
| 99 |
// Parse the response headers in advance for the user. There's really |
| 100 |
// not a great way to get all of them. So we need to parse the header string |
| 101 |
// we get back. It comes in a simple enough format: |
| 102 |
// |
| 103 |
// header-name: value here |
| 104 |
// content-type: application/json |
| 105 |
// other-header-here: some, other, values, or, whatever |
| 106 |
const allHeaders = xhr.getAllResponseHeaders(); |
| 107 |
this.responseHeaders = allHeaders |
| 108 |
? // Split the header text into lines |
| 109 |
allHeaders.split('\n').reduce((headers: Record<string, string>, line) => { |
| 110 |
// Split the lines on the first ": " as |
| 111 |
// "key: value". Note that the value could |
| 112 |
// technically have a ": " in it. |
| 113 |
const index = line.indexOf(': '); |
| 114 |
headers[line.slice(0, index)] = line.slice(index + 2); |
| 115 |
return headers; |
| 116 |
}, {}) |
| 117 |
: {}; |
| 118 |
|
| 119 |
this.response = getXHRResponse(xhr); |
| 120 |
const { loaded, total } = originalEvent; |
| 121 |
this.loaded = loaded; |
| 122 |
this.total = total; |
| 123 |
} |
| 124 |
} |