angular-cn/modules/angular2/src/http/http_utils.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

import {isString} from 'angular2/src/facade/lang';
import {RequestMethod} from './enums';
import {makeTypeError} from 'angular2/src/facade/exceptions';
import {Response} from './static_response';
2016-02-01 20:05:50 -05:00
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
if (isString(method)) {
var originalMethod = method;
2016-02-01 20:05:50 -05:00
method = (<string>method)
.replace(
/(\w)(\w*)/g,
(g0: string, g1: string, g2: string) => g1.toUpperCase() + g2.toLowerCase());
2016-02-01 20:05:50 -05:00
method = <number>(<{[key: string]: any}>RequestMethod)[method];
if (typeof method !== 'number')
throw makeTypeError(
`Invalid request method. The method "${originalMethod}" is not supported.`);
}
2016-02-01 20:05:50 -05:00
return <RequestMethod>method;
}
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);
export function getResponseURL(xhr: any): string {
if ('responseURL' in xhr) {
return xhr.responseURL;
}
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
return xhr.getResponseHeader('X-Request-URL');
}
return;
}
export {isJsObject} from 'angular2/src/facade/lang';