Skip to content

Instantly share code, notes, and snippets.

@sekizlipenguen
Last active January 30, 2022 10:32
Show Gist options
  • Save sekizlipenguen/d22c437a47f35a9df0894dbf5aea9a52 to your computer and use it in GitHub Desktop.
Save sekizlipenguen/d22c437a47f35a9df0894dbf5aea9a52 to your computer and use it in GitHub Desktop.
Javascript Connection
//import React from 'react';
const timeout = 5000;
const Connection = {
isObject: function (v) {
while (Object.prototype.toString.call(v) === '[object Object]') {
if ((v = Object.getPrototypeOf(v)) === null) {
return true;
}
}
return false;
},
fetchWrapper: function (url, options) {
return new Promise((resolve, reject) => {
fetch(url, options).then(resolve, reject);
if (options && options?.timeout) {
const e = new Error('Connection timed out');
setTimeout(reject, options?.timeout, e);
}
});
},
get: function (url, config) {
let then1 = {};
const defaultInit = {
method: 'GET',
timeout: timeout,
};
const init = Object.assign({}, defaultInit, config);
return new Promise((resolve, reject) => {
this.fetchWrapper(url, init).then((response) => {
then1 = response;
if (response.headers.get('content-type') && response.headers.get('content-type').match(/application\/json/)) {
return response.json();
}
return response;
}).then(res2 => {
const then2 = {data: res2};
const result = Object.assign({}, then2, then1, {config: init});
if (then1.ok) {
resolve(result);
} else {
reject(result);
}
}).catch(err => reject(err));
});
},
post: function (url, data, config) {
const isData = typeof data;
const body = isData === 'object' ? JSON.stringify(data) : data;
let then1 = {};
const defaultInit = {
method: 'POST',
body: body,
timeout: timeout,
};
const init = Object.assign({}, defaultInit, config);
return new Promise((resolve, reject) => {
this.fetchWrapper(url, init).then((response) => {
then1 = response;
if (response.headers.get('content-type') && response.headers.get('content-type').match(/application\/json/)) {
return response.json();
}
return response;
}).then(res2 => {
const then2 = {data: res2};
const result = Object.assign({}, then2, then1, {config: init});
if (then1.ok) {
resolve(result);
} else {
reject(result);
}
}).catch(err => reject(err));
});
},
put: function (url, data, config) {
const isData = typeof data;
const body = isData === 'object' ? JSON.stringify(data) : data;
let then1 = {};
const defaultInit = {
method: 'PUT',
body: body,
timeout: timeout,
};
const init = Object.assign({}, defaultInit, config);
return new Promise((resolve, reject) => {
fetch(url, init).then((response) => {
then1 = response;
if (response.headers.get('content-type') && response.headers.get('content-type').match(/application\/json/)) {
return response.json();
}
return response;
}).then(res2 => {
const then2 = {data: res2};
const result = Object.assign({}, then2, then1, {config: init});
if (then1.ok) {
resolve(result);
} else {
reject(result);
}
}).catch(err => reject(err));
});
},
delete: function (url, config) {
let then1 = {};
const defaultInit = {
method: 'DELETE',
timeout: timeout,
};
const init = Object.assign({}, defaultInit, config);
return new Promise((resolve, reject) => {
fetch(url, init).then((response) => {
then1 = response;
if (response.headers.get('content-type') && response.headers.get('content-type').match(/application\/json/)) {
return response.json();
}
return response;
}).then(res2 => {
const then2 = {data: res2};
const result = Object.assign({}, then2, then1, {config: init});
if (then1.ok) {
resolve(result);
} else {
reject(result);
}
}).catch(err => reject(err));
});
},
v2: function (url, data = {}, config = {}) {
let body = (this.isObject(data) && config.files !== true) ? JSON.stringify(data) : data;
const defaultInit = {
method: 'GET',
timeout: timeout,
progress: null,
headers: {},
};
const init = Object.assign({}, defaultInit, config);
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open(init.method, url);
request.upload.addEventListener('progress', function (e) {
if (init.progress) {
return init.progress(e);
}
});
request.onreadystatechange = (e) => {
const responseHeaders = request.responseHeaders;
if (request.readyState !== 4) {
return;
}
let result = request.response;
if (responseHeaders && responseHeaders['Content-Type'] && responseHeaders['Content-Type'].match(/application\/json/)) {
result = JSON.parse(request.response);
}
if (request.status >= 200 && request.status < 300) {
resolve(result);
} else {
reject(result);
}
};
Object.keys(init.headers).forEach(function (item) {
request.setRequestHeader(item, init.headers[item]);
});
request.send(body);
});
},
};
export default Connection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment