@@ -3,23 +3,18 @@ import {
33 Controller ,
44 DataClientDispatch ,
55 GenericDispatch ,
6- } from '@data-client/core' ;
6+ } from '../index.js' ;
7+ import { collapseFixture } from './collapseFixture.js' ;
8+ import { createFixtureMap } from './createFixtureMap.js' ;
9+ import type { Fixture , Interceptor } from './fixtureTypes.js' ;
10+ import { MockProps } from './mockTypes.js' ;
711
8- import { collapseFixture } from './collapseFixture' ;
9- import { createFixtureMap } from './createFixtureMap' ;
10- import type { Fixture , Interceptor } from './fixtureTypes' ;
11-
12- export interface MockProps < T = any > {
13- fixtures ?: ( Fixture | Interceptor < T > ) [ ] ;
14- getInitialInterceptorData ?: ( ) => T ;
15- }
16-
17- export default function MockController < TBase extends typeof Controller , T > (
12+ export function MockController < TBase extends typeof Controller , T > (
1813 Base : TBase ,
1914 {
2015 fixtures = [ ] ,
2116 getInitialInterceptorData = ( ) => ( { } ) as any ,
22- } : MockProps < T > = { } ,
17+ } : MockProps < T > ,
2318) : TBase {
2419 const [ fixtureMap , interceptors ] = createFixtureMap ( fixtures ) ;
2520
@@ -63,11 +58,11 @@ export default function MockController<TBase extends typeof Controller, T>(
6358 let fixture : Fixture | Interceptor | undefined ;
6459 if ( this . fixtureMap . has ( key ) ) {
6560 fixture = this . fixtureMap . get ( key ) as Fixture ;
66- if ( ! args ) args = ( fixture as any ) . args ;
61+ if ( ! args ) args = fixture . args ;
6762 // exact matches take priority; now test ComputedFixture
6863 } else {
6964 for ( const cfix of this . interceptors ) {
70- if ( ( cfix . endpoint as any ) . testKey ( key ) ) {
65+ if ( cfix . endpoint . testKey ( key ) ) {
7166 fixture = cfix ;
7267 break ;
7368 }
@@ -79,15 +74,15 @@ export default function MockController<TBase extends typeof Controller, T>(
7974 ...action ,
8075 } ;
8176 const delayMs =
82- typeof ( fixture as any ) . delay === 'function' ?
83- ( fixture as any ) . delay ( ...( args as any ) )
84- : ( ( fixture as any ) . delay ?? 0 ) ;
77+ typeof fixture . delay === 'function' ?
78+ fixture . delay ( ...( args as any ) )
79+ : ( fixture . delay ?? 0 ) ;
8580
8681 if ( 'fetchResponse' in fixture ) {
87- const { fetchResponse } = fixture as any ;
82+ const { fetchResponse } = fixture ;
8883 fixture = {
8984 endpoint : fixture . endpoint ,
90- response ( ...args : any [ ] ) {
85+ response ( ...args ) {
9186 const endpoint = ( action . endpoint as any ) . extend ( {
9287 fetchResponse : ( input : RequestInfo , init : RequestInit ) => {
9388 const ret = fetchResponse . call ( this , input , init ) ;
@@ -103,23 +98,23 @@ export default function MockController<TBase extends typeof Controller, T>(
10398 } ) ;
10499 return ( endpoint as any ) ( ...args ) ;
105100 } ,
106- } as any ;
101+ } ;
107102 }
108103 const fetch = async ( ) => {
109104 if ( ! fixture ) {
110105 throw new Error ( 'No fixture found' ) ;
111106 }
112107 // delayCollapse determines when the fixture function is 'collapsed' (aka 'run')
113108 // collapsed: https://en.wikipedia.org/wiki/Copenhagen_interpretation
114- if ( ( fixture as any ) . delayCollapse ) {
109+ if ( fixture . delayCollapse ) {
115110 await new Promise ( resolve => setTimeout ( resolve , delayMs ) ) ;
116111 }
117112 const result = await collapseFixture (
118113 fixture as any ,
119114 args as any ,
120115 this . interceptorData ,
121116 ) ;
122- if ( ! ( fixture as any ) . delayCollapse && delayMs ) {
117+ if ( ! fixture . delayCollapse && delayMs ) {
123118 await new Promise ( resolve => setTimeout ( resolve , delayMs ) ) ;
124119 }
125120 if ( result . error ) {
0 commit comments