ProcDecorator에서 chain 실패시 never 반환해야함

This commit is contained in:
2023-08-18 13:56:02 +00:00
parent c012a81377
commit df8b5e854b
3 changed files with 13 additions and 13 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ import { StartSession } from "../ProcDecorator/StartSession.js";
import { delay } from "../../util/delay.js";
import { declProcDecorators } from "../ProcDecorator/base.js";
import { z } from "zod";
import { LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js";
import { type LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js";
type BaseAPI = typeof structure.Login.LoginByID;
type RType = ExtractResponse<BaseAPI>;
+1 -1
View File
@@ -4,7 +4,7 @@ import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStruc
import { StartSession } from "../ProcDecorator/StartSession.js";
import { delay } from "../../util/delay.js";
import { declProcDecorators } from "../ProcDecorator/base.js";
import { LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js";
import { type LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js";
import { z } from "zod";
type BaseAPI = typeof structure.Login.LoginByToken;
type RType = ExtractResponse<BaseAPI>;
+11 -11
View File
@@ -16,7 +16,7 @@ export type DecoratorResultFalse = {
export type DecoratorResult = DecoratorResultTrue | DecoratorResultFalse;
export type DecoratorStack = DecoratorResult[];
export interface ProcDecorator<Out extends object, In extends object = Empty> {
export interface ProcDecorator<Out extends object, In = Empty> {
(inCtx: In & Partial<Out>, req: Request, res: Response)
: MayBePromise<[DecoratorResultTrue, Out] | [DecoratorResultFalse, In & Partial<Out>]>;
}
@@ -25,8 +25,8 @@ export interface PostProcDecorator<T extends object> {
(ctx: T, preResult: DecoratorResult, req: Request, res: Response, isValidRoute: boolean): MayBePromise<[DecoratorResult, T]>;
}
export interface ProcDecoratorRunner<Out extends object, In extends object> {
(inCtx: In & Partial<Out>, req: Request, res: Response): MayBePromise<[DecoratorStack, Out]>;
export interface ProcDecoratorRunner<Out extends object, In> {
(inCtx: In, req: Request, res: Response): MayBePromise<[DecoratorStack, Out]>;
}
export interface PostProcDecoratorRunner<T extends object> {
@@ -40,8 +40,8 @@ type PlainDecorator = ProcDecorator<any, any>;
type PlainPostDecorator = PostProcDecorator<any>;
export type ProcDecoratorGenerator<B extends object, A extends object> = ProcDecorator<B & A, A>;
export type ProcDecoratorPrePostGenerator<B extends object, A extends object> = [ProcDecorator<B & A, A>, PostProcDecorator<B & A>];
export type ProcDecoratorGenerator<Out extends object, In = Empty> = ProcDecorator<Out & In, In>;
export type ProcDecoratorPrePostGenerator<Out extends object, In> = [ProcDecorator<Out & In, In>, PostProcDecorator<Out & In>];
export type ProcDecoratorChain = readonly ((() => PlainDecorator) | (() => [PlainDecorator, PlainPostDecorator]))[];
@@ -58,7 +58,7 @@ export const EmptyProcDecorator: readonly [ProcDecoratorRunner<Empty, Empty>, Po
];
export function declProcDecorators<T extends ProcDecoratorChain>(...decorators: T) {
type OutType = ParseOutType<PackChain<T>>;
type OutType = Resolve<PackChain<T>>;
const preDecorator: PlainDecorator[] = [];
const postDecorator: (PlainPostDecorator | undefined)[] = [];
@@ -164,7 +164,7 @@ export function declProcDecorators<T extends ProcDecoratorChain>(...decorators:
return packedDecorators;
}
type Compose2<B extends object, A extends object, D extends object, C> = B extends C ? ProcDecorator<D & B, A> : never;
type Compose2<D extends object, C, B, A = Empty> = B extends C ? ProcDecorator<D & B, A> : never;
type PD1<B extends object, A extends object> = () => ProcDecorator<B, A>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -174,10 +174,10 @@ export type PackChain<T> =
T extends readonly [] ? ProcDecorator<Empty, Empty> :
T extends readonly [PD1<infer B, infer A>] ? ProcDecorator<B, A> :
T extends readonly [PD2<infer B, infer A>] ? ProcDecorator<B, A> :
T extends readonly [PD1<infer B, infer A>, PD1<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<B, A, D, C>, ...R]> :
T extends readonly [PD2<infer B, infer A>, PD1<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<B, A, D, C>, ...R]> :
T extends readonly [PD1<infer B, infer A>, PD2<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<B, A, D, C>, ...R]> :
T extends readonly [PD2<infer B, infer A>, PD2<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<B, A, D, C>, ...R]> :
T extends readonly [PD1<infer B, infer A>, PD1<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<D, C, B, A>, ...R]> :
T extends readonly [PD2<infer B, infer A>, PD1<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<D, C, B, A>, ...R]> :
T extends readonly [PD1<infer B, infer A>, PD2<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<D, C, B, A>, ...R]> :
T extends readonly [PD2<infer B, infer A>, PD2<infer D, infer C>, ... infer R] ? PackChain<[() => Compose2<D, C, B, A>, ...R]> :
never;
type Resolve<T> = T extends ProcDecorator<infer B, infer A> ? Empty extends A ? B : never : never;