2019-06-07 23:02:51 +00:00
|
|
|
/**
|
2023-04-19 09:46:13 +00:00
|
|
|
* @typedef FormattedResponse
|
|
|
|
* @property {Headers} headers
|
|
|
|
* @property {Response} original
|
|
|
|
* @property {Object|String} data
|
|
|
|
* @property {Boolean} redirected
|
|
|
|
* @property {Number} status
|
|
|
|
* @property {string} statusText
|
|
|
|
* @property {string} url
|
2019-06-07 23:02:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-04-19 09:46:13 +00:00
|
|
|
* Get the content from a fetch response.
|
|
|
|
* Checks the content-type header to determine the format.
|
|
|
|
* @param {Response} response
|
|
|
|
* @returns {Promise<Object|String>}
|
2019-06-07 23:02:51 +00:00
|
|
|
*/
|
2023-04-19 09:46:13 +00:00
|
|
|
async function getResponseContent(response) {
|
|
|
|
if (response.status === 204) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-07 23:02:51 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
const responseContentType = response.headers.get('Content-Type') || '';
|
|
|
|
const subType = responseContentType.split(';')[0].split('/').pop();
|
2019-06-07 23:02:51 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
if (subType === 'javascript' || subType === 'json') {
|
|
|
|
return response.json();
|
|
|
|
}
|
2019-06-07 23:02:51 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
return response.text();
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
export class HttpError extends Error {
|
2019-06-07 23:02:51 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
constructor(response, content) {
|
|
|
|
super(response.statusText);
|
|
|
|
this.data = content;
|
|
|
|
this.headers = response.headers;
|
|
|
|
this.redirected = response.redirected;
|
|
|
|
this.status = response.status;
|
|
|
|
this.statusText = response.statusText;
|
|
|
|
this.url = response.url;
|
|
|
|
this.original = response;
|
2020-07-04 15:53:02 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new HTTP request, setting the required CSRF information
|
|
|
|
* to communicate with the back-end. Parses & formats the response.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} options
|
2023-04-19 09:46:13 +00:00
|
|
|
* @returns {Promise<FormattedResponse>}
|
2019-06-07 23:02:51 +00:00
|
|
|
*/
|
|
|
|
async function request(url, options = {}) {
|
2023-04-19 09:46:13 +00:00
|
|
|
let requestUrl = url;
|
|
|
|
|
|
|
|
if (!requestUrl.startsWith('http')) {
|
|
|
|
requestUrl = window.baseUrl(requestUrl);
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.params) {
|
2023-04-19 09:46:13 +00:00
|
|
|
const urlObj = new URL(requestUrl);
|
2023-04-18 21:20:02 +00:00
|
|
|
for (const paramName of Object.keys(options.params)) {
|
2019-06-07 23:02:51 +00:00
|
|
|
const value = options.params[paramName];
|
|
|
|
if (typeof value !== 'undefined' && value !== null) {
|
|
|
|
urlObj.searchParams.set(paramName, value);
|
|
|
|
}
|
2018-04-01 12:21:11 +00:00
|
|
|
}
|
2023-04-19 09:46:13 +00:00
|
|
|
requestUrl = urlObj.toString();
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const csrfToken = document.querySelector('meta[name=token]').getAttribute('content');
|
2023-04-19 09:46:13 +00:00
|
|
|
const requestOptions = {...options, credentials: 'same-origin'};
|
|
|
|
requestOptions.headers = {
|
|
|
|
...requestOptions.headers || {},
|
2023-04-18 21:20:02 +00:00
|
|
|
baseURL: window.baseUrl(''),
|
2019-06-07 23:02:51 +00:00
|
|
|
'X-CSRF-TOKEN': csrfToken,
|
2023-04-18 21:20:02 +00:00
|
|
|
};
|
2019-06-07 23:02:51 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
const response = await fetch(requestUrl, requestOptions);
|
2019-06-07 23:02:51 +00:00
|
|
|
const content = await getResponseContent(response);
|
2020-07-04 15:53:02 +00:00
|
|
|
const returnData = {
|
2019-06-07 23:02:51 +00:00
|
|
|
data: content,
|
|
|
|
headers: response.headers,
|
|
|
|
redirected: response.redirected,
|
|
|
|
status: response.status,
|
|
|
|
statusText: response.statusText,
|
|
|
|
url: response.url,
|
|
|
|
original: response,
|
2020-07-04 15:53:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2023-01-26 12:16:23 +00:00
|
|
|
throw new HttpError(response, content);
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
2020-07-04 15:53:02 +00:00
|
|
|
|
|
|
|
return returnData;
|
2018-04-01 12:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 23:02:51 +00:00
|
|
|
/**
|
2023-04-19 09:46:13 +00:00
|
|
|
* Perform a HTTP request to the back-end that includes data in the body.
|
|
|
|
* Parses the body to JSON if an object, setting the correct headers.
|
|
|
|
* @param {String} method
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {Promise<FormattedResponse>}
|
2019-06-07 23:02:51 +00:00
|
|
|
*/
|
2023-04-19 09:46:13 +00:00
|
|
|
async function dataRequest(method, url, data = null) {
|
|
|
|
const options = {
|
|
|
|
method,
|
|
|
|
body: data,
|
|
|
|
};
|
2020-09-30 21:32:03 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
// Send data as JSON if a plain object
|
|
|
|
if (typeof data === 'object' && !(data instanceof FormData)) {
|
|
|
|
options.headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
};
|
|
|
|
options.body = JSON.stringify(data);
|
|
|
|
}
|
2019-06-07 23:02:51 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
// Ensure FormData instances are sent over POST
|
|
|
|
// Since Laravel does not read multipart/form-data from other types
|
|
|
|
// of request. Hence the addition of the magic _method value.
|
|
|
|
if (data instanceof FormData && method !== 'post') {
|
|
|
|
data.append('_method', method);
|
|
|
|
options.method = 'post';
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
return request(url, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a HTTP GET request.
|
|
|
|
* Can easily pass query parameters as the second parameter.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} params
|
|
|
|
* @returns {Promise<FormattedResponse>}
|
|
|
|
*/
|
|
|
|
export async function get(url, params = {}) {
|
|
|
|
return request(url, {
|
|
|
|
method: 'GET',
|
|
|
|
params,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a HTTP POST request.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {Promise<FormattedResponse>}
|
|
|
|
*/
|
|
|
|
export async function post(url, data = null) {
|
|
|
|
return dataRequest('POST', url, data);
|
2019-06-07 23:02:51 +00:00
|
|
|
}
|
2018-04-01 12:21:11 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
/**
|
|
|
|
* Perform a HTTP PUT request.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {Promise<FormattedResponse>}
|
|
|
|
*/
|
|
|
|
export async function put(url, data = null) {
|
|
|
|
return dataRequest('PUT', url, data);
|
|
|
|
}
|
2023-04-18 21:20:02 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
/**
|
|
|
|
* Perform a HTTP PATCH request.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {Promise<FormattedResponse>}
|
|
|
|
*/
|
|
|
|
export async function patch(url, data = null) {
|
|
|
|
return dataRequest('PATCH', url, data);
|
|
|
|
}
|
2023-04-18 21:20:02 +00:00
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
/**
|
|
|
|
* Perform a HTTP DELETE request.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {Promise<FormattedResponse>}
|
|
|
|
*/
|
|
|
|
async function performDelete(url, data = null) {
|
|
|
|
return dataRequest('DELETE', url, data);
|
2023-01-26 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 09:46:13 +00:00
|
|
|
export {performDelete as delete};
|