jsonp_backend.js
7.61 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @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 { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { ResponseOptions } from '../base_response_options';
import { ReadyState, RequestMethod, ResponseType } from '../enums';
import { ConnectionBackend } from '../interfaces';
import { Response } from '../static_response';
import { BrowserJsonp } from './browser_jsonp';
var /** @type {?} */ JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
var /** @type {?} */ JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
/**
* Abstract base class for an in-flight JSONP request.
*
* \@experimental
* @abstract
*/
export var JSONPConnection = (function () {
function JSONPConnection() {
}
/**
* Callback called when the JSONP request completes, to notify the application
* of the new data.
* @abstract
* @param {?=} data
* @return {?}
*/
JSONPConnection.prototype.finished = function (data) { };
return JSONPConnection;
}());
function JSONPConnection_tsickle_Closure_declarations() {
/**
* The {\@link ReadyState} of this request.
* @type {?}
*/
JSONPConnection.prototype.readyState;
/**
* The outgoing HTTP request.
* @type {?}
*/
JSONPConnection.prototype.request;
/**
* An observable that completes with the response, when the request is finished.
* @type {?}
*/
JSONPConnection.prototype.response;
}
export var JSONPConnection_ = (function (_super) {
__extends(JSONPConnection_, _super);
/**
* @param {?} req
* @param {?} _dom
* @param {?=} baseResponseOptions
*/
function JSONPConnection_(req, _dom, baseResponseOptions) {
var _this = this;
_super.call(this);
this._dom = _dom;
this.baseResponseOptions = baseResponseOptions;
this._finished = false;
if (req.method !== RequestMethod.Get) {
throw new TypeError(JSONP_ERR_WRONG_METHOD);
}
this.request = req;
this.response = new Observable(function (responseObserver) {
_this.readyState = ReadyState.Loading;
var id = _this._id = _dom.nextRequestID();
_dom.exposeConnection(id, _this);
// Workaround Dart
// url = url.replace(/=JSONP_CALLBACK(&|$)/, `generated method`);
var callback = _dom.requestCallback(_this._id);
var url = req.url;
if (url.indexOf('=JSONP_CALLBACK&') > -1) {
url = url.replace('=JSONP_CALLBACK&', "=" + callback + "&");
}
else if (url.lastIndexOf('=JSONP_CALLBACK') === url.length - '=JSONP_CALLBACK'.length) {
url = url.substring(0, url.length - '=JSONP_CALLBACK'.length) + ("=" + callback);
}
var script = _this._script = _dom.build(url);
var onLoad = function (event) {
if (_this.readyState === ReadyState.Cancelled)
return;
_this.readyState = ReadyState.Done;
_dom.cleanup(script);
if (!_this._finished) {
var responseOptions_1 = new ResponseOptions({ body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url: url });
if (baseResponseOptions) {
responseOptions_1 = baseResponseOptions.merge(responseOptions_1);
}
responseObserver.error(new Response(responseOptions_1));
return;
}
var responseOptions = new ResponseOptions({ body: _this._responseData, url: url });
if (_this.baseResponseOptions) {
responseOptions = _this.baseResponseOptions.merge(responseOptions);
}
responseObserver.next(new Response(responseOptions));
responseObserver.complete();
};
var onError = function (error) {
if (_this.readyState === ReadyState.Cancelled)
return;
_this.readyState = ReadyState.Done;
_dom.cleanup(script);
var responseOptions = new ResponseOptions({ body: error.message, type: ResponseType.Error });
if (baseResponseOptions) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
responseObserver.error(new Response(responseOptions));
};
script.addEventListener('load', onLoad);
script.addEventListener('error', onError);
_dom.send(script);
return function () {
_this.readyState = ReadyState.Cancelled;
script.removeEventListener('load', onLoad);
script.removeEventListener('error', onError);
_this._dom.cleanup(script);
};
});
}
/**
* @param {?=} data
* @return {?}
*/
JSONPConnection_.prototype.finished = function (data) {
// Don't leak connections
this._finished = true;
this._dom.removeConnection(this._id);
if (this.readyState === ReadyState.Cancelled)
return;
this._responseData = data;
};
return JSONPConnection_;
}(JSONPConnection));
function JSONPConnection__tsickle_Closure_declarations() {
/** @type {?} */
JSONPConnection_.prototype._id;
/** @type {?} */
JSONPConnection_.prototype._script;
/** @type {?} */
JSONPConnection_.prototype._responseData;
/** @type {?} */
JSONPConnection_.prototype._finished;
/** @type {?} */
JSONPConnection_.prototype._dom;
/** @type {?} */
JSONPConnection_.prototype.baseResponseOptions;
}
/**
* A {\@link ConnectionBackend} that uses the JSONP strategy of making requests.
*
* \@experimental
* @abstract
*/
export var JSONPBackend = (function (_super) {
__extends(JSONPBackend, _super);
function JSONPBackend() {
_super.apply(this, arguments);
}
return JSONPBackend;
}(ConnectionBackend));
export var JSONPBackend_ = (function (_super) {
__extends(JSONPBackend_, _super);
/**
* @param {?} _browserJSONP
* @param {?} _baseResponseOptions
*/
function JSONPBackend_(_browserJSONP, _baseResponseOptions) {
_super.call(this);
this._browserJSONP = _browserJSONP;
this._baseResponseOptions = _baseResponseOptions;
}
/**
* @param {?} request
* @return {?}
*/
JSONPBackend_.prototype.createConnection = function (request) {
return new JSONPConnection_(request, this._browserJSONP, this._baseResponseOptions);
};
JSONPBackend_.decorators = [
{ type: Injectable },
];
/** @nocollapse */
JSONPBackend_.ctorParameters = function () { return [
{ type: BrowserJsonp, },
{ type: ResponseOptions, },
]; };
return JSONPBackend_;
}(JSONPBackend));
function JSONPBackend__tsickle_Closure_declarations() {
/** @type {?} */
JSONPBackend_.decorators;
/**
* @nocollapse
* @type {?}
*/
JSONPBackend_.ctorParameters;
/** @type {?} */
JSONPBackend_.prototype._browserJSONP;
/** @type {?} */
JSONPBackend_.prototype._baseResponseOptions;
}
//# sourceMappingURL=jsonp_backend.js.map