fix(http): Update types for TypeScript nullability support

This reverts commit 268884296a.
This commit is contained in:
Miško Hevery
2017-04-17 11:12:53 -07:00
committed by Tobias Bosch
parent 014594fe8f
commit ec028b8109
18 changed files with 138 additions and 128 deletions

View File

@ -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 : {};

View File

@ -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) {

View File

@ -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) {

View File

@ -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;

View File

@ -41,7 +41,7 @@ export class Headers {
_normalizedNames: Map<string, string> = 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<string, string[]>) => void): void {
forEach(fn: (values: string[], name: string|undefined, headers: Map<string, string[]>) => 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;
}
/**

View File

@ -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<R
}
function mergeOptions(
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod,
url: string): RequestOptions {
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs | undefined,
method: RequestMethod, url: string): RequestArgs {
const newOptions = defaultOpts;
if (providedOpts) {
// Hack so Dart can used named parameters
@ -33,10 +34,10 @@ function mergeOptions(
body: providedOpts.body,
withCredentials: providedOpts.withCredentials,
responseType: providedOpts.responseType
}));
})) as RequestArgs;
}
return newOptions.merge(new RequestOptions({method, url}));
return newOptions.merge(new RequestOptions({method, url})) as RequestArgs;
}
/**

View File

@ -32,14 +32,14 @@ export function normalizeMethodName(method: string | RequestMethod): RequestMeth
export const isSuccess = (status: number): boolean => (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 {

View File

@ -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;
}

View File

@ -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 !;
}
/**

View File

@ -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 {

View File

@ -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;