@dhis2/app-service-data 3.16.0 → 3.17.0-beta.2
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/build/cjs/__tests__/integration.test.js +10 -16
- package/build/cjs/__tests__/mutations.test.js +5 -8
- package/build/cjs/index.js +16 -44
- package/build/cjs/react/components/CustomDataProvider.js +9 -11
- package/build/cjs/react/components/DataMutation.js +7 -8
- package/build/cjs/react/components/DataProvider.js +3 -4
- package/build/cjs/react/components/DataProvider.test.js +3 -4
- package/build/cjs/react/components/DataQuery.js +8 -9
- package/build/cjs/react/components/index.js +33 -0
- package/build/cjs/react/context/DataContext.js +2 -2
- package/build/cjs/react/context/defaultDataContext.js +13 -0
- package/build/cjs/react/context/{defaultContext.test.js → defaultDataContext.test.js} +3 -3
- package/build/cjs/react/hooks/index.js +26 -0
- package/build/cjs/react/hooks/useDataMutation.js +6 -7
- package/build/cjs/react/hooks/useDataMutation.test.js +44 -71
- package/build/cjs/react/hooks/useDataQuery.js +10 -14
- package/build/cjs/react/hooks/useDataQuery.test.js +172 -265
- package/build/cjs/react/hooks/useQueryExecutor.js +11 -13
- package/build/cjs/react/hooks/useQueryExecutor.test.js +12 -16
- package/build/cjs/react/hooks/useStaticInput.js +4 -5
- package/build/cjs/react/hooks/useStaticInput.test.js +24 -39
- package/build/cjs/react/index.js +22 -77
- package/build/es/__tests__/integration.test.js +10 -16
- package/build/es/__tests__/mutations.test.js +5 -8
- package/build/es/index.js +2 -3
- package/build/es/react/components/CustomDataProvider.js +7 -9
- package/build/es/react/components/DataMutation.js +7 -8
- package/build/es/react/components/DataProvider.js +1 -2
- package/build/es/react/components/DataProvider.test.js +1 -2
- package/build/es/react/components/DataQuery.js +8 -9
- package/build/es/react/components/index.js +4 -0
- package/build/es/react/context/DataContext.js +2 -2
- package/build/es/react/context/{defaultContext.js → defaultDataContext.js} +2 -3
- package/build/es/react/context/{defaultContext.test.js → defaultDataContext.test.js} +3 -3
- package/build/es/react/hooks/index.js +3 -0
- package/build/es/react/hooks/useDataMutation.js +6 -7
- package/build/es/react/hooks/useDataMutation.test.js +44 -71
- package/build/es/react/hooks/useDataQuery.js +10 -14
- package/build/es/react/hooks/useDataQuery.test.js +172 -265
- package/build/es/react/hooks/useQueryExecutor.js +10 -12
- package/build/es/react/hooks/useQueryExecutor.test.js +12 -16
- package/build/es/react/hooks/useStaticInput.js +4 -5
- package/build/es/react/hooks/useStaticInput.test.js +24 -39
- package/build/es/react/index.js +3 -11
- package/build/types/index.d.ts +2 -3
- package/build/types/react/components/CustomDataProvider.d.ts +1 -1
- package/build/types/react/components/DataMutation.d.ts +1 -1
- package/build/types/react/components/DataQuery.d.ts +1 -1
- package/build/types/react/components/index.d.ts +4 -0
- package/build/types/react/context/defaultDataContext.d.ts +4 -0
- package/build/types/react/hooks/index.d.ts +3 -0
- package/build/types/react/hooks/mergeAndCompareVariables.d.ts +1 -1
- package/build/types/react/hooks/useDataEngine.d.ts +1 -1
- package/build/types/react/hooks/useDataMutation.d.ts +1 -1
- package/build/types/react/hooks/useDataQuery.d.ts +1 -1
- package/build/types/react/index.d.ts +2 -11
- package/build/types/types.d.ts +2 -7
- package/package.json +4 -3
- package/build/cjs/engine/DataEngine.js +0 -73
- package/build/cjs/engine/DataEngine.test.js +0 -156
- package/build/cjs/engine/helpers/getMutationFetchType.js +0 -8
- package/build/cjs/engine/helpers/getMutationFetchType.test.js +0 -39
- package/build/cjs/engine/helpers/resolveDynamicQuery.js +0 -21
- package/build/cjs/engine/helpers/resolveDynamicQuery.test.js +0 -63
- package/build/cjs/engine/helpers/validate.js +0 -62
- package/build/cjs/engine/helpers/validate.test.js +0 -206
- package/build/cjs/engine/index.js +0 -104
- package/build/cjs/engine/types/DataEngineLink.js +0 -1
- package/build/cjs/engine/types/ExecuteOptions.js +0 -1
- package/build/cjs/engine/types/FetchError.js +0 -24
- package/build/cjs/engine/types/FetchError.test.js +0 -14
- package/build/cjs/engine/types/InvalidQueryError.js +0 -18
- package/build/cjs/engine/types/JsonValue.js +0 -1
- package/build/cjs/engine/types/Mutation.js +0 -1
- package/build/cjs/engine/types/PossiblyDynamic.js +0 -1
- package/build/cjs/engine/types/Query.js +0 -1
- package/build/cjs/engine/types/QueryParameters.js +0 -1
- package/build/cjs/links/CustomDataLink.js +0 -51
- package/build/cjs/links/CustomDataLink.test.js +0 -73
- package/build/cjs/links/ErrorLink.js +0 -20
- package/build/cjs/links/RestAPILink/fetchData.js +0 -80
- package/build/cjs/links/RestAPILink/fetchData.test.js +0 -132
- package/build/cjs/links/RestAPILink/metadataResources.js +0 -22
- package/build/cjs/links/RestAPILink/path.js +0 -14
- package/build/cjs/links/RestAPILink/path.test.js +0 -16
- package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +0 -58
- package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -73
- package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.js +0 -80
- package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -120
- package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +0 -170
- package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -246
- package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -14
- package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -20
- package/build/cjs/links/RestAPILink/queryToRequestOptions.js +0 -34
- package/build/cjs/links/RestAPILink/queryToRequestOptions.test.js +0 -107
- package/build/cjs/links/RestAPILink/queryToResourcePath.js +0 -82
- package/build/cjs/links/RestAPILink/queryToResourcePath.test.js +0 -173
- package/build/cjs/links/RestAPILink/validateQuery.js +0 -59
- package/build/cjs/links/RestAPILink/validateQuery.test.js +0 -209
- package/build/cjs/links/RestAPILink.js +0 -33
- package/build/cjs/links/RestAPILink.test.js +0 -21
- package/build/cjs/links/index.js +0 -38
- package/build/cjs/locales/en/translations.json +0 -3
- package/build/cjs/locales/index.js +0 -21
- package/build/cjs/react/context/defaultContext.js +0 -14
- package/build/es/engine/DataEngine.js +0 -66
- package/build/es/engine/DataEngine.test.js +0 -154
- package/build/es/engine/helpers/getMutationFetchType.js +0 -1
- package/build/es/engine/helpers/getMutationFetchType.test.js +0 -37
- package/build/es/engine/helpers/resolveDynamicQuery.js +0 -14
- package/build/es/engine/helpers/resolveDynamicQuery.test.js +0 -61
- package/build/es/engine/helpers/validate.js +0 -53
- package/build/es/engine/helpers/validate.test.js +0 -204
- package/build/es/engine/index.js +0 -10
- package/build/es/engine/types/DataEngineLink.js +0 -1
- package/build/es/engine/types/ExecuteOptions.js +0 -1
- package/build/es/engine/types/FetchError.js +0 -17
- package/build/es/engine/types/FetchError.test.js +0 -12
- package/build/es/engine/types/InvalidQueryError.js +0 -11
- package/build/es/engine/types/JsonValue.js +0 -1
- package/build/es/engine/types/Mutation.js +0 -1
- package/build/es/engine/types/PossiblyDynamic.js +0 -1
- package/build/es/engine/types/Query.js +0 -1
- package/build/es/engine/types/QueryParameters.js +0 -1
- package/build/es/links/CustomDataLink.js +0 -44
- package/build/es/links/CustomDataLink.test.js +0 -71
- package/build/es/links/ErrorLink.js +0 -13
- package/build/es/links/RestAPILink/fetchData.js +0 -71
- package/build/es/links/RestAPILink/fetchData.test.js +0 -130
- package/build/es/links/RestAPILink/metadataResources.js +0 -16
- package/build/es/links/RestAPILink/path.js +0 -7
- package/build/es/links/RestAPILink/path.test.js +0 -14
- package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +0 -47
- package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -71
- package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.js +0 -70
- package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -118
- package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +0 -151
- package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -244
- package/build/es/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -7
- package/build/es/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -18
- package/build/es/links/RestAPILink/queryToRequestOptions.js +0 -27
- package/build/es/links/RestAPILink/queryToRequestOptions.test.js +0 -105
- package/build/es/links/RestAPILink/queryToResourcePath.js +0 -75
- package/build/es/links/RestAPILink/queryToResourcePath.test.js +0 -171
- package/build/es/links/RestAPILink/validateQuery.js +0 -52
- package/build/es/links/RestAPILink/validateQuery.test.js +0 -207
- package/build/es/links/RestAPILink.js +0 -26
- package/build/es/links/RestAPILink.test.js +0 -19
- package/build/es/links/index.js +0 -4
- package/build/es/locales/en/translations.json +0 -3
- package/build/es/locales/index.js +0 -13
- package/build/types/engine/DataEngine.d.ts +0 -13
- package/build/types/engine/helpers/getMutationFetchType.d.ts +0 -3
- package/build/types/engine/helpers/resolveDynamicQuery.d.ts +0 -2
- package/build/types/engine/helpers/validate.d.ts +0 -4
- package/build/types/engine/index.d.ts +0 -9
- package/build/types/engine/types/DataEngineLink.d.ts +0 -9
- package/build/types/engine/types/ExecuteOptions.d.ts +0 -9
- package/build/types/engine/types/FetchError.d.ts +0 -19
- package/build/types/engine/types/InvalidQueryError.d.ts +0 -5
- package/build/types/engine/types/JsonValue.d.ts +0 -6
- package/build/types/engine/types/Mutation.d.ts +0 -29
- package/build/types/engine/types/PossiblyDynamic.d.ts +0 -1
- package/build/types/engine/types/Query.d.ts +0 -24
- package/build/types/engine/types/QueryParameters.d.ts +0 -12
- package/build/types/links/CustomDataLink.d.ts +0 -17
- package/build/types/links/ErrorLink.d.ts +0 -6
- package/build/types/links/RestAPILink/fetchData.d.ts +0 -4
- package/build/types/links/RestAPILink/metadataResources.d.ts +0 -2
- package/build/types/links/RestAPILink/path.d.ts +0 -1
- package/build/types/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.d.ts +0 -6
- package/build/types/links/RestAPILink/queryToRequestOptions/requestContentType.d.ts +0 -6
- package/build/types/links/RestAPILink/queryToRequestOptions/textPlainMatchers.d.ts +0 -14
- package/build/types/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.d.ts +0 -2
- package/build/types/links/RestAPILink/queryToRequestOptions.d.ts +0 -2
- package/build/types/links/RestAPILink/queryToResourcePath.d.ts +0 -3
- package/build/types/links/RestAPILink/validateQuery.d.ts +0 -2
- package/build/types/links/RestAPILink.d.ts +0 -10
- package/build/types/links/index.d.ts +0 -3
- package/build/types/react/context/defaultContext.d.ts +0 -4
|
@@ -1,15 +1,14 @@
|
|
|
1
|
+
import { FetchError } from '@dhis2/data-engine';
|
|
1
2
|
import { useState, useCallback, useRef, useEffect } from 'react';
|
|
2
|
-
import { FetchError } from '../../engine';
|
|
3
3
|
import { useStaticInput } from './useStaticInput';
|
|
4
|
-
export const useQueryExecutor =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} = _ref;
|
|
4
|
+
export const useQueryExecutor = ({
|
|
5
|
+
execute,
|
|
6
|
+
variables: initialVariables,
|
|
7
|
+
singular,
|
|
8
|
+
immediate,
|
|
9
|
+
onComplete,
|
|
10
|
+
onError
|
|
11
|
+
}) => {
|
|
13
12
|
const [theExecute] = useStaticInput(execute);
|
|
14
13
|
const [state, setState] = useState({
|
|
15
14
|
called: !!immediate,
|
|
@@ -32,8 +31,7 @@ export const useQueryExecutor = _ref => {
|
|
|
32
31
|
})
|
|
33
32
|
}));
|
|
34
33
|
}, [abort]);
|
|
35
|
-
const refetch = useCallback(
|
|
36
|
-
let newVariables = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
34
|
+
const refetch = useCallback((newVariables = {}) => {
|
|
37
35
|
setState(state => !state.called || !state.loading ? {
|
|
38
36
|
called: true,
|
|
39
37
|
loading: true
|
|
@@ -2,10 +2,9 @@ import { renderHook, act, waitFor } from '@testing-library/react';
|
|
|
2
2
|
import { useQueryExecutor } from './useQueryExecutor';
|
|
3
3
|
const testError = new Error('TEST ERROR');
|
|
4
4
|
let theSignal;
|
|
5
|
-
const execute = jest.fn(async
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} = _ref;
|
|
5
|
+
const execute = jest.fn(async ({
|
|
6
|
+
signal
|
|
7
|
+
}) => {
|
|
9
8
|
theSignal = signal;
|
|
10
9
|
return 42;
|
|
11
10
|
});
|
|
@@ -112,18 +111,15 @@ describe('useQueryExecutor', () => {
|
|
|
112
111
|
const {
|
|
113
112
|
result,
|
|
114
113
|
rerender
|
|
115
|
-
} = renderHook(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
onComplete
|
|
125
|
-
});
|
|
126
|
-
}, {
|
|
114
|
+
} = renderHook(({
|
|
115
|
+
onComplete
|
|
116
|
+
}) => useQueryExecutor({
|
|
117
|
+
execute,
|
|
118
|
+
immediate: true,
|
|
119
|
+
singular: true,
|
|
120
|
+
variables: {},
|
|
121
|
+
onComplete
|
|
122
|
+
}), {
|
|
127
123
|
initialProps: {
|
|
128
124
|
onComplete: () => null
|
|
129
125
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { useState, useEffect, useRef, useDebugValue } from 'react';
|
|
2
|
-
export const useStaticInput =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
2
|
+
export const useStaticInput = (staticValue, {
|
|
3
|
+
warn = false,
|
|
4
|
+
name = 'input'
|
|
5
|
+
} = {}) => {
|
|
7
6
|
const originalValue = useRef(staticValue);
|
|
8
7
|
const [value, setValue] = useState(() => originalValue.current);
|
|
9
8
|
useDebugValue(value, debugValue => `${name}: ${JSON.stringify(debugValue)}`);
|
|
@@ -19,12 +19,9 @@ describe('useStaticInput', () => {
|
|
|
19
19
|
const {
|
|
20
20
|
result,
|
|
21
21
|
rerender
|
|
22
|
-
} = renderHook(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
} = _ref;
|
|
26
|
-
return useStaticInput(value);
|
|
27
|
-
}, {
|
|
22
|
+
} = renderHook(({
|
|
23
|
+
value
|
|
24
|
+
}) => useStaticInput(value), {
|
|
28
25
|
initialProps: {
|
|
29
26
|
value: 42
|
|
30
27
|
}
|
|
@@ -40,14 +37,11 @@ describe('useStaticInput', () => {
|
|
|
40
37
|
const {
|
|
41
38
|
result,
|
|
42
39
|
rerender
|
|
43
|
-
} = renderHook(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
warn: true
|
|
49
|
-
});
|
|
50
|
-
}, {
|
|
40
|
+
} = renderHook(({
|
|
41
|
+
value
|
|
42
|
+
}) => useStaticInput(value, {
|
|
43
|
+
warn: true
|
|
44
|
+
}), {
|
|
51
45
|
initialProps: {
|
|
52
46
|
value: 42
|
|
53
47
|
}
|
|
@@ -68,15 +62,12 @@ describe('useStaticInput', () => {
|
|
|
68
62
|
const {
|
|
69
63
|
result,
|
|
70
64
|
rerender
|
|
71
|
-
} = renderHook(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
name: 'TESTING THING'
|
|
78
|
-
});
|
|
79
|
-
}, {
|
|
65
|
+
} = renderHook(({
|
|
66
|
+
value
|
|
67
|
+
}) => useStaticInput(value, {
|
|
68
|
+
warn: true,
|
|
69
|
+
name: 'TESTING THING'
|
|
70
|
+
}), {
|
|
80
71
|
initialProps: {
|
|
81
72
|
value: 42
|
|
82
73
|
}
|
|
@@ -93,14 +84,11 @@ describe('useStaticInput', () => {
|
|
|
93
84
|
const {
|
|
94
85
|
result,
|
|
95
86
|
rerender
|
|
96
|
-
} = renderHook(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
warn: true
|
|
102
|
-
});
|
|
103
|
-
}, {
|
|
87
|
+
} = renderHook(({
|
|
88
|
+
value
|
|
89
|
+
}) => useStaticInput(value, {
|
|
90
|
+
warn: true
|
|
91
|
+
}), {
|
|
104
92
|
initialProps: {
|
|
105
93
|
value: 42
|
|
106
94
|
}
|
|
@@ -129,14 +117,11 @@ describe('useStaticInput', () => {
|
|
|
129
117
|
const {
|
|
130
118
|
result,
|
|
131
119
|
rerender
|
|
132
|
-
} = renderHook(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
warn: true
|
|
138
|
-
});
|
|
139
|
-
}, {
|
|
120
|
+
} = renderHook(({
|
|
121
|
+
value
|
|
122
|
+
}) => useStaticInput(value, {
|
|
123
|
+
warn: true
|
|
124
|
+
}), {
|
|
140
125
|
initialProps: {
|
|
141
126
|
value: fn
|
|
142
127
|
}
|
package/build/es/react/index.js
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
4
|
-
export { DataQuery } from './components/DataQuery';
|
|
5
|
-
export { DataContext } from './context/DataContext';
|
|
6
|
-
export { defaultContext } from './context/defaultContext';
|
|
7
|
-
export { useDataEngine } from './hooks/useDataEngine';
|
|
8
|
-
export { useDataMutation } from './hooks/useDataMutation';
|
|
9
|
-
export { useDataQuery } from './hooks/useDataQuery';
|
|
10
|
-
export { useQueryExecutor } from './hooks/useQueryExecutor';
|
|
11
|
-
export { useStaticInput } from './hooks/useStaticInput';
|
|
1
|
+
export * from './components';
|
|
2
|
+
export * from './hooks';
|
|
3
|
+
export {};
|
package/build/types/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
3
|
-
export { FetchError } from './engine';
|
|
1
|
+
export * from './react';
|
|
2
|
+
export { FetchError } from '@dhis2/data-engine';
|
|
4
3
|
export type * from './types';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { QueryVariables } from '
|
|
1
|
+
import type { QueryVariables } from '@dhis2/data-engine';
|
|
2
2
|
export declare const mergeAndCompareVariables: (previousVariables?: QueryVariables, newVariables?: QueryVariables, previousHash?: string) => {
|
|
3
3
|
identical: boolean;
|
|
4
4
|
mergedVariablesHash: string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const useDataEngine: () => import("
|
|
1
|
+
export declare const useDataEngine: () => import("@dhis2/data-engine").DataEngine;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { QueryOptions, Mutation } from '
|
|
1
|
+
import type { QueryOptions, Mutation } from '@dhis2/data-engine';
|
|
2
2
|
import { MutationRenderInput } from '../../types';
|
|
3
3
|
export declare const useDataMutation: (mutation: Mutation, { onComplete, onError, variables, lazy }?: QueryOptions) => MutationRenderInput;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { Query, QueryOptions, QueryResult } from '
|
|
1
|
+
import type { Query, QueryOptions, QueryResult } from '@dhis2/data-engine';
|
|
2
2
|
import type { QueryRenderInput } from '../../types';
|
|
3
3
|
export declare const useDataQuery: <TQueryResult = QueryResult>(query: Query, { onComplete: userOnSuccess, onError: userOnError, variables: initialVariables, lazy: initialLazy, }?: QueryOptions<TQueryResult>) => QueryRenderInput<TQueryResult>;
|
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export { DataProvider } from './components/DataProvider';
|
|
4
|
-
export { DataQuery } from './components/DataQuery';
|
|
5
|
-
export { DataContext } from './context/DataContext';
|
|
6
|
-
export { defaultContext } from './context/defaultContext';
|
|
7
|
-
export { useDataEngine } from './hooks/useDataEngine';
|
|
8
|
-
export { useDataMutation } from './hooks/useDataMutation';
|
|
9
|
-
export { useDataQuery } from './hooks/useDataQuery';
|
|
10
|
-
export { useQueryExecutor } from './hooks/useQueryExecutor';
|
|
11
|
-
export { useStaticInput } from './hooks/useStaticInput';
|
|
1
|
+
export * from './components';
|
|
2
|
+
export * from './hooks';
|
package/build/types/types.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import DataEngine from '
|
|
2
|
-
|
|
3
|
-
import { FetchError } from './engine/types/FetchError';
|
|
4
|
-
import { JsonValue } from './engine/types/JsonValue';
|
|
5
|
-
import { QueryVariables, QueryResult } from './engine/types/Query';
|
|
6
|
-
export type { Mutation } from './engine/types/Mutation';
|
|
7
|
-
export type { Query } from './engine/types/Query';
|
|
1
|
+
import type { DataEngine, QueryExecuteOptions, FetchError, JsonValue, QueryVariables, QueryResult } from '@dhis2/data-engine';
|
|
2
|
+
export type { Mutation, Query } from '@dhis2/data-engine';
|
|
8
3
|
export interface ContextType {
|
|
9
4
|
engine: DataEngine;
|
|
10
5
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dhis2/app-service-data",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.17.0-beta.2",
|
|
4
4
|
"main": "./build/cjs/index.js",
|
|
5
5
|
"module": "./build/es/index.js",
|
|
6
|
-
"types": "build/types/index.d.ts",
|
|
6
|
+
"types": "./build/types/index.d.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./build/es/index.js",
|
|
9
9
|
"require": "./build/cjs/index.js",
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"prop-types": "^15.7.2"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@dhis2/app-service-config": "3.
|
|
40
|
+
"@dhis2/app-service-config": "3.17.0-beta.2",
|
|
41
|
+
"@dhis2/data-engine": "3.17.0-beta.2",
|
|
41
42
|
"react": "^16.8.6 || ^18",
|
|
42
43
|
"react-dom": "^16.8.6 || ^18"
|
|
43
44
|
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.DataEngine = void 0;
|
|
7
|
-
var _getMutationFetchType = require("./helpers/getMutationFetchType");
|
|
8
|
-
var _resolveDynamicQuery = require("./helpers/resolveDynamicQuery");
|
|
9
|
-
var _validate = require("./helpers/validate");
|
|
10
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
11
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
12
|
-
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
13
|
-
const reduceResponses = (responses, names) => responses.reduce((out, response, idx) => {
|
|
14
|
-
out[names[idx]] = response;
|
|
15
|
-
return out;
|
|
16
|
-
}, {});
|
|
17
|
-
class DataEngine {
|
|
18
|
-
constructor(link) {
|
|
19
|
-
_defineProperty(this, "link", void 0);
|
|
20
|
-
this.link = link;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Overload 1: When no generic is provided, accept any Query and return inferred type
|
|
24
|
-
|
|
25
|
-
// Overload 2: When generic is provided, enforce that query keys match the generic keys
|
|
26
|
-
|
|
27
|
-
query(query) {
|
|
28
|
-
let {
|
|
29
|
-
variables = {},
|
|
30
|
-
signal,
|
|
31
|
-
onComplete,
|
|
32
|
-
onError
|
|
33
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
34
|
-
const names = Object.keys(query);
|
|
35
|
-
const queries = names.map(name => query[name]).map(q => (0, _resolveDynamicQuery.resolveDynamicQuery)(q, variables));
|
|
36
|
-
(0, _validate.validateResourceQueries)(queries, names);
|
|
37
|
-
return Promise.all(queries.map(q => {
|
|
38
|
-
return this.link.executeResourceQuery('read', q, {
|
|
39
|
-
signal
|
|
40
|
-
});
|
|
41
|
-
})).then(results => {
|
|
42
|
-
const data = reduceResponses(results, names);
|
|
43
|
-
onComplete && onComplete(data);
|
|
44
|
-
return data;
|
|
45
|
-
}).catch(error => {
|
|
46
|
-
onError && onError(error);
|
|
47
|
-
throw error;
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
mutate(mutation) {
|
|
51
|
-
let {
|
|
52
|
-
variables = {},
|
|
53
|
-
signal,
|
|
54
|
-
onComplete,
|
|
55
|
-
onError
|
|
56
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
57
|
-
const query = (0, _resolveDynamicQuery.resolveDynamicQuery)(mutation, variables);
|
|
58
|
-
const type = (0, _getMutationFetchType.getMutationFetchType)(mutation);
|
|
59
|
-
(0, _validate.validateResourceQuery)(type, query);
|
|
60
|
-
const result = this.link.executeResourceQuery(type, query, {
|
|
61
|
-
signal
|
|
62
|
-
});
|
|
63
|
-
return result.then(data => {
|
|
64
|
-
onComplete && onComplete(data);
|
|
65
|
-
return data;
|
|
66
|
-
}).catch(error => {
|
|
67
|
-
onError && onError(error);
|
|
68
|
-
throw error;
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
exports.DataEngine = DataEngine;
|
|
73
|
-
var _default = exports.default = DataEngine;
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _DataEngine = require("./DataEngine");
|
|
4
|
-
const mockQuery = {
|
|
5
|
-
test: {
|
|
6
|
-
resource: 'test'
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
const mockMutation = {
|
|
10
|
-
resource: 'test',
|
|
11
|
-
type: 'create',
|
|
12
|
-
data: {}
|
|
13
|
-
};
|
|
14
|
-
const mockLink = {
|
|
15
|
-
executeResourceQuery: jest.fn(async (type, query) => {
|
|
16
|
-
if (query.resource === 'ERROR') {
|
|
17
|
-
throw new Error('MOCK ERROR');
|
|
18
|
-
}
|
|
19
|
-
return {
|
|
20
|
-
type,
|
|
21
|
-
resource: query.resource,
|
|
22
|
-
answer: 42
|
|
23
|
-
};
|
|
24
|
-
})
|
|
25
|
-
};
|
|
26
|
-
describe('DataEngine', () => {
|
|
27
|
-
beforeEach(() => {
|
|
28
|
-
jest.clearAllMocks();
|
|
29
|
-
});
|
|
30
|
-
it('Should construct successfully with a mock Link', () => {
|
|
31
|
-
expect(() => {
|
|
32
|
-
new _DataEngine.DataEngine(mockLink);
|
|
33
|
-
}).not.toThrow();
|
|
34
|
-
});
|
|
35
|
-
it('Should call the mock link with the passed query and options', () => {
|
|
36
|
-
const controller = new AbortController();
|
|
37
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
38
|
-
engine.query(mockQuery, {
|
|
39
|
-
signal: controller.signal
|
|
40
|
-
});
|
|
41
|
-
expect(mockLink.executeResourceQuery).toHaveBeenCalledWith('read', mockQuery.test, {
|
|
42
|
-
signal: controller.signal
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
it('Should call the mock link with the passed mutation and options', async () => {
|
|
46
|
-
const controller = new AbortController();
|
|
47
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
48
|
-
const result = await engine.mutate(mockMutation, {
|
|
49
|
-
signal: controller.signal
|
|
50
|
-
});
|
|
51
|
-
expect(mockLink.executeResourceQuery).toHaveBeenCalledWith(mockMutation.type, {
|
|
52
|
-
resource: mockMutation.resource,
|
|
53
|
-
data: {}
|
|
54
|
-
}, {
|
|
55
|
-
signal: controller.signal
|
|
56
|
-
});
|
|
57
|
-
expect(result).toMatchObject({
|
|
58
|
-
type: mockMutation.type,
|
|
59
|
-
resource: mockMutation.resource,
|
|
60
|
-
answer: 42
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
it('Should call multilple queries in parallel', async () => {
|
|
64
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
65
|
-
const result = await engine.query({
|
|
66
|
-
test: {
|
|
67
|
-
resource: 'test'
|
|
68
|
-
},
|
|
69
|
-
test2: {
|
|
70
|
-
resource: 'test2'
|
|
71
|
-
},
|
|
72
|
-
test3: {
|
|
73
|
-
resource: 'test3'
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
expect(mockLink.executeResourceQuery).toHaveBeenCalledTimes(3);
|
|
77
|
-
expect(result).toMatchObject({
|
|
78
|
-
test: {
|
|
79
|
-
type: 'read',
|
|
80
|
-
resource: 'test',
|
|
81
|
-
answer: 42
|
|
82
|
-
},
|
|
83
|
-
test2: {
|
|
84
|
-
type: 'read',
|
|
85
|
-
resource: 'test2',
|
|
86
|
-
answer: 42
|
|
87
|
-
},
|
|
88
|
-
test3: {
|
|
89
|
-
type: 'read',
|
|
90
|
-
resource: 'test3',
|
|
91
|
-
answer: 42
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
it('Should call onComplete callback only once for multiple-query method', async () => {
|
|
96
|
-
const options = {
|
|
97
|
-
onComplete: jest.fn()
|
|
98
|
-
};
|
|
99
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
100
|
-
await engine.query({
|
|
101
|
-
test: {
|
|
102
|
-
resource: 'test'
|
|
103
|
-
},
|
|
104
|
-
test2: {
|
|
105
|
-
resource: 'test2'
|
|
106
|
-
},
|
|
107
|
-
test3: {
|
|
108
|
-
resource: 'test3'
|
|
109
|
-
}
|
|
110
|
-
}, options);
|
|
111
|
-
expect(mockLink.executeResourceQuery).toHaveBeenCalledTimes(3);
|
|
112
|
-
expect(options.onComplete).toHaveBeenCalledTimes(1);
|
|
113
|
-
});
|
|
114
|
-
it('Should call onComplete callback only once for multiple-query method', async () => {
|
|
115
|
-
const options = {
|
|
116
|
-
onComplete: jest.fn(),
|
|
117
|
-
onError: jest.fn()
|
|
118
|
-
};
|
|
119
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
120
|
-
await expect(engine.query({
|
|
121
|
-
test: {
|
|
122
|
-
resource: 'test'
|
|
123
|
-
},
|
|
124
|
-
test2: {
|
|
125
|
-
resource: 'ERROR'
|
|
126
|
-
},
|
|
127
|
-
test3: {
|
|
128
|
-
resource: 'test3'
|
|
129
|
-
}
|
|
130
|
-
}, options)).rejects.toBeTruthy();
|
|
131
|
-
expect(options.onComplete).toHaveBeenCalledTimes(0);
|
|
132
|
-
expect(options.onError).toHaveBeenCalledTimes(1);
|
|
133
|
-
});
|
|
134
|
-
it('Should call onComplete callback after mutation', async () => {
|
|
135
|
-
const options = {
|
|
136
|
-
onComplete: jest.fn()
|
|
137
|
-
};
|
|
138
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
139
|
-
await engine.mutate(mockMutation, options);
|
|
140
|
-
expect(options.onComplete).toHaveBeenCalledTimes(1);
|
|
141
|
-
});
|
|
142
|
-
it('Should call onError callback after failed mutation', async () => {
|
|
143
|
-
const options = {
|
|
144
|
-
onComplete: jest.fn(),
|
|
145
|
-
onError: jest.fn()
|
|
146
|
-
};
|
|
147
|
-
const engine = new _DataEngine.DataEngine(mockLink);
|
|
148
|
-
await expect(engine.mutate({
|
|
149
|
-
resource: 'ERROR',
|
|
150
|
-
type: 'delete',
|
|
151
|
-
id: '2'
|
|
152
|
-
}, options)).rejects.toBeTruthy();
|
|
153
|
-
expect(options.onComplete).toHaveBeenCalledTimes(0);
|
|
154
|
-
expect(options.onError).toHaveBeenCalledTimes(1);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.getMutationFetchType = void 0;
|
|
7
|
-
const getMutationFetchType = mutation => mutation.type === 'update' ? mutation.partial ? 'update' : 'replace' : mutation.type;
|
|
8
|
-
exports.getMutationFetchType = getMutationFetchType;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _getMutationFetchType = require("./getMutationFetchType");
|
|
4
|
-
describe('getMutationFetchType', () => {
|
|
5
|
-
it('should return the passed fetch type if not `update`', () => {
|
|
6
|
-
expect((0, _getMutationFetchType.getMutationFetchType)({
|
|
7
|
-
type: 'create',
|
|
8
|
-
resource: 'test',
|
|
9
|
-
data: {}
|
|
10
|
-
})).toBe('create');
|
|
11
|
-
expect((0, _getMutationFetchType.getMutationFetchType)({
|
|
12
|
-
type: 'delete',
|
|
13
|
-
resource: 'test',
|
|
14
|
-
id: 'id'
|
|
15
|
-
})).toBe('delete');
|
|
16
|
-
expect((0, _getMutationFetchType.getMutationFetchType)({
|
|
17
|
-
type: 'json-patch',
|
|
18
|
-
resource: 'test',
|
|
19
|
-
data: {}
|
|
20
|
-
})).toBe('json-patch');
|
|
21
|
-
});
|
|
22
|
-
it('should return `replace` for non-partial `update`', () => {
|
|
23
|
-
expect((0, _getMutationFetchType.getMutationFetchType)({
|
|
24
|
-
type: 'update',
|
|
25
|
-
resource: 'test',
|
|
26
|
-
id: 'id',
|
|
27
|
-
data: {}
|
|
28
|
-
})).toBe('replace');
|
|
29
|
-
});
|
|
30
|
-
it('should return `update` for partial `update`', () => {
|
|
31
|
-
expect((0, _getMutationFetchType.getMutationFetchType)({
|
|
32
|
-
type: 'update',
|
|
33
|
-
partial: true,
|
|
34
|
-
resource: 'test',
|
|
35
|
-
id: 'id',
|
|
36
|
-
data: {}
|
|
37
|
-
})).toBe('update');
|
|
38
|
-
});
|
|
39
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.resolveDynamicQuery = void 0;
|
|
7
|
-
const resolveDynamicQuery = (_ref, variables) => {
|
|
8
|
-
let {
|
|
9
|
-
resource,
|
|
10
|
-
id,
|
|
11
|
-
data,
|
|
12
|
-
params
|
|
13
|
-
} = _ref;
|
|
14
|
-
return {
|
|
15
|
-
resource,
|
|
16
|
-
id: typeof id === 'function' ? id(variables) : id,
|
|
17
|
-
data: typeof data === 'function' ? data(variables) : data,
|
|
18
|
-
params: typeof params === 'function' ? params(variables) : params
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
exports.resolveDynamicQuery = resolveDynamicQuery;
|