Skip to content

Commit 8409b65

Browse files
authored
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.
1 parent 38e2203 commit 8409b65

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

modules/@angular/http/src/http_utils.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ import {isString} from '../src/facade/lang';
1111
import {RequestMethod} from './enums';
1212

1313
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
14-
if (isString(method)) {
15-
var originalMethod = method;
16-
method = (<string>method)
17-
.replace(
18-
/(\w)(\w*)/g,
19-
(g0: string, g1: string, g2: string) => g1.toUpperCase() + g2.toLowerCase());
20-
method = <RequestMethod>(<{[key: string]: any}>RequestMethod)[method];
21-
if (typeof method !== 'number')
22-
throw new Error(`Invalid request method. The method "${originalMethod}" is not supported.`);
14+
if (!isString(method)) return method;
15+
switch (method.toUpperCase()) {
16+
case 'GET':
17+
return RequestMethod.Get;
18+
case 'POST':
19+
return RequestMethod.Post;
20+
case 'PUT':
21+
return RequestMethod.Put;
22+
case 'DELETE':
23+
return RequestMethod.Delete;
24+
case 'OPTIONS':
25+
return RequestMethod.Options;
26+
case 'HEAD':
27+
return RequestMethod.Head;
28+
case 'PATCH':
29+
return RequestMethod.Patch;
2330
}
24-
return <RequestMethod>method;
31+
throw new Error(`Invalid request method. The method "${method}" is not supported.`);
2532
}
2633

2734
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);

0 commit comments

Comments
 (0)