You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.8 KiB
70 lines
1.8 KiB
import { u as useAuthStore } from './auth-QFkCoU1C.mjs';
|
|
import { a as useRouter } from './server.mjs';
|
|
|
|
async function request(url, body, extraHeaders) {
|
|
const auth = useAuthStore();
|
|
const router = useRouter();
|
|
const headers = {
|
|
"Content-Type": "application/json"
|
|
};
|
|
if (auth.token) {
|
|
headers["Authorization"] = auth.token;
|
|
}
|
|
if (extraHeaders) {
|
|
Object.assign(headers, extraHeaders);
|
|
}
|
|
const response = await fetch(url, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify(body)
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error: ${response.status}`);
|
|
}
|
|
const result = await response.json();
|
|
if (result.code === 18) {
|
|
auth.logout();
|
|
router.push("/");
|
|
throw new Error(result.msg || "Unauthorized");
|
|
}
|
|
if (result.code !== 0) {
|
|
throw new Error(result.msg || "Unknown error");
|
|
}
|
|
return result.data;
|
|
}
|
|
function useApi() {
|
|
const auth = useAuthStore();
|
|
async function webApi(method, data) {
|
|
const extraHeaders = {};
|
|
if (auth.currentAppId) {
|
|
extraHeaders["X-App-Id"] = String(auth.currentAppId);
|
|
}
|
|
return request(`/web/api/api_${method}`, data ?? {}, extraHeaders);
|
|
}
|
|
async function consoleApi(path, data) {
|
|
return request(`/console/api/${path}`, data ?? {});
|
|
}
|
|
async function uploadFile(file, path) {
|
|
const cosToken = await webApi("getcostoken", { path });
|
|
const uploadUrl = cosToken.upload_url || cosToken.url || "";
|
|
const putResponse = await fetch(uploadUrl, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": file.type || "application/octet-stream"
|
|
},
|
|
body: file
|
|
});
|
|
if (!putResponse.ok) {
|
|
throw new Error(`Upload failed: ${putResponse.status}`);
|
|
}
|
|
return uploadUrl.split("?")[0];
|
|
}
|
|
return {
|
|
webApi,
|
|
consoleApi,
|
|
uploadFile
|
|
};
|
|
}
|
|
|
|
export { useApi as u };
|
|
//# sourceMappingURL=useApi-B7CB0Cnn.mjs.map
|
|
|