12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- function checkStatus(response: any) {
- if (response.status >= 200 && response.status < 300) {
- return response;
- }
-
- const error = new Error(response.statusText);
- throw error;
- }
-
- export function parseUrl(obj: any): string {
- let result:string = "?";
- for (let key in obj) {
- if (obj[key]) {
- if (result === "?") {
- result += `${key}=${obj[key]}`;
- } else {
- result += `&${key}=${obj[key]}`;
- }
- }
- }
- if (result === "?") return "";
- return result;
- }
-
- /**
- * Requests a URL, returning a promise.
- *
- * @param {string} url The URL we want to request
- * @param {object} [options] The options we want to pass to "fetch"
- * @return {object} An object containing either "data" or "err"
- */
- export default async function request(url: string, options: any = {}): Promise<any> {
- let data, response: any;
- try {
- response = await fetch(url, { credentials: 'include', ...options });
-
- checkStatus(response);
- if (!response.body) return true;
- data = await response.clone().json();
- } catch (err) {
- if (response && response.clone) {
- data = await response.clone().text();
- }
- return false;
- }
-
- return data;
- }
|