@@ -20,6 +20,8 @@ import {
2020 WebSocketMockRule
2121} from './definitions/websocket-rule-definitions' ;
2222
23+ /// --- Matchers ---
24+
2325// Define maps to/from matcher keys to matcher classes, and
2426// types for the matchers & classes themselves; both the built-in
2527// ones and our own extra additions & overrides.
@@ -30,14 +32,14 @@ export const MatcherLookup = {
3032
3133export type MatcherClassKey = keyof typeof MatcherLookup ;
3234export type MatcherClass = typeof MatcherLookup [ MatcherClassKey ] ;
33- export type Matcher = InstanceType < MatcherClass > ;
35+ export type Matcher = typeof MatcherLookup extends {
36+ // Enforce that keys match .type or uiType for each matcher class:
37+ [ K in keyof typeof MatcherLookup ] : new ( ...args : any [ ] ) => { type : K } | { uiType : K }
38+ }
39+ ? InstanceType < MatcherClass >
40+ : never ;
3441
35- export const MatcherKeys = new Map < MatcherClass , MatcherClassKey > (
36- Object . entries ( MatcherLookup )
37- . map (
38- ( [ key , matcher ] ) => [ matcher , key ]
39- ) as Array < [ MatcherClass , MatcherClassKey ] >
40- ) ;
42+ /// --- Handlers ---
4143
4244// Define maps to/from handler keys to handler classes, and
4345// types for the handlers & classes themselves; both the built-in
@@ -47,26 +49,21 @@ export const HandlerLookup = {
4749 ...WebSocketHandlerLookup
4850} ;
4951
50- const PaidHandlerClasses : HandlerClass [ ] = [
51- StaticResponseHandler ,
52- FromFileResponseHandler ,
53- ForwardToHostHandler ,
54- TransformingHandler ,
55- TimeoutHandler ,
56- CloseConnectionHandler
57- ] ;
58-
59- export const isPaidHandler = ( handler : Handler ) => {
60- return _ . some ( PaidHandlerClasses , ( cls ) => handler instanceof cls ) ;
61- }
62-
63- export const isPaidHandlerClass = ( handlerClass : HandlerClass ) => {
64- return PaidHandlerClasses . includes ( handlerClass ) ;
65- }
52+ export const MatcherKeys = new Map < MatcherClass , MatcherClassKey > (
53+ Object . entries ( MatcherLookup )
54+ . map (
55+ ( [ key , matcher ] ) => [ matcher , key ]
56+ ) as Array < [ MatcherClass , MatcherClassKey ] >
57+ ) ;
6658
6759export type HandlerClassKey = keyof typeof HandlerLookup ;
6860export type HandlerClass = typeof HandlerLookup [ HandlerClassKey ] ;
69- export type Handler = InstanceType < HandlerClass > ;
61+ export type Handler = typeof HandlerLookup extends {
62+ // Enforce that keys match .type or uiType for each handler class:
63+ [ K in keyof typeof HandlerLookup ] : new ( ...args : any [ ] ) => { type : K } | { uiType : K }
64+ }
65+ ? InstanceType < HandlerClass >
66+ : never ;
7067
7168export const HandlerKeys = new Map < HandlerClass , HandlerClassKey > (
7269 Object . entries ( HandlerLookup )
@@ -75,13 +72,34 @@ export const HandlerKeys = new Map<HandlerClass, HandlerClassKey>(
7572 ) as Array < [ HandlerClass , HandlerClassKey ] >
7673) ;
7774
75+ /// --- Matcher/handler special categories ---
76+
7877export const InitialMatcherClasses = [
7978 WildcardMatcher ,
8079 ...Object . values ( MethodMatchers )
8180] ;
8281export type InitialMatcherClass = typeof InitialMatcherClasses [ 0 ] ;
8382export type InitialMatcher = InstanceType < InitialMatcherClass > ;
8483
84+ const PaidHandlerClasses : HandlerClass [ ] = [
85+ StaticResponseHandler ,
86+ FromFileResponseHandler ,
87+ ForwardToHostHandler ,
88+ TransformingHandler ,
89+ TimeoutHandler ,
90+ CloseConnectionHandler
91+ ] ;
92+
93+ export const isPaidHandler = ( handler : Handler ) => {
94+ return _ . some ( PaidHandlerClasses , ( cls ) => handler instanceof cls ) ;
95+ }
96+
97+ export const isPaidHandlerClass = ( handlerClass : HandlerClass ) => {
98+ return PaidHandlerClasses . includes ( handlerClass ) ;
99+ }
100+
101+ /// --- Rules ---
102+
85103export type HtkMockRule =
86104 | WebSocketMockRule
87105 | HttpMockRule ;
0 commit comments