2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2015-12-03 16:44:14 -05:00
|
|
|
import {RequestMethod} from './enums';
|
2015-09-21 03:46:16 -04:00
|
|
|
|
2016-02-01 20:05:50 -05:00
|
|
|
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
|
2016-10-19 16:42:39 -04:00
|
|
|
if (typeof method !== 'string') return method;
|
|
|
|
|
2016-10-18 14:21:54 -04:00
|
|
|
switch (method.toUpperCase()) {
|
|
|
|
case 'GET':
|
|
|
|
return RequestMethod.Get;
|
|
|
|
case 'POST':
|
|
|
|
return RequestMethod.Post;
|
|
|
|
case 'PUT':
|
|
|
|
return RequestMethod.Put;
|
|
|
|
case 'DELETE':
|
|
|
|
return RequestMethod.Delete;
|
|
|
|
case 'OPTIONS':
|
|
|
|
return RequestMethod.Options;
|
|
|
|
case 'HEAD':
|
|
|
|
return RequestMethod.Head;
|
|
|
|
case 'PATCH':
|
|
|
|
return RequestMethod.Patch;
|
2015-09-21 03:46:16 -04:00
|
|
|
}
|
2016-10-18 14:21:54 -04:00
|
|
|
throw new Error(`Invalid request method. The method "${method}" is not supported.`);
|
2015-09-21 03:46:16 -04:00
|
|
|
}
|
|
|
|
|
2015-11-19 20:29:41 -05:00
|
|
|
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);
|
|
|
|
|
2017-04-17 12:48:11 -04:00
|
|
|
export function getResponseURL(xhr: any): string {
|
2015-11-19 21:47:29 -05:00
|
|
|
if ('responseURL' in xhr) {
|
|
|
|
return xhr.responseURL;
|
|
|
|
}
|
|
|
|
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
|
|
|
|
return xhr.getResponseHeader('X-Request-URL');
|
|
|
|
}
|
2017-04-17 12:48:11 -04:00
|
|
|
return;
|
2015-11-19 21:47:29 -05:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:47:20 -04:00
|
|
|
export function stringToArrayBuffer(input: String): ArrayBuffer {
|
2016-11-12 08:08:58 -05:00
|
|
|
const view = new Uint16Array(input.length);
|
|
|
|
for (let i = 0, strLen = input.length; i < strLen; i++) {
|
2016-05-26 18:47:20 -04:00
|
|
|
view[i] = input.charCodeAt(i);
|
2016-02-24 10:37:18 -05:00
|
|
|
}
|
|
|
|
return view.buffer;
|
|
|
|
}
|