...
 
Commits (2)
......@@ -46,13 +46,9 @@ export default function(input, init, tag) {
const xhr = new XMLHttpRequest();
// manual implementation of timeout
xhr.timer = setTimeout(() => {
if (xhr.readyState !== XMLHttpRequest.DONE) {
reject(new TypeError('Network request failed'))
xhr.abort();
}
}, 5000);
if (init && init.timeout) {
xhr.timeout = init.timeout;
}
if (tag !== undefined) {
xhr.tag = tag;
......@@ -72,9 +68,11 @@ export default function(input, init, tag) {
return;
}
xhr.ontimeout = function() {
reject(new TypeError('Network request failed'))
}
xhr.onload = function() {
// clear timeout
clearTimeout(xhr.timer);
const status = (xhr.status === 1223) ? 204 : xhr.status
if (status < 100 || status > 599) {
......@@ -94,7 +92,6 @@ export default function(input, init, tag) {
}
xhr.onerror = function() {
clearTimeout(xhr.timer);
remove(xhr);
reject(new TypeError('Network request failed'));
}
......@@ -103,8 +100,6 @@ export default function(input, init, tag) {
xhr.onabort = () => {
reject(new Abort());
// clear timeout
clearTimeout(xhr.timer);
};
if (request.credentials === 'include') {
......
import Cancelable from 'promise-cancelable';
import session from './session.service';
import { MINDS_API_URI, MINDS_URI_SETTINGS } from '../../config/Config';
import { MINDS_API_URI, MINDS_URI_SETTINGS, NETWORK_TIMEOUT } from '../../config/Config';
import { btoa } from 'abab';
import abortableFetch from '../helpers/abortableFetch';
......@@ -57,7 +57,7 @@ class ApiService {
async get(url, params = {}, tag) {
const headers = this.buildHeaders();
try {
const response = await abortableFetch(MINDS_API_URI + this.buildUrl(url, params), { headers }, tag);
const response = await abortableFetch(MINDS_API_URI + this.buildUrl(url, params), { headers, timeout: NETWORK_TIMEOUT }, tag);
// Bad response
if (!response.ok) {
......@@ -87,7 +87,7 @@ class ApiService {
const headers = this.buildHeaders();
try {
let response = await abortableFetch(MINDS_API_URI + this.buildUrl(url), { method: 'POST', body: JSON.stringify(body), headers });
let response = await abortableFetch(MINDS_API_URI + this.buildUrl(url), { method: 'POST', body: JSON.stringify(body), headers, timeout: NETWORK_TIMEOUT });
if (!response.ok) {
throw response;
......@@ -116,7 +116,7 @@ class ApiService {
const headers = this.buildHeaders();
try {
let response = await abortableFetch(MINDS_API_URI + this.buildUrl(url), { method: 'PUT', body: JSON.stringify(body), headers });
let response = await abortableFetch(MINDS_API_URI + this.buildUrl(url), { method: 'PUT', body: JSON.stringify(body), headers, timeout: NETWORK_TIMEOUT });
if (!response.ok) {
throw response;
......@@ -145,7 +145,7 @@ class ApiService {
const headers = this.buildHeaders();
try {
let response = await abortableFetch(MINDS_API_URI + this.buildUrl(url), { method: 'DELETE', body: JSON.stringify(body), headers });
let response = await abortableFetch(MINDS_API_URI + this.buildUrl(url), { method: 'DELETE', body: JSON.stringify(body), headers, timeout: NETWORK_TIMEOUT });
if (!response.ok) {
throw response;
......
......@@ -10,6 +10,8 @@ import {
export const MINDS_URI = 'https://www.minds.com/';
export const MINDS_API_URI = 'https://www.minds.com/';
export const NETWORK_TIMEOUT = 5000;
export const MINDS_URI_SETTINGS = {
//basicAuth: 'crypto:ohms',
};
......