fix(http): make normalizeMethodName optimizer-compatible. (#12370)

`normalizeMethodName` reflectively accessed the RequestMethod enum. With a smart
optimizer, properties from the enum could be removed or renamed, and so user
code just passing in e.g. 'PATCH' might not work. This change fixes the code to
be more explicit and avoids the optimizer issue.
This commit is contained in:
Martin Probst 2016-10-18 11:21:54 -07:00 committed by GitHub
parent 38e2203b24
commit 8409b65153

View File

@ -11,17 +11,24 @@ import {isString} from '../src/facade/lang';
import {RequestMethod} from './enums'; import {RequestMethod} from './enums';
export function normalizeMethodName(method: string | RequestMethod): RequestMethod { export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
if (isString(method)) { if (!isString(method)) return method;
var originalMethod = method; switch (method.toUpperCase()) {
method = (<string>method) case 'GET':
.replace( return RequestMethod.Get;
/(\w)(\w*)/g, case 'POST':
(g0: string, g1: string, g2: string) => g1.toUpperCase() + g2.toLowerCase()); return RequestMethod.Post;
method = <RequestMethod>(<{[key: string]: any}>RequestMethod)[method]; case 'PUT':
if (typeof method !== 'number') return RequestMethod.Put;
throw new Error(`Invalid request method. The method "${originalMethod}" is not supported.`); case 'DELETE':
return RequestMethod.Delete;
case 'OPTIONS':
return RequestMethod.Options;
case 'HEAD':
return RequestMethod.Head;
case 'PATCH':
return RequestMethod.Patch;
} }
return <RequestMethod>method; throw new Error(`Invalid request method. The method "${method}" is not supported.`);
} }
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300); export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);