/** * @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 */ import { __decorate, __metadata, __param } from "tslib"; import { DOCUMENT } from '@angular/common'; import { Inject, Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { HttpErrorResponse, HttpEventType, HttpResponse } from './response'; // Every request made through JSONP needs a callback name that's unique across the // whole page. Each request is assigned an id and the callback name is constructed // from that. The next id to be assigned is tracked in a global variable here that // is shared among all applications on the page. var nextRequestId = 0; // Error text given when a JSONP script is injected, but doesn't invoke the callback // passed in its URL. export var JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.'; // Error text given when a request is passed to the JsonpClientBackend that doesn't // have a request method JSONP. export var JSONP_ERR_WRONG_METHOD = 'JSONP requests must use JSONP request method.'; export var JSONP_ERR_WRONG_RESPONSE_TYPE = 'JSONP requests must use Json response type.'; /** * DI token/abstract type representing a map of JSONP callbacks. * * In the browser, this should always be the `window` object. * * */ var JsonpCallbackContext = /** @class */ (function () { function JsonpCallbackContext() { } return JsonpCallbackContext; }()); export { JsonpCallbackContext }; /** * Processes an `HttpRequest` with the JSONP method, * by performing JSONP style requests. * @see `HttpHandler` * @see `HttpXhrBackend` * * @publicApi */ var JsonpClientBackend = /** @class */ (function () { function JsonpClientBackend(callbackMap, document) { this.callbackMap = callbackMap; this.document = document; } /** * Get the name of the next callback method, by incrementing the global `nextRequestId`. */ JsonpClientBackend.prototype.nextCallback = function () { return "ng_jsonp_callback_" + nextRequestId++; }; /** * Processes a JSONP request and returns an event stream of the results. * @param req The request object. * @returns An observable of the response events. * */ JsonpClientBackend.prototype.handle = function (req) { var _this = this; // Firstly, check both the method and response type. If either doesn't match // then the request was improperly routed here and cannot be handled. if (req.method !== 'JSONP') { throw new Error(JSONP_ERR_WRONG_METHOD); } else if (req.responseType !== 'json') { throw new Error(JSONP_ERR_WRONG_RESPONSE_TYPE); } // Everything else happens inside the Observable boundary. return new Observable(function (observer) { // The first step to make a request is to generate the callback name, and replace the // callback placeholder in the URL with the name. Care has to be taken here to ensure // a trailing &, if matched, gets inserted back into the URL in the correct place. var callback = _this.nextCallback(); var url = req.urlWithParams.replace(/=JSONP_CALLBACK(&|$)/, "=" + callback + "$1"); // Construct the