This commit is contained in:
syuilo 2021-06-26 02:00:22 +09:00
parent 154e0fe94a
commit e40f0800a0

View File

@ -28,6 +28,12 @@ type IsNeverType<T> = [T] extends [never] ? true : false;
type StrictExtract<Union, Cond> = Cond extends Union ? Union : never; 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];
export class APIClient { export class APIClient {
public origin: string; public origin: string;
public credential: string | null | undefined; public credential: string | null | undefined;
@ -46,17 +52,18 @@ export class APIClient {
public request<E extends keyof Endpoints, P extends Endpoints[E]['req']>( public request<E extends keyof Endpoints, P extends Endpoints[E]['req']>(
endpoint: E, params: P = {} as P, credential?: string | null | undefined, endpoint: E, params: P = {} as P, credential?: string | null | undefined,
): Promise<Endpoints[E]['res'] extends { $switch: { $cases: [any, any][]; $default: any; }; } ): Promise<Endpoints[E]['res'] extends { $switch: { $cases: [any, any][]; $default: any; }; }
? IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][0], [P, any]>> extends false ?
? StrictExtract<Endpoints[E]['res']['$switch']['$cases'][0], [P, any]>[1] IsCaseMatched<E, P, 0> extends true ? GetCaseResult<E, P, 0> :
: IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][1], [P, any]>> extends false IsCaseMatched<E, P, 1> extends true ? GetCaseResult<E, P, 1> :
? StrictExtract<Endpoints[E]['res']['$switch']['$cases'][1], [P, any]>[1] IsCaseMatched<E, P, 2> extends true ? GetCaseResult<E, P, 2> :
: IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][2], [P, any]>> extends false IsCaseMatched<E, P, 3> extends true ? GetCaseResult<E, P, 3> :
? StrictExtract<Endpoints[E]['res']['$switch']['$cases'][2], [P, any]>[1] IsCaseMatched<E, P, 4> extends true ? GetCaseResult<E, P, 4> :
: IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][3], [P, any]>> extends false IsCaseMatched<E, P, 5> extends true ? GetCaseResult<E, P, 5> :
? StrictExtract<Endpoints[E]['res']['$switch']['$cases'][3], [P, any]>[1] IsCaseMatched<E, P, 6> extends true ? GetCaseResult<E, P, 6> :
: IsNeverType<StrictExtract<Endpoints[E]['res']['$switch']['$cases'][4], [P, any]>> extends false IsCaseMatched<E, P, 7> extends true ? GetCaseResult<E, P, 7> :
? StrictExtract<Endpoints[E]['res']['$switch']['$cases'][4], [P, any]>[1] IsCaseMatched<E, P, 8> extends true ? GetCaseResult<E, P, 8> :
: Endpoints[E]['res']['$switch']['$default'] IsCaseMatched<E, P, 9> extends true ? GetCaseResult<E, P, 9> :
Endpoints[E]['res']['$switch']['$default']
: Endpoints[E]['res']> : Endpoints[E]['res']>
{ {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {