Co-authored-by: =?UTF-8?q?=E6=9E=9C=E7=89=A9=E3=83=AA=E3=83=B3?= <nassii74@gmail.com>
This commit is contained in:
cutestnekoaqua 2023-04-08 12:32:37 +02:00
parent ab06c4d1eb
commit e6e4c32ded
No known key found for this signature in database
GPG Key ID: 6BF0964A5069C1E0
3 changed files with 25 additions and 77 deletions

View File

@ -4,7 +4,7 @@ import { Component, markRaw, Ref, ref, defineAsyncComponent } from "vue";
import { EventEmitter } from "eventemitter3";
import insertTextAtCursor from "insert-text-at-cursor";
import * as Misskey from "calckey-js";
import { apiUrl, url } from "@/config";
import { url } from "@/config";
import MkPostFormDialog from "@/components/MkPostFormDialog.vue";
import MkWaitingDialog from "@/components/MkWaitingDialog.vue";
import MkToast from "@/components/MkToast.vue";
@ -12,110 +12,60 @@ import MkDialog from "@/components/MkDialog.vue";
import { MenuItem } from "@/types/menu";
import { $i } from "@/account";
export const pendingApiRequestsCount = ref(0);
const apiClient = new Misskey.api.APIClient({
// #v-ifdef VITE_CAPACITOR
origin: $i?.instanceUrl || window.location.origin,
// #v-else
origin: url,
// #v-endif
credential: $i?.token,
});
const noCredentialApi = new Misskey.api.APIClient({
// #v-ifdef VITE_CAPACITOR
origin: $i?.instanceUrl || window.location.origin,
// #v-else
origin: url,
// #v-endif
});
export const api = ((
endpoint: string,
endpoint: keyof Misskey.Endpoints,
data: Record<string, any> = {},
token?: string | null | undefined,
) => {
pendingApiRequestsCount.value++;
const onFinally = () => {
pendingApiRequestsCount.value--;
};
return apiClient.request(endpoint, data);
const authorizationToken = token ?? $i?.token ?? undefined;
const authorization = authorizationToken
? `Bearer ${authorizationToken}`
: undefined;
const promise = new Promise((resolve, reject) => {
fetch(endpoint.indexOf("://") > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
method: "POST",
body: JSON.stringify(data),
credentials: "omit",
cache: "no-cache",
headers: authorization ? { authorization } : {},
})
.then(async (res) => {
const body = res.status === 204 ? null : await res.json();
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve();
} else {
reject(body.error);
}
})
.catch(reject);
});
promise.then(onFinally, onFinally);
return promise;
}) as typeof apiClient.request;
export const apiGet = ((
endpoint: string,
endpoint: keyof Misskey.Endpoints,
data: Record<string, any> = {},
token?: string | null | undefined,
) => {
pendingApiRequestsCount.value++;
const onFinally = () => {
pendingApiRequestsCount.value--;
};
const query = new URLSearchParams(data);
const authorizationToken = token ?? $i?.token ?? undefined;
const authorization = authorizationToken
? `Bearer ${authorizationToken}`
: undefined;
const promise = new Promise((resolve, reject) => {
// Send request
fetch(`${apiUrl}/${endpoint}?${query}`, {
method: "GET",
credentials: "omit",
cache: "default",
headers: authorization ? { authorization } : {},
})
apiClient
.request(endpoint, { ...data })
.then(async (res) => {
const body = res.status === 204 ? null : await res.json();
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve();
} else {
reject(body.error);
}
resolve(res);
})
.catch(reject);
});
promise.then(onFinally, onFinally);
return promise;
}) as typeof apiClient.request;
export const apiWithDialog = ((
endpoint: string,
endpoint: keyof Misskey.Endpoints,
data: Record<string, any> = {},
token?: string | null | undefined,
) => {
const promise = api(endpoint, data, token);
const promise = api(endpoint, data);
promiseDialog(promise, null, (err) => {
alert({
type: "error",

View File

@ -11,15 +11,13 @@
<XStreamIndicator />
<!-- <div v-if="pendingApiRequestsCount > 0" id="wait"></div> -->
<div v-if="dev" id="devTicker"><span>DEV BUILD</span></div>
</template>
<script lang="ts" setup>
import { defineAsyncComponent } from "vue";
import { swInject } from "./sw-inject";
import { popup, popups, pendingApiRequestsCount } from "@/os";
import { popup, popups } from "@/os";
import { uploads } from "@/scripts/upload";
import * as sound from "@/scripts/sound";
import { $i } from "@/account";

View File

@ -43,7 +43,7 @@ export default defineConfig(({ command, mode }) => {
tsconfigPaths()
];
if (!process.env.VITE_CAPACITOR) {
if (process.env.VITE_CAPACITOR !== "true") {
plugins.push(viteCompression({
algorithm: "brotliCompress",
}));