static_request.js
6.71 KB
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { Body } from './body';
import { ContentType } from './enums';
import { Headers } from './headers';
import { normalizeMethodName } from './http_utils';
import { URLSearchParams } from './url_search_params';
/**
* Creates `Request` instances from provided values.
*
* The Request's interface is inspired by the Request constructor defined in the [Fetch
* Spec](https://fetch.spec.whatwg.org/#request-class),
* but is considered a static value whose body can be accessed many times. There are other
* differences in the implementation, but this is the most significant.
*
* `Request` instances are typically created by higher-level classes, like {\@link Http} and
* {\@link Jsonp}, but it may occasionally be useful to explicitly create `Request` instances.
* One such example is when creating services that wrap higher-level services, like {\@link Http},
* where it may be useful to generate a `Request` with arbitrary headers and search params.
*
* ```typescript
* import {Injectable, Injector} from '\@angular/core';
* import {HTTP_PROVIDERS, Http, Request, RequestMethod} from '\@angular/http';
*
* \@Injectable()
* class AutoAuthenticator {
* constructor(public http:Http) {}
* request(url:string) {
* return this.http.request(new Request({
* method: RequestMethod.Get,
* url: url,
* search: 'password=123'
* }));
* }
* }
*
* var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]);
* var authenticator = injector.get(AutoAuthenticator);
* authenticator.request('people.json').subscribe(res => {
* //URL should have included '?password=123'
* console.log('people', res.json());
* });
* ```
*
* \@experimental
*/
export var Request = (function (_super) {
__extends(Request, _super);
/**
* @param {?} requestOptions
*/
function Request(requestOptions) {
_super.call(this);
// TODO: assert that url is present
var url = requestOptions.url;
this.url = requestOptions.url;
if (requestOptions.search) {
var search = requestOptions.search.toString();
if (search.length > 0) {
var prefix = '?';
if (this.url.indexOf('?') != -1) {
prefix = (this.url[this.url.length - 1] == '&') ? '' : '&';
}
// TODO: just delete search-query-looking string in url?
this.url = url + prefix + search;
}
}
this._body = requestOptions.body;
this.method = normalizeMethodName(requestOptions.method);
// TODO(jeffbcross): implement behavior
// Defaults to 'omit', consistent with browser
this.headers = new Headers(requestOptions.headers);
this.contentType = this.detectContentType();
this.withCredentials = requestOptions.withCredentials;
this.responseType = requestOptions.responseType;
}
/**
* Returns the content type enum based on header options.
* @return {?}
*/
Request.prototype.detectContentType = function () {
switch (this.headers.get('content-type')) {
case 'application/json':
return ContentType.JSON;
case 'application/x-www-form-urlencoded':
return ContentType.FORM;
case 'multipart/form-data':
return ContentType.FORM_DATA;
case 'text/plain':
case 'text/html':
return ContentType.TEXT;
case 'application/octet-stream':
return this._body instanceof ArrayBuffer ? ContentType.ARRAY_BUFFER : ContentType.BLOB;
default:
return this.detectContentTypeFromBody();
}
};
/**
* Returns the content type of request's body based on its type.
* @return {?}
*/
Request.prototype.detectContentTypeFromBody = function () {
if (this._body == null) {
return ContentType.NONE;
}
else if (this._body instanceof URLSearchParams) {
return ContentType.FORM;
}
else if (this._body instanceof FormData) {
return ContentType.FORM_DATA;
}
else if (this._body instanceof Blob) {
return ContentType.BLOB;
}
else if (this._body instanceof ArrayBuffer) {
return ContentType.ARRAY_BUFFER;
}
else if (this._body && typeof this._body === 'object') {
return ContentType.JSON;
}
else {
return ContentType.TEXT;
}
};
/**
* Returns the request's body according to its type. If body is undefined, return
* null.
* @return {?}
*/
Request.prototype.getBody = function () {
switch (this.contentType) {
case ContentType.JSON:
return this.text();
case ContentType.FORM:
return this.text();
case ContentType.FORM_DATA:
return this._body;
case ContentType.TEXT:
return this.text();
case ContentType.BLOB:
return this.blob();
case ContentType.ARRAY_BUFFER:
return this.arrayBuffer();
default:
return null;
}
};
return Request;
}(Body));
function Request_tsickle_Closure_declarations() {
/**
* Http method with which to perform the request.
* @type {?}
*/
Request.prototype.method;
/**
* {\@link Headers} instance
* @type {?}
*/
Request.prototype.headers;
/**
* Url of the remote resource
* @type {?}
*/
Request.prototype.url;
/**
* Type of the request body *
* @type {?}
*/
Request.prototype.contentType;
/**
* Enable use credentials
* @type {?}
*/
Request.prototype.withCredentials;
/**
* Buffer to store the response
* @type {?}
*/
Request.prototype.responseType;
}
var /** @type {?} */ noop = function () { };
var /** @type {?} */ w = typeof window == 'object' ? window : noop;
var /** @type {?} */ FormData = ((w) /** TODO #9100 */)['FormData'] || noop;
var /** @type {?} */ Blob = ((w) /** TODO #9100 */)['Blob'] || noop;
export var /** @type {?} */ ArrayBuffer = ((w) /** TODO #9100 */)['ArrayBuffer'] || noop;
//# sourceMappingURL=static_request.js.map