@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.
Files changed (180) hide show
  1. package/build/cjs/__tests__/integration.test.js +10 -16
  2. package/build/cjs/__tests__/mutations.test.js +5 -8
  3. package/build/cjs/index.js +16 -44
  4. package/build/cjs/react/components/CustomDataProvider.js +9 -11
  5. package/build/cjs/react/components/DataMutation.js +7 -8
  6. package/build/cjs/react/components/DataProvider.js +3 -4
  7. package/build/cjs/react/components/DataProvider.test.js +3 -4
  8. package/build/cjs/react/components/DataQuery.js +8 -9
  9. package/build/cjs/react/components/index.js +33 -0
  10. package/build/cjs/react/context/DataContext.js +2 -2
  11. package/build/cjs/react/context/defaultDataContext.js +13 -0
  12. package/build/cjs/react/context/{defaultContext.test.js → defaultDataContext.test.js} +3 -3
  13. package/build/cjs/react/hooks/index.js +26 -0
  14. package/build/cjs/react/hooks/useDataMutation.js +6 -7
  15. package/build/cjs/react/hooks/useDataMutation.test.js +44 -71
  16. package/build/cjs/react/hooks/useDataQuery.js +10 -14
  17. package/build/cjs/react/hooks/useDataQuery.test.js +172 -265
  18. package/build/cjs/react/hooks/useQueryExecutor.js +11 -13
  19. package/build/cjs/react/hooks/useQueryExecutor.test.js +12 -16
  20. package/build/cjs/react/hooks/useStaticInput.js +4 -5
  21. package/build/cjs/react/hooks/useStaticInput.test.js +24 -39
  22. package/build/cjs/react/index.js +22 -77
  23. package/build/es/__tests__/integration.test.js +10 -16
  24. package/build/es/__tests__/mutations.test.js +5 -8
  25. package/build/es/index.js +2 -3
  26. package/build/es/react/components/CustomDataProvider.js +7 -9
  27. package/build/es/react/components/DataMutation.js +7 -8
  28. package/build/es/react/components/DataProvider.js +1 -2
  29. package/build/es/react/components/DataProvider.test.js +1 -2
  30. package/build/es/react/components/DataQuery.js +8 -9
  31. package/build/es/react/components/index.js +4 -0
  32. package/build/es/react/context/DataContext.js +2 -2
  33. package/build/es/react/context/{defaultContext.js → defaultDataContext.js} +2 -3
  34. package/build/es/react/context/{defaultContext.test.js → defaultDataContext.test.js} +3 -3
  35. package/build/es/react/hooks/index.js +3 -0
  36. package/build/es/react/hooks/useDataMutation.js +6 -7
  37. package/build/es/react/hooks/useDataMutation.test.js +44 -71
  38. package/build/es/react/hooks/useDataQuery.js +10 -14
  39. package/build/es/react/hooks/useDataQuery.test.js +172 -265
  40. package/build/es/react/hooks/useQueryExecutor.js +10 -12
  41. package/build/es/react/hooks/useQueryExecutor.test.js +12 -16
  42. package/build/es/react/hooks/useStaticInput.js +4 -5
  43. package/build/es/react/hooks/useStaticInput.test.js +24 -39
  44. package/build/es/react/index.js +3 -11
  45. package/build/types/index.d.ts +2 -3
  46. package/build/types/react/components/CustomDataProvider.d.ts +1 -1
  47. package/build/types/react/components/DataMutation.d.ts +1 -1
  48. package/build/types/react/components/DataQuery.d.ts +1 -1
  49. package/build/types/react/components/index.d.ts +4 -0
  50. package/build/types/react/context/defaultDataContext.d.ts +4 -0
  51. package/build/types/react/hooks/index.d.ts +3 -0
  52. package/build/types/react/hooks/mergeAndCompareVariables.d.ts +1 -1
  53. package/build/types/react/hooks/useDataEngine.d.ts +1 -1
  54. package/build/types/react/hooks/useDataMutation.d.ts +1 -1
  55. package/build/types/react/hooks/useDataQuery.d.ts +1 -1
  56. package/build/types/react/index.d.ts +2 -11
  57. package/build/types/types.d.ts +2 -7
  58. package/package.json +4 -3
  59. package/build/cjs/engine/DataEngine.js +0 -73
  60. package/build/cjs/engine/DataEngine.test.js +0 -156
  61. package/build/cjs/engine/helpers/getMutationFetchType.js +0 -8
  62. package/build/cjs/engine/helpers/getMutationFetchType.test.js +0 -39
  63. package/build/cjs/engine/helpers/resolveDynamicQuery.js +0 -21
  64. package/build/cjs/engine/helpers/resolveDynamicQuery.test.js +0 -63
  65. package/build/cjs/engine/helpers/validate.js +0 -62
  66. package/build/cjs/engine/helpers/validate.test.js +0 -206
  67. package/build/cjs/engine/index.js +0 -104
  68. package/build/cjs/engine/types/DataEngineLink.js +0 -1
  69. package/build/cjs/engine/types/ExecuteOptions.js +0 -1
  70. package/build/cjs/engine/types/FetchError.js +0 -24
  71. package/build/cjs/engine/types/FetchError.test.js +0 -14
  72. package/build/cjs/engine/types/InvalidQueryError.js +0 -18
  73. package/build/cjs/engine/types/JsonValue.js +0 -1
  74. package/build/cjs/engine/types/Mutation.js +0 -1
  75. package/build/cjs/engine/types/PossiblyDynamic.js +0 -1
  76. package/build/cjs/engine/types/Query.js +0 -1
  77. package/build/cjs/engine/types/QueryParameters.js +0 -1
  78. package/build/cjs/links/CustomDataLink.js +0 -51
  79. package/build/cjs/links/CustomDataLink.test.js +0 -73
  80. package/build/cjs/links/ErrorLink.js +0 -20
  81. package/build/cjs/links/RestAPILink/fetchData.js +0 -80
  82. package/build/cjs/links/RestAPILink/fetchData.test.js +0 -132
  83. package/build/cjs/links/RestAPILink/metadataResources.js +0 -22
  84. package/build/cjs/links/RestAPILink/path.js +0 -14
  85. package/build/cjs/links/RestAPILink/path.test.js +0 -16
  86. package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +0 -58
  87. package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -73
  88. package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.js +0 -80
  89. package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -120
  90. package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +0 -170
  91. package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -246
  92. package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -14
  93. package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -20
  94. package/build/cjs/links/RestAPILink/queryToRequestOptions.js +0 -34
  95. package/build/cjs/links/RestAPILink/queryToRequestOptions.test.js +0 -107
  96. package/build/cjs/links/RestAPILink/queryToResourcePath.js +0 -82
  97. package/build/cjs/links/RestAPILink/queryToResourcePath.test.js +0 -173
  98. package/build/cjs/links/RestAPILink/validateQuery.js +0 -59
  99. package/build/cjs/links/RestAPILink/validateQuery.test.js +0 -209
  100. package/build/cjs/links/RestAPILink.js +0 -33
  101. package/build/cjs/links/RestAPILink.test.js +0 -21
  102. package/build/cjs/links/index.js +0 -38
  103. package/build/cjs/locales/en/translations.json +0 -3
  104. package/build/cjs/locales/index.js +0 -21
  105. package/build/cjs/react/context/defaultContext.js +0 -14
  106. package/build/es/engine/DataEngine.js +0 -66
  107. package/build/es/engine/DataEngine.test.js +0 -154
  108. package/build/es/engine/helpers/getMutationFetchType.js +0 -1
  109. package/build/es/engine/helpers/getMutationFetchType.test.js +0 -37
  110. package/build/es/engine/helpers/resolveDynamicQuery.js +0 -14
  111. package/build/es/engine/helpers/resolveDynamicQuery.test.js +0 -61
  112. package/build/es/engine/helpers/validate.js +0 -53
  113. package/build/es/engine/helpers/validate.test.js +0 -204
  114. package/build/es/engine/index.js +0 -10
  115. package/build/es/engine/types/DataEngineLink.js +0 -1
  116. package/build/es/engine/types/ExecuteOptions.js +0 -1
  117. package/build/es/engine/types/FetchError.js +0 -17
  118. package/build/es/engine/types/FetchError.test.js +0 -12
  119. package/build/es/engine/types/InvalidQueryError.js +0 -11
  120. package/build/es/engine/types/JsonValue.js +0 -1
  121. package/build/es/engine/types/Mutation.js +0 -1
  122. package/build/es/engine/types/PossiblyDynamic.js +0 -1
  123. package/build/es/engine/types/Query.js +0 -1
  124. package/build/es/engine/types/QueryParameters.js +0 -1
  125. package/build/es/links/CustomDataLink.js +0 -44
  126. package/build/es/links/CustomDataLink.test.js +0 -71
  127. package/build/es/links/ErrorLink.js +0 -13
  128. package/build/es/links/RestAPILink/fetchData.js +0 -71
  129. package/build/es/links/RestAPILink/fetchData.test.js +0 -130
  130. package/build/es/links/RestAPILink/metadataResources.js +0 -16
  131. package/build/es/links/RestAPILink/path.js +0 -7
  132. package/build/es/links/RestAPILink/path.test.js +0 -14
  133. package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +0 -47
  134. package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -71
  135. package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.js +0 -70
  136. package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -118
  137. package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +0 -151
  138. package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -244
  139. package/build/es/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -7
  140. package/build/es/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -18
  141. package/build/es/links/RestAPILink/queryToRequestOptions.js +0 -27
  142. package/build/es/links/RestAPILink/queryToRequestOptions.test.js +0 -105
  143. package/build/es/links/RestAPILink/queryToResourcePath.js +0 -75
  144. package/build/es/links/RestAPILink/queryToResourcePath.test.js +0 -171
  145. package/build/es/links/RestAPILink/validateQuery.js +0 -52
  146. package/build/es/links/RestAPILink/validateQuery.test.js +0 -207
  147. package/build/es/links/RestAPILink.js +0 -26
  148. package/build/es/links/RestAPILink.test.js +0 -19
  149. package/build/es/links/index.js +0 -4
  150. package/build/es/locales/en/translations.json +0 -3
  151. package/build/es/locales/index.js +0 -13
  152. package/build/types/engine/DataEngine.d.ts +0 -13
  153. package/build/types/engine/helpers/getMutationFetchType.d.ts +0 -3
  154. package/build/types/engine/helpers/resolveDynamicQuery.d.ts +0 -2
  155. package/build/types/engine/helpers/validate.d.ts +0 -4
  156. package/build/types/engine/index.d.ts +0 -9
  157. package/build/types/engine/types/DataEngineLink.d.ts +0 -9
  158. package/build/types/engine/types/ExecuteOptions.d.ts +0 -9
  159. package/build/types/engine/types/FetchError.d.ts +0 -19
  160. package/build/types/engine/types/InvalidQueryError.d.ts +0 -5
  161. package/build/types/engine/types/JsonValue.d.ts +0 -6
  162. package/build/types/engine/types/Mutation.d.ts +0 -29
  163. package/build/types/engine/types/PossiblyDynamic.d.ts +0 -1
  164. package/build/types/engine/types/Query.d.ts +0 -24
  165. package/build/types/engine/types/QueryParameters.d.ts +0 -12
  166. package/build/types/links/CustomDataLink.d.ts +0 -17
  167. package/build/types/links/ErrorLink.d.ts +0 -6
  168. package/build/types/links/RestAPILink/fetchData.d.ts +0 -4
  169. package/build/types/links/RestAPILink/metadataResources.d.ts +0 -2
  170. package/build/types/links/RestAPILink/path.d.ts +0 -1
  171. package/build/types/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.d.ts +0 -6
  172. package/build/types/links/RestAPILink/queryToRequestOptions/requestContentType.d.ts +0 -6
  173. package/build/types/links/RestAPILink/queryToRequestOptions/textPlainMatchers.d.ts +0 -14
  174. package/build/types/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.d.ts +0 -2
  175. package/build/types/links/RestAPILink/queryToRequestOptions.d.ts +0 -2
  176. package/build/types/links/RestAPILink/queryToResourcePath.d.ts +0 -3
  177. package/build/types/links/RestAPILink/validateQuery.d.ts +0 -2
  178. package/build/types/links/RestAPILink.d.ts +0 -10
  179. package/build/types/links/index.d.ts +0 -3
  180. 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 = _ref => {
5
- let {
6
- execute,
7
- variables: initialVariables,
8
- singular,
9
- immediate,
10
- onComplete,
11
- onError
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(function () {
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 _ref => {
6
- let {
7
- signal
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(_ref2 => {
116
- let {
117
- onComplete
118
- } = _ref2;
119
- return useQueryExecutor({
120
- execute,
121
- immediate: true,
122
- singular: true,
123
- variables: {},
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 = function (staticValue) {
3
- let {
4
- warn = false,
5
- name = 'input'
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(_ref => {
23
- let {
24
- value
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(_ref2 => {
44
- let {
45
- value
46
- } = _ref2;
47
- return useStaticInput(value, {
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(_ref3 => {
72
- let {
73
- value
74
- } = _ref3;
75
- return useStaticInput(value, {
76
- warn: true,
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(_ref4 => {
97
- let {
98
- value
99
- } = _ref4;
100
- return useStaticInput(value, {
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(_ref5 => {
133
- let {
134
- value
135
- } = _ref5;
136
- return useStaticInput(value, {
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
  }
@@ -1,11 +1,3 @@
1
- export { CustomDataProvider } from './components/CustomDataProvider';
2
- export { DataMutation } from './components/DataMutation';
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';
3
+ export {};
@@ -1,4 +1,3 @@
1
- export { DataQuery, DataMutation, DataProvider, CustomDataProvider, } from './react';
2
- export { useDataEngine, useDataQuery, useDataMutation } from './react';
3
- export { FetchError } from './engine';
1
+ export * from './react';
2
+ export { FetchError } from '@dhis2/data-engine';
4
3
  export type * from './types';
@@ -1,5 +1,5 @@
1
+ import type { CustomData, CustomLinkOptions } from '@dhis2/data-engine';
1
2
  import React from 'react';
2
- import { CustomData, CustomLinkOptions } from '../../links';
3
3
  interface CustomProviderInput {
4
4
  children: React.ReactNode;
5
5
  data: CustomData;
@@ -1,4 +1,4 @@
1
- import { Mutation, MutationOptions } from '../../engine';
1
+ import type { Mutation, MutationOptions } from '@dhis2/data-engine';
2
2
  import { MutationRenderInput } from '../../types';
3
3
  interface MutationInput extends MutationOptions {
4
4
  mutation: Mutation;
@@ -1,4 +1,4 @@
1
- import { Query, QueryOptions } from '../../engine';
1
+ import type { Query, QueryOptions } from '@dhis2/data-engine';
2
2
  import { QueryRenderInput } from '../../types';
3
3
  interface QueryInput extends QueryOptions {
4
4
  query: Query;
@@ -0,0 +1,4 @@
1
+ export { CustomDataProvider } from './CustomDataProvider';
2
+ export { DataMutation } from './DataMutation';
3
+ export { DataProvider } from './DataProvider';
4
+ export { DataQuery } from './DataQuery';
@@ -0,0 +1,4 @@
1
+ import { DataEngine } from '@dhis2/data-engine';
2
+ export declare const defaultDataContext: {
3
+ engine: DataEngine;
4
+ };
@@ -0,0 +1,3 @@
1
+ export { useDataEngine } from './useDataEngine';
2
+ export { useDataMutation } from './useDataMutation';
3
+ export { useDataQuery } from './useDataQuery';
@@ -1,4 +1,4 @@
1
- import type { QueryVariables } from '../../engine';
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("../../engine").DataEngine;
1
+ export declare const useDataEngine: () => import("@dhis2/data-engine").DataEngine;
@@ -1,3 +1,3 @@
1
- import { QueryOptions, Mutation } from '../../engine';
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 '../../engine';
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 { CustomDataProvider } from './components/CustomDataProvider';
2
- export { DataMutation } from './components/DataMutation';
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';
@@ -1,10 +1,5 @@
1
- import DataEngine from './engine/DataEngine';
2
- import { QueryExecuteOptions } from './engine/types/ExecuteOptions';
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.16.0",
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.16.0",
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;