From 0853b2fe42048f73993f52941aaaa24fadf81272 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 21 May 2023 13:53:55 +0000 Subject: [PATCH] wip --- packages/frontend/src/scripts/api.ts | 26 +++++++++++++--------- packages/misskey-js/src/api.ts | 9 +++----- packages/misskey-js/src/endpoints.types.ts | 7 ++++-- pnpm-lock.yaml | 10 ++++----- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/packages/frontend/src/scripts/api.ts b/packages/frontend/src/scripts/api.ts index 97081d170f..ab8d70e4b8 100644 --- a/packages/frontend/src/scripts/api.ts +++ b/packages/frontend/src/scripts/api.ts @@ -1,21 +1,25 @@ -import { Endpoints } from 'misskey-js/built/api.types'; +import type { Endpoints, SchemaOrUndefined, ResponseOf, IEndpointMeta } from 'misskey-js/built/endpoints.types'; import { ref } from 'vue'; import { apiUrl } from '@/config'; import { $i } from '@/account'; export const pendingApiRequestsCount = ref(0); // Implements Misskey.api.ApiClient.request -export function api(endpoint: E, data: P = {} as any, token?: string | null | undefined, signal?: AbortSignal): Promise { +export function api, D extends IEndpointMeta = Endpoints[E]>( + endpoint: E, params?: P, token?: string | null | undefined, signal?: AbortSignal +): Promise> { + const data: (P | Record) & { i?: string | null } = params ?? {}; + pendingApiRequestsCount.value++; const onFinally = () => { pendingApiRequestsCount.value--; }; - const promise = new Promise((resolve, reject) => { + const promise = new Promise | void>((resolve, reject) => { // Append a credential - if ($i) (data as any).i = $i.token; - if (token !== undefined) (data as any).i = token; + if ($i) data.i = $i.token; + if (token !== undefined) data.i = token; // Send request window.fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, { @@ -42,20 +46,22 @@ export function api(en promise.then(onFinally, onFinally); - return promise; + return promise as Promise>; } // Implements Misskey.api.ApiClient.request -export function apiGet (endpoint: E, data: P = {} as any): Promise { +export function apiGet, D extends IEndpointMeta = Endpoints[E]>( + endpoint: E, params?: P, token?: string | null | undefined, signal?: AbortSignal +): Promise> { pendingApiRequestsCount.value++; const onFinally = () => { pendingApiRequestsCount.value--; }; - const query = new URLSearchParams(data as any); + const query = new URLSearchParams((params ?? {}) as Record); - const promise = new Promise((resolve, reject) => { + const promise = new Promise | void>((resolve, reject) => { // Send request window.fetch(`${apiUrl}/${endpoint}?${query}`, { method: 'GET', @@ -76,5 +82,5 @@ export function apiGet >; } diff --git a/packages/misskey-js/src/api.ts b/packages/misskey-js/src/api.ts index 19aa9f630e..ebade16931 100644 --- a/packages/misskey-js/src/api.ts +++ b/packages/misskey-js/src/api.ts @@ -1,4 +1,4 @@ -import type { Endpoints, SchemaOrUndefined, IEndpointMeta, EndpointDefines } from './endpoints.types'; +import type { Endpoints, SchemaOrUndefined, IEndpointMeta, ResponseOf } from './endpoints.types'; const MK_API_ERROR = Symbol(); @@ -25,9 +25,6 @@ export type FetchLike = (input: string, init?: { json(): Promise; }>; -type Response, DD extends EndpointDefines[number] = D['defines'][number]> = - P extends DD['req'] ? SchemaOrUndefined : never; - export class APIClient { public origin: string; public credential: string | null | undefined; @@ -45,8 +42,8 @@ export class APIClient { this.fetch = opts.fetch ?? ((...args) => fetch(...args)); } - public request, D extends IEndpointMeta = Endpoints[E], R = Response>( - endpoint: E, params: P = {} as P, credential?: string | null | undefined, + public request, D extends IEndpointMeta = Endpoints[E], R = ResponseOf>( + endpoint: E, params: P, credential?: string | null | undefined, ): Promise { const promise = new Promise((resolve, reject) => { diff --git a/packages/misskey-js/src/endpoints.types.ts b/packages/misskey-js/src/endpoints.types.ts index 5f1d8405db..9d2cafc7a4 100644 --- a/packages/misskey-js/src/endpoints.types.ts +++ b/packages/misskey-js/src/endpoints.types.ts @@ -37,7 +37,7 @@ export interface IEndpointMeta { }; }; - readonly defines: EndpointDefines; + readonly defines: ReadonlyArray<{ req: JSONSchema7 | undefined; res: JSONSchema7 | undefined; }>; /** * このエンドポイントにリクエストするのにユーザー情報が必須か否か @@ -123,6 +123,9 @@ export interface IEndpointMeta { readonly cacheSec?: number; } -export type SchemaOrUndefined = T extends JSONSchema7 ? SchemaType : never; +export type SchemaOrUndefined = T extends JSONSchema7 ? SchemaType : (void | Record); + +export type ResponseOf = + P extends SchemaOrUndefined ? SchemaOrUndefined : never; export type Endpoints = typeof endpoints; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e8d8b0b68e..b98240e2a2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -626,7 +626,7 @@ importers: version: 29.5.0 schema-type: specifier: github:misskey-dev/schema-type - version: github.com/misskey-dev/schema-type/428153dc8034004951ad8a309c65f4e3fe579441(typescript@5.0.4) + version: github.com/misskey-dev/schema-type/e24efd7bba40f638b3b687298873fa56de56aab3(typescript@5.0.4) packages/frontend: dependencies: @@ -1026,7 +1026,7 @@ importers: version: 4.4.0 schema-type: specifier: github:misskey-dev/schema-type - version: github.com/misskey-dev/schema-type/428153dc8034004951ad8a309c65f4e3fe579441(typescript@5.0.4) + version: github.com/misskey-dev/schema-type/e24efd7bba40f638b3b687298873fa56de56aab3(typescript@5.0.4) ts-essentials: specifier: ^9.3.2 version: 9.3.2(typescript@5.0.4) @@ -20455,9 +20455,9 @@ packages: version: 0.0.0 dev: false - github.com/misskey-dev/schema-type/428153dc8034004951ad8a309c65f4e3fe579441(typescript@5.0.4): - resolution: {tarball: https://codeload.github.com/misskey-dev/schema-type/tar.gz/428153dc8034004951ad8a309c65f4e3fe579441} - id: github.com/misskey-dev/schema-type/428153dc8034004951ad8a309c65f4e3fe579441 + github.com/misskey-dev/schema-type/e24efd7bba40f638b3b687298873fa56de56aab3(typescript@5.0.4): + resolution: {tarball: https://codeload.github.com/misskey-dev/schema-type/tar.gz/e24efd7bba40f638b3b687298873fa56de56aab3} + id: github.com/misskey-dev/schema-type/e24efd7bba40f638b3b687298873fa56de56aab3 name: schema-type version: 1.0.0 dependencies: