@openfn/language-commcare 4.2.1 → 4.3.1

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/dist/index.cjs CHANGED
@@ -386,8 +386,64 @@ var import_util3 = require("@openfn/language-common/util");
386
386
  function post2(path, data, params = {}) {
387
387
  return request3("POST", path, data, params);
388
388
  }
389
- function get2(path, params = {}) {
390
- return request3("GET", path, null, params);
389
+ function get2(path, params = {}, callback) {
390
+ return async (state) => {
391
+ var _a;
392
+ const { domain } = state.configuration;
393
+ const [resolvedPath, resolvedParams] = (0, import_util3.expandReferences)(
394
+ state,
395
+ path,
396
+ params
397
+ );
398
+ const { paginate, ...requestParams } = resolvedParams;
399
+ const callbackMode = typeof callback === "function";
400
+ const url = buildUrl(resolvedPath, domain);
401
+ try {
402
+ let currentParams = { ...requestParams };
403
+ let results = null;
404
+ let nextState = state;
405
+ do {
406
+ const response = await request(state.configuration, url, {
407
+ method: "GET",
408
+ params: currentParams,
409
+ contentType: "application/json"
410
+ });
411
+ const body = response.body;
412
+ const items = Object.values(body).find(Array.isArray) ?? [];
413
+ if (callbackMode) {
414
+ const pageState = prepareNextState(state, response);
415
+ nextState = await callback({ ...pageState, data: items });
416
+ } else {
417
+ nextState = prepareNextState(state, response);
418
+ if (!results)
419
+ results = [];
420
+ results.push(...items);
421
+ }
422
+ const next = (body == null ? void 0 : body.next) ?? ((_a = body == null ? void 0 : body.meta) == null ? void 0 : _a.next);
423
+ const hasMore = paginate === true && next;
424
+ if (hasMore) {
425
+ if (body == null ? void 0 : body.next) {
426
+ const cursor2 = new URL(body.next).searchParams.get("cursor");
427
+ currentParams = { ...requestParams, cursor: cursor2 };
428
+ } else {
429
+ currentParams = {
430
+ ...requestParams,
431
+ offset: body.meta.offset + body.meta.limit,
432
+ limit: body.meta.limit
433
+ };
434
+ }
435
+ } else {
436
+ break;
437
+ }
438
+ } while (true);
439
+ return {
440
+ ...nextState,
441
+ data: callbackMode ? {} : results ?? nextState.data
442
+ };
443
+ } catch (error) {
444
+ throw error;
445
+ }
446
+ };
391
447
  }
392
448
  function request3(method, path, body, params = {}) {
393
449
  return async (state) => {
package/dist/index.js CHANGED
@@ -357,8 +357,64 @@ import { expandReferences as expandReferences2 } from "@openfn/language-common/u
357
357
  function post2(path, data, params = {}) {
358
358
  return request3("POST", path, data, params);
359
359
  }
360
- function get2(path, params = {}) {
361
- return request3("GET", path, null, params);
360
+ function get2(path, params = {}, callback) {
361
+ return async (state) => {
362
+ var _a;
363
+ const { domain } = state.configuration;
364
+ const [resolvedPath, resolvedParams] = expandReferences2(
365
+ state,
366
+ path,
367
+ params
368
+ );
369
+ const { paginate, ...requestParams } = resolvedParams;
370
+ const callbackMode = typeof callback === "function";
371
+ const url = buildUrl(resolvedPath, domain);
372
+ try {
373
+ let currentParams = { ...requestParams };
374
+ let results = null;
375
+ let nextState = state;
376
+ do {
377
+ const response = await request(state.configuration, url, {
378
+ method: "GET",
379
+ params: currentParams,
380
+ contentType: "application/json"
381
+ });
382
+ const body = response.body;
383
+ const items = Object.values(body).find(Array.isArray) ?? [];
384
+ if (callbackMode) {
385
+ const pageState = prepareNextState(state, response);
386
+ nextState = await callback({ ...pageState, data: items });
387
+ } else {
388
+ nextState = prepareNextState(state, response);
389
+ if (!results)
390
+ results = [];
391
+ results.push(...items);
392
+ }
393
+ const next = (body == null ? void 0 : body.next) ?? ((_a = body == null ? void 0 : body.meta) == null ? void 0 : _a.next);
394
+ const hasMore = paginate === true && next;
395
+ if (hasMore) {
396
+ if (body == null ? void 0 : body.next) {
397
+ const cursor2 = new URL(body.next).searchParams.get("cursor");
398
+ currentParams = { ...requestParams, cursor: cursor2 };
399
+ } else {
400
+ currentParams = {
401
+ ...requestParams,
402
+ offset: body.meta.offset + body.meta.limit,
403
+ limit: body.meta.limit
404
+ };
405
+ }
406
+ } else {
407
+ break;
408
+ }
409
+ } while (true);
410
+ return {
411
+ ...nextState,
412
+ data: callbackMode ? {} : results ?? nextState.data
413
+ };
414
+ } catch (error) {
415
+ throw error;
416
+ }
417
+ };
362
418
  }
363
419
  function request3(method, path, body, params = {}) {
364
420
  return async (state) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-commcare",
3
3
  "label": "CommCare",
4
- "version": "4.2.1",
4
+ "version": "4.3.1",
5
5
  "description": "OpenFn adaptor for CommCare (Dimagi)",
6
6
  "homepage": "https://docs.openfn.org",
7
7
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "js2xmlparser": "^1.0.0",
23
23
  "lodash-fp": "^0.10.4",
24
24
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",
25
- "@openfn/language-common": "3.3.4"
25
+ "@openfn/language-common": "3.3.5"
26
26
  },
27
27
  "devDependencies": {
28
28
  "assertion-error": "^1.1.0",
package/types/http.d.ts CHANGED
@@ -29,14 +29,24 @@ export function post(path: string, data: object, params?: any): Operation;
29
29
  * Make a GET request to CommCare. Use this to retrieve resources directly from the CommCare REST API.
30
30
  * @example <caption>Get cases using the v1 API (prefixed with /a/[domain]/api/)</caption>
31
31
  * http.get('case/v1');
32
+ * @example <caption>Filter cases by type using query parameters</caption>
33
+ * http.get('case/v1', { case_type: 'patient', limit: 100 });
34
+ * @example <caption>Paginate all results into state.data</caption>
35
+ * http.get('case/v2', { paginate: true });
36
+ * @example <caption>Stream each page to a callback without accumulating in memory</caption>
37
+ * http.get('case/v2', { paginate: true }, state => {
38
+ * console.log(state.data); // one page at a time
39
+ * return state;
40
+ * });
32
41
  * @function
33
42
  * @public
34
43
  * @param {string} path - Path to resource. Relative paths are prefixed with `/a/[domain]/api/`; paths starting with `/` are used as-is.
35
- * @param {Object} [params] - Optional request params
44
+ * @param {Object} [params] - Query parameters to append to the URL. All keys except `params.paginate` are sent as query strings (e.g. `{ case_type: 'patient' }` becomes `?case_type=patient`). Set `params.paginate` to `true` to enable automatic pagination.
45
+ * @param {Function} [callback] - Optional per-page callback. When provided alongside `params.paginate`, each page's records are passed as `state.data` and the full dataset is NOT accumulated. `state.data` will be `{}` when the operation completes.
36
46
  * @returns {Operation}
37
47
  * @state {CommCareState}
38
48
  */
39
- export function get(path: string, params?: any): Operation;
49
+ export function get(path: string, params?: any, callback?: Function): Operation;
40
50
  /**
41
51
  * Make a general HTTP request against the CommCare server. Use this to make any request to CommCare REST API.
42
52
  * @example <caption>Get cases using the v1 API (prefixed with /a/[domain]/api/)</caption>