diff --git a/packages/frontend/src/scripts/api.ts b/packages/frontend/src/scripts/api.ts index a9ae821d27..82b1ba3cea 100644 --- a/packages/frontend/src/scripts/api.ts +++ b/packages/frontend/src/scripts/api.ts @@ -1,21 +1,18 @@ -import * as Misskey from 'misskey-js'; +import { Endpoints } from 'misskey-js/built/api.types'; import { ref } from 'vue'; -import { apiUrl, url } from '@/config'; +import { apiUrl } from '@/config'; import { $i } from '@/account'; export const pendingApiRequestsCount = ref(0); -const apiClient = new Misskey.api.APIClient({ - origin: url, -}); - -export const api = ((endpoint: string, data: Record = {}, token?: string | null | undefined) => { +// Implements Misskey.api.ApiClient.request +export function api(endpoint: E, data: P = {} as any, token?: string | null | undefined): Promise { pendingApiRequestsCount.value++; const onFinally = () => { pendingApiRequestsCount.value--; }; - const promise = new Promise((resolve, reject) => { + const promise = new Promise((resolve, reject) => { // Append a credential if ($i) (data as any).i = $i.token; if (token !== undefined) (data as any).i = token; @@ -45,18 +42,19 @@ export const api = ((endpoint: string, data: Record = {}, token?: s promise.then(onFinally, onFinally); return promise; -}) as typeof apiClient.request; +}; -export const apiGet = ((endpoint: string, data: Record = {}) => { +// Implements Misskey.api.ApiClient.request +export function apiGet(endpoint: E, data: P = {} as any): Promise { pendingApiRequestsCount.value++; const onFinally = () => { pendingApiRequestsCount.value--; }; - const query = new URLSearchParams(data); + const query = new URLSearchParams(data as any); - const promise = new Promise((resolve, reject) => { + const promise = new Promise((resolve, reject) => { // Send request window.fetch(`${apiUrl}/${endpoint}?${query}`, { method: 'GET', @@ -78,4 +76,4 @@ export const apiGet = ((endpoint: string, data: Record = {}) => { promise.then(onFinally, onFinally); return promise; -}) as typeof apiClient.request; +};