@openfn/language-commcare 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ast.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "callback"
9
9
  ],
10
10
  "docs": {
11
- "description": "Make a GET request to any commcare endpoint. The returned objects will be written to state.data.\nA `response` key will be added to state with the HTTP response and a `meta` key",
11
+ "description": "Make a GET request to any commcare endpoint. The returned objects will be written to state.data.\nUnless an `offset` is passed, `get()` will automatically pull down all pages of data if the response\nis paginated.\nA `response` key will be added to state with the HTTP response and a `meta` key",
12
12
  "tags": [
13
13
  {
14
14
  "title": "public",
@@ -50,7 +50,7 @@
50
50
  },
51
51
  {
52
52
  "title": "param",
53
- "description": "Optional callback to handle the response",
53
+ "description": "Callback invoked once per page of data retrieved.",
54
54
  "type": {
55
55
  "type": "OptionalType",
56
56
  "expression": {
package/dist/index.cjs CHANGED
@@ -142,28 +142,62 @@ function execute(...operations) {
142
142
  }
143
143
  function get(path, params = {}, callback = (s) => s) {
144
144
  return async (state) => {
145
+ var _a, _b;
145
146
  const { domain } = state.configuration;
146
147
  const [resolvedPath, resolvedParams] = (0, import_util2.expandReferences)(
147
148
  state,
148
149
  path,
149
150
  params
150
151
  );
152
+ let offset, limit;
153
+ let nextState = state;
154
+ let result;
155
+ let allowPagination = isNaN(resolvedParams.offset);
151
156
  try {
152
- const response = await request(
153
- state.configuration,
154
- `/a/${domain}/api/v0.5/${resolvedPath}`,
155
- {
156
- method: "GET",
157
- params: resolvedParams,
158
- contentType: "application/json"
157
+ let requestParams = {
158
+ ...resolvedParams
159
+ };
160
+ do {
161
+ const response = await request(
162
+ state.configuration,
163
+ `/a/${domain}/api/v0.5/${resolvedPath}`,
164
+ {
165
+ method: "GET",
166
+ params: requestParams,
167
+ contentType: "application/json"
168
+ }
169
+ );
170
+ nextState = prepareNextState(state, response, callback);
171
+ if ((_b = (_a = response == null ? void 0 : response.body) == null ? void 0 : _a.meta) == null ? void 0 : _b.next) {
172
+ if (!result) {
173
+ result = [];
174
+ }
175
+ offset = response.body.meta.offset + response.body.meta.limit;
176
+ limit = response.body.meta.limit;
177
+ requestParams = {
178
+ ...requestParams,
179
+ offset,
180
+ limit
181
+ };
182
+ result.push(...nextState.data);
183
+ } else {
184
+ if (result) {
185
+ result.push(...nextState.data);
186
+ } else {
187
+ result = nextState.data;
188
+ }
189
+ break;
159
190
  }
160
- );
161
- return prepareNextState(state, response, callback);
191
+ } while (allowPagination);
192
+ return {
193
+ ...nextState,
194
+ data: result
195
+ };
162
196
  } catch (e) {
163
197
  if (e.statusCode === 404) {
164
198
  e.body = { error: `Resource ${path} not found` };
165
199
  }
166
- throw e.body.error ?? e;
200
+ throw e;
167
201
  }
168
202
  };
169
203
  }
package/dist/index.js CHANGED
@@ -116,28 +116,62 @@ function execute(...operations) {
116
116
  }
117
117
  function get(path, params = {}, callback = (s) => s) {
118
118
  return async (state) => {
119
+ var _a, _b;
119
120
  const { domain } = state.configuration;
120
121
  const [resolvedPath, resolvedParams] = expandReferences(
121
122
  state,
122
123
  path,
123
124
  params
124
125
  );
126
+ let offset, limit;
127
+ let nextState = state;
128
+ let result;
129
+ let allowPagination = isNaN(resolvedParams.offset);
125
130
  try {
126
- const response = await request(
127
- state.configuration,
128
- `/a/${domain}/api/v0.5/${resolvedPath}`,
129
- {
130
- method: "GET",
131
- params: resolvedParams,
132
- contentType: "application/json"
131
+ let requestParams = {
132
+ ...resolvedParams
133
+ };
134
+ do {
135
+ const response = await request(
136
+ state.configuration,
137
+ `/a/${domain}/api/v0.5/${resolvedPath}`,
138
+ {
139
+ method: "GET",
140
+ params: requestParams,
141
+ contentType: "application/json"
142
+ }
143
+ );
144
+ nextState = prepareNextState(state, response, callback);
145
+ if ((_b = (_a = response == null ? void 0 : response.body) == null ? void 0 : _a.meta) == null ? void 0 : _b.next) {
146
+ if (!result) {
147
+ result = [];
148
+ }
149
+ offset = response.body.meta.offset + response.body.meta.limit;
150
+ limit = response.body.meta.limit;
151
+ requestParams = {
152
+ ...requestParams,
153
+ offset,
154
+ limit
155
+ };
156
+ result.push(...nextState.data);
157
+ } else {
158
+ if (result) {
159
+ result.push(...nextState.data);
160
+ } else {
161
+ result = nextState.data;
162
+ }
163
+ break;
133
164
  }
134
- );
135
- return prepareNextState(state, response, callback);
165
+ } while (allowPagination);
166
+ return {
167
+ ...nextState,
168
+ data: result
169
+ };
136
170
  } catch (e) {
137
171
  if (e.statusCode === 404) {
138
172
  e.body = { error: `Resource ${path} not found` };
139
173
  }
140
- throw e.body.error ?? e;
174
+ throw e;
141
175
  }
142
176
  };
143
177
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-commcare",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Commcare Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -20,9 +20,8 @@
20
20
  "JSONPath": "^0.10.0",
21
21
  "js2xmlparser": "^1.0.0",
22
22
  "lodash-fp": "^0.10.4",
23
- "sinon": "^18.0.0",
24
23
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz",
25
- "@openfn/language-common": "1.14.0"
24
+ "@openfn/language-common": "1.15.0"
26
25
  },
27
26
  "devDependencies": {
28
27
  "@openfn/simple-ast": "^0.4.1",
@@ -36,6 +36,8 @@
36
36
  export function execute(...operations: Operations): Operation;
37
37
  /**
38
38
  * Make a GET request to any commcare endpoint. The returned objects will be written to state.data.
39
+ * Unless an `offset` is passed, `get()` will automatically pull down all pages of data if the response
40
+ * is paginated.
39
41
  * A `response` key will be added to state with the HTTP response and a `meta` key
40
42
  * @public
41
43
  * @example <caption>Get a list of cases</caption>
@@ -45,7 +47,7 @@ export function execute(...operations: Operations): Operation;
45
47
  * @function
46
48
  * @param {string} path - Path to resource
47
49
  * @param {RequestQueries} params - Optional request params such as limit and offset.
48
- * @param {function} [callback] - Optional callback to handle the response
50
+ * @param {function} [callback] - Callback invoked once per page of data retrieved.
49
51
  * @returns {Operation}
50
52
  */
51
53
  export function get(path: string, params?: RequestQueries, callback?: Function): Operation;