mirror of
https://github.com/TeamPiped/Piped.git
synced 2026-08-09 23:31:28 +00:00
Migrate code to composition api.
This commit is contained in:
36
src/composables/useApi.js
Normal file
36
src/composables/useApi.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { getPreferenceBoolean, getPreferenceString } from "./usePreferences.js";
|
||||
|
||||
export function fetchJson(url, params, options) {
|
||||
if (params) {
|
||||
url = new URL(url);
|
||||
for (var param in params) url.searchParams.set(param, params[param]);
|
||||
}
|
||||
return fetch(url, options).then(response => {
|
||||
return response.json();
|
||||
});
|
||||
}
|
||||
|
||||
export function hashCode(s) {
|
||||
return s.split("").reduce(function (a, b) {
|
||||
a = (a << 5) - a + b.charCodeAt(0);
|
||||
return a & a;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
export function apiUrl() {
|
||||
return getPreferenceString("instance", import.meta.env.VITE_PIPED_API);
|
||||
}
|
||||
|
||||
export function authApiUrl() {
|
||||
if (getPreferenceBoolean("authInstance", false)) {
|
||||
return getPreferenceString("auth_instance_url", apiUrl());
|
||||
} else return apiUrl();
|
||||
}
|
||||
|
||||
export function getAuthToken() {
|
||||
return getPreferenceString("authToken" + hashCode(authApiUrl()));
|
||||
}
|
||||
|
||||
export function isAuthenticated() {
|
||||
return getAuthToken() !== undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user