This commit is contained in:
tamaina 2023-05-21 14:06:11 +00:00
parent 0853b2fe42
commit 89480d9029
3 changed files with 12 additions and 12 deletions

View File

@ -5,9 +5,9 @@ import { $i } from '@/account';
export const pendingApiRequestsCount = ref(0);
// Implements Misskey.api.ApiClient.request
export function api<E extends keyof Endpoints, P extends SchemaOrUndefined<Endpoints[E]['defines'][number]['req']>, D extends IEndpointMeta = Endpoints[E]>(
export function api<E extends keyof Endpoints, P extends SchemaOrUndefined<Endpoints[E]['defines'][number]['req']>, M extends IEndpointMeta = Endpoints[E]>(
endpoint: E, params?: P, token?: string | null | undefined, signal?: AbortSignal
): Promise<ResponseOf<D, P>> {
): Promise<ResponseOf<M, P>> {
const data: (P | Record<string, any>) & { i?: string | null } = params ?? {};
pendingApiRequestsCount.value++;
@ -16,7 +16,7 @@ export function api<E extends keyof Endpoints, P extends SchemaOrUndefined<Endpo
pendingApiRequestsCount.value--;
};
const promise = new Promise<ResponseOf<D, P> | void>((resolve, reject) => {
const promise = new Promise<ResponseOf<M, P> | void>((resolve, reject) => {
// Append a credential
if ($i) data.i = $i.token;
if (token !== undefined) data.i = token;
@ -46,13 +46,13 @@ export function api<E extends keyof Endpoints, P extends SchemaOrUndefined<Endpo
promise.then(onFinally, onFinally);
return promise as Promise<ResponseOf<D, P>>;
return promise as Promise<ResponseOf<M, P>>;
}
// Implements Misskey.api.ApiClient.request
export function apiGet<E extends keyof Endpoints, P extends SchemaOrUndefined<Endpoints[E]['defines'][number]['req']>, D extends IEndpointMeta = Endpoints[E]>(
export function apiGet<E extends keyof Endpoints, P extends SchemaOrUndefined<Endpoints[E]['defines'][number]['req']>, M extends IEndpointMeta = Endpoints[E]>(
endpoint: E, params?: P, token?: string | null | undefined, signal?: AbortSignal
): Promise<ResponseOf<D, P>> {
): Promise<ResponseOf<M, P>> {
pendingApiRequestsCount.value++;
const onFinally = () => {
@ -61,7 +61,7 @@ export function apiGet<E extends keyof Endpoints, P extends SchemaOrUndefined<En
const query = new URLSearchParams((params ?? {}) as Record<string, string>);
const promise = new Promise<ResponseOf<D, P> | void>((resolve, reject) => {
const promise = new Promise<ResponseOf<M, P> | void>((resolve, reject) => {
// Send request
window.fetch(`${apiUrl}/${endpoint}?${query}`, {
method: 'GET',
@ -82,5 +82,5 @@ export function apiGet<E extends keyof Endpoints, P extends SchemaOrUndefined<En
promise.then(onFinally, onFinally);
return promise as Promise<ResponseOf<D, P>>;
return promise as Promise<ResponseOf<M, P>>;
}

View File

@ -42,9 +42,9 @@ export class APIClient {
this.fetch = opts.fetch ?? ((...args) => fetch(...args));
}
public request<E extends keyof Endpoints, P extends SchemaOrUndefined<D['defines'][number]['req']>, D extends IEndpointMeta = Endpoints[E], R = ResponseOf<D, P>>(
public request<E extends keyof Endpoints, P extends SchemaOrUndefined<D['defines'][number]['req']>, M extends IEndpointMeta = Endpoints[E]>(
endpoint: E, params: P, credential?: string | null | undefined,
): Promise<R>
): Promise<ResponseOf<M, P>>
{
const promise = new Promise((resolve, reject) => {
this.fetch(`${this.origin}/api/${endpoint}`, {
@ -74,6 +74,6 @@ export class APIClient {
}).catch(reject);
});
return promise as any;
return promise as Promise<ResponseOf<M, P>>;
}
}

View File

@ -1,4 +1,4 @@
import { Endpoints } from './api.types.js';
import { Endpoints } from './endpoints.types.js';
import Stream, { Connection } from './streaming.js';
import { Channels } from './streaming.types.js';
import { Acct } from './acct.js';