webRequest.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { IWebRequest } from './interfaces/iWebRequest';
  2. import { Nullable } from '../types';
  3. /**
  4. * Extended version of XMLHttpRequest with support for customizations (headers, ...)
  5. */
  6. export class WebRequest implements IWebRequest {
  7. private _xhr = new XMLHttpRequest();
  8. /**
  9. * Custom HTTP Request Headers to be sent with XMLHttpRequests
  10. * i.e. when loading files, where the server/service expects an Authorization header
  11. */
  12. public static CustomRequestHeaders: { [key: string]: string } = {};
  13. /**
  14. * Add callback functions in this array to update all the requests before they get sent to the network
  15. */
  16. public static CustomRequestModifiers = new Array<(request: XMLHttpRequest, url: string) => void>();
  17. private _injectCustomRequestHeaders(): void {
  18. for (let key in WebRequest.CustomRequestHeaders) {
  19. const val = WebRequest.CustomRequestHeaders[key];
  20. if (val) {
  21. this._xhr.setRequestHeader(key, val);
  22. }
  23. }
  24. }
  25. /**
  26. * Gets or sets a function to be called when loading progress changes
  27. */
  28. public get onprogress(): ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null {
  29. return this._xhr.onprogress;
  30. }
  31. public set onprogress(value: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null) {
  32. this._xhr.onprogress = value;
  33. }
  34. /**
  35. * Returns client's state
  36. */
  37. public get readyState(): number {
  38. return this._xhr.readyState;
  39. }
  40. /**
  41. * Returns client's status
  42. */
  43. public get status(): number {
  44. return this._xhr.status;
  45. }
  46. /**
  47. * Returns client's status as a text
  48. */
  49. public get statusText(): string {
  50. return this._xhr.statusText;
  51. }
  52. /**
  53. * Returns client's response
  54. */
  55. public get response(): any {
  56. return this._xhr.response;
  57. }
  58. /**
  59. * Returns client's response url
  60. */
  61. public get responseURL(): string {
  62. return this._xhr.responseURL;
  63. }
  64. /**
  65. * Returns client's response as text
  66. */
  67. public get responseText(): string {
  68. return this._xhr.responseText;
  69. }
  70. /**
  71. * Gets or sets the expected response type
  72. */
  73. public get responseType(): XMLHttpRequestResponseType {
  74. return this._xhr.responseType;
  75. }
  76. public set responseType(value: XMLHttpRequestResponseType) {
  77. this._xhr.responseType = value;
  78. }
  79. /** @hidden */
  80. public addEventListener<K extends keyof XMLHttpRequestEventMap>(type: K, listener: (this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
  81. public addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void {
  82. this._xhr.addEventListener(type, listener, options);
  83. }
  84. /** @hidden */
  85. public removeEventListener<K extends keyof XMLHttpRequestEventMap>(type: K, listener: (this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
  86. public removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void {
  87. this._xhr.removeEventListener(type, listener, options);
  88. }
  89. /**
  90. * Cancels any network activity
  91. */
  92. public abort() {
  93. this._xhr.abort();
  94. }
  95. /**
  96. * Initiates the request. The optional argument provides the request body. The argument is ignored if request method is GET or HEAD
  97. * @param body defines an optional request body
  98. */
  99. public send(body?: Document | BodyInit | null): void {
  100. if (WebRequest.CustomRequestHeaders) {
  101. this._injectCustomRequestHeaders();
  102. }
  103. this._xhr.send(body);
  104. }
  105. /**
  106. * Sets the request method, request URL
  107. * @param method defines the method to use (GET, POST, etc..)
  108. * @param url defines the url to connect with
  109. */
  110. public open(method: string, url: string): void {
  111. for (var update of WebRequest.CustomRequestModifiers) {
  112. update(this._xhr, url);
  113. }
  114. // Clean url
  115. url = url.replace("file:http:", "http:");
  116. url = url.replace("file:https:", "https:");
  117. return this._xhr.open(method, url, true);
  118. }
  119. /**
  120. * Sets the value of a request header.
  121. * @param name The name of the header whose value is to be set
  122. * @param value The value to set as the body of the header
  123. */
  124. setRequestHeader(name: string, value: string): void {
  125. this._xhr.setRequestHeader(name, value);
  126. }
  127. /**
  128. * Get the string containing the text of a particular header's value.
  129. * @param name The name of the header
  130. * @returns The string containing the text of the given header name
  131. */
  132. getResponseHeader(name: string): Nullable<string> {
  133. return this._xhr.getResponseHeader(name);
  134. }
  135. }