@openfn/language-commcare 4.2.0 → 4.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/dist/index.cjs +58 -2
- package/dist/index.js +58 -2
- package/package.json +1 -1
- package/types/http.d.ts +17 -7
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
|
|
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
|
|
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
package/types/http.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
/**
|
|
10
10
|
* Make a POST request to CommCare. Use this to send resources directly to Commcare REST API.
|
|
11
11
|
* You can pass CommCare body data as a JSON object.
|
|
12
|
-
* @example <caption>Post to the V2 case API with JSON data (prefixed with /a
|
|
12
|
+
* @example <caption>Post to the V2 case API with JSON data (prefixed with /a/[domain]/api/)</caption>
|
|
13
13
|
* http.post('case/v2', {
|
|
14
14
|
* case_type: 'patient',
|
|
15
15
|
* case_name: 'Elizabeth Harmon',
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* });
|
|
19
19
|
* @function
|
|
20
20
|
* @public
|
|
21
|
-
* @param {string} path - Path to resource. Relative paths are prefixed with `/a
|
|
21
|
+
* @param {string} path - Path to resource. Relative paths are prefixed with `/a/[domain]/api/`; paths starting with `/` are used as-is.
|
|
22
22
|
* @param {object} data - Object or JSON to create a resource
|
|
23
23
|
* @param {Object} [params] - Optional request params
|
|
24
24
|
* @returns {Operation}
|
|
@@ -27,19 +27,29 @@
|
|
|
27
27
|
export function post(path: string, data: object, params?: any): Operation;
|
|
28
28
|
/**
|
|
29
29
|
* Make a GET request to CommCare. Use this to retrieve resources directly from the CommCare REST API.
|
|
30
|
-
* @example <caption>Get cases using the v1 API (prefixed with /a
|
|
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
|
-
* @param {string} path - Path to resource. Relative paths are prefixed with `/a
|
|
35
|
-
* @param {Object} [params] -
|
|
43
|
+
* @param {string} path - Path to resource. Relative paths are prefixed with `/a/[domain]/api/`; paths starting with `/` are used as-is.
|
|
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
|
-
* @example <caption>Get cases using the v1 API (prefixed with /a
|
|
52
|
+
* @example <caption>Get cases using the v1 API (prefixed with /a/[domain]/api/)</caption>
|
|
43
53
|
* http.request('GET', 'case/v1');
|
|
44
54
|
* @function
|
|
45
55
|
* @public
|