@data-client/vue 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +10 -4
  2. package/dist/index.js +114 -34
  3. package/dist/index.umd.min.js +1 -1
  4. package/lib/consumers/useFetch.js +12 -8
  5. package/lib/consumers/useSuspense.js +38 -13
  6. package/lib/context.d.ts.map +1 -1
  7. package/lib/context.js +3 -1
  8. package/lib/index.d.ts.map +1 -1
  9. package/lib/providers/DataClientPlugin.d.ts +22 -0
  10. package/lib/providers/DataClientPlugin.d.ts.map +1 -0
  11. package/lib/providers/DataClientPlugin.js +40 -0
  12. package/lib/providers/{provideDataClient.d.ts → createDataClient.d.ts} +10 -6
  13. package/lib/providers/createDataClient.d.ts.map +1 -0
  14. package/lib/providers/createDataClient.js +103 -0
  15. package/lib/providers/index.d.ts +2 -1
  16. package/lib/providers/index.d.ts.map +1 -1
  17. package/lib/providers/index.js +3 -2
  18. package/lib/test/MockController.d.ts +8 -0
  19. package/lib/test/MockController.d.ts.map +1 -0
  20. package/lib/test/MockController.js +125 -0
  21. package/lib/test/collapseFixture.d.ts +6 -0
  22. package/lib/test/collapseFixture.d.ts.map +1 -0
  23. package/lib/test/collapseFixture.js +19 -0
  24. package/lib/test/createFixtureMap.d.ts +3 -0
  25. package/lib/test/createFixtureMap.d.ts.map +1 -0
  26. package/lib/test/createFixtureMap.js +23 -0
  27. package/lib/test/fixtureTypes.d.ts +99 -0
  28. package/lib/test/fixtureTypes.d.ts.map +1 -0
  29. package/lib/test/fixtureTypes.js +2 -0
  30. package/lib/test/index.d.ts +8 -0
  31. package/lib/test/index.d.ts.map +1 -0
  32. package/lib/test/index.js +7 -0
  33. package/lib/test/mockState.d.ts +4 -0
  34. package/lib/test/mockState.d.ts.map +1 -0
  35. package/lib/test/mockState.js +44 -0
  36. package/lib/test/renderDataClient.d.ts +40 -0
  37. package/lib/test/renderDataClient.d.ts.map +1 -0
  38. package/lib/test/renderDataClient.js +264 -0
  39. package/package.json +13 -6
  40. package/src/__tests__/__snapshots__/useQuery.web.ts.snap +126 -0
  41. package/src/__tests__/__snapshots__/useSubscription.web.ts.snap +7 -0
  42. package/src/__tests__/integration-garbage-collection.web.ts +155 -0
  43. package/src/__tests__/useFetch.web.ts +192 -51
  44. package/src/__tests__/useLive.web.ts +66 -34
  45. package/src/__tests__/useQuery.web.ts +263 -109
  46. package/src/__tests__/useSubscription.web.ts +16 -3
  47. package/src/__tests__/useSuspense.web.ts +141 -70
  48. package/src/consumers/useFetch.ts +16 -9
  49. package/src/consumers/useSuspense.ts +37 -15
  50. package/src/context.ts +2 -0
  51. package/src/providers/DataClientPlugin.ts +39 -0
  52. package/src/providers/__tests__/DataClientPlugin.test.ts +387 -0
  53. package/src/providers/{provideDataClient.ts → createDataClient.ts} +33 -24
  54. package/src/providers/index.ts +2 -1
  55. package/src/test/MockController.ts +151 -0
  56. package/src/test/README.md +245 -0
  57. package/src/test/collapseFixture.ts +21 -0
  58. package/src/test/createFixtureMap.ts +26 -0
  59. package/src/test/fixtureTypes.ts +121 -0
  60. package/src/test/index.ts +23 -0
  61. package/src/test/mockState.ts +57 -0
  62. package/src/test/renderDataClient.ts +316 -0
  63. package/ts3.4/providers/DataClientPlugin.d.ts +22 -0
  64. package/ts3.4/providers/createDataClient.d.ts +25 -0
  65. package/ts3.4/providers/index.d.ts +2 -1
  66. package/ts3.4/test/MockController.d.ts +8 -0
  67. package/ts3.4/test/collapseFixture.d.ts +6 -0
  68. package/ts3.4/test/createFixtureMap.d.ts +6 -0
  69. package/ts3.4/test/fixtureTypes.d.ts +99 -0
  70. package/ts3.4/test/index.d.ts +8 -0
  71. package/ts3.4/test/mockState.d.ts +4 -0
  72. package/ts3.4/test/renderDataClient.d.ts +40 -0
  73. package/lib/providers/provideDataClient.d.ts.map +0 -1
  74. package/lib/providers/provideDataClient.js +0 -94
  75. package/ts3.4/providers/provideDataClient.d.ts +0 -21
@@ -0,0 +1,387 @@
1
+ import {
2
+ Controller,
3
+ NetworkManager,
4
+ PollingSubscription,
5
+ SubscriptionManager,
6
+ } from '@data-client/core';
7
+ import type { State } from '@data-client/core';
8
+ import { describe, it, expect, jest, beforeEach } from '@jest/globals';
9
+ import { mount } from '@vue/test-utils';
10
+ import { defineComponent, h, inject } from 'vue';
11
+
12
+ import { ControllerKey, StateKey } from '../../context';
13
+ import { DataClientPlugin } from '../DataClientPlugin';
14
+
15
+ describe('DataClientPlugin', () => {
16
+ beforeEach(() => {
17
+ jest.clearAllMocks();
18
+ });
19
+
20
+ it('should install plugin with default options', () => {
21
+ const TestComponent = defineComponent({
22
+ name: 'TestComponent',
23
+ setup() {
24
+ const controller = inject(ControllerKey);
25
+ const state = inject(StateKey);
26
+ return () =>
27
+ h('div', [
28
+ h('div', { id: 'has-controller' }, controller ? 'yes' : 'no'),
29
+ h('div', { id: 'has-state' }, state ? 'yes' : 'no'),
30
+ ]);
31
+ },
32
+ });
33
+
34
+ const wrapper = mount(TestComponent, {
35
+ global: {
36
+ plugins: [[DataClientPlugin]],
37
+ },
38
+ });
39
+
40
+ expect(wrapper.find('#has-controller').text()).toBe('yes');
41
+ expect(wrapper.find('#has-state').text()).toBe('yes');
42
+
43
+ wrapper.unmount();
44
+ });
45
+
46
+ it('should provide Controller instance', () => {
47
+ const TestComponent = defineComponent({
48
+ name: 'TestComponent',
49
+ setup() {
50
+ const controller = inject(ControllerKey);
51
+ return () =>
52
+ h(
53
+ 'div',
54
+ {
55
+ id: 'controller-type',
56
+ },
57
+ controller instanceof Controller ? 'Controller' : 'unknown',
58
+ );
59
+ },
60
+ });
61
+
62
+ const wrapper = mount(TestComponent, {
63
+ global: {
64
+ plugins: [[DataClientPlugin]],
65
+ },
66
+ });
67
+
68
+ expect(wrapper.find('#controller-type').text()).toBe('Controller');
69
+
70
+ wrapper.unmount();
71
+ });
72
+
73
+ it('should provide state ref', () => {
74
+ const TestComponent = defineComponent({
75
+ name: 'TestComponent',
76
+ setup() {
77
+ const stateRef = inject(StateKey);
78
+ return () =>
79
+ h(
80
+ 'div',
81
+ {
82
+ id: 'has-state-value',
83
+ },
84
+ stateRef?.value !== undefined ? 'yes' : 'no',
85
+ );
86
+ },
87
+ });
88
+
89
+ const wrapper = mount(TestComponent, {
90
+ global: {
91
+ plugins: [[DataClientPlugin]],
92
+ },
93
+ });
94
+
95
+ expect(wrapper.find('#has-state-value').text()).toBe('yes');
96
+
97
+ wrapper.unmount();
98
+ });
99
+
100
+ it('should accept custom managers option', () => {
101
+ const customNetworkManager = new NetworkManager();
102
+ const customSubscriptionManager = new SubscriptionManager(
103
+ PollingSubscription,
104
+ );
105
+ const customManagers = [customNetworkManager, customSubscriptionManager];
106
+
107
+ const TestComponent = defineComponent({
108
+ name: 'TestComponent',
109
+ setup() {
110
+ inject(ControllerKey);
111
+ return () => h('div', 'test');
112
+ },
113
+ });
114
+
115
+ const wrapper = mount(TestComponent, {
116
+ global: {
117
+ plugins: [[DataClientPlugin, { managers: customManagers }]],
118
+ },
119
+ });
120
+
121
+ expect(wrapper.vm).toBeDefined();
122
+ wrapper.unmount();
123
+ });
124
+
125
+ it('should accept custom initialState option', () => {
126
+ const customState: State<unknown> = {
127
+ entities: {},
128
+ indexes: {},
129
+ endpoints: {},
130
+ meta: {},
131
+ entitiesMeta: {},
132
+ optimistic: [],
133
+ lastReset: Date.now(),
134
+ };
135
+
136
+ const TestComponent = defineComponent({
137
+ name: 'TestComponent',
138
+ setup() {
139
+ const stateRef = inject(StateKey);
140
+ return () =>
141
+ h(
142
+ 'div',
143
+ {
144
+ id: 'last-reset',
145
+ },
146
+ String(stateRef?.value?.lastReset),
147
+ );
148
+ },
149
+ });
150
+
151
+ const wrapper = mount(TestComponent, {
152
+ global: {
153
+ plugins: [[DataClientPlugin, { initialState: customState }]],
154
+ },
155
+ });
156
+
157
+ expect(wrapper.find('#last-reset').text()).toBe(
158
+ String(customState.lastReset),
159
+ );
160
+
161
+ wrapper.unmount();
162
+ });
163
+
164
+ it('should call start on beforeMount lifecycle', () => {
165
+ const startSpy = jest.fn();
166
+ const originalCreateDataClient =
167
+ require('../createDataClient.js').createDataClient;
168
+
169
+ // Mock createDataClient to spy on start
170
+ const mockCreateDataClient = jest.fn(options => {
171
+ const provider = originalCreateDataClient(options);
172
+ const originalStart = provider.start;
173
+ provider.start = () => {
174
+ startSpy();
175
+ originalStart();
176
+ };
177
+ return provider;
178
+ });
179
+
180
+ jest
181
+ .spyOn(require('../createDataClient.js'), 'createDataClient')
182
+ .mockImplementation(mockCreateDataClient as any);
183
+
184
+ const TestComponent = defineComponent({
185
+ name: 'TestComponent',
186
+ setup() {
187
+ return () => h('div', 'test');
188
+ },
189
+ });
190
+
191
+ const wrapper = mount(TestComponent, {
192
+ global: {
193
+ plugins: [[DataClientPlugin]],
194
+ },
195
+ });
196
+
197
+ expect(startSpy).toHaveBeenCalled();
198
+
199
+ wrapper.unmount();
200
+ jest.restoreAllMocks();
201
+ });
202
+
203
+ it('should call stop on beforeUnmount lifecycle', () => {
204
+ const stopSpy = jest.fn();
205
+ const originalCreateDataClient =
206
+ require('../createDataClient.js').createDataClient;
207
+
208
+ // Mock createDataClient to spy on stop
209
+ const mockCreateDataClient = jest.fn(options => {
210
+ const provider = originalCreateDataClient(options);
211
+ const originalStop = provider.stop;
212
+ provider.stop = () => {
213
+ stopSpy();
214
+ originalStop();
215
+ };
216
+ return provider;
217
+ });
218
+
219
+ jest
220
+ .spyOn(require('../createDataClient.js'), 'createDataClient')
221
+ .mockImplementation(mockCreateDataClient as any);
222
+
223
+ const TestComponent = defineComponent({
224
+ name: 'TestComponent',
225
+ setup() {
226
+ return () => h('div', 'test');
227
+ },
228
+ });
229
+
230
+ const wrapper = mount(TestComponent, {
231
+ global: {
232
+ plugins: [[DataClientPlugin]],
233
+ },
234
+ });
235
+
236
+ expect(stopSpy).not.toHaveBeenCalled();
237
+ wrapper.unmount();
238
+ expect(stopSpy).toHaveBeenCalled();
239
+
240
+ jest.restoreAllMocks();
241
+ });
242
+
243
+ it('should work with multiple components', () => {
244
+ const ComponentA = defineComponent({
245
+ name: 'ComponentA',
246
+ setup() {
247
+ const controller = inject(ControllerKey);
248
+ return () =>
249
+ h('div', { id: 'component-a' }, controller ? 'A-yes' : 'A-no');
250
+ },
251
+ });
252
+
253
+ const ComponentB = defineComponent({
254
+ name: 'ComponentB',
255
+ setup() {
256
+ const controller = inject(ControllerKey);
257
+ return () =>
258
+ h('div', { id: 'component-b' }, controller ? 'B-yes' : 'B-no');
259
+ },
260
+ });
261
+
262
+ const ParentComponent = defineComponent({
263
+ name: 'ParentComponent',
264
+ setup() {
265
+ return () => h('div', [h(ComponentA), h(ComponentB)]);
266
+ },
267
+ });
268
+
269
+ const wrapper = mount(ParentComponent, {
270
+ global: {
271
+ plugins: [[DataClientPlugin]],
272
+ },
273
+ });
274
+
275
+ expect(wrapper.find('#component-a').text()).toBe('A-yes');
276
+ expect(wrapper.find('#component-b').text()).toBe('B-yes');
277
+
278
+ wrapper.unmount();
279
+ });
280
+
281
+ it('should provide reactive state', async () => {
282
+ const TestComponent = defineComponent({
283
+ name: 'TestComponent',
284
+ setup() {
285
+ const stateRef = inject(StateKey);
286
+
287
+ return () =>
288
+ h('div', [
289
+ h('div', { id: 'has-state' }, stateRef ? 'yes' : 'no'),
290
+ h(
291
+ 'div',
292
+ { id: 'has-entities' },
293
+ stateRef?.value?.entities ? 'yes' : 'no',
294
+ ),
295
+ ]);
296
+ },
297
+ });
298
+
299
+ const wrapper = mount(TestComponent, {
300
+ global: {
301
+ plugins: [[DataClientPlugin]],
302
+ },
303
+ });
304
+
305
+ expect(wrapper.find('#has-state').text()).toBe('yes');
306
+ expect(wrapper.find('#has-entities').text()).toBe('yes');
307
+
308
+ wrapper.unmount();
309
+ });
310
+
311
+ it('should return ProvidedDataClient from install', () => {
312
+ const app = {
313
+ mixin: jest.fn(),
314
+ provide: jest.fn(),
315
+ } as any;
316
+
317
+ const result = DataClientPlugin.install(app, {});
318
+
319
+ expect(result).toBeDefined();
320
+ expect(result.controller).toBeDefined();
321
+ expect(result.stateRef).toBeDefined();
322
+ expect(typeof result.start).toBe('function');
323
+ expect(typeof result.stop).toBe('function');
324
+ expect(app.provide).toHaveBeenCalledTimes(2);
325
+ });
326
+
327
+ it('should handle app without mixin method gracefully', () => {
328
+ const app = {
329
+ provide: jest.fn(),
330
+ } as any;
331
+
332
+ expect(() => {
333
+ DataClientPlugin.install(app, {});
334
+ }).not.toThrow();
335
+ expect(app.provide).toHaveBeenCalledTimes(2);
336
+ });
337
+
338
+ it('should use getDefaultManagers when no managers provided', () => {
339
+ const TestComponent = defineComponent({
340
+ name: 'TestComponent',
341
+ setup() {
342
+ inject(ControllerKey);
343
+ return () => h('div', 'test');
344
+ },
345
+ });
346
+
347
+ const wrapper = mount(TestComponent, {
348
+ global: {
349
+ plugins: [[DataClientPlugin]],
350
+ },
351
+ });
352
+
353
+ // Should not throw and should provide controller
354
+ expect(wrapper.vm).toBeDefined();
355
+
356
+ wrapper.unmount();
357
+ });
358
+
359
+ it('should accept custom Controller class', () => {
360
+ class CustomController extends Controller {}
361
+
362
+ const TestComponent = defineComponent({
363
+ name: 'TestComponent',
364
+ setup() {
365
+ const controller = inject(ControllerKey);
366
+ return () =>
367
+ h(
368
+ 'div',
369
+ {
370
+ id: 'controller-type',
371
+ },
372
+ controller instanceof CustomController ? 'custom' : 'default',
373
+ );
374
+ },
375
+ });
376
+
377
+ const wrapper = mount(TestComponent, {
378
+ global: {
379
+ plugins: [[DataClientPlugin, { Controller: CustomController }]],
380
+ },
381
+ });
382
+
383
+ expect(wrapper.find('#controller-type').text()).toBe('custom');
384
+
385
+ wrapper.unmount();
386
+ });
387
+ });
@@ -6,13 +6,7 @@ import {
6
6
  createReducer,
7
7
  } from '@data-client/core';
8
8
  import type { State, Manager, GCInterface } from '@data-client/core';
9
- import {
10
- onMounted,
11
- onUnmounted,
12
- provide,
13
- shallowRef,
14
- type ShallowRef,
15
- } from 'vue';
9
+ import { provide, shallowRef, type ShallowRef, type App } from 'vue';
16
10
 
17
11
  import { ControllerKey, StateKey } from '../context.js';
18
12
  import { getDefaultManagers } from './getDefaultManagers.js';
@@ -22,22 +16,27 @@ export interface ProvideOptions {
22
16
  initialState?: State<unknown>;
23
17
  Controller?: typeof DataController;
24
18
  gcPolicy?: GCInterface;
19
+ app?: App;
25
20
  }
26
21
 
27
22
  export interface ProvidedDataClient {
28
23
  controller: InstanceType<typeof DataController>;
29
24
  /** Optimistic overlay state ref provided to consumers */
30
25
  stateRef: ShallowRef<State<unknown>>;
26
+ /** Start the provider (called on mount) */
27
+ start: () => void;
28
+ /** Stop the provider (called on unmount) */
29
+ stop: () => void;
31
30
  }
32
31
 
33
32
  /**
34
- * Provide/inject setup for @data-client/vue. Call inside setup() of your root component.
35
- * Mirrors React DataProvider but as a composable.
33
+ * Core provider logic that can be used by both composable and plugin.
34
+ * This function handles the actual setup of the data client without Vue-specific concerns.
36
35
  */
37
- export function provideDataClient(
36
+ export function createDataClient(
38
37
  options: ProvideOptions = {},
39
38
  ): ProvidedDataClient {
40
- const { Controller = DataController, gcPolicy } = options;
39
+ const { Controller = DataController, gcPolicy, app } = options;
41
40
 
42
41
  // stable singletons for this provider scope
43
42
  const controller = new Controller({ gcPolicy });
@@ -100,27 +99,37 @@ export function provideDataClient(
100
99
  dispatch: (a: any) => outerDispatch(a),
101
100
  } as any);
102
101
 
103
- // provide to children
104
- provide(StateKey, stateRef);
105
- provide(ControllerKey, controller as any);
106
-
107
- // run managers after mount and cleanup on unmount
102
+ // setup lifecycle management
108
103
  let cleanup: void | (() => void);
109
- onMounted(() => {
104
+ const start = () => {
110
105
  cleanup = mgrEffect();
111
- });
112
- onUnmounted(() => {
106
+ };
107
+ const stop = () => {
113
108
  if (cleanup) cleanup();
114
- });
109
+ };
115
110
 
116
- return { controller: controller as any, stateRef };
111
+ // provide to children using Vue's provide or app.provide
112
+ if (app) {
113
+ // Plugin mode: use app.provide
114
+ app.provide(StateKey, stateRef);
115
+ app.provide(ControllerKey, controller as any);
116
+ } else {
117
+ // Composable mode: use provide (must be called within setup)
118
+ provide(StateKey, stateRef);
119
+ provide(ControllerKey, controller as any);
120
+ }
121
+
122
+ return {
123
+ controller: controller as any,
124
+ stateRef,
125
+ start,
126
+ stop,
127
+ };
117
128
 
118
129
  function computeOptimistic(state: State<unknown>): State<unknown> {
119
- // mirror React’s optimistic overlay
130
+ // mirror React's optimistic overlay
120
131
  // reduce over pending optimistic actions to derive the effective state
121
132
  // reducer is stable from closure
122
133
  return state.optimistic.reduce(reducer as any, state);
123
134
  }
124
135
  }
125
-
126
- export default provideDataClient;
@@ -1,2 +1,3 @@
1
1
  export { getDefaultManagers } from './getDefaultManagers.js';
2
- export { provideDataClient } from '../providers/provideDataClient.js';
2
+ export { createDataClient } from './createDataClient.js';
3
+ export { DataClientPlugin } from './DataClientPlugin.js';
@@ -0,0 +1,151 @@
1
+ import {
2
+ actionTypes,
3
+ Controller,
4
+ DataClientDispatch,
5
+ GenericDispatch,
6
+ } from '@data-client/core';
7
+
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>(
18
+ Base: TBase,
19
+ {
20
+ fixtures = [],
21
+ getInitialInterceptorData = () => ({}) as any,
22
+ }: MockProps<T> = {},
23
+ ): TBase {
24
+ const [fixtureMap, interceptors] = createFixtureMap(fixtures);
25
+
26
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
27
+ // @ts-ignore
28
+ return class MockedController<
29
+ D extends GenericDispatch = DataClientDispatch,
30
+ > extends Base<D> {
31
+ // legacy compatibility (re-declaration)
32
+ // TODO: drop when drop support for destructuring (0.14 and below)
33
+ declare protected _dispatch: D;
34
+
35
+ fixtureMap: Map<string, Fixture> = fixtureMap;
36
+ interceptors: Interceptor<any>[] = interceptors;
37
+ interceptorData: T = getInitialInterceptorData();
38
+
39
+ constructor(...args: any[]) {
40
+ super(...args);
41
+
42
+ // legacy compatibility
43
+ // TODO: drop when drop support for destructuring (0.14 and below)
44
+ if (!this._dispatch) {
45
+ this._dispatch = (args[0] as any).dispatch;
46
+ }
47
+ }
48
+
49
+ // legacy compatibility - we need this to work with 0.14 and below as they do not have this setter
50
+ // TODO: drop when drop support for destructuring (0.14 and below)
51
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
52
+ // @ts-ignore
53
+ set dispatch(dispatch: D) {
54
+ this._dispatch = dispatch;
55
+ }
56
+
57
+ get dispatch(): D {
58
+ return ((action: Parameters<D>[0]): Promise<void> => {
59
+ // support legacy that has _TYPE suffix
60
+ if (action.type === (actionTypes.FETCH ?? actionTypes.FETCH_TYPE)) {
61
+ // eslint-disable-next-line prefer-const
62
+ let { key, args } = action;
63
+ let fixture: Fixture | Interceptor | undefined;
64
+ if (this.fixtureMap.has(key)) {
65
+ fixture = this.fixtureMap.get(key) as Fixture;
66
+ if (!args) args = (fixture as any).args;
67
+ // exact matches take priority; now test ComputedFixture
68
+ } else {
69
+ for (const cfix of this.interceptors) {
70
+ if ((cfix.endpoint as any).testKey(key)) {
71
+ fixture = cfix;
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ // we have a match
77
+ if (fixture !== undefined) {
78
+ const replacedAction: typeof action = {
79
+ ...action,
80
+ };
81
+ const delayMs =
82
+ typeof (fixture as any).delay === 'function' ?
83
+ (fixture as any).delay(...(args as any))
84
+ : ((fixture as any).delay ?? 0);
85
+
86
+ if ('fetchResponse' in fixture) {
87
+ const { fetchResponse } = fixture as any;
88
+ fixture = {
89
+ endpoint: fixture.endpoint,
90
+ response(...args: any[]) {
91
+ const endpoint = (action.endpoint as any).extend({
92
+ fetchResponse: (input: RequestInfo, init: RequestInit) => {
93
+ const ret = fetchResponse.call(this, input, init);
94
+ return Promise.resolve(
95
+ new Response(JSON.stringify(ret), {
96
+ status: 200,
97
+ headers: new Headers({
98
+ 'Content-Type': 'application/json',
99
+ }),
100
+ }),
101
+ );
102
+ },
103
+ });
104
+ return (endpoint as any)(...args);
105
+ },
106
+ } as any;
107
+ }
108
+ const fetch = async () => {
109
+ if (!fixture) {
110
+ throw new Error('No fixture found');
111
+ }
112
+ // delayCollapse determines when the fixture function is 'collapsed' (aka 'run')
113
+ // collapsed: https://en.wikipedia.org/wiki/Copenhagen_interpretation
114
+ if ((fixture as any).delayCollapse) {
115
+ await new Promise(resolve => setTimeout(resolve, delayMs));
116
+ }
117
+ const result = await collapseFixture(
118
+ fixture as any,
119
+ args as any,
120
+ this.interceptorData,
121
+ );
122
+ if (!(fixture as any).delayCollapse && delayMs) {
123
+ await new Promise(resolve => setTimeout(resolve, delayMs));
124
+ }
125
+ if (result.error) {
126
+ throw result.response;
127
+ }
128
+ return result.response;
129
+ };
130
+ if (typeof (replacedAction.endpoint as any).extend === 'function') {
131
+ replacedAction.endpoint = (replacedAction.endpoint as any).extend(
132
+ {
133
+ fetch,
134
+ },
135
+ );
136
+ } else {
137
+ // TODO: full testing of this
138
+ replacedAction.endpoint = fetch as any;
139
+ (replacedAction.endpoint as any).__proto__ = action.endpoint;
140
+ }
141
+
142
+ // TODO: make super.dispatch (once we drop support for destructuring)
143
+ return this._dispatch(replacedAction);
144
+ }
145
+ }
146
+ // TODO: make super.dispatch (once we drop support for destructuring)
147
+ return this._dispatch(action);
148
+ }) as any;
149
+ }
150
+ };
151
+ }