@data-client/vue 0.3.2 → 0.15.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 (43) hide show
  1. package/CHANGELOG.md +151 -3
  2. package/README.md +39 -35
  3. package/dist/index.js +2 -2
  4. package/dist/index.umd.min.js +1 -1
  5. package/lib/context.d.ts.map +1 -1
  6. package/lib/context.js +3 -3
  7. package/lib/test/MockPlugin.d.ts +34 -0
  8. package/lib/test/MockPlugin.d.ts.map +1 -0
  9. package/lib/test/MockPlugin.js +71 -0
  10. package/lib/test/index.d.ts +3 -2
  11. package/lib/test/index.d.ts.map +1 -1
  12. package/lib/test/index.js +3 -2
  13. package/lib/test/mockState.d.ts +1 -3
  14. package/lib/test/mockState.d.ts.map +1 -1
  15. package/lib/test/mockState.js +2 -44
  16. package/lib/test/mountDataClient.d.ts +1 -1
  17. package/lib/test/mountDataClient.d.ts.map +1 -1
  18. package/lib/test/mountDataClient.js +2 -2
  19. package/package.json +4 -5
  20. package/src/__tests__/__snapshots__/context.test.ts.snap +13 -0
  21. package/src/__tests__/context.test.ts +2 -15
  22. package/src/context.ts +6 -4
  23. package/src/providers/__tests__/MockPlugin.test.ts +726 -0
  24. package/src/test/MockPlugin.ts +89 -0
  25. package/src/test/index.ts +3 -2
  26. package/src/test/mockState.ts +1 -57
  27. package/src/test/mountDataClient.ts +2 -2
  28. package/lib/test/MockController.d.ts +0 -8
  29. package/lib/test/MockController.d.ts.map +0 -1
  30. package/lib/test/MockController.js +0 -125
  31. package/lib/test/collapseFixture.d.ts +0 -6
  32. package/lib/test/collapseFixture.d.ts.map +0 -1
  33. package/lib/test/collapseFixture.js +0 -19
  34. package/lib/test/createFixtureMap.d.ts +0 -3
  35. package/lib/test/createFixtureMap.d.ts.map +0 -1
  36. package/lib/test/createFixtureMap.js +0 -23
  37. package/lib/test/fixtureTypes.d.ts +0 -99
  38. package/lib/test/fixtureTypes.d.ts.map +0 -1
  39. package/lib/test/fixtureTypes.js +0 -2
  40. package/src/test/MockController.ts +0 -151
  41. package/src/test/collapseFixture.ts +0 -21
  42. package/src/test/createFixtureMap.ts +0 -26
  43. package/src/test/fixtureTypes.ts +0 -121
@@ -1,151 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
1
- import type { Fixture, ResponseInterceptor } from './fixtureTypes';
2
-
3
- export async function collapseFixture(
4
- fixture: Fixture | ResponseInterceptor,
5
- args: any[],
6
- interceptorData: any,
7
- ) {
8
- let error = 'error' in fixture ? fixture.error : false;
9
- let response = fixture.response;
10
- if (typeof fixture.response === 'function') {
11
- try {
12
- response = await fixture.response.apply(interceptorData, args);
13
- // dispatch goes through user-code that can sometimes fail.
14
- // let's ensure we always handle errors
15
- } catch (e: any) {
16
- response = e;
17
- error = true;
18
- }
19
- }
20
- return { response, error };
21
- }
@@ -1,26 +0,0 @@
1
- import type { Fixture, Interceptor } from './fixtureTypes';
2
-
3
- export function createFixtureMap(fixtures: (Fixture | Interceptor)[] = []) {
4
- const map: Map<string, Fixture> = new Map();
5
- const computed: Interceptor[] = [];
6
- for (const fixture of fixtures) {
7
- if ('args' in fixture) {
8
- if (typeof fixture.response !== 'function') {
9
- const key = fixture.endpoint.key(...fixture.args);
10
- map.set(key, fixture);
11
- } else {
12
- // this has to be a typo. probably needs to remove args
13
- console.warn(
14
- `Fixture found with function response, and explicit args. Interceptors should not specify args.
15
- ${fixture.endpoint.name}: ${JSON.stringify(fixture.args)}
16
-
17
- Treating as Interceptor`,
18
- );
19
- computed.push(fixture as any);
20
- }
21
- } else {
22
- computed.push(fixture);
23
- }
24
- }
25
- return [map, computed] as const;
26
- }
@@ -1,121 +0,0 @@
1
- import type { EndpointInterface, ResolveType } from '@data-client/core';
2
-
3
- type Updater = (
4
- result: any,
5
- ...args: any
6
- ) => Record<string, (...args: any) => any>;
7
-
8
- export interface SuccessFixtureEndpoint<
9
- E extends EndpointInterface & { update?: Updater } = EndpointInterface,
10
- > {
11
- readonly endpoint: E;
12
- readonly args: Parameters<E>;
13
- readonly response:
14
- | ResolveType<E>
15
- | ((...args: Parameters<E>) => ResolveType<E>);
16
- readonly error?: false;
17
- /** Number of miliseconds to wait before resolving */
18
- readonly delay?: number;
19
- /** Waits to run `response()` after `delay` time */
20
- readonly delayCollapse?: boolean;
21
- }
22
-
23
- export interface ResponseInterceptor<
24
- T = any,
25
- E extends EndpointInterface & {
26
- update?: Updater;
27
- testKey(key: string): boolean;
28
- } = EndpointInterface & { testKey(key: string): boolean },
29
- > {
30
- readonly endpoint: E;
31
- response(this: T, ...args: Parameters<E>): ResolveType<E>;
32
- /** Number of miliseconds (or function that returns) to wait before resolving */
33
- readonly delay?: number | ((...args: Parameters<E>) => number);
34
- /** Waits to run `response()` after `delay` time */
35
- readonly delayCollapse?: boolean;
36
- }
37
- export interface FetchInterceptor<
38
- T = any,
39
- E extends EndpointInterface & {
40
- update?: Updater;
41
- testKey(key: string): boolean;
42
- fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
43
- extend(options: any): any;
44
- } = EndpointInterface & {
45
- testKey(key: string): boolean;
46
- fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
47
- extend(options: any): any;
48
- },
49
- > {
50
- readonly endpoint: E;
51
- fetchResponse(this: T, input: RequestInfo, init: RequestInit): ResolveType<E>;
52
- /** Number of miliseconds (or function that returns) to wait before resolving */
53
- readonly delay?: number | ((...args: Parameters<E>) => number);
54
- /** Waits to run `response()` after `delay` time */
55
- readonly delayCollapse?: boolean;
56
- }
57
- /** Interceptors match and compute dynamic responses based on args
58
- *
59
- * @see https://dataclient.io/docs/api/Fixtures#interceptor
60
- */
61
- export type Interceptor<
62
- T = any,
63
- E extends EndpointInterface & {
64
- update?: Updater;
65
- testKey(key: string): boolean;
66
- fetchResponse?(input: RequestInfo, init: RequestInit): Promise<Response>;
67
- extend?(options: any): any;
68
- } = EndpointInterface & {
69
- testKey(key: string): boolean;
70
- fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
71
- extend(options: any): any;
72
- },
73
- > =
74
- | ResponseInterceptor<T, E>
75
- | (E extends (
76
- {
77
- fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
78
- extend(options: any): any;
79
- }
80
- ) ?
81
- FetchInterceptor<T, E>
82
- : never);
83
-
84
- export interface ErrorFixtureEndpoint<
85
- E extends EndpointInterface & { update?: Updater } = EndpointInterface,
86
- > {
87
- readonly endpoint: E;
88
- readonly args: Parameters<E>;
89
- readonly response: any;
90
- readonly error: true;
91
- /** Number of miliseconds to wait before resolving */
92
- readonly delay?: number;
93
- /** Waits to run `response()` after `delay` time */
94
- readonly delayCollapse?: boolean;
95
- }
96
-
97
- export type FixtureEndpoint<
98
- E extends EndpointInterface & { update?: Updater } = EndpointInterface,
99
- > = SuccessFixtureEndpoint<E> | ErrorFixtureEndpoint<E>;
100
-
101
- /** Represents a successful response
102
- *
103
- * @see https://dataclient.io/docs/api/Fixtures#successfixture
104
- */
105
- export type SuccessFixture<
106
- E extends EndpointInterface & { update?: Updater } = EndpointInterface,
107
- > = SuccessFixtureEndpoint<E>;
108
- /** Represents a failed/errored response
109
- *
110
- * @see https://dataclient.io/docs/api/Fixtures#errorfixtures
111
- */
112
- export type ErrorFixture<
113
- E extends EndpointInterface & { update?: Updater } = EndpointInterface,
114
- > = ErrorFixtureEndpoint<E>;
115
- /** Represents a static response
116
- *
117
- * @see https://dataclient.io/docs/api/Fixtures
118
- */
119
- export type Fixture<
120
- E extends EndpointInterface & { update?: Updater } = EndpointInterface,
121
- > = FixtureEndpoint<E>;