2019-06-07 19:02:51 -04:00
|
|
|
/**
|
2023-04-19 05:46:13 -04: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 19:02:51 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-04-19 05:46:13 -04: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 19:02:51 -04:00
|
|
|
*/
|
2023-04-19 05:46:13 -04:00
|
|
|
async function getResponseContent(response) {
|
|
|
|
if (response.status === 204) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-07 19:02:51 -04:00
|
|
|
|
2023-04-19 05:46:13 -04:00
|
|
|
const responseContentType = response.headers.get('Content-Type') || '';
|
|
|
|
const subType = responseContentType.split(';')[0].split('/').pop();
|
2019-06-07 19:02:51 -04:00
|
|
|
|
2023-04-19 05:46:13 -04:00
|
|
|
if (subType === 'javascript' || subType === 'json') {
|
|
|
|
return response.json();
|
|
|
|
}
|
2019-06-07 19:02:51 -04:00
|
|
|
|
2023-04-19 05:46:13 -04:00
|
|
|
return response.text();
|
2019-06-07 19:02:51 -04:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:46:13 -04:00
|
|
|
export class HttpError extends Error {
|
2019-06-07 19:02:51 -04:00
|
|
|
|
2023-04-19 05:46:13 -04: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 11:53:02 -04:00
|
|
|
}
|
|
|
|
|
2019-06-07 19:02:51 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 13:18:08 -04:00
|
|
|
/**
|
|
|
|
* @param {String} method
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} events
|
|
|
|
* @return {XMLHttpRequest}
|
|
|
|
*/
|
|
|
|
export function createXMLHttpRequest(method, url, events = {}) {
|
|
|
|
const csrfToken = document.querySelector('meta[name=token]').getAttribute('content');
|
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
|
|
|
|
for (const [eventName, callback] of Object.entries(events)) {
|
|
|
|
req.addEventListener(eventName, callback.bind(req));
|
|
|
|
}
|
|
|
|
|
|
|
|
req.open(method, url);
|
|
|
|
req.withCredentials = true;
|
|
|
|
req.setRequestHeader('X-CSRF-TOKEN', csrfToken);
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2019-06-07 19:02:51 -04: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 05:46:13 -04:00
|
|
|
* @returns {Promise<FormattedResponse>}
|
2019-06-07 19:02:51 -04:00
|
|
|
*/
|
|
|
|
async function request(url, options = {}) {
|
2023-04-19 05:46:13 -04:00
|
|
|
let requestUrl = url;
|
|
|
|
|
|
|
|
if (!requestUrl.startsWith('http')) {
|
|
|
|
requestUrl = window.baseUrl(requestUrl);
|
2019-06-07 19:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.params) {
|
2023-04-19 05:46:13 -04:00
|
|
|
const urlObj = new URL(requestUrl);
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const paramName of Object.keys(options.params)) {
|
2019-06-07 19:02:51 -04:00
|
|
|
const value = options.params[paramName];
|
|
|
|
if (typeof value !== 'undefined' && value !== null) {
|
|
|
|
urlObj.searchParams.set(paramName, value);
|
|
|
|
}
|
2018-04-01 08:21:11 -04:00
|
|
|
}
|
2023-04-19 05:46:13 -04:00
|
|
|
requestUrl = urlObj.toString();
|
2019-06-07 19:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const csrfToken = document.querySelector('meta[name=token]').getAttribute('content');
|
2023-04-19 05:46:13 -04:00
|
|
|
const requestOptions = {...options, credentials: 'same-origin'};
|
|
|
|
requestOptions.headers = {
|
|
|
|
...requestOptions.headers || {},
|
2023-04-18 17:20:02 -04:00
|
|
|
baseURL: window.baseUrl(''),
|
2019-06-07 19:02:51 -04:00
|
|
|
'X-CSRF-TOKEN': csrfToken,
|
2023-04-18 17:20:02 -04:00
|
|
|
};
|
2019-06-07 19:02:51 -04:00
|
|
|
|
2023-04-19 05:46:13 -04:00
|
|
|
const response = await fetch(requestUrl, requestOptions);
|
2019-06-07 19:02:51 -04:00
|
|
|
const content = await getResponseContent(response);
|
2020-07-04 11:53:02 -04:00
|
|
|
const returnData = {
|
2019-06-07 19:02:51 -04:00
|
|
|
data: content,
|
|
|
|
headers: response.headers,
|
|
|
|
redirected: response.redirected,
|
|
|
|
status: response.status,
|
|
|
|
statusText: response.statusText,
|
|
|
|
url: response.url,
|
|
|
|
original: response,
|
2020-07-04 11:53:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2023-01-26 07:16:23 -05:00
|
|
|
throw new HttpError(response, content);
|
2019-06-07 19:02:51 -04:00
|
|
|
}
|
2020-07-04 11:53:02 -04:00
|
|
|
|
|
|
|
return returnData;
|
2018-04-01 08:21:11 -04:00
|
|
|
}
|
|
|
|
|
2019-06-07 19:02:51 -04:00
|
|
|
/**
|
2023-04-19 05:46:13 -04: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 19:02:51 -04:00
|
|
|
*/
|
2023-04-19 05:46:13 -04:00
|
|
|
async function dataRequest(method, url, data = null) {
|
|
|
|
const options = {
|
|
|
|
method,
|
|
|
|
body: data,
|
|
|
|
};
|
2020-09-30 17:32:03 -04:00
|
|
|
|
2023-04-19 05:46:13 -04: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 19:02:51 -04:00
|
|
|
|
2023-04-19 05:46:13 -04: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 19:02:51 -04:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:46:13 -04: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 19:02:51 -04:00
|
|
|
}
|
2018-04-01 08:21:11 -04:00
|
|
|
|
2023-04-19 05:46:13 -04: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 17:20:02 -04:00
|
|
|
|
2023-04-19 05:46:13 -04: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 17:20:02 -04:00
|
|
|
|
2023-04-19 05:46:13 -04: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 07:16:23 -05:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:46:13 -04:00
|
|
|
export {performDelete as delete};
|
2024-05-18 16:18:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the response text for an error response to a user
|
|
|
|
* presentable string. Handles a range of errors responses including
|
|
|
|
* validation responses & server response text.
|
|
|
|
* @param {String} text
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
export function formatErrorResponseText(text) {
|
|
|
|
const data = text.startsWith('{') ? JSON.parse(text) : {message: text};
|
|
|
|
if (!data) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.message || data.error) {
|
|
|
|
return data.message || data.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const values = Object.values(data);
|
|
|
|
const isValidation = values.every(val => {
|
|
|
|
return Array.isArray(val) || val.every(x => typeof x === 'string');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isValidation) {
|
|
|
|
return values.flat().join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|