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

View File

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

View File

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