From e6e4c32ded135245f2b4e857cf2efa6bbfd5fe6b Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Sat, 8 Apr 2023 12:32:37 +0200 Subject: [PATCH] mrrr Co-authored-by: =?UTF-8?q?=E6=9E=9C=E7=89=A9=E3=83=AA=E3=83=B3?= --- packages/client/src/os.ts | 96 ++++++---------------- packages/client/src/ui/_common_/common.vue | 4 +- packages/client/vite.config.ts | 2 +- 3 files changed, 25 insertions(+), 77 deletions(-) diff --git a/packages/client/src/os.ts b/packages/client/src/os.ts index 895fe41a41..66cecfb66a 100644 --- a/packages/client/src/os.ts +++ b/packages/client/src/os.ts @@ -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 = {}, 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 = {}, 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 } : {}, - }) - .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); + // Send request + apiClient + .request(endpoint, { ...data }) + .then(async (res) => { + resolve(res); + }) + .catch(reject); + }); return promise; }) as typeof apiClient.request; export const apiWithDialog = (( - endpoint: string, + endpoint: keyof Misskey.Endpoints, data: Record = {}, token?: string | null | undefined, ) => { - const promise = api(endpoint, data, token); + const promise = api(endpoint, data); promiseDialog(promise, null, (err) => { alert({ type: "error", diff --git a/packages/client/src/ui/_common_/common.vue b/packages/client/src/ui/_common_/common.vue index c47b0f85e4..e5d4fc8666 100644 --- a/packages/client/src/ui/_common_/common.vue +++ b/packages/client/src/ui/_common_/common.vue @@ -11,15 +11,13 @@ - -
DEV BUILD