This commit is contained in:
tamaina
2023-05-21 13:53:55 +00:00
parent 13fe20d47e
commit 0853b2fe42
4 changed files with 29 additions and 23 deletions

View File

@@ -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<any>;
}>;
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;
public credential: string | null | undefined;
@@ -45,8 +42,8 @@ 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 = Response<D, P>>(
endpoint: E, params: P = {} as P, credential?: string | null | undefined,
public request<E extends keyof Endpoints, P extends SchemaOrUndefined<D['defines'][number]['req']>, D extends IEndpointMeta = Endpoints[E], R = ResponseOf<D, P>>(
endpoint: E, params: P, credential?: string | null | undefined,
): Promise<R>
{
const promise = new Promise((resolve, reject) => {

View File

@@ -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 | undefined> = T extends JSONSchema7 ? SchemaType<T, References> : never;
export type SchemaOrUndefined<T extends JSONSchema7 | undefined> = T extends JSONSchema7 ? SchemaType<T, References> : (void | Record<string, never>);
export type ResponseOf<D extends IEndpointMeta, P, DD extends D['defines'][number] = D['defines'][number]> =
P extends SchemaOrUndefined<DD['req']> ? SchemaOrUndefined<DD['res']> : never;
export type Endpoints = typeof endpoints;