This commit is contained in:
tamaina 2023-01-09 17:33:29 +00:00
parent b3d59e2aae
commit eb5c22d0b2

View file

@ -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<string, any> = {}, token?: string | null | undefined) => {
// Implements Misskey.api.ApiClient.request
export function api<E extends keyof Endpoints, P extends Endpoints[E]['req']>(endpoint: E, data: P = {} as any, token?: string | null | undefined): Promise<Endpoints[E]['res']> {
pendingApiRequestsCount.value++;
const onFinally = () => {
pendingApiRequestsCount.value--;
};
const promise = new Promise<any>((resolve, reject) => {
const promise = new Promise<Endpoints[E]['res'] | void>((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<string, any> = {}, token?: s
promise.then(onFinally, onFinally);
return promise;
}) as typeof apiClient.request;
};
export const apiGet = ((endpoint: string, data: Record<string, any> = {}) => {
// Implements Misskey.api.ApiClient.request
export function apiGet<E extends keyof Endpoints, P extends Endpoints[E]['req']>(endpoint: E, data: P = {} as any): Promise<Endpoints[E]['res']> {
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<Endpoints[E]['res'] | void>((resolve, reject) => {
// Send request
window.fetch(`${apiUrl}/${endpoint}?${query}`, {
method: 'GET',
@ -78,4 +76,4 @@ export const apiGet = ((endpoint: string, data: Record<string, any> = {}) => {
promise.then(onFinally, onFinally);
return promise;
}) as typeof apiClient.request;
};