This commit is contained in:
tamaina 2023-05-21 12:54:39 +00:00
parent 361ab296c7
commit 13fe20d47e
4 changed files with 135 additions and 148 deletions

View file

@ -1,4 +1,4 @@
import type { Endpoints } from './api.types.js';
import type { Endpoints, SchemaOrUndefined, IEndpointMeta, EndpointDefines } from './endpoints.types';
const MK_API_ERROR = Symbol();
@ -25,15 +25,8 @@ export type FetchLike = (input: string, init?: {
json(): Promise<any>;
}>;
type IsNeverType<T> = [T] extends [never] ? true : false;
type StrictExtract<Union, Cond> = Cond extends Union ? Union : never;
type IsCaseMatched<E extends keyof Endpoints, P extends Endpoints[E]['req'], C extends number> =
IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][C], [P, any]>> extends false ? true : false;
type GetCaseResult<E extends keyof Endpoints, P extends Endpoints[E]['req'], C extends number> =
StrictExtract<Endpoints[E]['res']['$switch']['$cases'][C], [P, any]>[1];
type Response<D extends IEndpointMeta, P extends SchemaOrUndefined<D['defines'][number]['req']>, DD extends EndpointDefines[number] = D['defines'][number]> =
P extends DD['req'] ? SchemaOrUndefined<DD['res']> : never;
export class APIClient {
public origin: string;
@ -52,22 +45,9 @@ export class APIClient {
this.fetch = opts.fetch ?? ((...args) => fetch(...args));
}
public request<E extends keyof Endpoints, P extends Endpoints[E]['req']>(
public request<E extends keyof Endpoints, P extends SchemaOrUndefined<D['defines'][number]['req']>, D extends IEndpointMeta = Endpoints[E], R = Response<D, P>>(
endpoint: E, params: P = {} as P, credential?: string | null | undefined,
): Promise<Endpoints[E]['res'] extends { $switch: { $cases: [any, any][]; $default: any; }; }
?
IsCaseMatched<E, P, 0> extends true ? GetCaseResult<E, P, 0> :
IsCaseMatched<E, P, 1> extends true ? GetCaseResult<E, P, 1> :
IsCaseMatched<E, P, 2> extends true ? GetCaseResult<E, P, 2> :
IsCaseMatched<E, P, 3> extends true ? GetCaseResult<E, P, 3> :
IsCaseMatched<E, P, 4> extends true ? GetCaseResult<E, P, 4> :
IsCaseMatched<E, P, 5> extends true ? GetCaseResult<E, P, 5> :
IsCaseMatched<E, P, 6> extends true ? GetCaseResult<E, P, 6> :
IsCaseMatched<E, P, 7> extends true ? GetCaseResult<E, P, 7> :
IsCaseMatched<E, P, 8> extends true ? GetCaseResult<E, P, 8> :
IsCaseMatched<E, P, 9> extends true ? GetCaseResult<E, P, 9> :
Endpoints[E]['res']['$switch']['$default']
: Endpoints[E]['res']>
): Promise<R>
{
const promise = new Promise((resolve, reject) => {
this.fetch(`${this.origin}/api/${endpoint}`, {

View file

@ -1,126 +1,5 @@
import { IEndpointMeta } from "./endpoints.types";
import { localUsernameSchema, passwordSchema } from "./schemas/user";
import type { JSONSchema7 } from 'schema-type';
export type RolePolicies = {
gtlAvailable: boolean;
ltlAvailable: boolean;
canPublicNote: boolean;
canInvite: boolean;
canManageCustomEmojis: boolean;
canSearchNotes: boolean;
canHideAds: boolean;
driveCapacityMb: number;
pinLimit: number;
antennaLimit: number;
wordMuteLimit: number;
webhookLimit: number;
clipLimit: number;
noteEachClipsLimit: number;
userListLimit: number;
userEachUserListsLimit: number;
rateLimitFactor: number;
};
export type EndpointDefines = ReadonlyArray<{ req: JSONSchema7 | undefined; res: JSONSchema7 | undefined; }>;
export interface IEndpointMeta {
readonly stability?: 'deprecated' | 'experimental' | 'stable';
readonly tags?: ReadonlyArray<string>;
readonly errors?: {
readonly [key: string]: {
readonly message: string;
readonly code: string;
readonly id: string;
};
};
readonly defines: EndpointDefines;
/**
*
* false
*/
readonly requireCredential?: boolean;
/**
* isModeratorなロールを必要とするか
*/
readonly requireModerator?: boolean;
/**
* isAdministratorなロールを必要とするか
*/
readonly requireAdmin?: boolean;
readonly requireRolePolicy?: keyof RolePolicies;
/**
*
* false
*/
readonly prohibitMoved?: boolean;
/**
*
*
*/
readonly limit?: {
/**
*
*/
readonly key?: string;
/**
* (ms)
* max
*/
readonly duration?: number;
/**
* durationで指定した期間内にいくつまでリクエストできるのか
* duration
*/
readonly max?: number;
/**
* (ms)
*/
readonly minInterval?: number;
};
/**
*
* false
*/
readonly requireFile?: boolean;
/**
*
* false
*/
readonly secure?: boolean;
/**
*
*
*/
readonly kind?: string;
readonly description?: string;
/**
* GETでのリクエストを許容するか否か
*/
readonly allowGet?: boolean;
/**
* (Cache-Control: public)
*/
readonly cacheSec?: number;
}
export const endpoints = {
"admin/accounts/create": {

View file

@ -0,0 +1,128 @@
import type { JSONSchema7, SchemaType } from 'schema-type';
import type { References } from './schemas';
import type { endpoints } from './endpoints';
export type RolePolicies = {
gtlAvailable: boolean;
ltlAvailable: boolean;
canPublicNote: boolean;
canInvite: boolean;
canManageCustomEmojis: boolean;
canSearchNotes: boolean;
canHideAds: boolean;
driveCapacityMb: number;
pinLimit: number;
antennaLimit: number;
wordMuteLimit: number;
webhookLimit: number;
clipLimit: number;
noteEachClipsLimit: number;
userListLimit: number;
userEachUserListsLimit: number;
rateLimitFactor: number;
};
export type EndpointDefines = ReadonlyArray<{ req: JSONSchema7 | undefined; res: JSONSchema7 | undefined; }>;
export interface IEndpointMeta {
readonly stability?: 'deprecated' | 'experimental' | 'stable';
readonly tags?: ReadonlyArray<string>;
readonly errors?: {
readonly [key: string]: {
readonly message: string;
readonly code: string;
readonly id: string;
};
};
readonly defines: EndpointDefines;
/**
*
* false
*/
readonly requireCredential?: boolean;
/**
* isModeratorなロールを必要とするか
*/
readonly requireModerator?: boolean;
/**
* isAdministratorなロールを必要とするか
*/
readonly requireAdmin?: boolean;
readonly requireRolePolicy?: keyof RolePolicies;
/**
*
* false
*/
readonly prohibitMoved?: boolean;
/**
*
*
*/
readonly limit?: {
/**
*
*/
readonly key?: string;
/**
* (ms)
* max
*/
readonly duration?: number;
/**
* durationで指定した期間内にいくつまでリクエストできるのか
* duration
*/
readonly max?: number;
/**
* (ms)
*/
readonly minInterval?: number;
};
/**
*
* false
*/
readonly requireFile?: boolean;
/**
*
* false
*/
readonly secure?: boolean;
/**
*
*
*/
readonly kind?: string;
readonly description?: string;
/**
* GETでのリクエストを許容するか否か
*/
readonly allowGet?: boolean;
/**
* (Cache-Control: public)
*/
readonly cacheSec?: number;
}
export type SchemaOrUndefined<T extends JSONSchema7 | undefined> = T extends JSONSchema7 ? SchemaType<T, References> : never;
export type Endpoints = typeof endpoints;

View file

@ -76,7 +76,7 @@ export const refs = {
Announcement: packedAnnouncementSchema,
} as const satisfies { [x: string]: JSONSchema7Definition };
type References = GetRefs<typeof refs>;
export type References = GetRefs<typeof refs>;
export type Packed<x extends GetKeys<References, 'https://misskey-hub.net/api/schemas/'>> = GetDef<References, x, 'https://misskey-hub.net/api/schemas/'>;
export type Def<x extends GetKeys<References>> = GetDef<References, x>;