mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-11-26 19:14:37 +01:00
✌️
This commit is contained in:
parent
b3d59e2aae
commit
eb5c22d0b2
1 changed files with 11 additions and 13 deletions
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue