@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,171 +0,0 @@
1
- import { RestAPILink } from '../RestAPILink';
2
- import { queryToResourcePath } from './queryToResourcePath';
3
- const createLink = config => new RestAPILink(config);
4
- const defaultConfig = {
5
- basePath: '<base>',
6
- apiVersion: '37',
7
- serverVersion: {
8
- major: 2,
9
- minor: 37,
10
- patch: 11
11
- }
12
- };
13
- const link = createLink(defaultConfig);
14
- const apiPath = link.versionedApiPath;
15
- const actionPrefix = `dhis-web-commons/`;
16
- const actionPostfix = '.action';
17
- describe('queryToResourcePath', () => {
18
- describe('action', () => {
19
- it('should return action URL with no querystring if not query parameters are passed', () => {
20
- const query = {
21
- resource: 'action::test'
22
- };
23
- expect(queryToResourcePath(link, query, 'read')).toBe(`${actionPrefix}test${actionPostfix}`);
24
- });
25
- it('should return action URL with a simple querystring if query parameters are passed', () => {
26
- const query = {
27
- resource: 'action::test',
28
- params: {
29
- key: 'value'
30
- }
31
- };
32
- expect(queryToResourcePath(link, query, 'read')).toBe(`${actionPrefix}test${actionPostfix}?key=value`);
33
- });
34
- });
35
- describe('resource with dot', () => {
36
- it('should leave dots in resources', () => {
37
- const query = {
38
- resource: 'svg.pdf'
39
- };
40
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/svg.pdf`);
41
- });
42
- });
43
- it('should return resource url with no querystring if not query parameters are passed', () => {
44
- const query = {
45
- resource: 'test'
46
- };
47
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test`);
48
- });
49
- it('should return resource url and singular parameter separated by ?', () => {
50
- const query = {
51
- resource: 'test',
52
- params: {
53
- key: 'value'
54
- }
55
- };
56
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key=value`);
57
- });
58
- it('should return resource url and multiple parameters separated by ? and &', () => {
59
- const query = {
60
- resource: 'test',
61
- params: {
62
- key: 'value',
63
- param: 'value2'
64
- }
65
- };
66
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key=value&param=value2`);
67
- });
68
- it('should url encode special characters in query keys', () => {
69
- const query = {
70
- resource: 'test',
71
- params: {
72
- 'key=42&val': 'value'
73
- }
74
- };
75
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key%3D42%26val=value`);
76
- });
77
- it('should url encode special characters in string parameters', () => {
78
- const query = {
79
- resource: 'test',
80
- params: {
81
- key: 'value?=42',
82
- param: 'value2&& 53'
83
- }
84
- };
85
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key=value%3F%3D42&param=value2%26%26%2053`);
86
- });
87
- it('should support numeric (integer and float) parameters', () => {
88
- const query = {
89
- resource: 'test',
90
- params: {
91
- key: 42,
92
- param: 193.75
93
- }
94
- };
95
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key=42&param=193.75`);
96
- });
97
- it('should support boolean parameters', () => {
98
- const query = {
99
- resource: 'test',
100
- params: {
101
- key: 42,
102
- someflag: true
103
- }
104
- };
105
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key=42&someflag=true`);
106
- });
107
- it('should join array parameters with commas', () => {
108
- const query = {
109
- resource: 'test',
110
- params: {
111
- key: ['asdf', 123]
112
- }
113
- };
114
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?key=asdf,123`);
115
- });
116
- it('should include multiple filter parameters when array of filters provided', () => {
117
- const query = {
118
- resource: 'test',
119
- params: {
120
- filter: ['asdf', 123]
121
- }
122
- };
123
- expect(queryToResourcePath(link, query, 'read')).toBe(`${apiPath}/test?filter=asdf&filter=123`);
124
- });
125
- it('should NOT YET support name-aliased parameters', () => {
126
- const query = {
127
- resource: 'test',
128
- params: {
129
- key: {
130
- asdf: 'fdsa'
131
- }
132
- }
133
- };
134
- expect(() => queryToResourcePath(link, query, 'read')).toThrow();
135
- });
136
- it('should throw if passed something crazy like a function', () => {
137
- const query = {
138
- resource: 'test',
139
- params: {
140
- key: a => a
141
- }
142
- };
143
- expect(() => queryToResourcePath(link, query, 'read')).toThrow();
144
- });
145
- it('should return an unversioned endpoint for the new tracker importer (in version 2.37)', () => {
146
- const query = {
147
- resource: 'tracker'
148
- };
149
- expect(queryToResourcePath(link, query, 'read')).toBe(`${link.unversionedApiPath}/tracker`);
150
- });
151
- it('should return an unversioned endpoint sub-resources of the new tracker importer (in version 2.37)', () => {
152
- const query = {
153
- resource: 'tracker/test'
154
- };
155
- expect(queryToResourcePath(link, query, 'read')).toBe(`${link.unversionedApiPath}/tracker/test`);
156
- });
157
- it('should return a VERSIONED endpoint for the new tracker importer (in version 2.38)', () => {
158
- const query = {
159
- resource: 'tracker'
160
- };
161
- const v38config = {
162
- ...defaultConfig,
163
- serverVersion: {
164
- major: 2,
165
- minor: 38,
166
- patch: 0
167
- }
168
- };
169
- expect(queryToResourcePath(createLink(v38config), query, 'read')).toBe(`${link.versionedApiPath}/tracker`);
170
- });
171
- });
@@ -1,52 +0,0 @@
1
- import { normativeMetadataResources } from './metadataResources';
2
- const validatePagination = (query, warn) => {
3
- var _query$params, _query$params2;
4
- if (!normativeMetadataResources.includes(query.resource)) {
5
- return true;
6
- }
7
- if (((_query$params = query.params) === null || _query$params === void 0 ? void 0 : _query$params.paging) === false || ((_query$params2 = query.params) === null || _query$params2 === void 0 ? void 0 : _query$params2.paging) === 'false') {
8
- warn('Data queries with paging=false are deprecated and should not be used!', query);
9
- return false;
10
- }
11
-
12
- // TODO: validate sub-resource pagination (i.e. fields=users~paging(1,50)[name] )
13
-
14
- return true;
15
- };
16
- const validateDeclarativeFields = (query, warn) => {
17
- var _query$params3;
18
- if (!normativeMetadataResources.includes(query.resource)) {
19
- return true;
20
- }
21
- if (!((_query$params3 = query.params) !== null && _query$params3 !== void 0 && _query$params3.fields)) {
22
- warn('Data queries should always specify fields to return', query);
23
- return false;
24
- } else {
25
- var _fields;
26
- let fields = undefined;
27
- if (typeof query.params.fields === 'string') {
28
- fields = query.params.fields.split(',').map(field => field.trim());
29
- } else if (Array.isArray(query.params.fields)) {
30
- fields = query.params.fields.map(field => String(field).trim());
31
- }
32
- if ((_fields = fields) !== null && _fields !== void 0 && _fields.find(field => field.match(/(^\*$|^:.+)/))) {
33
- warn('Data queries should not use wildcard or dynamic field groups', query.params.fields, query);
34
- return false;
35
- }
36
- }
37
-
38
- // TODO: validate sub-resource wildcard fields (i.e. fields=users[*])
39
- return true;
40
- };
41
- export const validateResourceQuery = (query, type) => {
42
- let valid = true;
43
- if (process.env.NODE_ENV === 'development') {
44
- // Support build-time dead code elimination in production
45
- const warn = console.warn;
46
- if (type === 'read') {
47
- valid = validatePagination(query, warn) && valid;
48
- valid = validateDeclarativeFields(query, warn) && valid;
49
- }
50
- }
51
- return valid;
52
- };
@@ -1,207 +0,0 @@
1
- import { validateResourceQuery } from './validateQuery';
2
- describe('validateQuery', () => {
3
- const originalConsoleWarn = console.warn;
4
- afterAll(() => {
5
- console.warn = originalConsoleWarn;
6
- });
7
- it('Should return true and NOT console.warn for a valid query', () => {
8
- const warn = console.warn = jest.fn();
9
- process.env.NODE_ENV = 'development';
10
- expect(validateResourceQuery({
11
- resource: 'indicators',
12
- params: {
13
- fields: 'name'
14
- }
15
- }, 'read')).toBe(true);
16
- expect(console.warn).not.toHaveBeenCalled();
17
- expect(validateResourceQuery({
18
- resource: 'users',
19
- params: {
20
- fields: ['name', 'id'],
21
- pageSize: 35,
22
- page: 2
23
- }
24
- }, 'read')).toBe(true);
25
- expect(console.warn).not.toHaveBeenCalled();
26
- expect(validateResourceQuery({
27
- resource: 'userGroups',
28
- params: {
29
- fields: ['name', 'shortName'],
30
- paging: true,
31
- pageSize: 20
32
- }
33
- }, 'read')).toBe(true);
34
- expect(console.warn).not.toHaveBeenCalled();
35
- });
36
- it('Should return true for mutations', () => {
37
- const warn = console.warn = jest.fn();
38
- process.env.NODE_ENV = 'development';
39
- expect(validateResourceQuery({
40
- resource: 'trackedEntityInstances',
41
- params: {
42
- paging: false
43
- }
44
- }, 'create')).toBe(true);
45
- expect(console.warn).not.toHaveBeenCalled();
46
- expect(validateResourceQuery({
47
- resource: 'visualizations'
48
- }, 'update')).toBe(true);
49
- expect(console.warn).not.toHaveBeenCalled();
50
- expect(validateResourceQuery({
51
- resource: 'maps'
52
- }, 'delete')).toBe(true);
53
- expect(console.warn).not.toHaveBeenCalled();
54
- });
55
- it('Should skip validation for non-normative and non-metadata resources', () => {
56
- const warn = console.warn = jest.fn();
57
- process.env.NODE_ENV = 'development';
58
- expect(validateResourceQuery({
59
- resource: 'analytics',
60
- params: {
61
- fields: ['*']
62
- }
63
- }, 'read')).toBe(true);
64
- expect(console.warn).not.toHaveBeenCalled();
65
- expect(validateResourceQuery({
66
- resource: 'dataStore',
67
- params: {
68
- fields: ['*']
69
- }
70
- }, 'read')).toBe(true);
71
- expect(console.warn).not.toHaveBeenCalled();
72
- expect(validateResourceQuery({
73
- resource: 'icons',
74
- params: {
75
- fields: ['*']
76
- }
77
- }, 'read')).toBe(true);
78
- expect(console.warn).not.toHaveBeenCalled();
79
- expect(validateResourceQuery({
80
- resource: 'apps',
81
- params: {
82
- fields: ['*']
83
- }
84
- }, 'read')).toBe(true);
85
- expect(console.warn).not.toHaveBeenCalled();
86
- process.env.NODE_ENV = 'test';
87
- });
88
- it('Should return true and not warn in production mode', () => {
89
- console.warn = jest.fn();
90
- process.env.NODE_ENV = 'production';
91
- expect(validateResourceQuery({
92
- resource: 'programTrackedEntityAttributeGroups',
93
- params: {
94
- fields: ['*']
95
- }
96
- }, 'read')).toBe(true);
97
- expect(console.warn).not.toHaveBeenCalled();
98
- process.env.NODE_ENV = 'test';
99
- });
100
- it('Should return false and warn in development mode', () => {
101
- const warn = console.warn = jest.fn();
102
- process.env.NODE_ENV = 'development';
103
- expect(validateResourceQuery({
104
- resource: 'dataElements',
105
- params: {
106
- fields: ['*']
107
- }
108
- }, 'read')).toBe(false);
109
- expect(warn).toHaveBeenCalledTimes(1);
110
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should not use wildcard or dynamic field groups"`);
111
- process.env.NODE_ENV = 'test';
112
- });
113
- it('Should detect missing fields', () => {
114
- const warn = console.warn = jest.fn();
115
- process.env.NODE_ENV = 'development';
116
- expect(validateResourceQuery({
117
- resource: 'mapViews'
118
- }, 'read')).toBe(false);
119
- expect(warn).toHaveBeenCalledTimes(1);
120
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should always specify fields to return"`);
121
- process.env.NODE_ENV = 'test';
122
- });
123
- it('Should detect paging false', () => {
124
- const warn = console.warn = jest.fn();
125
- process.env.NODE_ENV = 'development';
126
- expect(validateResourceQuery({
127
- resource: 'attributes',
128
- params: {
129
- fields: ['name'],
130
- paging: false
131
- }
132
- }, 'read')).toBe(false);
133
- expect(warn).toHaveBeenCalledTimes(1);
134
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries with paging=false are deprecated and should not be used!"`);
135
- expect(validateResourceQuery({
136
- resource: 'optionSets',
137
- params: {
138
- fields: ['name'],
139
- paging: 'false'
140
- }
141
- }, 'read')).toBe(false);
142
- expect(warn).toHaveBeenCalledTimes(1);
143
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries with paging=false are deprecated and should not be used!"`);
144
- process.env.NODE_ENV = 'test';
145
- });
146
- it('Should detect wildcard fields', () => {
147
- const warn = console.warn = jest.fn();
148
- process.env.NODE_ENV = 'development';
149
- expect(validateResourceQuery({
150
- resource: 'categoryOptionCombos',
151
- params: {
152
- fields: ['*']
153
- }
154
- }, 'read')).toBe(false);
155
- expect(warn).toHaveBeenCalledTimes(1);
156
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should not use wildcard or dynamic field groups"`);
157
- expect(validateResourceQuery({
158
- resource: 'organisationUnits',
159
- params: {
160
- fields: 'test, :all, something'
161
- }
162
- }, 'read')).toBe(false);
163
- expect(warn).toHaveBeenCalledTimes(1);
164
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should not use wildcard or dynamic field groups"`);
165
- expect(validateResourceQuery({
166
- resource: 'organisationUnitGroups',
167
- params: {
168
- fields: ['asdf', ':owner']
169
- }
170
- }, 'read')).toBe(false);
171
- expect(warn).toHaveBeenCalledTimes(1);
172
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should not use wildcard or dynamic field groups"`);
173
- expect(validateResourceQuery({
174
- resource: 'dashboards',
175
- params: {
176
- fields: ['*', ':owner', ':all']
177
- }
178
- }, 'read')).toBe(false);
179
- expect(warn).toHaveBeenCalledTimes(1);
180
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should not use wildcard or dynamic field groups"`);
181
- process.env.NODE_ENV = 'test';
182
- });
183
- it('Should warn for multiple errors in one query', () => {
184
- const warn = console.warn = jest.fn();
185
- process.env.NODE_ENV = 'development';
186
- expect(validateResourceQuery({
187
- resource: 'dataSets',
188
- params: {
189
- fields: ['*'],
190
- paging: false
191
- }
192
- }, 'read')).toBe(false);
193
- expect(warn).toHaveBeenCalledTimes(2);
194
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries with paging=false are deprecated and should not be used!"`);
195
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should not use wildcard or dynamic field groups"`);
196
- expect(validateResourceQuery({
197
- resource: 'dashboardItems',
198
- params: {
199
- paging: false
200
- }
201
- }, 'read')).toBe(false);
202
- expect(warn).toHaveBeenCalledTimes(2);
203
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries with paging=false are deprecated and should not be used!"`);
204
- expect(warn.mock.calls.shift()[0]).toMatchInlineSnapshot(`"Data queries should always specify fields to return"`);
205
- process.env.NODE_ENV = 'test';
206
- });
207
- });
@@ -1,26 +0,0 @@
1
- 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; }
2
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
3
- 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); }
4
- import { fetchData } from './RestAPILink/fetchData';
5
- import { joinPath } from './RestAPILink/path';
6
- import { queryToRequestOptions } from './RestAPILink/queryToRequestOptions';
7
- import { queryToResourcePath } from './RestAPILink/queryToResourcePath';
8
- export class RestAPILink {
9
- constructor(config) {
10
- _defineProperty(this, "config", void 0);
11
- _defineProperty(this, "versionedApiPath", void 0);
12
- _defineProperty(this, "unversionedApiPath", void 0);
13
- this.config = config;
14
- this.versionedApiPath = joinPath('api', String(config.apiVersion));
15
- this.unversionedApiPath = joinPath('api');
16
- }
17
- fetch(path, options) {
18
- return fetchData(joinPath(this.config.baseUrl, path), options);
19
- }
20
- executeResourceQuery(type, query, _ref) {
21
- let {
22
- signal
23
- } = _ref;
24
- return this.fetch(queryToResourcePath(this, query, type), queryToRequestOptions(type, query, signal));
25
- }
26
- }
@@ -1,19 +0,0 @@
1
- import { fetchData } from './RestAPILink/fetchData';
2
- import { RestAPILink } from '.';
3
- jest.mock('./RestAPILink/fetchData', () => ({
4
- fetchData: jest.fn(async () => null)
5
- }));
6
- describe('RestAPILink', () => {
7
- it('should call fetch with the expected URL', async () => {
8
- const link = new RestAPILink({
9
- baseUrl: 'http://url',
10
- apiVersion: 42
11
- });
12
- await link.executeResourceQuery('read', {
13
- resource: 'something'
14
- }, {});
15
- expect(fetchData).toHaveBeenCalledWith('http://url/api/42/something', {
16
- method: 'GET'
17
- });
18
- });
19
- });
@@ -1,4 +0,0 @@
1
- export * from './CustomDataLink';
2
- export * from './ErrorLink';
3
- export * from './RestAPILink';
4
- export {};
@@ -1,3 +0,0 @@
1
- {
2
- "__MANIFEST_APP_TITLE_Application title": "@dhis2/app-service-data"
3
- }
@@ -1,13 +0,0 @@
1
- //------------------------------------------------------------------------------
2
- // <auto-generated>
3
- // This code was generated by d2-i18n-generate.
4
- //
5
- // Changes to this file may cause incorrect behavior and will be lost if
6
- // the code is regenerated.
7
- // </auto-generated>
8
- //------------------------------------------------------------------------------
9
- import i18n from '@dhis2/d2-i18n';
10
- import enTranslations from './en/translations.json';
11
- const namespace = 'default';
12
- i18n.addResources('en', namespace, enTranslations);
13
- export default i18n;
@@ -1,13 +0,0 @@
1
- import { DataEngineLink } from './types/DataEngineLink';
2
- import { QueryExecuteOptions } from './types/ExecuteOptions';
3
- import { JsonValue } from './types/JsonValue';
4
- import { Mutation } from './types/Mutation';
5
- import type { Query, ResourceQuery } from './types/Query';
6
- export declare class DataEngine {
7
- private link;
8
- constructor(link: DataEngineLink);
9
- query(query: Query, options?: QueryExecuteOptions): Promise<Record<keyof typeof query, unknown>>;
10
- query<T extends Record<string, unknown>>(query: Record<keyof T, ResourceQuery>, options?: QueryExecuteOptions): Promise<T>;
11
- mutate(mutation: Mutation, { variables, signal, onComplete, onError, }?: QueryExecuteOptions): Promise<JsonValue>;
12
- }
13
- export default DataEngine;
@@ -1,3 +0,0 @@
1
- import { FetchType } from '../types/ExecuteOptions';
2
- import { Mutation } from '../types/Mutation';
3
- export declare const getMutationFetchType: (mutation: Mutation) => FetchType;
@@ -1,2 +0,0 @@
1
- import { QueryVariables, ResourceQuery, ResolvedResourceQuery } from '../types/Query';
2
- export declare const resolveDynamicQuery: ({ resource, id, data, params }: ResourceQuery, variables: QueryVariables) => ResolvedResourceQuery;
@@ -1,4 +0,0 @@
1
- import { ResolvedResourceQuery } from '../types/Query';
2
- export declare const getResourceQueryErrors: (type: string, query: ResolvedResourceQuery) => string[];
3
- export declare const validateResourceQueries: (queries: ResolvedResourceQuery[], names?: string[]) => void;
4
- export declare const validateResourceQuery: (type: string, query: ResolvedResourceQuery) => void;
@@ -1,9 +0,0 @@
1
- export * from './DataEngine';
2
- export * from './types/DataEngineLink';
3
- export * from './types/ExecuteOptions';
4
- export * from './types/FetchError';
5
- export * from './types/JsonValue';
6
- export * from './types/Mutation';
7
- export * from './types/PossiblyDynamic';
8
- export * from './types/Query';
9
- export * from './types/QueryParameters';
@@ -1,9 +0,0 @@
1
- import { FetchType } from './ExecuteOptions';
2
- import { JsonValue } from './JsonValue';
3
- import { ResolvedResourceQuery } from './Query';
4
- export interface DataEngineLinkExecuteOptions {
5
- signal?: AbortSignal;
6
- }
7
- export interface DataEngineLink {
8
- executeResourceQuery: (type: FetchType, query: ResolvedResourceQuery, options: DataEngineLinkExecuteOptions) => Promise<JsonValue>;
9
- }
@@ -1,9 +0,0 @@
1
- import { FetchError } from './FetchError';
2
- import { QueryVariables } from './Query';
3
- export type FetchType = 'create' | 'read' | 'update' | 'json-patch' | 'replace' | 'delete';
4
- export interface QueryExecuteOptions {
5
- variables?: QueryVariables;
6
- signal?: AbortSignal;
7
- onComplete?: (data: any) => void;
8
- onError?: (error: FetchError) => void;
9
- }
@@ -1,19 +0,0 @@
1
- export type FetchErrorTypeName = 'network' | 'unknown' | 'access' | 'aborted';
2
- export type FetchErrorDetails = {
3
- httpStatus?: string;
4
- httpStatusCode?: number;
5
- status?: string;
6
- message?: string;
7
- errorCode?: string;
8
- [x: string]: any;
9
- };
10
- export interface FetchErrorPayload {
11
- type: FetchErrorTypeName;
12
- details?: FetchErrorDetails;
13
- message: string;
14
- }
15
- export declare class FetchError extends Error implements FetchErrorPayload {
16
- type: FetchErrorTypeName;
17
- details: FetchErrorDetails;
18
- constructor({ message, type, details }: FetchErrorPayload);
19
- }
@@ -1,5 +0,0 @@
1
- export declare class InvalidQueryError extends Error {
2
- type: string;
3
- details: string[];
4
- constructor(errors: string[]);
5
- }
@@ -1,6 +0,0 @@
1
- export type JsonValue = boolean | number | string | null | JsonArray | JsonMap;
2
- export interface JsonMap {
3
- [key: string]: JsonValue;
4
- }
5
- export interface JsonArray extends Array<JsonValue> {
6
- }
@@ -1,29 +0,0 @@
1
- import { FetchError } from './FetchError';
2
- import { ResourceQuery, QueryVariables } from './Query';
3
- export type MutationType = 'create' | 'update' | 'json-patch' | 'replace' | 'delete';
4
- export interface MutationData {
5
- [key: string]: any;
6
- }
7
- export interface BaseMutation extends ResourceQuery {
8
- type: MutationType;
9
- }
10
- export interface CreateMutation extends BaseMutation {
11
- type: 'create';
12
- data: MutationData;
13
- }
14
- export interface UpdateMutation extends BaseMutation {
15
- type: 'update' | 'replace' | 'json-patch';
16
- id: string;
17
- partial?: boolean;
18
- data: MutationData;
19
- }
20
- export interface DeleteMutation extends BaseMutation {
21
- type: 'delete';
22
- id: string;
23
- }
24
- export type Mutation = CreateMutation | UpdateMutation | DeleteMutation;
25
- export interface MutationOptions {
26
- variables?: QueryVariables;
27
- onError?: (error: FetchError) => any;
28
- onComplete?: (data: any) => any;
29
- }
@@ -1 +0,0 @@
1
- export type PossiblyDynamic<Type, InputType> = Type | ((input: InputType) => Type);
@@ -1,24 +0,0 @@
1
- import { FetchError } from './FetchError';
2
- import { JsonMap } from './JsonValue';
3
- import { PossiblyDynamic } from './PossiblyDynamic';
4
- import { QueryParameters } from './QueryParameters';
5
- export type QueryVariables = Record<string, any>;
6
- export interface ResourceQuery {
7
- resource: string;
8
- id?: PossiblyDynamic<string, QueryVariables>;
9
- data?: PossiblyDynamic<any, QueryVariables>;
10
- params?: PossiblyDynamic<QueryParameters, QueryVariables>;
11
- }
12
- export interface ResolvedResourceQuery extends ResourceQuery {
13
- id?: string;
14
- data?: any;
15
- params?: QueryParameters;
16
- }
17
- export type Query = Record<string, ResourceQuery>;
18
- export type QueryResult = JsonMap;
19
- export interface QueryOptions<TQueryResult = QueryResult> {
20
- variables?: QueryVariables;
21
- onComplete?: (data: TQueryResult) => void;
22
- onError?: (error: FetchError) => void;
23
- lazy?: boolean;
24
- }
@@ -1,12 +0,0 @@
1
- type QueryParameterSingularValue = string | number | boolean;
2
- interface QueryParameterAliasedValue {
3
- [name: string]: QueryParameterSingularValue;
4
- }
5
- type QueryParameterSingularOrAliasedValue = QueryParameterSingularValue | QueryParameterAliasedValue;
6
- type QueryParameterMultipleValue = QueryParameterSingularOrAliasedValue[];
7
- export type QueryParameterValue = QueryParameterSingularValue | QueryParameterAliasedValue | QueryParameterMultipleValue | undefined;
8
- export interface QueryParameters {
9
- pageSize?: number;
10
- [key: string]: QueryParameterValue;
11
- }
12
- export {};