@openfn/language-commcare 4.1.2 → 4.2.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 CHANGED
@@ -43,6 +43,7 @@ __export(src_exports, {
43
43
  fn: () => import_language_common3.fn,
44
44
  fnIf: () => import_language_common3.fnIf,
45
45
  get: () => get,
46
+ http: () => http_exports,
46
47
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
47
48
  log: () => import_language_common3.log,
48
49
  map: () => import_language_common3.map,
@@ -94,6 +95,16 @@ var import_xlsx = __toESM(require("xlsx"), 1);
94
95
  // src/Utils.js
95
96
  var import_language_common = require("@openfn/language-common");
96
97
  var import_util = require("@openfn/language-common/util");
98
+ var buildUrl = (url, domain) => {
99
+ let finalUrl = "";
100
+ const absoluteUrl = url.startsWith("/");
101
+ if (absoluteUrl) {
102
+ finalUrl = url;
103
+ } else {
104
+ finalUrl = `/a/${domain}/api/${url}`;
105
+ }
106
+ return finalUrl;
107
+ };
97
108
  var configureAuth = (auth, headers = {}) => {
98
109
  if ("apiKey" in auth) {
99
110
  Object.assign(headers, {
@@ -364,6 +375,35 @@ function bulk(type, data, params) {
364
375
  };
365
376
  }
366
377
 
378
+ // src/http.js
379
+ var http_exports = {};
380
+ __export(http_exports, {
381
+ get: () => get2,
382
+ post: () => post2,
383
+ request: () => request3
384
+ });
385
+ var import_util3 = require("@openfn/language-common/util");
386
+ function post2(path, data, params = {}) {
387
+ return request3("POST", path, data, params);
388
+ }
389
+ function get2(path, params = {}) {
390
+ return request3("GET", path, null, params);
391
+ }
392
+ function request3(method, path, body, params = {}) {
393
+ return async (state) => {
394
+ const { domain } = state.configuration;
395
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedParams] = (0, import_util3.expandReferences)(state, method, path, body, params);
396
+ const url = buildUrl(resolvedPath, domain);
397
+ const response = await request(state.configuration, url, {
398
+ method: resolvedMethod,
399
+ data: resolvedBody,
400
+ params: resolvedParams,
401
+ contentType: "application/json"
402
+ });
403
+ return prepareNextState(state, response);
404
+ };
405
+ }
406
+
367
407
  // src/index.js
368
408
  var src_default = Adaptor_exports;
369
409
  // Annotate the CommonJS export names for ESM import in node:
@@ -385,6 +425,7 @@ var src_default = Adaptor_exports;
385
425
  fn,
386
426
  fnIf,
387
427
  get,
428
+ http,
388
429
  lastReferenceValue,
389
430
  log,
390
431
  map,
package/dist/index.js CHANGED
@@ -47,6 +47,16 @@ import {
47
47
  makeBasicAuthHeader,
48
48
  logResponse
49
49
  } from "@openfn/language-common/util";
50
+ var buildUrl = (url, domain) => {
51
+ let finalUrl = "";
52
+ const absoluteUrl = url.startsWith("/");
53
+ if (absoluteUrl) {
54
+ finalUrl = url;
55
+ } else {
56
+ finalUrl = `/a/${domain}/api/${url}`;
57
+ }
58
+ return finalUrl;
59
+ };
50
60
  var configureAuth = (auth, headers = {}) => {
51
61
  if ("apiKey" in auth) {
52
62
  Object.assign(headers, {
@@ -336,6 +346,35 @@ function bulk(type, data, params) {
336
346
  };
337
347
  }
338
348
 
349
+ // src/http.js
350
+ var http_exports = {};
351
+ __export(http_exports, {
352
+ get: () => get2,
353
+ post: () => post2,
354
+ request: () => request3
355
+ });
356
+ import { expandReferences as expandReferences2 } from "@openfn/language-common/util";
357
+ function post2(path, data, params = {}) {
358
+ return request3("POST", path, data, params);
359
+ }
360
+ function get2(path, params = {}) {
361
+ return request3("GET", path, null, params);
362
+ }
363
+ function request3(method, path, body, params = {}) {
364
+ return async (state) => {
365
+ const { domain } = state.configuration;
366
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedParams] = expandReferences2(state, method, path, body, params);
367
+ const url = buildUrl(resolvedPath, domain);
368
+ const response = await request(state.configuration, url, {
369
+ method: resolvedMethod,
370
+ data: resolvedBody,
371
+ params: resolvedParams,
372
+ contentType: "application/json"
373
+ });
374
+ return prepareNextState(state, response);
375
+ };
376
+ }
377
+
339
378
  // src/index.js
340
379
  var src_default = Adaptor_exports;
341
380
  export {
@@ -357,6 +396,7 @@ export {
357
396
  fn,
358
397
  fnIf,
359
398
  get,
399
+ http_exports as http,
360
400
  lastReferenceValue,
361
401
  log,
362
402
  map,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-commcare",
3
3
  "label": "CommCare",
4
- "version": "4.1.2",
4
+ "version": "4.2.0",
5
5
  "description": "OpenFn adaptor for CommCare (Dimagi)",
6
6
  "homepage": "https://docs.openfn.org",
7
7
  "repository": {
package/types/Utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export function request(configuration: any, path: any, opts: any): Promise<any>;
2
+ export function buildUrl(url: any, domain: any): string;
2
3
  export function configureAuth(auth: any, headers?: {}): {};
3
4
  export function prepareNextState(state: any, response: any, callback?: (s: any) => any): any;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * State object
3
+ * @typedef {Object} CommCareState
4
+ * @property data - The response body (as JSON)
5
+ * @property response - The HTTP response from the CommCare server (excluding the body)
6
+ * @property references - An array of all previous data objects used in the Job
7
+ * @private
8
+ */
9
+ /**
10
+ * Make a POST request to CommCare. Use this to send resources directly to Commcare REST API.
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/<domain>/api/)</caption>
13
+ * http.post('case/v2', {
14
+ * case_type: 'patient',
15
+ * case_name: 'Elizabeth Harmon',
16
+ * owner_id: '20cc9dda-b90a-4af3-aa3d-fc67184e73ef',
17
+ * properties: { dob: '1948-11-02' },
18
+ * });
19
+ * @function
20
+ * @public
21
+ * @param {string} path - Path to resource. Relative paths are prefixed with `/a/<domain>/api/`; paths starting with `/` are used as-is.
22
+ * @param {object} data - Object or JSON to create a resource
23
+ * @param {Object} [params] - Optional request params
24
+ * @returns {Operation}
25
+ * @state {CommCareState}
26
+ */
27
+ export function post(path: string, data: object, params?: any): Operation;
28
+ /**
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/<domain>/api/)</caption>
31
+ * http.get('case/v1');
32
+ * @function
33
+ * @public
34
+ * @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
36
+ * @returns {Operation}
37
+ * @state {CommCareState}
38
+ */
39
+ export function get(path: string, params?: any): Operation;
40
+ /**
41
+ * 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/<domain>/api/)</caption>
43
+ * http.request('GET', 'case/v1');
44
+ * @function
45
+ * @public
46
+ * @param {string} method - HTTP method to use
47
+ * @param {string} path - Path to resource
48
+ * @param {object} body - Object which will be attached to the body
49
+ * @param {object} params - An object of query parameters to be encoded into the URL
50
+ * @returns {Operation}
51
+ * @state {CommCareState}
52
+ */
53
+ export function request(method: string, path: string, body: object, params?: object): Operation;
54
+ /**
55
+ * State object
56
+ */
57
+ export type CommCareState = {
58
+ /**
59
+ * - The response body (as JSON)
60
+ */
61
+ data: any;
62
+ /**
63
+ * - The HTTP response from the CommCare server (excluding the body)
64
+ */
65
+ response: any;
66
+ /**
67
+ * - An array of all previous data objects used in the Job
68
+ */
69
+ references: any;
70
+ };
package/types/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export default Adaptor;
2
2
  export * from "./Adaptor.js";
3
+ export * as http from "./http.js";
3
4
  import * as Adaptor from "./Adaptor.js";