1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/fetchhelper.js

109 lines
3 KiB
JavaScript
Raw Normal View History

2023-04-19 01:56:05 -04:00
export function getFetchPromise(request) {
const headers = request.headers || {};
2023-04-19 01:56:05 -04:00
if (request.dataType === 'json') {
headers.accept = 'application/json';
}
2023-04-19 01:56:05 -04:00
const fetchRequest = {
headers: headers,
method: request.type,
credentials: 'same-origin'
};
2023-04-19 01:56:05 -04:00
let contentType = request.contentType;
2023-04-19 01:56:05 -04:00
if (request.data) {
if (typeof request.data === 'string') {
fetchRequest.body = request.data;
} else {
fetchRequest.body = paramsToString(request.data);
2023-04-19 01:56:05 -04:00
contentType = contentType || 'application/x-www-form-urlencoded; charset=UTF-8';
}
2023-04-19 01:56:05 -04:00
}
2023-04-19 01:56:05 -04:00
if (contentType) {
headers['Content-Type'] = contentType;
}
2023-04-19 01:56:05 -04:00
let url = request.url;
2023-04-19 01:56:05 -04:00
if (request.query) {
const paramString = paramsToString(request.query);
if (paramString) {
url += `?${paramString}`;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2023-04-19 01:56:05 -04:00
if (!request.timeout) {
return fetch(url, fetchRequest);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
return fetchWithTimeout(url, fetchRequest, request.timeout);
}
function fetchWithTimeout(url, options, timeoutMs) {
console.debug(`fetchWithTimeout: timeoutMs: ${timeoutMs}, url: ${url}`);
2023-04-19 01:56:05 -04:00
return new Promise(function (resolve, reject) {
const timeout = setTimeout(reject, timeoutMs);
2023-04-19 01:56:05 -04:00
options = options || {};
options.credentials = 'same-origin';
2023-04-19 01:56:05 -04:00
fetch(url, options).then(function (response) {
clearTimeout(timeout);
2023-04-19 01:56:05 -04:00
console.debug(`fetchWithTimeout: succeeded connecting to url: ${url}`);
2023-04-19 01:56:05 -04:00
resolve(response);
}, function (error) {
clearTimeout(timeout);
2023-04-19 01:56:05 -04:00
console.debug(`fetchWithTimeout: timed out connecting to url: ${url}`);
2023-04-19 01:56:05 -04:00
reject(error);
});
2023-04-19 01:56:05 -04:00
});
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
/**
2020-05-28 23:06:36 +02:00
* @param params {Record<string, string | number | boolean>}
* @returns {string} Query string
*/
2023-04-19 01:56:05 -04:00
function paramsToString(params) {
return Object.entries(params)
.filter(([, v]) => v !== null && v !== undefined && v !== '')
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
}
export function ajax(request) {
if (!request) {
throw new Error('Request cannot be null');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
request.headers = request.headers || {};
2023-04-19 01:56:05 -04:00
console.debug(`requesting url: ${request.url}`);
2023-04-19 01:56:05 -04:00
return getFetchPromise(request).then(function (response) {
console.debug(`response status: ${response.status}, url: ${request.url}`);
if (response.status < 400) {
if (request.dataType === 'json' || request.headers.accept === 'application/json') {
return response.json();
} else if (request.dataType === 'text' || (response.headers.get('Content-Type') || '').toLowerCase().startsWith('text/')) {
return response.text();
} else {
2023-04-19 01:56:05 -04:00
return response;
}
2023-04-19 01:56:05 -04:00
} else {
return Promise.reject(response);
}
}, function (err) {
console.error(`request failed to url: ${request.url}`);
throw err;
});
}