- 1 :
/**
- 2 :
* Class embodying Load functionality.
- 3 :
* @memberof Spyral
- 4 :
* @class
- 5 :
* @hideconstructor
- 6 :
*/
- 7 :
class Load {
- 8 :
static baseUrl;
- 9 :
- 10 :
/**
- 11 :
* Set the base URL for use with the Load class
- 12 :
* @param {string} baseUrl
- 13 :
* @static
- 14 :
*/
- 15 :
static setBaseUrl(baseUrl) {
- 16 :
this.baseUrl = baseUrl;
- 17 :
}
- 18 :
- 19 :
/**
- 20 :
* Make a call to trombone
- 21 :
* @param {Object} config
- 22 :
* @param {Object} params
- 23 :
* @returns {JSON}
- 24 :
* @static
- 25 :
*/
- 26 :
static trombone(config = {}, params) {
- 27 :
let url = new URL(config.trombone ? config.trombone : this.baseUrl + 'trombone', window.location.origin);
- 28 :
delete config.trombone;
- 29 :
- 30 :
let all = { ...config, ...params };
- 31 :
for (let key in all) {
- 32 :
if (all[key] === undefined) { delete all[key]; }
- 33 :
}
- 34 :
- 35 :
let method = all.method;
- 36 :
if (method === undefined) {
- 37 :
method = 'GET';
- 38 :
} else {
- 39 :
delete all.method;
- 40 :
}
- 41 :
- 42 :
let opt = {};
- 43 :
if (method === 'GET' || method === 'POST') {
- 44 :
if (method === 'POST' || JSON.stringify(all).length > 1000) {
- 45 :
opt = {
- 46 :
method: 'POST'
- 47 :
};
- 48 :
if ('body' in all) {
- 49 :
// TODO assume FormData or set this header to ensure UTF-8?
- 50 :
// opt.headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' };
- 51 :
opt.body = all['body'];
- 52 :
} else {
- 53 :
// don't set header as it messes up boundaries
- 54 :
const formData = new FormData();
- 55 :
for (let key in all) {
- 56 :
if (all[key] instanceof Array) {
- 57 :
all[key].forEach((val) => {
- 58 :
formData.append(key, val);
- 59 :
});
- 60 :
} else {
- 61 :
formData.set(key, all[key]);
- 62 :
}
- 63 :
}
- 64 :
opt.body = formData;
- 65 :
}
- 66 :
} else {
- 67 :
for (let key in all) {
- 68 :
if (all[key] instanceof Array) {
- 69 :
all[key].forEach((val) => {
- 70 :
url.searchParams.append(key, val);
- 71 :
});
- 72 :
} else {
- 73 :
url.searchParams.set(key, all[key]);
- 74 :
}
- 75 :
}
- 76 :
}
- 77 :
} else {
- 78 :
throw Error('Load.trombone: unsupported method:', method);
- 79 :
}
- 80 :
- 81 :
return fetch(url.toString(), opt).then(response => {
- 82 :
if (response.ok) {
- 83 :
return response.json();
- 84 :
}
- 85 :
else {
- 86 :
return response.text().then(text => {
- 87 :
if (window.console) { console.error(text); }
- 88 :
throw Error(text);
- 89 :
});
- 90 :
}
- 91 :
});
- 92 :
}
- 93 :
- 94 :
/**
- 95 :
* Fetch content from a URL, often resolving cross-domain data constraints
- 96 :
* @param {string} urlToFetch
- 97 :
* @param {Object} config
- 98 :
* @returns {Response}
- 99 :
* @static
- 100 :
*/
- 101 :
static load(urlToFetch, config) {
- 102 :
let url = new URL(config && config.trombone ? config.trombone : this.baseUrl + 'trombone');
- 103 :
url.searchParams.set('fetchData', urlToFetch);
- 104 :
return fetch(url.toString()).then(response => {
- 105 :
if (response.ok) {
- 106 :
return response;
- 107 :
}
- 108 :
else {
- 109 :
return response.text().then(text => {
- 110 :
if (window.console) { console.error(text); }
- 111 :
throw Error(text);
- 112 :
});
- 113 :
}
- 114 :
}).catch(err => { throw err; });
- 115 :
}
- 116 :
- 117 :
/**
- 118 :
* Fetch HTML content from a URL
- 119 :
* @param {string} url
- 120 :
* @returns {Document}
- 121 :
* @static
- 122 :
*/
- 123 :
static html(url) {
- 124 :
return this.text(url).then(text => new DOMParser().parseFromString(text, 'text/html'));
- 125 :
}
- 126 :
- 127 :
/**
- 128 :
* Fetch XML content from a URL
- 129 :
* @param {string} url
- 130 :
* @returns {XMLDocument}
- 131 :
* @static
- 132 :
*/
- 133 :
static xml(url) {
- 134 :
return this.text(url).then(text => new DOMParser().parseFromString(text, 'text/xml'));
- 135 :
}
- 136 :
- 137 :
/**
- 138 :
* Fetch JSON content from a URL
- 139 :
* @param {string} url
- 140 :
* @returns {JSON}
- 141 :
* @static
- 142 :
*/
- 143 :
static json(url) {
- 144 :
return this.load(url).then(response => response.json());
- 145 :
}
- 146 :
- 147 :
/**
- 148 :
* Fetch text content from a URL
- 149 :
* @param {string} url
- 150 :
* @returns {string}
- 151 :
* @static
- 152 :
*/
- 153 :
static text(url) {
- 154 :
return this.load(url).then(response => response.text());
- 155 :
}
- 156 :
}
- 157 :
- 158 :
export default Load;