feat(misskey-js): エラーを型として出力するように

This commit is contained in:
kakkokari-gtyih 2024-06-22 22:29:13 +09:00
parent bf403aa656
commit 1dfe08a8c1
12 changed files with 6817 additions and 1210 deletions

View file

@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
export interface IErrPromise<TSuccess, TError = unknown> {
then<TResult1 = TSuccess, TResult2 = never>(onfulfilled?: ((value: TSuccess) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: TError) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
catch<TResult = never>(onrejected?: ((reason: TError) => TResult | PromiseLike<TResult>) | undefined | null): Promise<TSuccess | TResult>;
}
/** rejectに型付けができるPromise */
export class ErrPromise<TSuccess, TError> extends Promise<TSuccess> implements IErrPromise<TSuccess, TError> {
constructor(executor: (resolve: (value: TSuccess | PromiseLike<TSuccess>) => void, reject: (reason: TError) => void) => void) {
super(executor);
}
}

View file

@ -7,6 +7,7 @@ import * as Misskey from 'misskey-js';
import { ref } from 'vue';
import { apiUrl } from '@/config.js';
import { $i } from '@/account.js';
import { ErrPromise } from '@/scripts/err-promise.js';
export const pendingApiRequestsCount = ref(0);
// Implements Misskey.api.ApiClient.request
@ -14,13 +15,14 @@ export function misskeyApi<
ResT = void,
E extends keyof Misskey.Endpoints = keyof Misskey.Endpoints,
P extends Misskey.Endpoints[E]['req'] = Misskey.Endpoints[E]['req'],
RE extends Misskey.Endpoints[E]['errors'] = Misskey.Endpoints[E]['errors'],
_ResT = ResT extends void ? Misskey.api.SwitchCaseResponseType<E, P> : ResT,
>(
endpoint: E,
data: P = {} as any,
token?: string | null | undefined,
signal?: AbortSignal,
): Promise<_ResT> {
): ErrPromise<_ResT, RE> {
if (endpoint.includes('://')) throw new Error('invalid endpoint');
pendingApiRequestsCount.value++;
@ -28,7 +30,7 @@ export function misskeyApi<
pendingApiRequestsCount.value--;
};
const promise = new Promise<_ResT>((resolve, reject) => {
const promise = new ErrPromise<_ResT, RE>((resolve, reject) => {
// Append a credential
if ($i) (data as any).i = $i.token;
if (token !== undefined) (data as any).i = token;
@ -47,7 +49,7 @@ export function misskeyApi<
const body = res.status === 204 ? null : await res.json();
if (res.status === 200) {
resolve(body);
resolve(body as _ResT);
} else if (res.status === 204) {
resolve(undefined as _ResT); // void -> undefined
} else {
@ -66,11 +68,12 @@ export function misskeyApiGet<
ResT = void,
E extends keyof Misskey.Endpoints = keyof Misskey.Endpoints,
P extends Misskey.Endpoints[E]['req'] = Misskey.Endpoints[E]['req'],
RE extends Misskey.Endpoints[E]['errors'] = Misskey.Endpoints[E]['errors'],
_ResT = ResT extends void ? Misskey.api.SwitchCaseResponseType<E, P> : ResT,
>(
endpoint: E,
data: P = {} as any,
): Promise<_ResT> {
): ErrPromise<_ResT, RE> {
pendingApiRequestsCount.value++;
const onFinally = () => {
@ -79,7 +82,7 @@ export function misskeyApiGet<
const query = new URLSearchParams(data as any);
const promise = new Promise<_ResT>((resolve, reject) => {
const promise = new ErrPromise<_ResT, RE>((resolve, reject) => {
// Send request
window.fetch(`${apiUrl}/${endpoint}?${query}`, {
method: 'GET',