Improve type

This commit is contained in:
syuilo 2021-05-31 23:00:48 +09:00
parent b2549d98f9
commit a1f2dd7846
2 changed files with 24 additions and 6 deletions

View File

@ -24,6 +24,10 @@ export type FetchLike = (input: string, init?: {
json(): Promise<any>; json(): Promise<any>;
}>; }>;
type IsNeverType<T> = [T] extends [never] ? true : false;
type StrictExtract<Union, Cond> = Cond extends Union ? Union : never;
export class APIClient { export class APIClient {
public origin: string; public origin: string;
public credential: string | null | undefined; public credential: string | null | undefined;
@ -39,14 +43,19 @@ export class APIClient {
this.fetch = opts.fetch || fetch; this.fetch = opts.fetch || fetch;
} }
public request<E extends keyof Endpoints>( public request<E extends keyof Endpoints, P extends Endpoints[E]['req']>(
endpoint: E, data: Endpoints[E]['req'] = {}, credential?: string | null | undefined, endpoint: E, params: P, credential?: string | null | undefined,
): Promise<Endpoints[E]['res']> { ): Promise<Endpoints[E]['res'] extends { $switch: { $cases: [any, any][]; $default: any; }; }
? IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][number], [P, any]>> extends true
? Endpoints[E]['res']['$switch']['$default']
: StrictExtract<Endpoints[E]['res']['$switch']['$cases'][number], [P, any]>[1]
: Endpoints[E]['res']>
{
const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => { const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => {
this.fetch(`${this.origin}/api/${endpoint}`, { this.fetch(`${this.origin}/api/${endpoint}`, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
...data, ...params,
i: credential !== undefined ? credential : this.credential i: credential !== undefined ? credential : this.credential
}), }),
credentials: 'omit', credentials: 'omit',

View File

@ -1,5 +1,6 @@
import { import {
Ad, Announcement, Antenna, App, AuthSession, Clip, DriveFile, DriveFolder, GalleryPost, InstanceMetadata, Ad, Announcement, Antenna, App, AuthSession, Clip, DetailedInstanceMetadata, DriveFile, DriveFolder, GalleryPost, InstanceMetadata,
LiteInstanceMetadata,
Note, OriginType, Page, ServerInfo, Stats, User, UserGroup, UserList, UserSorting Note, OriginType, Page, ServerInfo, Stats, User, UserGroup, UserList, UserSorting
} from './entities'; } from './entities';
@ -257,7 +258,15 @@ export type Endpoints = {
'messaging/messages/read': { req: TODO; res: TODO; }; 'messaging/messages/read': { req: TODO; res: TODO; };
// meta // meta
'meta': { req: { detail?: boolean; }; res: InstanceMetadata; }; // TODO: 「detail が true なら DetailedInstanceMetadata を返す」のような型付けをしたい 'meta': { req: { detail?: boolean; }; res: {
$switch: {
$cases: [[
{ detail: true; },
DetailedInstanceMetadata,
]];
$default: LiteInstanceMetadata;
};
}; };
// miauth // miauth
'miauth/gen-token': { req: TODO; res: TODO; }; 'miauth/gen-token': { req: TODO; res: TODO; };