From ec028b81095f89aa21711026c78eda4dec61211a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=A1ko=20Hevery?= Date: Mon, 17 Apr 2017 11:12:53 -0700 Subject: [PATCH] fix(http): Update types for TypeScript nullability support This reverts commit 268884296ac6c4d39f6570778608bd6b862300da. --- packages/http/src/backends/browser_jsonp.ts | 2 +- packages/http/src/backends/xhr_backend.ts | 2 +- packages/http/src/base_request_options.ts | 14 ++--- packages/http/src/base_response_options.ts | 12 ++-- packages/http/src/headers.ts | 13 +++-- packages/http/src/http.ts | 11 ++-- packages/http/src/http_utils.ts | 4 +- packages/http/src/interfaces.ts | 28 ++++----- packages/http/src/static_request.ts | 8 +-- packages/http/src/static_response.ts | 10 ++-- packages/http/src/url_search_params.ts | 2 +- .../http/test/backends/jsonp_backend_spec.ts | 9 +-- .../http/test/backends/mock_backend_spec.ts | 12 ++-- .../http/test/backends/xhr_backend_spec.ts | 50 ++++++++-------- packages/http/test/http_spec.ts | 4 +- packages/http/test/static_request_spec.ts | 19 +++--- packages/http/test/url_search_params_spec.ts | 8 +-- tools/public_api_guard/http/http.d.ts | 58 +++++++++---------- 18 files changed, 138 insertions(+), 128 deletions(-) diff --git a/packages/http/src/backends/browser_jsonp.ts b/packages/http/src/backends/browser_jsonp.ts index 2734836afc..d5112c7a88 100644 --- a/packages/http/src/backends/browser_jsonp.ts +++ b/packages/http/src/backends/browser_jsonp.ts @@ -10,7 +10,7 @@ import {Injectable} from '@angular/core'; let _nextRequestId = 0; export const JSONP_HOME = '__ng_jsonp__'; -let _jsonpConnections: {[key: string]: any} = null; +let _jsonpConnections: {[key: string]: any}|null = null; function _getJsonpConnections(): {[key: string]: any} { const w: {[key: string]: any} = typeof window == 'object' ? window : {}; diff --git a/packages/http/src/backends/xhr_backend.ts b/packages/http/src/backends/xhr_backend.ts index 923f805bad..b5262d1fb8 100644 --- a/packages/http/src/backends/xhr_backend.ts +++ b/packages/http/src/backends/xhr_backend.ts @@ -115,7 +115,7 @@ export class XHRConnection implements Connection { if (!req.headers.has('Accept')) { req.headers.append('Accept', 'application/json, text/plain, */*'); } - req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(','))); + req.headers.forEach((values, name) => _xhr.setRequestHeader(name !, values.join(','))); // Select the correct buffer type to store the response if (req.responseType != null && _xhr.responseType != null) { diff --git a/packages/http/src/base_request_options.ts b/packages/http/src/base_request_options.ts index 1d0942b95d..76e873c25b 100644 --- a/packages/http/src/base_request_options.ts +++ b/packages/http/src/base_request_options.ts @@ -44,11 +44,11 @@ export class RequestOptions { * Http method with which to execute a {@link Request}. * Acceptable methods are defined in the {@link RequestMethod} enum. */ - method: RequestMethod|string; + method: RequestMethod|string|null; /** * {@link Headers} to be attached to a {@link Request}. */ - headers: Headers; + headers: Headers|null; /** * Body to be used when creating a {@link Request}. */ @@ -56,7 +56,7 @@ export class RequestOptions { /** * Url with which to perform a {@link Request}. */ - url: string; + url: string|null; /** * Search parameters to be included in a {@link Request}. */ @@ -72,11 +72,11 @@ export class RequestOptions { /** * Enable use credentials for a {@link Request}. */ - withCredentials: boolean; + withCredentials: boolean|null; /* * Select a buffer to store the response, such as ArrayBuffer, Blob, Json (or Document) */ - responseType: ResponseContentType; + responseType: ResponseContentType|null; // TODO(Dzmitry): remove search when this.search is removed constructor( @@ -128,8 +128,8 @@ export class RequestOptions { }); } - private _mergeSearchParams(params: string|URLSearchParams| - {[key: string]: any | any[]}): URLSearchParams { + private _mergeSearchParams(params?: string|URLSearchParams|{[key: string]: any | any[]}| + null): URLSearchParams { if (!params) return this.params; if (params instanceof URLSearchParams) { diff --git a/packages/http/src/base_response_options.ts b/packages/http/src/base_response_options.ts index a9e56aebe3..1e62aea15c 100644 --- a/packages/http/src/base_response_options.ts +++ b/packages/http/src/base_response_options.ts @@ -46,25 +46,25 @@ export class ResponseOptions { /** * String, Object, ArrayBuffer or Blob representing the body of the {@link Response}. */ - body: string|Object|ArrayBuffer|Blob; + body: string|Object|ArrayBuffer|Blob|null; /** * Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code} * associated with the response. */ - status: number; + status: number|null; /** * Response {@link Headers headers} */ - headers: Headers; + headers: Headers|null; /** * @internal */ - statusText: string; + statusText: string|null; /** * @internal */ - type: ResponseType; - url: string; + type: ResponseType|null; + url: string|null; constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) { this.body = body != null ? body : null; this.status = status != null ? status : null; diff --git a/packages/http/src/headers.ts b/packages/http/src/headers.ts index dc1be57ce8..029362c9aa 100644 --- a/packages/http/src/headers.ts +++ b/packages/http/src/headers.ts @@ -41,7 +41,7 @@ export class Headers { _normalizedNames: Map = new Map(); // TODO(vicb): any -> string|string[] - constructor(headers?: Headers|{[name: string]: any}) { + constructor(headers?: Headers|{[name: string]: any}|null) { if (!headers) { return; } @@ -100,7 +100,8 @@ export class Headers { this._headers.delete(lcName); } - forEach(fn: (values: string[], name: string, headers: Map) => void): void { + forEach(fn: (values: string[], name: string|undefined, headers: Map) => void): + void { this._headers.forEach( (values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers)); } @@ -108,7 +109,7 @@ export class Headers { /** * Returns first header that matches given name. */ - get(name: string): string { + get(name: string): string|null { const values = this.getAll(name); if (values === null) { @@ -157,7 +158,7 @@ export class Headers { this._headers.forEach((values: string[], name: string) => { const split: string[] = []; values.forEach(v => split.push(...v.split(','))); - serialized[this._normalizedNames.get(name)] = split; + serialized[this._normalizedNames.get(name) !] = split; }); return serialized; @@ -166,8 +167,8 @@ export class Headers { /** * Returns list of header values for a given name. */ - getAll(name: string): string[] { - return this.has(name) ? this._headers.get(name.toLowerCase()) : null; + getAll(name: string): string[]|null { + return this.has(name) ? this._headers.get(name.toLowerCase()) || null : null; } /** diff --git a/packages/http/src/http.ts b/packages/http/src/http.ts index 4487eb5121..9f453832d5 100644 --- a/packages/http/src/http.ts +++ b/packages/http/src/http.ts @@ -8,9 +8,10 @@ import {Injectable} from '@angular/core'; import {Observable} from 'rxjs/Observable'; + import {BaseRequestOptions, RequestOptions} from './base_request_options'; import {RequestMethod} from './enums'; -import {ConnectionBackend, RequestOptionsArgs} from './interfaces'; +import {ConnectionBackend, RequestArgs, RequestOptionsArgs} from './interfaces'; import {Request} from './static_request'; import {Response} from './static_response'; @@ -19,8 +20,8 @@ function httpRequest(backend: ConnectionBackend, request: Request): Observable (status >= 200 && status < 300); -export function getResponseURL(xhr: any): string { +export function getResponseURL(xhr: any): string|null { if ('responseURL' in xhr) { return xhr.responseURL; } if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) { return xhr.getResponseHeader('X-Request-URL'); } - return; + return null; } export function stringToArrayBuffer(input: String): ArrayBuffer { diff --git a/packages/http/src/interfaces.ts b/packages/http/src/interfaces.ts index 112e71d14d..8637d27869 100644 --- a/packages/http/src/interfaces.ts +++ b/packages/http/src/interfaces.ts @@ -46,21 +46,21 @@ export abstract class XSRFStrategy { abstract configureRequest(req: Request): vo * @experimental */ export interface RequestOptionsArgs { - url?: string; - method?: string|RequestMethod; + url?: string|null; + method?: string|RequestMethod|null; /** @deprecated from 4.0.0. Use params instead. */ - search?: string|URLSearchParams|{[key: string]: any | any[]}; - params?: string|URLSearchParams|{[key: string]: any | any[]}; - headers?: Headers; + search?: string|URLSearchParams|{[key: string]: any | any[]}|null; + params?: string|URLSearchParams|{[key: string]: any | any[]}|null; + headers?: Headers|null; body?: any; - withCredentials?: boolean; - responseType?: ResponseContentType; + withCredentials?: boolean|null; + responseType?: ResponseContentType|null; } /** * Required structure when constructing new Request(); */ -export interface RequestArgs extends RequestOptionsArgs { url: string; } +export interface RequestArgs extends RequestOptionsArgs { url: string|null; } /** * Interface for options to construct a Response, based on @@ -69,10 +69,10 @@ export interface RequestArgs extends RequestOptionsArgs { url: string; } * @experimental */ export interface ResponseOptionsArgs { - body?: string|Object|FormData|ArrayBuffer|Blob; - status?: number; - statusText?: string; - headers?: Headers; - type?: ResponseType; - url?: string; + body?: string|Object|FormData|ArrayBuffer|Blob|null; + status?: number|null; + statusText?: string|null; + headers?: Headers|null; + type?: ResponseType|null; + url?: string|null; } diff --git a/packages/http/src/static_request.ts b/packages/http/src/static_request.ts index 05947426f6..2e635913a3 100644 --- a/packages/http/src/static_request.ts +++ b/packages/http/src/static_request.ts @@ -75,7 +75,7 @@ export class Request extends Body { super(); // TODO: assert that url is present const url = requestOptions.url; - this.url = requestOptions.url; + this.url = requestOptions.url !; if (requestOptions.params) { const params = requestOptions.params.toString(); if (params.length > 0) { @@ -88,13 +88,13 @@ export class Request extends Body { } } this._body = requestOptions.body; - this.method = normalizeMethodName(requestOptions.method); + 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; + this.withCredentials = requestOptions.withCredentials !; + this.responseType = requestOptions.responseType !; } /** diff --git a/packages/http/src/static_response.ts b/packages/http/src/static_response.ts index 199fd08af5..f46d4e0491 100644 --- a/packages/http/src/static_response.ts +++ b/packages/http/src/static_response.ts @@ -63,7 +63,7 @@ export class Response extends Body { * * Defaults to "OK" */ - statusText: string; + statusText: string|null; /** * Non-standard property * @@ -81,17 +81,17 @@ export class Response extends Body { * Headers object based on the `Headers` class in the [Fetch * Spec](https://fetch.spec.whatwg.org/#headers-class). */ - headers: Headers; + headers: Headers|null; constructor(responseOptions: ResponseOptions) { super(); this._body = responseOptions.body; - this.status = responseOptions.status; + this.status = responseOptions.status !; this.ok = (this.status >= 200 && this.status <= 299); this.statusText = responseOptions.statusText; this.headers = responseOptions.headers; - this.type = responseOptions.type; - this.url = responseOptions.url; + this.type = responseOptions.type !; + this.url = responseOptions.url !; } toString(): string { diff --git a/packages/http/src/url_search_params.ts b/packages/http/src/url_search_params.ts index 3effcad374..ffb1086da7 100644 --- a/packages/http/src/url_search_params.ts +++ b/packages/http/src/url_search_params.ts @@ -93,7 +93,7 @@ export class URLSearchParams { has(param: string): boolean { return this.paramsMap.has(param); } - get(param: string): string { + get(param: string): string|null { const storedParam = this.paramsMap.get(param); return Array.isArray(storedParam) ? storedParam[0] : null; diff --git a/packages/http/test/backends/jsonp_backend_spec.ts b/packages/http/test/backends/jsonp_backend_spec.ts index 5f0e4d501f..ecef2c529e 100644 --- a/packages/http/test/backends/jsonp_backend_spec.ts +++ b/packages/http/test/backends/jsonp_backend_spec.ts @@ -59,13 +59,14 @@ export function main() { ]); backend = injector.get(JSONPBackend); const base = new BaseRequestOptions(); - sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'}))); + sampleRequest = + new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any); }); afterEach(() => { existingScripts = []; }); it('should create a connection', () => { - let instance: JSONPConnection; + let instance: JSONPConnection = undefined !; expect(() => instance = backend.createConnection(sampleRequest)).not.toThrow(); expect(instance).toBeAnInstanceOf(JSONPConnection); }); @@ -146,8 +147,8 @@ export function main() { RequestMethod.Head, RequestMethod.Patch] .forEach(method => { const base = new BaseRequestOptions(); - const req = new Request( - base.merge(new RequestOptions({url: 'https://google.com', method: method}))); + const req = new Request(base.merge( + new RequestOptions({url: 'https://google.com', method: method})) as any); expect(() => new JSONPConnection_(req, new MockBrowserJsonp()).response.subscribe()) .toThrowError(); }); diff --git a/packages/http/test/backends/mock_backend_spec.ts b/packages/http/test/backends/mock_backend_spec.ts index ee81874791..3b4640decc 100644 --- a/packages/http/test/backends/mock_backend_spec.ts +++ b/packages/http/test/backends/mock_backend_spec.ts @@ -31,9 +31,11 @@ export function main() { [{provide: ResponseOptions, useClass: BaseResponseOptions}, MockBackend]); backend = injector.get(MockBackend); const base = new BaseRequestOptions(); - sampleRequest1 = new Request(base.merge(new RequestOptions({url: 'https://google.com'}))); + sampleRequest1 = + new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any); sampleResponse1 = new Response(new ResponseOptions({body: 'response1'})); - sampleRequest2 = new Request(base.merge(new RequestOptions({url: 'https://google.com'}))); + sampleRequest2 = + new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any); sampleResponse2 = new Response(new ResponseOptions({body: 'response2'})); }); @@ -65,7 +67,7 @@ export function main() { it('should allow responding after subscription with an error', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { const connection: MockConnection = backend.createConnection(sampleRequest1); - connection.response.subscribe(null, () => { async.done(); }); + connection.response.subscribe(null !, () => { async.done(); }); connection.mockError(new Error('nope')); })); @@ -98,12 +100,12 @@ export function main() { xit('should allow double subscribing', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { const responses: Response[] = [sampleResponse1, sampleResponse2]; - backend.connections.subscribe((c: MockConnection) => c.mockRespond(responses.shift())); + backend.connections.subscribe((c: MockConnection) => c.mockRespond(responses.shift() !)); const responseObservable: ReplaySubject = backend.createConnection(sampleRequest1).response; responseObservable.subscribe(res => expect(res.text()).toBe('response1')); responseObservable.subscribe( - res => expect(res.text()).toBe('response2'), null, async.done); + res => expect(res.text()).toBe('response2'), null !, async.done); })); // TODO(robwormald): readyStates are leaving? diff --git a/packages/http/test/backends/xhr_backend_spec.ts b/packages/http/test/backends/xhr_backend_spec.ts index f62840474c..8be7db3b00 100644 --- a/packages/http/test/backends/xhr_backend_spec.ts +++ b/packages/http/test/backends/xhr_backend_spec.ts @@ -75,7 +75,7 @@ class MockBrowserXHR extends BrowserXhr { removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); } - dispatchEvent(type: string) { this.callbacks.get(type)({}); } + dispatchEvent(type: string) { this.callbacks.get(type) !({}); } build() { const xhr = new MockBrowserXHR(); @@ -99,7 +99,8 @@ export function main() { beforeEach(inject([XHRBackend], (be: XHRBackend) => { backend = be; const base = new BaseRequestOptions(); - sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'}))); + sampleRequest = + new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any); })); afterEach(() => { existingXHRs = []; }); @@ -163,7 +164,7 @@ export function main() { sampleRequest, new MockBrowserXHR(), new ResponseOptions({type: ResponseType.Error})); connection.response.subscribe( - (res: Response) => { expect(res.type).toBe(ResponseType.Error); }, null, + (res: Response) => { expect(res.type).toBe(ResponseType.Error); }, null !, () => { async.done(); }); existingXHRs[0].setStatusCode(200); existingXHRs[0].dispatchEvent('load'); @@ -181,7 +182,7 @@ export function main() { const connection = new XHRConnection( sampleRequest, new MockBrowserXHR(), new ResponseOptions({type: ResponseType.Error})); - connection.response.subscribe(null, (res: Response) => { + connection.response.subscribe(null !, (res: Response) => { expect(res.type).toBe(ResponseType.Error); async.done(); }); @@ -193,7 +194,7 @@ export function main() { const connection = new XHRConnection( sampleRequest, new MockBrowserXHR(), new ResponseOptions({type: ResponseType.Error})); - connection.response.subscribe(null, (res: Response) => { + connection.response.subscribe(null !, (res: Response) => { expect(res.type).toBe(ResponseType.Error); expect(res.status).toEqual(0); expect(res.statusText).toEqual(''); @@ -217,7 +218,7 @@ export function main() { const body = 'Some body to love'; const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR()); expect(sendSpy).not.toHaveBeenCalled(); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith(body); @@ -229,7 +230,8 @@ export function main() { const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({headers: headers}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({headers: headers})) as any), + new MockBrowserXHR()); connection.response.subscribe(); expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/xml'); expect(setRequestHeaderSpy).toHaveBeenCalledWith('Breaking-Bad', '<3'); @@ -240,7 +242,7 @@ export function main() { const headers = new Headers(); const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({headers})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(setRequestHeaderSpy) .toHaveBeenCalledWith('Accept', 'application/json, text/plain, */*'); @@ -250,7 +252,7 @@ export function main() { const headers = new Headers({'Accept': 'text/xml'}); const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({headers})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(setRequestHeaderSpy).toHaveBeenCalledWith('Accept', 'text/xml'); }); @@ -260,7 +262,7 @@ export function main() { const body = {test: 'val'}; const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body, headers: headers}))), + new Request(base.merge(new RequestOptions({body: body, headers: headers})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/plain'); @@ -272,7 +274,7 @@ export function main() { const body = {test: 'val'}; const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith(JSON.stringify(body, null, 2)); expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'application/json'); @@ -282,7 +284,7 @@ export function main() { const body = 23; const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith('23'); expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/plain'); @@ -292,7 +294,7 @@ export function main() { const body = 'some string'; const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith(body); expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/plain'); @@ -304,7 +306,7 @@ export function main() { body.set('test2', 'val2'); const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR()); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith('test1=val1&test2=val2'); expect(setRequestHeaderSpy) @@ -337,7 +339,8 @@ export function main() { body.append('userfile', blob); const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), + new MockBrowserXHR()); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith(body); expect(setRequestHeaderSpy).not.toHaveBeenCalledWith(); @@ -347,14 +350,15 @@ export function main() { const body = createBlob(['body { color: red; }'], 'text/css'); const base = new BaseRequestOptions(); const connection = new XHRConnection( - new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); + new Request(base.merge(new RequestOptions({body: body})) as any), + new MockBrowserXHR()); connection.response.subscribe(); expect(sendSpy).toHaveBeenCalledWith(body); expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/css'); }); it('should use blob body without type to the request', () => { - const body = createBlob(['body { color: red; }'], null); + const body = createBlob(['body { color: red; }'], null !); const base = new BaseRequestOptions(); const connection = new XHRConnection( new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); @@ -366,7 +370,7 @@ export function main() { it('should use blob body without type with custom content type header to the request', () => { const headers = new Headers({'Content-Type': 'text/css'}); - const body = createBlob(['body { color: red; }'], null); + const body = createBlob(['body { color: red; }'], null !); const base = new BaseRequestOptions(); const connection = new XHRConnection( new Request(base.merge(new RequestOptions({body: body, headers: headers}))), @@ -590,7 +594,7 @@ export function main() { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { const conn = new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions()); - conn.response.subscribe(null, (res: Response) => { + conn.response.subscribe(null !, (res: Response) => { expect(res.text()).toBe('{json: "object"}'); async.done(); }); @@ -611,10 +615,10 @@ Transfer-Encoding: chunked Connection: keep-alive`; connection.response.subscribe((res: Response) => { - expect(res.headers.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT'); - expect(res.headers.get('Content-Type')).toEqual('application/json; charset=utf-8'); - expect(res.headers.get('Transfer-Encoding')).toEqual('chunked'); - expect(res.headers.get('Connection')).toEqual('keep-alive'); + expect(res.headers !.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT'); + expect(res.headers !.get('Content-Type')).toEqual('application/json; charset=utf-8'); + expect(res.headers !.get('Transfer-Encoding')).toEqual('chunked'); + expect(res.headers !.get('Connection')).toEqual('keep-alive'); async.done(); }); diff --git a/packages/http/test/http_spec.ts b/packages/http/test/http_spec.ts index 544d58745c..3f0fb7b5c0 100644 --- a/packages/http/test/http_spec.ts +++ b/packages/http/test/http_spec.ts @@ -175,7 +175,7 @@ export function main() { backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse)); http.request('http://basic.connection') .subscribe( - (res: Response) => { expect(res.text()).toBe('base response'); }, null, + (res: Response) => { expect(res.text()).toBe('base response'); }, null !, () => { async.done(); }); })); @@ -188,7 +188,7 @@ export function main() { }); http.request('http://basic.connection') .subscribe( - (res: Response) => { expect(res.text()).toBe('base response'); }, null, + (res: Response) => { expect(res.text()).toBe('base response'); }, null !, () => { async.done(); }); })); diff --git a/packages/http/test/static_request_spec.ts b/packages/http/test/static_request_spec.ts index 41c9d2b586..bdf05d213a 100644 --- a/packages/http/test/static_request_spec.ts +++ b/packages/http/test/static_request_spec.ts @@ -17,7 +17,8 @@ export function main() { describe('Request', () => { describe('detectContentType', () => { it('should return ContentType.NONE', () => { - const req = new Request(new RequestOptions({url: 'test', method: 'GET', body: null})); + const req = + new Request(new RequestOptions({url: 'test', method: 'GET', body: null}) as any); expect(req.detectContentType()).toEqual(ContentType.NONE); }); @@ -28,7 +29,7 @@ export function main() { method: 'GET', body: null, headers: new Headers({'content-type': 'application/json'}) - })); + }) as any); expect(req.detectContentType()).toEqual(ContentType.JSON); }); @@ -39,7 +40,7 @@ export function main() { method: 'GET', body: null, headers: new Headers({'content-type': 'application/x-www-form-urlencoded'}) - })); + }) as any); expect(req.detectContentType()).toEqual(ContentType.FORM); }); @@ -50,7 +51,7 @@ export function main() { method: 'GET', body: null, headers: new Headers({'content-type': 'multipart/form-data'}) - })); + }) as any); expect(req.detectContentType()).toEqual(ContentType.FORM_DATA); }); @@ -61,7 +62,7 @@ export function main() { method: 'GET', body: null, headers: new Headers({'content-type': 'text/plain'}) - })); + }) as any); expect(req.detectContentType()).toEqual(ContentType.TEXT); }); @@ -72,7 +73,7 @@ export function main() { method: 'GET', body: null, headers: new Headers({'content-type': 'application/octet-stream'}) - })); + }) as any); expect(req.detectContentType()).toEqual(ContentType.BLOB); }); @@ -83,7 +84,7 @@ export function main() { method: 'GET', body: new ArrayBuffer(1), headers: new Headers({'content-type': 'application/octet-stream'}) - })); + }) as any); expect(req.detectContentType()).toEqual(ContentType.ARRAY_BUFFER); }); @@ -95,7 +96,7 @@ export function main() { method: 'GET', body: null, headers: new Headers({'content-type': 'application/json'}) - })); + }) as any); expect(req.text()).toEqual(''); }); @@ -104,7 +105,7 @@ export function main() { const reqOptions = new RequestOptions( {url: 'test', method: 'GET', headers: new Headers({'content-type': 'application/json'})}); delete reqOptions.body; - const req = new Request(reqOptions); + const req = new Request(reqOptions as any); expect(req.text()).toEqual(''); }); diff --git a/packages/http/test/url_search_params_spec.ts b/packages/http/test/url_search_params_spec.ts index 1cb725f147..e95620ed46 100644 --- a/packages/http/test/url_search_params_spec.ts +++ b/packages/http/test/url_search_params_spec.ts @@ -151,19 +151,19 @@ export function main() { it('should remove the parameter when set to undefined or null', () => { const params = new URLSearchParams('q=Q'); - params.set('q', undefined); + params.set('q', undefined !); expect(params.has('q')).toBe(false); expect(params.toString()).toEqual(''); - params.set('q', null); + params.set('q', null !); expect(params.has('q')).toBe(false); expect(params.toString()).toEqual(''); }); it('should ignore the value when append undefined or null', () => { const params = new URLSearchParams('q=Q'); - params.append('q', undefined); + params.append('q', undefined !); expect(params.toString()).toEqual('q=Q'); - params.append('q', null); + params.append('q', null !); expect(params.toString()).toEqual('q=Q'); }); diff --git a/tools/public_api_guard/http/http.d.ts b/tools/public_api_guard/http/http.d.ts index 2b0196aa37..e1bd37fd8f 100644 --- a/tools/public_api_guard/http/http.d.ts +++ b/tools/public_api_guard/http/http.d.ts @@ -36,13 +36,13 @@ export declare class CookieXSRFStrategy implements XSRFStrategy { export declare class Headers { constructor(headers?: Headers | { [name: string]: any; - }); + } | null); append(name: string, value: string): void; delete(name: string): void; entries(): void; - forEach(fn: (values: string[], name: string, headers: Map) => void): void; - get(name: string): string; - getAll(name: string): string[]; + forEach(fn: (values: string[], name: string | undefined, headers: Map) => void): void; + get(name: string): string | null; + getAll(name: string): string[] | null; has(name: string): boolean; keys(): string[]; set(name: string, value: string | string[]): void; @@ -137,13 +137,13 @@ export declare enum RequestMethod { /** @experimental */ export declare class RequestOptions { body: any; - headers: Headers; - method: RequestMethod | string; + headers: Headers | null; + method: RequestMethod | string | null; params: URLSearchParams; - responseType: ResponseContentType; + responseType: ResponseContentType | null; /** @deprecated */ search: URLSearchParams; - url: string; - withCredentials: boolean; + url: string | null; + withCredentials: boolean | null; constructor({method, headers, body, url, search, params, withCredentials, responseType}?: RequestOptionsArgs); merge(options?: RequestOptionsArgs): RequestOptions; } @@ -151,26 +151,26 @@ export declare class RequestOptions { /** @experimental */ export interface RequestOptionsArgs { body?: any; - headers?: Headers; - method?: string | RequestMethod; + headers?: Headers | null; + method?: string | RequestMethod | null; params?: string | URLSearchParams | { [key: string]: any | any[]; - }; - responseType?: ResponseContentType; + } | null; + responseType?: ResponseContentType | null; /** @deprecated */ search?: string | URLSearchParams | { [key: string]: any | any[]; - }; - url?: string; - withCredentials?: boolean; + } | null; + url?: string | null; + withCredentials?: boolean | null; } /** @experimental */ export declare class Response extends Body { bytesLoaded: number; - headers: Headers; + headers: Headers | null; ok: boolean; status: number; - statusText: string; + statusText: string | null; totalBytes: number; type: ResponseType; url: string; @@ -188,22 +188,22 @@ export declare enum ResponseContentType { /** @experimental */ export declare class ResponseOptions { - body: string | Object | ArrayBuffer | Blob; - headers: Headers; - status: number; - url: string; + body: string | Object | ArrayBuffer | Blob | null; + headers: Headers | null; + status: number | null; + url: string | null; constructor({body, status, headers, statusText, type, url}?: ResponseOptionsArgs); merge(options?: ResponseOptionsArgs): ResponseOptions; } /** @experimental */ export interface ResponseOptionsArgs { - body?: string | Object | FormData | ArrayBuffer | Blob; - headers?: Headers; - status?: number; - statusText?: string; - type?: ResponseType; - url?: string; + body?: string | Object | FormData | ArrayBuffer | Blob | null; + headers?: Headers | null; + status?: number | null; + statusText?: string | null; + type?: ResponseType | null; + url?: string | null; } /** @experimental */ @@ -224,7 +224,7 @@ export declare class URLSearchParams { appendAll(searchParams: URLSearchParams): void; clone(): URLSearchParams; delete(param: string): void; - get(param: string): string; + get(param: string): string | null; getAll(param: string): string[]; has(param: string): boolean; replaceAll(searchParams: URLSearchParams): void;