@data-client/core 0.15.0-beta-20251006024044-92bd01c4976f2921993b8c9f1e4dbb87af87ba7b → 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.
- package/CHANGELOG.md +1191 -0
- package/dist/index.umd.min.js +1 -1
- package/legacy/manager/SubscriptionManager.js +1 -1
- package/legacy/middlewareTypes.js +1 -1
- package/legacy/mock/MockController.js +124 -0
- package/legacy/mock/collapseFixture.js +19 -0
- package/legacy/mock/createFixtureMap.js +23 -0
- package/legacy/mock/fixtureTypes.js +2 -0
- package/legacy/mock/index.js +5 -0
- package/legacy/mock/mockState.js +44 -0
- package/legacy/mock/mockTypes.js +2 -0
- package/lib/manager/SubscriptionManager.d.ts.map +1 -1
- package/lib/manager/SubscriptionManager.js +1 -1
- package/lib/middlewareTypes.d.ts.map +1 -1
- package/lib/middlewareTypes.js +1 -1
- package/lib/mock/MockController.d.ts +4 -0
- package/lib/mock/MockController.d.ts.map +1 -0
- package/lib/mock/MockController.js +125 -0
- package/lib/mock/collapseFixture.d.ts +6 -0
- package/lib/mock/collapseFixture.d.ts.map +1 -0
- package/lib/mock/collapseFixture.js +19 -0
- package/lib/mock/createFixtureMap.d.ts +3 -0
- package/lib/mock/createFixtureMap.d.ts.map +1 -0
- package/lib/mock/createFixtureMap.js +23 -0
- package/lib/mock/fixtureTypes.d.ts +99 -0
- package/lib/mock/fixtureTypes.d.ts.map +1 -0
- package/lib/mock/fixtureTypes.js +2 -0
- package/lib/mock/index.d.ts +7 -0
- package/lib/mock/index.d.ts.map +1 -0
- package/lib/mock/index.js +5 -0
- package/lib/mock/mockState.d.ts +4 -0
- package/lib/mock/mockState.d.ts.map +1 -0
- package/lib/mock/mockState.js +44 -0
- package/lib/mock/mockTypes.d.ts +6 -0
- package/lib/mock/mockTypes.d.ts.map +1 -0
- package/lib/mock/mockTypes.js +2 -0
- package/package.json +16 -6
- package/src/manager/SubscriptionManager.ts +1 -2
- package/src/middlewareTypes.ts +3 -2
- package/src/mock/MockController.ts +146 -0
- package/src/mock/collapseFixture.ts +21 -0
- package/src/mock/createFixtureMap.ts +26 -0
- package/src/mock/fixtureTypes.ts +121 -0
- package/src/mock/index.ts +16 -0
- package/src/mock/mockState.ts +56 -0
- package/src/mock/mockTypes.ts +6 -0
- package/ts3.4/mock/MockController.d.ts +4 -0
- package/ts3.4/mock/collapseFixture.d.ts +6 -0
- package/ts3.4/mock/createFixtureMap.d.ts +6 -0
- package/ts3.4/mock/fixtureTypes.d.ts +99 -0
- package/ts3.4/mock/index.d.ts +7 -0
- package/ts3.4/mock/mockState.d.ts +4 -0
- package/ts3.4/mock/mockTypes.d.ts +6 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
actionTypes,
|
|
3
|
+
Controller,
|
|
4
|
+
DataClientDispatch,
|
|
5
|
+
GenericDispatch,
|
|
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';
|
|
11
|
+
|
|
12
|
+
export function MockController<TBase extends typeof Controller, T>(
|
|
13
|
+
Base: TBase,
|
|
14
|
+
{
|
|
15
|
+
fixtures = [],
|
|
16
|
+
getInitialInterceptorData = () => ({}) as any,
|
|
17
|
+
}: MockProps<T>,
|
|
18
|
+
): TBase {
|
|
19
|
+
const [fixtureMap, interceptors] = createFixtureMap(fixtures);
|
|
20
|
+
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
return class MockedController<
|
|
24
|
+
D extends GenericDispatch = DataClientDispatch,
|
|
25
|
+
> extends Base<D> {
|
|
26
|
+
// legacy compatibility (re-declaration)
|
|
27
|
+
// TODO: drop when drop support for destructuring (0.14 and below)
|
|
28
|
+
declare protected _dispatch: D;
|
|
29
|
+
|
|
30
|
+
fixtureMap: Map<string, Fixture> = fixtureMap;
|
|
31
|
+
interceptors: Interceptor<any>[] = interceptors;
|
|
32
|
+
interceptorData: T = getInitialInterceptorData();
|
|
33
|
+
|
|
34
|
+
constructor(...args: any[]) {
|
|
35
|
+
super(...args);
|
|
36
|
+
|
|
37
|
+
// legacy compatibility
|
|
38
|
+
// TODO: drop when drop support for destructuring (0.14 and below)
|
|
39
|
+
if (!this._dispatch) {
|
|
40
|
+
this._dispatch = (args[0] as any).dispatch;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// legacy compatibility - we need this to work with 0.14 and below as they do not have this setter
|
|
45
|
+
// TODO: drop when drop support for destructuring (0.14 and below)
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
set dispatch(dispatch: D) {
|
|
49
|
+
this._dispatch = dispatch;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get dispatch(): D {
|
|
53
|
+
return ((action: Parameters<D>[0]): Promise<void> => {
|
|
54
|
+
// support legacy that has _TYPE suffix
|
|
55
|
+
if (action.type === (actionTypes.FETCH ?? actionTypes.FETCH_TYPE)) {
|
|
56
|
+
// eslint-disable-next-line prefer-const
|
|
57
|
+
let { key, args } = action;
|
|
58
|
+
let fixture: Fixture | Interceptor | undefined;
|
|
59
|
+
if (this.fixtureMap.has(key)) {
|
|
60
|
+
fixture = this.fixtureMap.get(key) as Fixture;
|
|
61
|
+
if (!args) args = fixture.args;
|
|
62
|
+
// exact matches take priority; now test ComputedFixture
|
|
63
|
+
} else {
|
|
64
|
+
for (const cfix of this.interceptors) {
|
|
65
|
+
if (cfix.endpoint.testKey(key)) {
|
|
66
|
+
fixture = cfix;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// we have a match
|
|
72
|
+
if (fixture !== undefined) {
|
|
73
|
+
const replacedAction: typeof action = {
|
|
74
|
+
...action,
|
|
75
|
+
};
|
|
76
|
+
const delayMs =
|
|
77
|
+
typeof fixture.delay === 'function' ?
|
|
78
|
+
fixture.delay(...(args as any))
|
|
79
|
+
: (fixture.delay ?? 0);
|
|
80
|
+
|
|
81
|
+
if ('fetchResponse' in fixture) {
|
|
82
|
+
const { fetchResponse } = fixture;
|
|
83
|
+
fixture = {
|
|
84
|
+
endpoint: fixture.endpoint,
|
|
85
|
+
response(...args) {
|
|
86
|
+
const endpoint = (action.endpoint as any).extend({
|
|
87
|
+
fetchResponse: (input: RequestInfo, init: RequestInit) => {
|
|
88
|
+
const ret = fetchResponse.call(this, input, init);
|
|
89
|
+
return Promise.resolve(
|
|
90
|
+
new Response(JSON.stringify(ret), {
|
|
91
|
+
status: 200,
|
|
92
|
+
headers: new Headers({
|
|
93
|
+
'Content-Type': 'application/json',
|
|
94
|
+
}),
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
return (endpoint as any)(...args);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const fetch = async () => {
|
|
104
|
+
if (!fixture) {
|
|
105
|
+
throw new Error('No fixture found');
|
|
106
|
+
}
|
|
107
|
+
// delayCollapse determines when the fixture function is 'collapsed' (aka 'run')
|
|
108
|
+
// collapsed: https://en.wikipedia.org/wiki/Copenhagen_interpretation
|
|
109
|
+
if (fixture.delayCollapse) {
|
|
110
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
111
|
+
}
|
|
112
|
+
const result = await collapseFixture(
|
|
113
|
+
fixture as any,
|
|
114
|
+
args as any,
|
|
115
|
+
this.interceptorData,
|
|
116
|
+
);
|
|
117
|
+
if (!fixture.delayCollapse && delayMs) {
|
|
118
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
119
|
+
}
|
|
120
|
+
if (result.error) {
|
|
121
|
+
throw result.response;
|
|
122
|
+
}
|
|
123
|
+
return result.response;
|
|
124
|
+
};
|
|
125
|
+
if (typeof (replacedAction.endpoint as any).extend === 'function') {
|
|
126
|
+
replacedAction.endpoint = (replacedAction.endpoint as any).extend(
|
|
127
|
+
{
|
|
128
|
+
fetch,
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
} else {
|
|
132
|
+
// TODO: full testing of this
|
|
133
|
+
replacedAction.endpoint = fetch as any;
|
|
134
|
+
(replacedAction.endpoint as any).__proto__ = action.endpoint;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// TODO: make super.dispatch (once we drop support for destructuring)
|
|
138
|
+
return this._dispatch(replacedAction);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// TODO: make super.dispatch (once we drop support for destructuring)
|
|
142
|
+
return this._dispatch(action);
|
|
143
|
+
}) as any;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Fixture, ResponseInterceptor } from './fixtureTypes.js';
|
|
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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Fixture, Interceptor } from './fixtureTypes.js';
|
|
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
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { EndpointInterface, ResolveType } from '@data-client/endpoint';
|
|
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>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { MockController } from './MockController.js';
|
|
2
|
+
export type { MockProps } from './mockTypes.js';
|
|
3
|
+
export type {
|
|
4
|
+
Fixture,
|
|
5
|
+
SuccessFixture,
|
|
6
|
+
ErrorFixture,
|
|
7
|
+
Interceptor,
|
|
8
|
+
ResponseInterceptor,
|
|
9
|
+
FetchInterceptor,
|
|
10
|
+
FixtureEndpoint,
|
|
11
|
+
SuccessFixtureEndpoint,
|
|
12
|
+
ErrorFixtureEndpoint,
|
|
13
|
+
} from './fixtureTypes.js';
|
|
14
|
+
export { collapseFixture } from './collapseFixture.js';
|
|
15
|
+
export { createFixtureMap } from './createFixtureMap.js';
|
|
16
|
+
export { default as mockInitialState } from './mockState.js';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SetAction,
|
|
3
|
+
State,
|
|
4
|
+
ActionTypes,
|
|
5
|
+
Controller,
|
|
6
|
+
createReducer,
|
|
7
|
+
__INTERNAL__,
|
|
8
|
+
} from '../index.js';
|
|
9
|
+
import type { Fixture } from './fixtureTypes.js';
|
|
10
|
+
|
|
11
|
+
const { initialState } = __INTERNAL__;
|
|
12
|
+
|
|
13
|
+
export default function mockInitialState(
|
|
14
|
+
fixtures: Fixture[] = [],
|
|
15
|
+
): State<unknown> {
|
|
16
|
+
const actions: SetAction[] = [];
|
|
17
|
+
const dispatch = (action: any) => {
|
|
18
|
+
actions.push(action);
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
};
|
|
21
|
+
const controller = new Controller({ dispatch });
|
|
22
|
+
const reducer: (
|
|
23
|
+
state: State<unknown> | undefined,
|
|
24
|
+
action: ActionTypes,
|
|
25
|
+
) => State<unknown> = createReducer(controller);
|
|
26
|
+
|
|
27
|
+
fixtures.forEach(fixture => {
|
|
28
|
+
dispatchFixture(fixture, fixture.args, controller);
|
|
29
|
+
});
|
|
30
|
+
return actions.reduce(reducer, initialState);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function dispatchFixture(
|
|
34
|
+
fixture: Fixture,
|
|
35
|
+
args: any[],
|
|
36
|
+
controller: Controller,
|
|
37
|
+
fetchedAt?: number,
|
|
38
|
+
) {
|
|
39
|
+
// eslint-disable-next-line prefer-const
|
|
40
|
+
let { endpoint } = fixture;
|
|
41
|
+
const { response, error } = fixture;
|
|
42
|
+
if (controller.resolve) {
|
|
43
|
+
controller.resolve(endpoint, {
|
|
44
|
+
args,
|
|
45
|
+
response,
|
|
46
|
+
error,
|
|
47
|
+
fetchedAt: fetchedAt ?? Date.now(),
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
if (error === true) {
|
|
51
|
+
controller.setError(endpoint, ...args, response);
|
|
52
|
+
} else {
|
|
53
|
+
controller.setResponse(endpoint, ...args, response);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Controller } from '../index.js';
|
|
2
|
+
import { MockProps } from './mockTypes.js';
|
|
3
|
+
export declare function MockController<TBase extends typeof Controller, T>(Base: TBase, { fixtures, getInitialInterceptorData, }: MockProps<T>): TBase;
|
|
4
|
+
//# sourceMappingURL=MockController.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Fixture, ResponseInterceptor } from './fixtureTypes.js';
|
|
2
|
+
export declare function collapseFixture(fixture: Fixture | ResponseInterceptor, args: any[], interceptorData: any): Promise<{
|
|
3
|
+
response: any;
|
|
4
|
+
error: boolean | undefined;
|
|
5
|
+
}>;
|
|
6
|
+
//# sourceMappingURL=collapseFixture.d.ts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { EndpointInterface, ResolveType } from '@data-client/endpoint';
|
|
2
|
+
type Updater = (result: any, ...args: any) => Record<string, (...args: any) => any>;
|
|
3
|
+
export interface SuccessFixtureEndpoint<E extends EndpointInterface & {
|
|
4
|
+
update?: Updater;
|
|
5
|
+
} = EndpointInterface> {
|
|
6
|
+
readonly endpoint: E;
|
|
7
|
+
readonly args: Parameters<E>;
|
|
8
|
+
readonly response: ResolveType<E> | ((...args: Parameters<E>) => ResolveType<E>);
|
|
9
|
+
readonly error?: false;
|
|
10
|
+
/** Number of miliseconds to wait before resolving */
|
|
11
|
+
readonly delay?: number;
|
|
12
|
+
/** Waits to run `response()` after `delay` time */
|
|
13
|
+
readonly delayCollapse?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface ResponseInterceptor<T = any, E extends EndpointInterface & {
|
|
16
|
+
update?: Updater;
|
|
17
|
+
testKey(key: string): boolean;
|
|
18
|
+
} = EndpointInterface & {
|
|
19
|
+
testKey(key: string): boolean;
|
|
20
|
+
}> {
|
|
21
|
+
readonly endpoint: E;
|
|
22
|
+
response(this: T, ...args: Parameters<E>): ResolveType<E>;
|
|
23
|
+
/** Number of miliseconds (or function that returns) to wait before resolving */
|
|
24
|
+
readonly delay?: number | ((...args: Parameters<E>) => number);
|
|
25
|
+
/** Waits to run `response()` after `delay` time */
|
|
26
|
+
readonly delayCollapse?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface FetchInterceptor<T = any, E extends EndpointInterface & {
|
|
29
|
+
update?: Updater;
|
|
30
|
+
testKey(key: string): boolean;
|
|
31
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
32
|
+
extend(options: any): any;
|
|
33
|
+
} = EndpointInterface & {
|
|
34
|
+
testKey(key: string): boolean;
|
|
35
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
36
|
+
extend(options: any): any;
|
|
37
|
+
}> {
|
|
38
|
+
readonly endpoint: E;
|
|
39
|
+
fetchResponse(this: T, input: RequestInfo, init: RequestInit): ResolveType<E>;
|
|
40
|
+
/** Number of miliseconds (or function that returns) to wait before resolving */
|
|
41
|
+
readonly delay?: number | ((...args: Parameters<E>) => number);
|
|
42
|
+
/** Waits to run `response()` after `delay` time */
|
|
43
|
+
readonly delayCollapse?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/** Interceptors match and compute dynamic responses based on args
|
|
46
|
+
*
|
|
47
|
+
* @see https://dataclient.io/docs/api/Fixtures#interceptor
|
|
48
|
+
*/
|
|
49
|
+
export type Interceptor<T = any, E extends EndpointInterface & {
|
|
50
|
+
update?: Updater;
|
|
51
|
+
testKey(key: string): boolean;
|
|
52
|
+
fetchResponse?(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
53
|
+
extend?(options: any): any;
|
|
54
|
+
} = EndpointInterface & {
|
|
55
|
+
testKey(key: string): boolean;
|
|
56
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
57
|
+
extend(options: any): any;
|
|
58
|
+
}> = ResponseInterceptor<T, E> | (E extends ({
|
|
59
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
60
|
+
extend(options: any): any;
|
|
61
|
+
}) ? FetchInterceptor<T, E> : never);
|
|
62
|
+
export interface ErrorFixtureEndpoint<E extends EndpointInterface & {
|
|
63
|
+
update?: Updater;
|
|
64
|
+
} = EndpointInterface> {
|
|
65
|
+
readonly endpoint: E;
|
|
66
|
+
readonly args: Parameters<E>;
|
|
67
|
+
readonly response: any;
|
|
68
|
+
readonly error: true;
|
|
69
|
+
/** Number of miliseconds to wait before resolving */
|
|
70
|
+
readonly delay?: number;
|
|
71
|
+
/** Waits to run `response()` after `delay` time */
|
|
72
|
+
readonly delayCollapse?: boolean;
|
|
73
|
+
}
|
|
74
|
+
export type FixtureEndpoint<E extends EndpointInterface & {
|
|
75
|
+
update?: Updater;
|
|
76
|
+
} = EndpointInterface> = SuccessFixtureEndpoint<E> | ErrorFixtureEndpoint<E>;
|
|
77
|
+
/** Represents a successful response
|
|
78
|
+
*
|
|
79
|
+
* @see https://dataclient.io/docs/api/Fixtures#successfixture
|
|
80
|
+
*/
|
|
81
|
+
export type SuccessFixture<E extends EndpointInterface & {
|
|
82
|
+
update?: Updater;
|
|
83
|
+
} = EndpointInterface> = SuccessFixtureEndpoint<E>;
|
|
84
|
+
/** Represents a failed/errored response
|
|
85
|
+
*
|
|
86
|
+
* @see https://dataclient.io/docs/api/Fixtures#errorfixtures
|
|
87
|
+
*/
|
|
88
|
+
export type ErrorFixture<E extends EndpointInterface & {
|
|
89
|
+
update?: Updater;
|
|
90
|
+
} = EndpointInterface> = ErrorFixtureEndpoint<E>;
|
|
91
|
+
/** Represents a static response
|
|
92
|
+
*
|
|
93
|
+
* @see https://dataclient.io/docs/api/Fixtures
|
|
94
|
+
*/
|
|
95
|
+
export type Fixture<E extends EndpointInterface & {
|
|
96
|
+
update?: Updater;
|
|
97
|
+
} = EndpointInterface> = FixtureEndpoint<E>;
|
|
98
|
+
export {};
|
|
99
|
+
//# sourceMappingURL=fixtureTypes.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { MockController } from './MockController.js';
|
|
2
|
+
export { MockProps } from './mockTypes.js';
|
|
3
|
+
export { Fixture, SuccessFixture, ErrorFixture, Interceptor, ResponseInterceptor, FetchInterceptor, FixtureEndpoint, SuccessFixtureEndpoint, ErrorFixtureEndpoint, } from './fixtureTypes.js';
|
|
4
|
+
export { collapseFixture } from './collapseFixture.js';
|
|
5
|
+
export { createFixtureMap } from './createFixtureMap.js';
|
|
6
|
+
export { default as mockInitialState } from './mockState.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|