@openfn/language-commcare 3.0.0 → 3.1.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/ast.json CHANGED
@@ -1,139 +1,5 @@
1
1
  {
2
2
  "operations": [
3
- {
4
- "name": "execute",
5
- "params": [
6
- "operations"
7
- ],
8
- "docs": {
9
- "description": "Queries provided to the GET request",
10
- "tags": [
11
- {
12
- "title": "typedef",
13
- "description": null,
14
- "type": {
15
- "type": "NameExpression",
16
- "name": "Object"
17
- },
18
- "name": "RequestQueries"
19
- },
20
- {
21
- "title": "public",
22
- "description": null,
23
- "type": null
24
- },
25
- {
26
- "title": "property",
27
- "description": "The maximum number of records to return. Default: 20. Maximum: 5000.",
28
- "type": {
29
- "type": "NameExpression",
30
- "name": "number"
31
- },
32
- "name": "limit"
33
- },
34
- {
35
- "title": "property",
36
- "description": "The number of records to offset in the results. Default: 0",
37
- "type": {
38
- "type": "NameExpression",
39
- "name": "number"
40
- },
41
- "name": "offset"
42
- },
43
- {
44
- "title": "property",
45
- "description": "Optional form XML namespace. See the [Commcare Docs](https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143979045/Finding+a+Form%27s+XMLNS)",
46
- "type": {
47
- "type": "NameExpression",
48
- "name": "string"
49
- },
50
- "name": "xmlns"
51
- },
52
- {
53
- "title": "property",
54
- "description": "Optional date (and time). Will return only forms that have had data modified since the passed in date.",
55
- "type": {
56
- "type": "NameExpression",
57
- "name": "string"
58
- },
59
- "name": "indexed_on_start"
60
- },
61
- {
62
- "title": "property",
63
- "description": "Optional date (and time). Will return only forms that have had data modified before the passed in date.",
64
- "type": {
65
- "type": "NameExpression",
66
- "name": "string"
67
- },
68
- "name": "indexed_on_end"
69
- },
70
- {
71
- "title": "property",
72
- "description": "Optional date (and time). Will return only forms that were received after the passed in date.",
73
- "type": {
74
- "type": "NameExpression",
75
- "name": "string"
76
- },
77
- "name": "received_on_start"
78
- },
79
- {
80
- "title": "property",
81
- "description": "Optional date (and time). Will return only forms that were received before the passed in date.",
82
- "type": {
83
- "type": "NameExpression",
84
- "name": "string"
85
- },
86
- "name": "received_on_end"
87
- },
88
- {
89
- "title": "property",
90
- "description": "The returned records will be limited to the application defined",
91
- "type": {
92
- "type": "NameExpression",
93
- "name": "string"
94
- },
95
- "name": "app_id"
96
- },
97
- {
98
- "title": "property",
99
- "description": "A case UUID. Will only return forms which updated that case.",
100
- "type": {
101
- "type": "NameExpression",
102
- "name": "string"
103
- },
104
- "name": "case_id"
105
- },
106
- {
107
- "title": "property",
108
- "description": "Optional user of group UUID used when getting cases",
109
- "type": {
110
- "type": "NameExpression",
111
- "name": "string"
112
- },
113
- "name": "owner_id"
114
- },
115
- {
116
- "title": "property",
117
- "description": "Optional UUID for all cases last modified by that user",
118
- "type": {
119
- "type": "NameExpression",
120
- "name": "string"
121
- },
122
- "name": "user_id"
123
- },
124
- {
125
- "title": "property",
126
- "description": "Optional case type to get all matching cases",
127
- "type": {
128
- "type": "NameExpression",
129
- "name": "string"
130
- },
131
- "name": "type"
132
- }
133
- ]
134
- },
135
- "valid": false
136
- },
137
3
  {
138
4
  "name": "get",
139
5
  "params": [
@@ -142,27 +8,37 @@
142
8
  "callback"
143
9
  ],
144
10
  "docs": {
145
- "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",
11
+ "description": "Make a GET request to any CommCare endpoint. The response body will be returned to `state.data` as JSON.\nPaginated responses will be fully downloaded and returned as a single array, _unless_ an `offset` is passed.",
146
12
  "tags": [
147
13
  {
148
14
  "title": "public",
149
15
  "description": null,
150
16
  "type": null
151
17
  },
18
+ {
19
+ "title": "function",
20
+ "description": null,
21
+ "name": null
22
+ },
152
23
  {
153
24
  "title": "example",
154
- "description": "get(\"case\", { limit: 20 })",
155
- "caption": "Get a list of cases"
25
+ "description": "get(\"/case/12345\")",
26
+ "caption": "Get a specific case by id"
156
27
  },
157
28
  {
158
29
  "title": "example",
159
- "description": "get(\"case/12345\")",
160
- "caption": "Get a specific case"
30
+ "description": "get(\"/case\", { offset:0, limit: 20 })",
31
+ "caption": "Get exactly 20 cases"
161
32
  },
162
33
  {
163
- "title": "function",
164
- "description": null,
165
- "name": null
34
+ "title": "example",
35
+ "description": "get(\"/form\", { app_id: \"02bf50ab803a89ea4963799362874f0c\" })",
36
+ "caption": "Get forms by app id"
37
+ },
38
+ {
39
+ "title": "example",
40
+ "description": "get(\"/case\", {}, (state) => {\n state.cases.push(...state.data) // adds 50 cases to the cases array\n return state;\n})",
41
+ "caption": "Get all cases, 50 at a time, and add them to state"
166
42
  },
167
43
  {
168
44
  "title": "param",
@@ -175,16 +51,19 @@
175
51
  },
176
52
  {
177
53
  "title": "param",
178
- "description": "Optional request params such as limit and offset.",
54
+ "description": "Input parameters for the request. These vary by endpoint, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143957366/Data+APIs CommCare docs}.",
179
55
  "type": {
180
- "type": "NameExpression",
181
- "name": "RequestQueries"
56
+ "type": "OptionalType",
57
+ "expression": {
58
+ "type": "NameExpression",
59
+ "name": "Object"
60
+ }
182
61
  },
183
62
  "name": "params"
184
63
  },
185
64
  {
186
65
  "title": "param",
187
- "description": "Callback invoked once per page of data retrieved.",
66
+ "description": "Optional callback function. Invoked once per page of data retrieved.",
188
67
  "type": {
189
68
  "type": "OptionalType",
190
69
  "expression": {
@@ -194,6 +73,10 @@
194
73
  },
195
74
  "name": "callback"
196
75
  },
76
+ {
77
+ "title": "state",
78
+ "description": "{CommcareHttpState}"
79
+ },
197
80
  {
198
81
  "title": "returns",
199
82
  "description": null,
@@ -215,11 +98,12 @@
215
98
  "callback"
216
99
  ],
217
100
  "docs": {
218
- "description": "Make a post request to commcare",
101
+ "description": "Make a POST request to any CommCare endpoint.",
219
102
  "tags": [
220
103
  {
221
104
  "title": "example",
222
- "description": "post( \"user\", { \"username\":\"test\", \"password\":\"somepassword\" })"
105
+ "description": "post(\"/user\", { \"username\":\"test\", \"password\":\"somepassword\" })",
106
+ "caption": "Post a user object to to the /user endpoint"
223
107
  },
224
108
  {
225
109
  "title": "function",
@@ -242,7 +126,7 @@
242
126
  },
243
127
  {
244
128
  "title": "param",
245
- "description": "Object or JSON which defines data that will be used to create a given instance of resource",
129
+ "description": "Object or JSON to use as the request body",
246
130
  "type": {
247
131
  "type": "NameExpression",
248
132
  "name": "object"
@@ -251,10 +135,13 @@
251
135
  },
252
136
  {
253
137
  "title": "param",
254
- "description": "Optional request params.",
138
+ "description": "Optional request params",
255
139
  "type": {
256
- "type": "NameExpression",
257
- "name": "Object"
140
+ "type": "OptionalType",
141
+ "expression": {
142
+ "type": "NameExpression",
143
+ "name": "Object"
144
+ }
258
145
  },
259
146
  "name": "params"
260
147
  },
@@ -277,6 +164,10 @@
277
164
  "type": "NameExpression",
278
165
  "name": "Operation"
279
166
  }
167
+ },
168
+ {
169
+ "title": "state",
170
+ "description": "{CommcareHttpState}"
280
171
  }
281
172
  ]
282
173
  },
@@ -285,44 +176,49 @@
285
176
  {
286
177
  "name": "submitXls",
287
178
  "params": [
288
- "formData",
179
+ "data",
289
180
  "params"
290
181
  ],
291
182
  "docs": {
292
- "description": "Convert form data to xls then submit.",
183
+ "description": "Bulk upload data to CommCare. Accepts an array of objects, converts them into\nan XLS representation, and uploads.",
293
184
  "tags": [
294
185
  {
295
186
  "title": "public",
296
187
  "description": null,
297
188
  "type": null
298
189
  },
299
- {
300
- "title": "example",
301
- "description": "submitXls(\n [\n {name: 'Mamadou', phone: '000000'},\n ],\n {\n case_type: 'student',\n search_field: 'external_id',\n create_new_cases: 'on',\n }\n)"
302
- },
303
190
  {
304
191
  "title": "function",
305
192
  "description": null,
306
193
  "name": null
307
194
  },
195
+ {
196
+ "title": "example",
197
+ "description": "submitXls(\n [\n {name: 'Mamadou', phone: '000000'},\n ],\n {\n case_type: 'student',\n search_field: 'external_id',\n create_new_cases: 'on',\n }\n)",
198
+ "caption": "Upload a single row of data"
199
+ },
308
200
  {
309
201
  "title": "param",
310
- "description": "Object including form data.",
202
+ "description": "Array of objects to upload",
311
203
  "type": {
312
204
  "type": "NameExpression",
313
- "name": "Object"
205
+ "name": "array"
314
206
  },
315
- "name": "formData"
207
+ "name": "data"
316
208
  },
317
209
  {
318
210
  "title": "param",
319
- "description": "Request params including case type.",
211
+ "description": "Input parameters, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143946459/Bulk+Upload+Case+Data CommCare docs}.",
320
212
  "type": {
321
213
  "type": "NameExpression",
322
- "name": "RequestOptions"
214
+ "name": "Object"
323
215
  },
324
216
  "name": "params"
325
217
  },
218
+ {
219
+ "title": "state",
220
+ "description": "data - the response from the CommCare Server"
221
+ },
326
222
  {
327
223
  "title": "returns",
328
224
  "description": null,
@@ -338,33 +234,38 @@
338
234
  {
339
235
  "name": "submit",
340
236
  "params": [
341
- "formData"
237
+ "data"
342
238
  ],
343
239
  "docs": {
344
- "description": "Submit form data",
240
+ "description": "Submit forms to CommCare. Accepts an array of JSON\nobjects, converts them into XML, and submits to CommCare as an x-form.",
345
241
  "tags": [
346
242
  {
347
243
  "title": "public",
348
244
  "description": null,
349
245
  "type": null
350
246
  },
351
- {
352
- "title": "example",
353
- "description": "submit(\n fields(\n field(\"@\", function(state) {\n return {\n \"xmlns\": \"http://openrosa.org/formdesigner/form-id-here\"\n };\n }),\n field(\"question1\", dataValue(\"answer1\")),\n field(\"question2\", \"Some answer here.\")\n )\n )"
354
- },
355
247
  {
356
248
  "title": "function",
357
249
  "description": null,
358
250
  "name": null
359
251
  },
252
+ {
253
+ "title": "example",
254
+ "description": "submit(\n fields(\n field(\"@\", (state) => ({\n \"xmlns\": `http://openrosa.org/formdesigner/${state.formId}`\n }),\n field(\"question1\", (state) => state.data.answer1),\n field(\"question2\", (state) => state.data.answer2),\n )\n )",
255
+ "caption": "Submit a form to CommCare"
256
+ },
360
257
  {
361
258
  "title": "param",
362
- "description": "Object including form data.",
259
+ "description": "The form as a JSON object",
363
260
  "type": {
364
261
  "type": "NameExpression",
365
262
  "name": "Object"
366
263
  },
367
- "name": "formData"
264
+ "name": "data"
265
+ },
266
+ {
267
+ "title": "state",
268
+ "description": "data - the response from the CommCare Server"
368
269
  },
369
270
  {
370
271
  "title": "returns",
@@ -386,7 +287,7 @@
386
287
  "postUrl"
387
288
  ],
388
289
  "docs": {
389
- "description": "Make a GET request to CommCare's Reports API\nand POST the response to somewhere else.",
290
+ "description": "Make a GET request to CommCare's Reports API\nand POST the response somewhere else.",
390
291
  "tags": [
391
292
  {
392
293
  "title": "public",
@@ -395,7 +296,8 @@
395
296
  },
396
297
  {
397
298
  "title": "example",
398
- "description": "fetchReportData(reportId, params, postUrl)"
299
+ "description": "fetchReportData(\n \"9aab0eeb88555a7b4568676883e7379a\",\n { limit: 10 },\n \"https://www.example.com/api/\"\n)",
300
+ "caption": "Fetch 10 records from a report and post them to example.com"
399
301
  },
400
302
  {
401
303
  "title": "function",
@@ -413,16 +315,16 @@
413
315
  },
414
316
  {
415
317
  "title": "param",
416
- "description": "Query params, incl: limit, offset, and any custom report filters.",
318
+ "description": "Input parameters for the request, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143957341/Download+Report+Data Commcare docs}.",
417
319
  "type": {
418
320
  "type": "NameExpression",
419
- "name": "RequestQueries"
321
+ "name": "Object"
420
322
  },
421
323
  "name": "params"
422
324
  },
423
325
  {
424
326
  "title": "param",
425
- "description": "Url to which the response object will be posted.",
327
+ "description": "URL to which the response object will be posted.",
426
328
  "type": {
427
329
  "type": "NameExpression",
428
330
  "name": "String"
@@ -1014,6 +916,92 @@
1014
916
  ]
1015
917
  },
1016
918
  "valid": true
919
+ },
920
+ {
921
+ "name": "cursor",
922
+ "params": [
923
+ "value",
924
+ "options"
925
+ ],
926
+ "docs": {
927
+ "description": "Sets a cursor property on state.\nSupports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,\nwhich will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones\nare not yet supported.\nYou can provide a formatter to customise the final cursor value, which is useful for normalising\ndifferent inputs. The custom formatter runs after natural language date conversion.\nSee the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}",
928
+ "tags": [
929
+ {
930
+ "title": "public",
931
+ "description": null,
932
+ "type": null
933
+ },
934
+ {
935
+ "title": "function",
936
+ "description": null,
937
+ "name": null
938
+ },
939
+ {
940
+ "title": "example",
941
+ "description": "cursor($.cursor, { defaultValue: 'today' })",
942
+ "caption": "Use a cursor from state if present, or else use the default value"
943
+ },
944
+ {
945
+ "title": "example",
946
+ "description": "cursor(22)",
947
+ "caption": "Use a pagination cursor"
948
+ },
949
+ {
950
+ "title": "param",
951
+ "description": "the cursor value. Usually an ISO date, natural language date, or page number",
952
+ "type": {
953
+ "type": "NameExpression",
954
+ "name": "any"
955
+ },
956
+ "name": "value"
957
+ },
958
+ {
959
+ "title": "param",
960
+ "description": "options to control the cursor.",
961
+ "type": {
962
+ "type": "NameExpression",
963
+ "name": "object"
964
+ },
965
+ "name": "options"
966
+ },
967
+ {
968
+ "title": "param",
969
+ "description": "set the cursor key. Will persist through the whole run.",
970
+ "type": {
971
+ "type": "NameExpression",
972
+ "name": "string"
973
+ },
974
+ "name": "options.key"
975
+ },
976
+ {
977
+ "title": "param",
978
+ "description": "the value to use if value is falsy",
979
+ "type": {
980
+ "type": "NameExpression",
981
+ "name": "any"
982
+ },
983
+ "name": "options.defaultValue"
984
+ },
985
+ {
986
+ "title": "param",
987
+ "description": "custom formatter for the final cursor value",
988
+ "type": {
989
+ "type": "NameExpression",
990
+ "name": "Function"
991
+ },
992
+ "name": "options.format"
993
+ },
994
+ {
995
+ "title": "returns",
996
+ "description": null,
997
+ "type": {
998
+ "type": "NameExpression",
999
+ "name": "Operation"
1000
+ }
1001
+ }
1002
+ ]
1003
+ },
1004
+ "valid": false
1017
1005
  }
1018
1006
  ]
1019
1007
  }
package/dist/index.cjs CHANGED
@@ -28,8 +28,10 @@ __export(src_exports, {
28
28
  alterState: () => import_language_common3.alterState,
29
29
  arrayToString: () => import_language_common3.arrayToString,
30
30
  combine: () => import_language_common3.combine,
31
+ cursor: () => import_language_common3.cursor,
31
32
  dataPath: () => import_language_common3.dataPath,
32
33
  dataValue: () => import_language_common3.dataValue,
34
+ dateFns: () => import_language_common3.dateFns,
33
35
  default: () => src_default,
34
36
  each: () => import_language_common3.each,
35
37
  execute: () => execute,
@@ -55,8 +57,10 @@ __export(Adaptor_exports, {
55
57
  alterState: () => import_language_common3.alterState,
56
58
  arrayToString: () => import_language_common3.arrayToString,
57
59
  combine: () => import_language_common3.combine,
60
+ cursor: () => import_language_common3.cursor,
58
61
  dataPath: () => import_language_common3.dataPath,
59
62
  dataValue: () => import_language_common3.dataValue,
63
+ dateFns: () => import_language_common3.dateFns,
60
64
  each: () => import_language_common3.each,
61
65
  execute: () => execute,
62
66
  fetchReportData: () => fetchReportData,
@@ -227,10 +231,10 @@ function post(path, data, params = {}, callback = (s) => s) {
227
231
  }
228
232
  };
229
233
  }
230
- function submitXls(formData, params) {
234
+ function submitXls(data, params) {
231
235
  return async (state) => {
232
236
  const { domain } = state.configuration;
233
- const [json] = (0, import_util2.expandReferences)(state, formData);
237
+ const [json] = (0, import_util2.expandReferences)(state, data);
234
238
  const { case_type, search_field, create_new_cases } = params;
235
239
  const path = `/a/${domain}/importer/excel/bulk_upload_api/`;
236
240
  const workbook = import_xlsx.default.utils.book_new();
@@ -238,15 +242,15 @@ function submitXls(formData, params) {
238
242
  const ws_name = "SheetJS";
239
243
  import_xlsx.default.utils.book_append_sheet(workbook, worksheet, ws_name);
240
244
  const buffer = import_xlsx.default.write(workbook, { type: "buffer", bookType: "xls" });
241
- const data = new FormData();
242
- data.append("file", new import_node_buffer.Blob([buffer]), "output.xls");
243
- data.append("case_type", case_type);
244
- data.append("search_field", search_field);
245
- data.append("create_new_cases", create_new_cases);
245
+ const form = new FormData();
246
+ form.append("file", new import_node_buffer.Blob([buffer]), "output.xls");
247
+ form.append("case_type", case_type);
248
+ form.append("search_field", search_field);
249
+ form.append("create_new_cases", create_new_cases);
246
250
  try {
247
251
  const response = await request(state.configuration, path, {
248
252
  method: "POST",
249
- data
253
+ data: form
250
254
  });
251
255
  return prepareNextState(state, response);
252
256
  } catch (e) {
@@ -254,9 +258,9 @@ function submitXls(formData, params) {
254
258
  }
255
259
  };
256
260
  }
257
- function submit(formData) {
261
+ function submit(data) {
258
262
  return async (state) => {
259
- const [jsonBody] = (0, import_util2.expandReferences)(state, formData);
263
+ const [jsonBody] = (0, import_util2.expandReferences)(state, data);
260
264
  const body = (0, import_js2xmlparser.default)("data", jsonBody);
261
265
  const { domain, appId } = state.configuration;
262
266
  const path = `/a/${domain}/receiver/${appId}/`;
@@ -298,8 +302,10 @@ var src_default = Adaptor_exports;
298
302
  alterState,
299
303
  arrayToString,
300
304
  combine,
305
+ cursor,
301
306
  dataPath,
302
307
  dataValue,
308
+ dateFns,
303
309
  each,
304
310
  execute,
305
311
  fetchReportData,
package/dist/index.js CHANGED
@@ -10,8 +10,10 @@ __export(Adaptor_exports, {
10
10
  alterState: () => alterState,
11
11
  arrayToString: () => arrayToString,
12
12
  combine: () => combine,
13
+ cursor: () => cursor,
13
14
  dataPath: () => dataPath,
14
15
  dataValue: () => dataValue,
16
+ dateFns: () => dateFns,
15
17
  each: () => each,
16
18
  execute: () => execute,
17
19
  fetchReportData: () => fetchReportData,
@@ -92,8 +94,10 @@ async function request(configuration, path, opts) {
92
94
  import {
93
95
  fn,
94
96
  fnIf,
97
+ cursor,
95
98
  alterState,
96
99
  arrayToString,
100
+ dateFns,
97
101
  combine,
98
102
  dataPath,
99
103
  dataValue,
@@ -201,10 +205,10 @@ function post(path, data, params = {}, callback = (s) => s) {
201
205
  }
202
206
  };
203
207
  }
204
- function submitXls(formData, params) {
208
+ function submitXls(data, params) {
205
209
  return async (state) => {
206
210
  const { domain } = state.configuration;
207
- const [json] = expandReferences(state, formData);
211
+ const [json] = expandReferences(state, data);
208
212
  const { case_type, search_field, create_new_cases } = params;
209
213
  const path = `/a/${domain}/importer/excel/bulk_upload_api/`;
210
214
  const workbook = xlsx.utils.book_new();
@@ -212,15 +216,15 @@ function submitXls(formData, params) {
212
216
  const ws_name = "SheetJS";
213
217
  xlsx.utils.book_append_sheet(workbook, worksheet, ws_name);
214
218
  const buffer = xlsx.write(workbook, { type: "buffer", bookType: "xls" });
215
- const data = new FormData();
216
- data.append("file", new Blob([buffer]), "output.xls");
217
- data.append("case_type", case_type);
218
- data.append("search_field", search_field);
219
- data.append("create_new_cases", create_new_cases);
219
+ const form = new FormData();
220
+ form.append("file", new Blob([buffer]), "output.xls");
221
+ form.append("case_type", case_type);
222
+ form.append("search_field", search_field);
223
+ form.append("create_new_cases", create_new_cases);
220
224
  try {
221
225
  const response = await request(state.configuration, path, {
222
226
  method: "POST",
223
- data
227
+ data: form
224
228
  });
225
229
  return prepareNextState(state, response);
226
230
  } catch (e) {
@@ -228,9 +232,9 @@ function submitXls(formData, params) {
228
232
  }
229
233
  };
230
234
  }
231
- function submit(formData) {
235
+ function submit(data) {
232
236
  return async (state) => {
233
- const [jsonBody] = expandReferences(state, formData);
237
+ const [jsonBody] = expandReferences(state, data);
234
238
  const body = js2xmlparser("data", jsonBody);
235
239
  const { domain, appId } = state.configuration;
236
240
  const path = `/a/${domain}/receiver/${appId}/`;
@@ -271,8 +275,10 @@ export {
271
275
  alterState,
272
276
  arrayToString,
273
277
  combine,
278
+ cursor,
274
279
  dataPath,
275
280
  dataValue,
281
+ dateFns,
276
282
  src_default as default,
277
283
  each,
278
284
  execute,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-commcare",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "description": "Commcare Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -21,7 +21,7 @@
21
21
  "js2xmlparser": "^1.0.0",
22
22
  "lodash-fp": "^0.10.4",
23
23
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz",
24
- "@openfn/language-common": "2.0.0"
24
+ "@openfn/language-common": "2.0.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@openfn/simple-ast": "^0.4.1",
@@ -37,6 +37,7 @@
37
37
  "exports": {
38
38
  ".": {
39
39
  "import": "./dist/index.js",
40
+ "types": "./types/index.d.ts",
40
41
  "require": "./dist/index.cjs"
41
42
  },
42
43
  "./package.json": "./package.json"
@@ -1,27 +1,9 @@
1
1
  /**
2
- * Queries provided to the GET request
3
- * @typedef {Object} RequestQueries
4
- * @public
5
- * @property {number} limit - The maximum number of records to return. Default: 20. Maximum: 5000.
6
- * @property {number} offset - The number of records to offset in the results. Default: 0
7
- * @property {string} xmlns - Optional form XML namespace. See the [Commcare Docs](https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143979045/Finding+a+Form%27s+XMLNS)
8
- * @property {string} indexed_on_start - Optional date (and time). Will return only forms that have had data modified since the passed in date.
9
- * @property {string} indexed_on_end - Optional date (and time). Will return only forms that have had data modified before the passed in date.
10
- * @property {string} received_on_start - Optional date (and time). Will return only forms that were received after the passed in date.
11
- * @property {string} received_on_end - Optional date (and time). Will return only forms that were received before the passed in date.
12
- * @property {string} app_id - The returned records will be limited to the application defined
13
- * @property {string} case_id - A case UUID. Will only return forms which updated that case.
14
- * @property {string} owner_id - Optional user of group UUID used when getting cases
15
- * @property {string} user_id - Optional UUID for all cases last modified by that user
16
- * @property {string} type - Optional case type to get all matching cases
17
- */
18
- /**
19
- * Queries provided to the submitXls request
20
- * @typedef {Object} RequestOptions
21
- * @public
22
- * @property {string} case_type - Optional case type
23
- * @property {string} search_field - Optional search field
24
- * @property {string} create_new_cases - Optional for allowing to create new cases. Default `:on`
2
+ * State object
3
+ * @typedef {Object} CommcareHttpState
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
25
7
  */
26
8
  /**
27
9
  * Execute a sequence of operations.
@@ -37,39 +19,48 @@
37
19
  */
38
20
  export function execute(...operations: Operations): Operation;
39
21
  /**
40
- * Make a GET request to any commcare endpoint. The returned objects will be written to state.data.
41
- * Unless an `offset` is passed, `get()` will automatically pull down all pages of data if the response
42
- * is paginated.
43
- * A `response` key will be added to state with the HTTP response and a `meta` key
22
+ * Make a GET request to any CommCare endpoint. The response body will be returned to `state.data` as JSON.
23
+ * Paginated responses will be fully downloaded and returned as a single array, _unless_ an `offset` is passed.
44
24
  * @public
45
- * @example <caption>Get a list of cases</caption>
46
- * get("case", { limit: 20 })
47
- * @example <caption>Get a specific case </caption>
48
- * get("case/12345")
49
25
  * @function
26
+ * @example <caption>Get a specific case by id</caption>
27
+ * get("/case/12345")
28
+ * @example <caption>Get exactly 20 cases</caption>
29
+ * get("/case", { offset:0, limit: 20 })
30
+ * @example <caption>Get forms by app id</caption>
31
+ * get("/form", { app_id: "02bf50ab803a89ea4963799362874f0c" })
32
+ * @example <caption>Get all cases, 50 at a time, and add them to state</caption>
33
+ * get("/case", {}, (state) => {
34
+ * state.cases.push(...state.data) // adds 50 cases to the cases array
35
+ * return state;
36
+ * })
50
37
  * @param {string} path - Path to resource
51
- * @param {RequestQueries} params - Optional request params such as limit and offset.
52
- * @param {function} [callback] - Callback invoked once per page of data retrieved.
38
+ * @param {Object} [params] - Input parameters for the request. These vary by endpoint, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143957366/Data+APIs CommCare docs}.
39
+ * @param {function} [callback] - Optional callback function. Invoked once per page of data retrieved.
40
+ * @state {CommcareHttpState}
53
41
  * @returns {Operation}
54
42
  */
55
- export function get(path: string, params?: RequestQueries, callback?: Function): Operation;
43
+ export function get(path: string, params?: any, callback?: Function): Operation;
56
44
  /**
57
- * Make a post request to commcare
58
- * @example
59
- * post( "user", { "username":"test", "password":"somepassword" })
45
+ * Make a POST request to any CommCare endpoint.
46
+ * @example <caption>Post a user object to to the /user endpoint</caption>
47
+ * post("/user", { "username":"test", "password":"somepassword" })
60
48
  * @function
61
49
  * @public
62
50
  * @param {string} path - Path to resource
63
- * @param {object} data - Object or JSON which defines data that will be used to create a given instance of resource
64
- * @param {Object} params - Optional request params.
51
+ * @param {object} data - Object or JSON to use as the request body
52
+ * @param {Object} [params] - Optional request params
65
53
  * @param {function} [callback] - Optional callback to handle the response
66
54
  * @returns {Operation}
55
+ * @state {CommcareHttpState}
67
56
  */
68
57
  export function post(path: string, data: object, params?: any, callback?: Function): Operation;
69
58
  /**
70
- * Convert form data to xls then submit.
59
+ * Bulk upload data to CommCare. Accepts an array of objects, converts them into
60
+ * an XLS representation, and uploads.
71
61
  * @public
72
- * @example
62
+ * @function
63
+ * @example <caption>Upload a single row of data</caption>
73
64
  * submitXls(
74
65
  * [
75
66
  * {name: 'Mamadou', phone: '000000'},
@@ -80,51 +71,64 @@ export function post(path: string, data: object, params?: any, callback?: Functi
80
71
  * create_new_cases: 'on',
81
72
  * }
82
73
  * )
83
- * @function
84
- * @param {Object} formData - Object including form data.
85
- * @param {RequestOptions} params - Request params including case type.
74
+ * @param {array} data - Array of objects to upload
75
+ * @param {Object} params - Input parameters, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143946459/Bulk+Upload+Case+Data CommCare docs}.
76
+ * @state data - the response from the CommCare Server
86
77
  * @returns {Operation}
87
78
  */
88
- export function submitXls(formData: any, params: RequestOptions): Operation;
79
+ export function submitXls(data: any[], params: any): Operation;
89
80
  /**
90
- * Submit form data
81
+ * Submit forms to CommCare. Accepts an array of JSON
82
+ * objects, converts them into XML, and submits to CommCare as an x-form.
91
83
  * @public
92
- * @example
84
+ * @function
85
+ * @example <caption>Submit a form to CommCare</caption>
93
86
  * submit(
94
87
  * fields(
95
- * field("@", function(state) {
96
- * return {
97
- * "xmlns": "http://openrosa.org/formdesigner/form-id-here"
98
- * };
88
+ * field("@", (state) => ({
89
+ * "xmlns": `http://openrosa.org/formdesigner/${state.formId}`
99
90
  * }),
100
- * field("question1", dataValue("answer1")),
101
- * field("question2", "Some answer here.")
91
+ * field("question1", (state) => state.data.answer1),
92
+ * field("question2", (state) => state.data.answer2),
102
93
  * )
103
94
  * )
104
- * @function
105
- * @param {Object} formData - Object including form data.
95
+ * @param {Object} data - The form as a JSON object
96
+ * @state data - the response from the CommCare Server
106
97
  * @returns {Operation}
107
98
  */
108
- export function submit(formData: any): Operation;
99
+ export function submit(data: any): Operation;
109
100
  /**
110
101
  * Make a GET request to CommCare's Reports API
111
- * and POST the response to somewhere else.
102
+ * and POST the response somewhere else.
112
103
  * @public
113
- * @example
114
- * fetchReportData(reportId, params, postUrl)
104
+ * @example <caption>Fetch 10 records from a report and post them to example.com</caption>
105
+ * fetchReportData(
106
+ * "9aab0eeb88555a7b4568676883e7379a",
107
+ * { limit: 10 },
108
+ * "https://www.example.com/api/"
109
+ * )
115
110
  * @function
116
111
  * @param {String} reportId - API name of the report.
117
- * @param {RequestQueries} params - Query params, incl: limit, offset, and any custom report filters.
118
- * @param {String} postUrl - Url to which the response object will be posted.
112
+ * @param {Object} params - Input parameters for the request, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143957341/Download+Report+Data Commcare docs}.
113
+ * @param {String} postUrl - URL to which the response object will be posted.
119
114
  * @returns {Operation}
120
115
  */
121
- export function fetchReportData(reportId: string, params: RequestQueries, postUrl: string): Operation;
122
- /**
123
- * Queries provided to the GET request
124
- */
125
- export type RequestQueries = any;
116
+ export function fetchReportData(reportId: string, params: any, postUrl: string): Operation;
126
117
  /**
127
- * Queries provided to the submitXls request
118
+ * State object
128
119
  */
129
- export type RequestOptions = any;
130
- export { fn, fnIf, alterState, arrayToString, combine, dataPath, dataValue, each, field, fields, http, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";
120
+ export type CommcareHttpState = {
121
+ /**
122
+ * - The response body (as JSON)
123
+ */
124
+ data: any;
125
+ /**
126
+ * - The HTTP response from the CommCare server (excluding the body)
127
+ */
128
+ response: any;
129
+ /**
130
+ * - An array of all previous data objects used in the Job
131
+ */
132
+ references: any;
133
+ };
134
+ export { fn, fnIf, cursor, alterState, arrayToString, dateFns, combine, dataPath, dataValue, each, field, fields, http, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";