@data-client/vue 0.3.2 → 0.15.1

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 (45) hide show
  1. package/CHANGELOG.md +160 -3
  2. package/README.md +41 -37
  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 +5 -8
  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/data_client_logo_and_text.svg +0 -64
  29. package/lib/test/MockController.d.ts +0 -8
  30. package/lib/test/MockController.d.ts.map +0 -1
  31. package/lib/test/MockController.js +0 -125
  32. package/lib/test/collapseFixture.d.ts +0 -6
  33. package/lib/test/collapseFixture.d.ts.map +0 -1
  34. package/lib/test/collapseFixture.js +0 -19
  35. package/lib/test/createFixtureMap.d.ts +0 -3
  36. package/lib/test/createFixtureMap.d.ts.map +0 -1
  37. package/lib/test/createFixtureMap.js +0 -23
  38. package/lib/test/fixtureTypes.d.ts +0 -99
  39. package/lib/test/fixtureTypes.d.ts.map +0 -1
  40. package/lib/test/fixtureTypes.js +0 -2
  41. package/src/test/MockController.ts +0 -151
  42. package/src/test/collapseFixture.ts +0 -21
  43. package/src/test/createFixtureMap.ts +0 -26
  44. package/src/test/fixtureTypes.ts +0 -121
  45. package/typescript.svg +0 -8
@@ -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>;
package/typescript.svg DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <svg width="21px" height="21px" viewBox="0 0 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
3
- <g>
4
- <polygon fill="#007ACC" transform="translate(128.000000, 128.000000) scale(1, -1) translate(-128.000000, -128.000000) " points="0 128 0 0 128 0 256 0 256 128 256 256 128 256 0 256"></polygon>
5
- <path d="M146.658132,223.436863 L146.739401,212.953054 L130.079084,212.953054 L113.418767,212.953054 L113.418767,165.613371 L113.418767,118.273689 L101.63464,118.273689 L89.8505126,118.273689 L89.8505126,165.613371 L89.8505126,212.953054 L73.1901951,212.953054 L56.5298776,212.953054 L56.5298776,223.233689 C56.5298776,228.922577 56.6517824,233.676863 56.8143221,233.798768 C56.9362269,233.961308 77.2130522,234.042577 101.797179,234.001943 L146.536227,233.880038 L146.658132,223.436863 Z" fill="#FFFFFF" transform="translate(101.634640, 176.142993) rotate(-180.000000) translate(-101.634640, -176.142993) "></path>
6
- <path d="M206.566631,234.272145 C213.068219,232.646748 218.025679,229.761668 222.57679,225.048018 C224.933616,222.528653 228.428219,217.936907 228.712663,216.839764 C228.793933,216.514684 217.659965,209.037859 210.914568,204.852462 C210.670758,204.689922 209.69552,205.74643 208.598377,207.371827 C205.306949,212.166748 201.852981,214.239129 196.570441,214.604843 C188.809171,215.133097 183.811076,211.069605 183.851711,204.283573 C183.851711,202.292462 184.136155,201.114049 184.948854,199.488653 C186.65552,195.953414 189.825044,193.840399 199.7806,189.533097 C218.106949,181.649922 225.949489,176.448653 230.825679,169.053097 C236.270758,160.804208 237.489806,147.638494 233.792028,137.845478 C229.728536,127.199129 219.651076,119.966113 205.469489,117.568653 C201.080917,116.796589 190.678377,116.918494 185.964727,117.771827 C175.684092,119.600399 165.931711,124.679764 159.917743,131.343891 C157.560917,133.944526 152.969171,140.730557 153.253616,141.218176 C153.37552,141.380716 154.432028,142.030875 155.610441,142.721668 C156.748219,143.371827 161.05552,145.850557 165.119012,148.207383 L172.473933,152.474049 L174.01806,150.198494 C176.171711,146.907065 180.885362,142.396589 183.729806,140.893097 C191.897425,136.585795 203.112663,137.195319 208.639012,142.15278 C210.995838,144.30643 211.971076,146.541351 211.971076,149.83278 C211.971076,152.799129 211.605362,154.099446 210.061235,156.334367 C208.070123,159.178811 204.006631,161.576272 192.466314,166.574367 C179.259965,172.263256 173.571076,175.798494 168.369806,181.406113 C165.362822,184.656907 162.518377,189.858176 161.339965,194.206113 C160.364727,197.822621 160.120917,206.884208 160.892981,210.541351 C163.61552,223.300716 173.245996,232.199764 187.143139,234.841034 C191.653616,235.694367 202.137425,235.369287 206.566631,234.272145 Z" fill="#FFFFFF" transform="translate(194.578507, 176.190240) scale(1, -1) translate(-194.578507, -176.190240) "></path>
7
- </g>
8
- </svg>