ih-black-lion
Version:
State handler for Arus projects
114 lines (89 loc) • 2.24 kB
JavaScript
import axios from 'axios';
import base64 from 'base-64';
export default class Request {
static get(params) {
const { url, auth } = params;
let { headers } = params;
if (!url) {
throw new Error('Endpoint url not defined');
}
if (auth) {
const creds = `${auth[0]}:${auth[1]}`;
const encoded = `Basic ${base64.encode(creds)}`;
if (headers) {
headers.authorization = encoded;
} else {
headers = {
authorization: encoded,
};
}
}
return axios.get(params.url, { headers });
}
static post(params) {
const { url, auth } = params;
let { send, headers } = params;
if (!url) {
throw new Error('Endpoint url not defined');
}
if (!send) {
send = params.body || '';
}
if (auth) {
const creds = `${auth[0]}:${auth[1]}`;
const encoded = `Basic ${base64.encode(creds)}`;
if (headers) {
headers.authorization = encoded;
} else {
headers = {
authorization: encoded,
};
}
}
return axios.post(url, send, { headers });
}
static update(params) {
const { url, auth } = params;
let { send, headers } = params;
if (!url) {
throw new Error('Endpoint url not defined');
}
if (!send) {
send = params.body || '';
}
if (auth) {
const creds = `${auth[0]}:${auth[1]}`;
const encoded = `Basic ${base64.encode(creds)}`;
if (headers) {
headers.authorization = encoded;
} else {
headers = {
authorization: encoded,
};
}
}
return axios.put(url, send, { headers });
}
static delete(params) {
const { url, auth } = params;
let { send, headers } = params;
if (!url) {
throw new Error('Endpoint url not defined');
}
if (!send) {
send = params.body || '';
}
if (auth) {
const creds = `${auth[0]}:${auth[1]}`;
const encoded = `Basic ${base64.encode(creds)}`;
if (headers) {
headers.authorization = encoded;
} else {
headers = {
authorization: encoded,
};
}
}
return axios.delete(url, send, { headers });
}
}