mirror of
https://github.com/jellyfin/jellyfin-web.git
synced 2024-11-16 02:18:16 -07:00
Move fetchLocal function to utils
This commit is contained in:
parent
0227d981e9
commit
0df6c66646
@ -1,44 +1,7 @@
|
||||
import DefaultConfig from '../../config.json';
|
||||
import fetchLocal from '../../utils/fetchLocal.ts';
|
||||
|
||||
let data;
|
||||
const urlResolver = document.createElement('a');
|
||||
|
||||
// `fetch` with `file:` support
|
||||
// Recent browsers seem to support `file` protocol under some conditions.
|
||||
// Based on https://github.com/github/fetch/pull/92#issuecomment-174730593
|
||||
// https://github.com/github/fetch/pull/92#issuecomment-512187452
|
||||
async function fetchLocal(url, options) {
|
||||
urlResolver.href = url;
|
||||
|
||||
const requestURL = urlResolver.href;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest;
|
||||
|
||||
xhr.onload = () => {
|
||||
// `file` protocol has invalid OK status
|
||||
let status = xhr.status;
|
||||
if (requestURL.startsWith('file:') && status === 0) {
|
||||
status = 200;
|
||||
}
|
||||
|
||||
/* eslint-disable-next-line compat/compat */
|
||||
resolve(new Response(xhr.responseText, { status: status }));
|
||||
};
|
||||
|
||||
xhr.onerror = () => {
|
||||
reject(new TypeError('Local request failed'));
|
||||
};
|
||||
|
||||
xhr.open('GET', url);
|
||||
|
||||
if (options && options.cache) {
|
||||
xhr.setRequestHeader('Cache-Control', options.cache);
|
||||
}
|
||||
|
||||
xhr.send(null);
|
||||
});
|
||||
}
|
||||
|
||||
async function getConfig() {
|
||||
if (data) return Promise.resolve(data);
|
||||
|
42
src/utils/fetchLocal.ts
Normal file
42
src/utils/fetchLocal.ts
Normal file
@ -0,0 +1,42 @@
|
||||
interface FetchOptions {
|
||||
cache?: string
|
||||
}
|
||||
|
||||
const URL_RESOLVER = document.createElement('a');
|
||||
|
||||
// `fetch` with `file:` support
|
||||
// Recent browsers seem to support `file` protocol under some conditions.
|
||||
// Based on https://github.com/github/fetch/pull/92#issuecomment-174730593
|
||||
// https://github.com/github/fetch/pull/92#issuecomment-512187452
|
||||
export default async function fetchLocal(url: string, options?: FetchOptions) {
|
||||
URL_RESOLVER.href = url;
|
||||
|
||||
const requestURL = URL_RESOLVER.href;
|
||||
|
||||
return new Promise<Response>((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest;
|
||||
|
||||
xhr.onload = () => {
|
||||
// `file` protocol has invalid OK status
|
||||
let status = xhr.status;
|
||||
if (requestURL.startsWith('file:') && status === 0) {
|
||||
status = 200;
|
||||
}
|
||||
|
||||
/* eslint-disable-next-line compat/compat */
|
||||
resolve(new Response(xhr.responseText, { status }));
|
||||
};
|
||||
|
||||
xhr.onerror = () => {
|
||||
reject(new TypeError('Local request failed'));
|
||||
};
|
||||
|
||||
xhr.open('GET', url);
|
||||
|
||||
if (options && options.cache) {
|
||||
xhr.setRequestHeader('Cache-Control', options.cache);
|
||||
}
|
||||
|
||||
xhr.send(null);
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user