PageRenderTime 52ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/@angular/http/esm/src/static_request.js

https://gitlab.com/btluis/Angular2_Ejemplo
JavaScript | 157 lines | 86 code | 0 blank | 71 comment | 27 complexity | b739ac3956cfe4062848fdff4681ecad MD5 | raw file
  1. import { StringWrapper, isPresent } from '../src/facade/lang';
  2. import { ContentType } from './enums';
  3. import { Headers } from './headers';
  4. import { normalizeMethodName } from './http_utils';
  5. import { URLSearchParams } from './url_search_params';
  6. // TODO(jeffbcross): properly implement body accessors
  7. /**
  8. * Creates `Request` instances from provided values.
  9. *
  10. * The Request's interface is inspired by the Request constructor defined in the [Fetch
  11. * Spec](https://fetch.spec.whatwg.org/#request-class),
  12. * but is considered a static value whose body can be accessed many times. There are other
  13. * differences in the implementation, but this is the most significant.
  14. *
  15. * `Request` instances are typically created by higher-level classes, like {@link Http} and
  16. * {@link Jsonp}, but it may occasionally be useful to explicitly create `Request` instances.
  17. * One such example is when creating services that wrap higher-level services, like {@link Http},
  18. * where it may be useful to generate a `Request` with arbitrary headers and search params.
  19. *
  20. * ```typescript
  21. * import {Injectable, Injector} from '@angular/core';
  22. * import {HTTP_PROVIDERS, Http, Request, RequestMethod} from '@angular/http';
  23. *
  24. * @Injectable()
  25. * class AutoAuthenticator {
  26. * constructor(public http:Http) {}
  27. * request(url:string) {
  28. * return this.http.request(new Request({
  29. * method: RequestMethod.Get,
  30. * url: url,
  31. * search: 'password=123'
  32. * }));
  33. * }
  34. * }
  35. *
  36. * var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]);
  37. * var authenticator = injector.get(AutoAuthenticator);
  38. * authenticator.request('people.json').subscribe(res => {
  39. * //URL should have included '?password=123'
  40. * console.log('people', res.json());
  41. * });
  42. * ```
  43. */
  44. export class Request {
  45. constructor(requestOptions) {
  46. // TODO: assert that url is present
  47. let url = requestOptions.url;
  48. this.url = requestOptions.url;
  49. if (isPresent(requestOptions.search)) {
  50. let search = requestOptions.search.toString();
  51. if (search.length > 0) {
  52. let prefix = '?';
  53. if (StringWrapper.contains(this.url, '?')) {
  54. prefix = (this.url[this.url.length - 1] == '&') ? '' : '&';
  55. }
  56. // TODO: just delete search-query-looking string in url?
  57. this.url = url + prefix + search;
  58. }
  59. }
  60. this._body = requestOptions.body;
  61. this.contentType = this.detectContentType();
  62. this.method = normalizeMethodName(requestOptions.method);
  63. // TODO(jeffbcross): implement behavior
  64. // Defaults to 'omit', consistent with browser
  65. // TODO(jeffbcross): implement behavior
  66. this.headers = new Headers(requestOptions.headers);
  67. this.withCredentials = requestOptions.withCredentials;
  68. }
  69. /**
  70. * Returns the request's body as string, assuming that body exists. If body is undefined, return
  71. * empty
  72. * string.
  73. */
  74. text() { return isPresent(this._body) ? this._body.toString() : ''; }
  75. /**
  76. * Returns the request's body as JSON string, assuming that body exists. If body is undefined,
  77. * return
  78. * empty
  79. * string.
  80. */
  81. json() { return isPresent(this._body) ? JSON.stringify(this._body) : ''; }
  82. /**
  83. * Returns the request's body as array buffer, assuming that body exists. If body is undefined,
  84. * return
  85. * null.
  86. */
  87. arrayBuffer() {
  88. if (this._body instanceof ArrayBuffer)
  89. return this._body;
  90. throw 'The request body isn\'t an array buffer';
  91. }
  92. /**
  93. * Returns the request's body as blob, assuming that body exists. If body is undefined, return
  94. * null.
  95. */
  96. blob() {
  97. if (this._body instanceof Blob)
  98. return this._body;
  99. if (this._body instanceof ArrayBuffer)
  100. return new Blob([this._body]);
  101. throw 'The request body isn\'t either a blob or an array buffer';
  102. }
  103. /**
  104. * Returns the content type of request's body based on its type.
  105. */
  106. detectContentType() {
  107. if (this._body == null) {
  108. return ContentType.NONE;
  109. }
  110. else if (this._body instanceof URLSearchParams) {
  111. return ContentType.FORM;
  112. }
  113. else if (this._body instanceof FormData) {
  114. return ContentType.FORM_DATA;
  115. }
  116. else if (this._body instanceof Blob) {
  117. return ContentType.BLOB;
  118. }
  119. else if (this._body instanceof ArrayBuffer) {
  120. return ContentType.ARRAY_BUFFER;
  121. }
  122. else if (this._body && typeof this._body == 'object') {
  123. return ContentType.JSON;
  124. }
  125. else {
  126. return ContentType.TEXT;
  127. }
  128. }
  129. /**
  130. * Returns the request's body according to its type. If body is undefined, return
  131. * null.
  132. */
  133. getBody() {
  134. switch (this.contentType) {
  135. case ContentType.JSON:
  136. return this.json();
  137. case ContentType.FORM:
  138. return this.text();
  139. case ContentType.FORM_DATA:
  140. return this._body;
  141. case ContentType.TEXT:
  142. return this.text();
  143. case ContentType.BLOB:
  144. return this.blob();
  145. case ContentType.ARRAY_BUFFER:
  146. return this.arrayBuffer();
  147. default:
  148. return null;
  149. }
  150. }
  151. }
  152. const noop = function () { };
  153. const w = typeof window == 'object' ? window : noop;
  154. const FormData = w['FormData'] || noop;
  155. const Blob = w['Blob'] || noop;
  156. const ArrayBuffer = w['ArrayBuffer'] || noop;
  157. //# sourceMappingURL=static_request.js.map