add basic services
This commit is contained in:
100
src/services/fetch.ts
Normal file
100
src/services/fetch.ts
Normal file
@ -0,0 +1,100 @@
|
||||
declare var window: any;
|
||||
const defaultBasePath = '';
|
||||
|
||||
export function fetchAsync(method: 'GET' | 'POST' | 'DELETE' | 'PUT', url: string, body?: any) {
|
||||
const headers = { 'Content-Type': 'application/json; charset=utf-8' };
|
||||
// if (access_token) headers['Authorization'] = `Token ${access_token}`;
|
||||
return window.fetch(`${defaultBasePath}${url}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body && JSON.stringify(body)
|
||||
}).then((response: any) => {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/auth/login';
|
||||
// throw new Error('401');
|
||||
}
|
||||
if (response.status === 403 && url === '/api/v1/organization/') {
|
||||
// this indicates a user that is not supposed to be in bx-console
|
||||
window.location.href = '/auth/logout';
|
||||
}
|
||||
const result = response.json();
|
||||
if (!response.ok) throw result;
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
export function get<T>(url: string): Promise<T> {
|
||||
return fetchAsync('GET', url);
|
||||
}
|
||||
|
||||
export function post<T>(url: string, body?: any): Promise<T> {
|
||||
return fetchAsync('POST', url, body);
|
||||
}
|
||||
|
||||
export function postWithFile<T>(url: string, body?: any): Promise<T> {
|
||||
return fetchWithFile('POST', url, body);
|
||||
}
|
||||
|
||||
export function putWithFile<T>(url: string, body?: any): Promise<T> {
|
||||
return fetchWithFile('PUT', url, body);
|
||||
}
|
||||
|
||||
export function fetchWithFile<T>(method: string, url: string, body?: any): Promise<T> {
|
||||
return window.fetch(`${defaultBasePath}${url}`, {
|
||||
method: method,
|
||||
body: body
|
||||
}).then((response: any) => {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/auth/login';
|
||||
// throw new Error('401');
|
||||
}
|
||||
const result = response.json();
|
||||
if (!response.ok) throw result;
|
||||
return result;
|
||||
})
|
||||
}
|
||||
|
||||
export function del(url: string) {
|
||||
return fetchAsync('DELETE', url);
|
||||
}
|
||||
|
||||
export function put(url: string, body?: any) {
|
||||
return fetchAsync('PUT', url, body);
|
||||
}
|
||||
export function toQueryString(obj: any) {
|
||||
const parts = [];
|
||||
for (var i in obj) {
|
||||
if (obj.hasOwnProperty(i)) {
|
||||
parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
|
||||
}
|
||||
}
|
||||
return parts.join("&");
|
||||
}
|
||||
|
||||
export function serializeObject<T>(form: any) {
|
||||
let obj: {[key: string]: any} = {};
|
||||
if (typeof form == 'object' && form.nodeName == "FORM") {
|
||||
for (let i = 0; i < form.elements.length; i++) {
|
||||
const field = form.elements[i];
|
||||
if (field.name
|
||||
&& field.type != 'file'
|
||||
&& field.type != 'reset'
|
||||
&& field.type != 'submit'
|
||||
&& field.type != 'button') {
|
||||
if (field.type == 'select-multiple') {
|
||||
obj[field.name] = '';
|
||||
let tempvalue = '';
|
||||
for (let j = 0; j < form.elements[i].options.length; j++) {
|
||||
if (field.options[j].selected)
|
||||
tempvalue += field.options[j].value + ';';
|
||||
}
|
||||
if (tempvalue.charAt(tempvalue.length - 1) === ';') obj[field.name] = tempvalue.substring(0, tempvalue.length - 1);
|
||||
|
||||
} else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) {
|
||||
obj[field.name] = field.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj as T;
|
||||
}
|
Reference in New Issue
Block a user