@openfn/language-http 6.5.4 → 7.0.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,119 +1,11 @@
1
1
  {
2
2
  "operations": [
3
- {
4
- "name": "execute",
5
- "params": [
6
- "operations"
7
- ],
8
- "docs": {
9
- "description": "Options provided to the HTTP request",
10
- "tags": [
11
- {
12
- "title": "typedef",
13
- "description": null,
14
- "type": {
15
- "type": "NameExpression",
16
- "name": "Object"
17
- },
18
- "name": "RequestOptions"
19
- },
20
- {
21
- "title": "public",
22
- "description": null,
23
- "type": null
24
- },
25
- {
26
- "title": "property",
27
- "description": "body data to append to the request. JSON will be converted to a string (but a content-type header will not be attached to the request).",
28
- "type": {
29
- "type": "UnionType",
30
- "elements": [
31
- {
32
- "type": "NameExpression",
33
- "name": "object"
34
- },
35
- {
36
- "type": "NameExpression",
37
- "name": "string"
38
- }
39
- ]
40
- },
41
- "name": "body"
42
- },
43
- {
44
- "title": "property",
45
- "description": "Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors for this code.",
46
- "type": {
47
- "type": "NameExpression",
48
- "name": "object"
49
- },
50
- "name": "errors"
51
- },
52
- {
53
- "title": "property",
54
- "description": "Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.",
55
- "type": {
56
- "type": "NameExpression",
57
- "name": "object"
58
- },
59
- "name": "form"
60
- },
61
- {
62
- "title": "property",
63
- "description": "An object of query parameters to be encoded into the URL.",
64
- "type": {
65
- "type": "NameExpression",
66
- "name": "object"
67
- },
68
- "name": "query"
69
- },
70
- {
71
- "title": "property",
72
- "description": "An object of headers to append to the request.",
73
- "type": {
74
- "type": "NameExpression",
75
- "name": "object"
76
- },
77
- "name": "headers"
78
- },
79
- {
80
- "title": "property",
81
- "description": "Parse the response body as json, text or stream. By default will use the response headers.",
82
- "type": {
83
- "type": "NameExpression",
84
- "name": "string"
85
- },
86
- "name": "parseAs"
87
- },
88
- {
89
- "title": "property",
90
- "description": "Request timeout in ms. Default: 300 seconds.",
91
- "type": {
92
- "type": "NameExpression",
93
- "name": "number"
94
- },
95
- "name": "timeout"
96
- },
97
- {
98
- "title": "property",
99
- "description": "TLS/SSL authentication options. See https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions",
100
- "type": {
101
- "type": "NameExpression",
102
- "name": "object"
103
- },
104
- "name": "tls"
105
- }
106
- ]
107
- },
108
- "valid": false
109
- },
110
3
  {
111
4
  "name": "request",
112
5
  "params": [
113
6
  "method",
114
7
  "path",
115
- "params",
116
- "callback"
8
+ "options"
117
9
  ],
118
10
  "docs": {
119
11
  "description": "Make a HTTP request. If `configuration.baseUrl` is set, paths must be relative.",
@@ -125,7 +17,7 @@
125
17
  },
126
18
  {
127
19
  "title": "example",
128
- "description": "request(\n 'GET',\n '/myEndpoint',\n {\n query: {foo: 'bar', a: 1},\n headers: {'content-type': 'application/json'},\n }\n)"
20
+ "description": "request(\n 'GET',\n '/patient',\n {\n query: {foo: 'bar', a: 1},\n headers: {'content-type': 'application/json'},\n }\n)"
129
21
  },
130
22
  {
131
23
  "title": "function",
@@ -152,21 +44,12 @@
152
44
  },
153
45
  {
154
46
  "title": "param",
155
- "description": "Query, Headers and Authentication parameters",
47
+ "description": "Body, Query, Headers and Authentication parameters",
156
48
  "type": {
157
49
  "type": "NameExpression",
158
50
  "name": "RequestOptions"
159
51
  },
160
- "name": "params"
161
- },
162
- {
163
- "title": "param",
164
- "description": "(Optional) Callback function",
165
- "type": {
166
- "type": "NameExpression",
167
- "name": "function"
168
- },
169
- "name": "callback"
52
+ "name": "options"
170
53
  },
171
54
  {
172
55
  "title": "state",
@@ -188,8 +71,7 @@
188
71
  "name": "get",
189
72
  "params": [
190
73
  "path",
191
- "params",
192
- "callback"
74
+ "options"
193
75
  ],
194
76
  "docs": {
195
77
  "description": "Make a GET request. If `configuration.baseUrl` is set, paths must be relative.",
@@ -201,7 +83,7 @@
201
83
  },
202
84
  {
203
85
  "title": "example",
204
- "description": "get('/myEndpoint', {\n query: {foo: 'bar', a: 1},\n headers: {'content-type': 'application/json'},\n})"
86
+ "description": "get('/patient', {\n query: {foo: 'bar', a: 1},\n headers: {'content-type': 'application/json'},\n})"
205
87
  },
206
88
  {
207
89
  "title": "function",
@@ -219,21 +101,12 @@
219
101
  },
220
102
  {
221
103
  "title": "param",
222
- "description": "Query, Headers and Authentication parameters",
104
+ "description": "Body, Query, Headers and Authentication parameters",
223
105
  "type": {
224
106
  "type": "NameExpression",
225
107
  "name": "RequestOptions"
226
108
  },
227
- "name": "params"
228
- },
229
- {
230
- "title": "param",
231
- "description": "(Optional) Callback function",
232
- "type": {
233
- "type": "NameExpression",
234
- "name": "function"
235
- },
236
- "name": "callback"
109
+ "name": "options"
237
110
  },
238
111
  {
239
112
  "title": "state",
@@ -255,8 +128,8 @@
255
128
  "name": "post",
256
129
  "params": [
257
130
  "path",
258
- "params",
259
- "callback"
131
+ "data",
132
+ "options"
260
133
  ],
261
134
  "docs": {
262
135
  "description": "Make a POST request. If `configuration.baseUrl` is set, paths must be relative.",
@@ -268,7 +141,13 @@
268
141
  },
269
142
  {
270
143
  "title": "example",
271
- "description": "post('/myEndpoint', {\n body: {'foo': 'bar'},\n headers: {'content-type': 'application/json'},\n })"
144
+ "description": "post('/patient' $.data)",
145
+ "caption": "POST a resource with from state"
146
+ },
147
+ {
148
+ "title": "example",
149
+ "description": "post('/patient' $.data, {\n headers: { 'content-type': 'application/fhir+json' },\n})",
150
+ "caption": "POST a resource with custom headers"
272
151
  },
273
152
  {
274
153
  "title": "function",
@@ -286,21 +165,21 @@
286
165
  },
287
166
  {
288
167
  "title": "param",
289
- "description": "Body, Query, Headers and Authentication parameters",
168
+ "description": "Body data to append to the request. JSON will be converted to a string.",
290
169
  "type": {
291
170
  "type": "NameExpression",
292
- "name": "RequestOptions"
171
+ "name": "object"
293
172
  },
294
- "name": "params"
173
+ "name": "data"
295
174
  },
296
175
  {
297
176
  "title": "param",
298
- "description": "(Optional) Callback function",
177
+ "description": "Query, Headers and Authentication parameters",
299
178
  "type": {
300
179
  "type": "NameExpression",
301
- "name": "function"
180
+ "name": "RequestOptions"
302
181
  },
303
- "name": "callback"
182
+ "name": "options"
304
183
  },
305
184
  {
306
185
  "title": "state",
@@ -322,8 +201,8 @@
322
201
  "name": "put",
323
202
  "params": [
324
203
  "path",
325
- "params",
326
- "callback"
204
+ "data",
205
+ "options"
327
206
  ],
328
207
  "docs": {
329
208
  "description": "Make a PUT request. If `configuration.baseUrl` is set, paths must be relative.",
@@ -335,7 +214,13 @@
335
214
  },
336
215
  {
337
216
  "title": "example",
338
- "description": "put('/myEndpoint', {\n body: {'foo': 'bar'},\n headers: {'content-type': 'application/json'},\n })"
217
+ "description": "put('/patient', $.data)",
218
+ "caption": "PUT a resource from state"
219
+ },
220
+ {
221
+ "title": "example",
222
+ "description": "put('/patient', $.data, {\n headers: { 'content-type': 'application/fhir+json' },\n})",
223
+ "caption": "PUT a resource with custom headers"
339
224
  },
340
225
  {
341
226
  "title": "function",
@@ -353,21 +238,21 @@
353
238
  },
354
239
  {
355
240
  "title": "param",
356
- "description": "Body, Query, Headers and Auth parameters",
241
+ "description": "Body data to append to the request. JSON will be converted to a string.",
357
242
  "type": {
358
243
  "type": "NameExpression",
359
- "name": "RequestOptions"
244
+ "name": "object"
360
245
  },
361
- "name": "params"
246
+ "name": "data"
362
247
  },
363
248
  {
364
249
  "title": "param",
365
- "description": "(Optional) Callback function",
250
+ "description": "Query, Headers and Auth parameters",
366
251
  "type": {
367
252
  "type": "NameExpression",
368
- "name": "function"
253
+ "name": "RequestOptions"
369
254
  },
370
- "name": "callback"
255
+ "name": "options-null"
371
256
  },
372
257
  {
373
258
  "title": "state",
@@ -383,14 +268,14 @@
383
268
  }
384
269
  ]
385
270
  },
386
- "valid": true
271
+ "valid": false
387
272
  },
388
273
  {
389
274
  "name": "patch",
390
275
  "params": [
391
276
  "path",
392
- "params",
393
- "callback"
277
+ "data",
278
+ "options"
394
279
  ],
395
280
  "docs": {
396
281
  "description": "Make a PATCH request. If `configuration.baseUrl` is set, paths must be relative.",
@@ -402,7 +287,13 @@
402
287
  },
403
288
  {
404
289
  "title": "example",
405
- "description": "patch('/myEndpoint', {\n body: {'foo': 'bar'},\n headers: {'content-type': 'application/json'},\n })"
290
+ "description": "patch('/patient', $.data)",
291
+ "caption": "PATCH a resource from state"
292
+ },
293
+ {
294
+ "title": "example",
295
+ "description": "patch('/patient', $.data, {\n headers: { 'content-type': 'application/fhir+json' },\n})",
296
+ "caption": "PATCH a resource with custom headers"
406
297
  },
407
298
  {
408
299
  "title": "function",
@@ -420,21 +311,21 @@
420
311
  },
421
312
  {
422
313
  "title": "param",
423
- "description": "Body, Query, Headers and Auth parameters",
314
+ "description": "Body data to append to the request. JSON will be converted to a string.",
424
315
  "type": {
425
316
  "type": "NameExpression",
426
- "name": "RequestOptions"
317
+ "name": "object"
427
318
  },
428
- "name": "params"
319
+ "name": "data"
429
320
  },
430
321
  {
431
322
  "title": "param",
432
- "description": "(Optional) Callback function",
323
+ "description": "Query, Headers and Auth parameters",
433
324
  "type": {
434
325
  "type": "NameExpression",
435
- "name": "function"
326
+ "name": "RequestOptions"
436
327
  },
437
- "name": "callback"
328
+ "name": "options"
438
329
  },
439
330
  {
440
331
  "title": "state",
@@ -456,8 +347,7 @@
456
347
  "name": "del",
457
348
  "params": [
458
349
  "path",
459
- "params",
460
- "callback"
350
+ "options"
461
351
  ],
462
352
  "docs": {
463
353
  "description": "Make a DELETE request. If `configuration.baseUrl` is set, paths must be relative.",
@@ -469,7 +359,7 @@
469
359
  },
470
360
  {
471
361
  "title": "example",
472
- "description": "del(`/myendpoint/${state => state.data.id}`, {\n headers: {'content-type': 'application/json'}\n })"
362
+ "description": "del(`/myendpoint/${$.data.id}`)"
473
363
  },
474
364
  {
475
365
  "title": "function",
@@ -487,21 +377,12 @@
487
377
  },
488
378
  {
489
379
  "title": "param",
490
- "description": "Body, Query, Headers and Auth parameters",
380
+ "description": "Query, Headers and Auth parameters",
491
381
  "type": {
492
382
  "type": "NameExpression",
493
383
  "name": "RequestOptions"
494
384
  },
495
- "name": "params"
496
- },
497
- {
498
- "title": "param",
499
- "description": "(Optional) Callback function",
500
- "type": {
501
- "type": "NameExpression",
502
- "name": "function"
503
- },
504
- "name": "callback"
385
+ "name": "options"
505
386
  },
506
387
  {
507
388
  "title": "state",
@@ -522,9 +403,8 @@
522
403
  {
523
404
  "name": "parseXML",
524
405
  "params": [
525
- "body",
526
- "script",
527
- "callback"
406
+ "data",
407
+ "script"
528
408
  ],
529
409
  "docs": {
530
410
  "description": "Parse XML with the Cheerio parser",
@@ -540,7 +420,7 @@
540
420
  },
541
421
  {
542
422
  "title": "example",
543
- "description": "parseXML(\n (state) => state.response,\n ($) => {\n return $(\"table[class=your_table]\").parsetable(true, true, true);\n },\n (next) => ({ ...next, results: next.data.body })\n);",
423
+ "description": "parseXML(\n (state) => state.response,\n ($) => {\n return $(\"table[class=your_table]\").parsetable(true, true, true);\n },\n (next) => ({ ...next, results: next.data.data })\n);",
544
424
  "caption": "Using parseXML with a callback"
545
425
  },
546
426
  {
@@ -550,12 +430,12 @@
550
430
  },
551
431
  {
552
432
  "title": "param",
553
- "description": "data string to be parsed",
433
+ "description": "Body string to be parsed",
554
434
  "type": {
555
435
  "type": "NameExpression",
556
436
  "name": "String"
557
437
  },
558
- "name": "body"
438
+ "name": "data"
559
439
  },
560
440
  {
561
441
  "title": "param",
@@ -566,15 +446,6 @@
566
446
  },
567
447
  "name": "script"
568
448
  },
569
- {
570
- "title": "param",
571
- "description": "(Optional) Callback function",
572
- "type": {
573
- "type": "NameExpression",
574
- "name": "function"
575
- },
576
- "name": "callback"
577
- },
578
449
  {
579
450
  "title": "state",
580
451
  "description": "data - the parsed XML as a JSON object"
package/dist/index.cjs CHANGED
@@ -99,6 +99,7 @@ var import_language_common2 = require("@openfn/language-common");
99
99
  // src/util.js
100
100
  var util_exports = {};
101
101
  __export(util_exports, {
102
+ CONTENT_TYPES: () => CONTENT_TYPES,
102
103
  decode: () => import_util.decode,
103
104
  encode: () => import_util.encode,
104
105
  request: () => request,
@@ -144,7 +145,12 @@ var assertUrl = (pathOrUrl, baseUrl) => {
144
145
  throw e;
145
146
  }
146
147
  };
147
- function request(method, path, params, callback = (s) => s) {
148
+ var CONTENT_TYPES = {
149
+ xml: "application/xml",
150
+ json: "application/json",
151
+ string: "text/plain"
152
+ };
153
+ function request(method, path, params) {
148
154
  return (state) => {
149
155
  var _a;
150
156
  const [resolvedPath, resolvedParams = {}] = (0, import_util.expandReferences)(
@@ -152,15 +158,21 @@ function request(method, path, params, callback = (s) => s) {
152
158
  path,
153
159
  params
154
160
  );
155
- let { body, headers = {} } = resolvedParams;
156
- if (resolvedParams.json) {
157
- console.warn(
158
- "WARNING: The `json` option has been deprecated. Use `body` instead"
159
- );
160
- body = resolvedParams.json;
161
- }
162
- if (resolvedParams.form) {
163
- body = encodeFormBody(resolvedParams.form);
161
+ let { body, contentType = "json", headers = {} } = resolvedParams;
162
+ const contentTypeHeader = Object.keys(headers).find(
163
+ (key) => key.toLowerCase() === "content-type"
164
+ );
165
+ if (contentType === "form") {
166
+ delete headers[contentTypeHeader];
167
+ headers = { ...headers };
168
+ body = encodeFormBody(body);
169
+ } else if (contentTypeHeader) {
170
+ headers = { ...headers };
171
+ } else {
172
+ headers = {
173
+ ...headers,
174
+ "Content-Type": CONTENT_TYPES[contentType] || "application/json"
175
+ };
164
176
  }
165
177
  const baseUrl = (_a = state.configuration) == null ? void 0 : _a.baseUrl;
166
178
  assertUrl(resolvedPath, baseUrl);
@@ -183,18 +195,19 @@ function request(method, path, params, callback = (s) => s) {
183
195
  maxRedirections
184
196
  };
185
197
  return (0, import_util.request)(method, resolvedPath, options).then((response) => {
198
+ const { body: body2, ...responseWithoutBody } = response;
186
199
  (0, import_util.logResponse)(response);
187
200
  return {
188
- ...(0, import_language_common.composeNextState)(state, response.body),
189
- response
201
+ ...(0, import_language_common.composeNextState)(state, body2),
202
+ response: responseWithoutBody
190
203
  };
191
- }).then(callback).catch((err) => {
204
+ }).catch((err) => {
192
205
  (0, import_util.logResponse)(err);
193
206
  throw err;
194
207
  });
195
208
  };
196
209
  }
197
- function xmlParser(body, script, callback = (s) => s) {
210
+ function xmlParser(body, script) {
198
211
  return (state) => {
199
212
  const [resolvedBody] = (0, import_util.expandReferences)(state, body);
200
213
  const $ = cheerio.load(resolvedBody);
@@ -203,12 +216,12 @@ function xmlParser(body, script, callback = (s) => s) {
203
216
  const result = script($);
204
217
  try {
205
218
  const r = JSON.parse(result);
206
- return callback((0, import_language_common.composeNextState)(state, r));
219
+ return (0, import_language_common.composeNextState)(state, r);
207
220
  } catch (e) {
208
- return callback((0, import_language_common.composeNextState)(state, { body: result }));
221
+ return (0, import_language_common.composeNextState)(state, { body: result });
209
222
  }
210
223
  } else {
211
- return callback((0, import_language_common.composeNextState)(state, { body: resolvedBody }));
224
+ return (0, import_language_common.composeNextState)(state, { body: resolvedBody });
212
225
  }
213
226
  };
214
227
  }
@@ -224,26 +237,26 @@ function execute(...operations) {
224
237
  return (0, import_language_common2.execute)(...operations)({ ...initialState, ...state });
225
238
  };
226
239
  }
227
- function request2(method, path, params, callback) {
228
- return request(method, path, params, callback);
240
+ function request2(method, path, options) {
241
+ return request(method, path, options);
229
242
  }
230
- function get(path, params, callback) {
231
- return request("GET", path, params, callback);
243
+ function get(path, options) {
244
+ return request("GET", path, options);
232
245
  }
233
- function post(path, params, callback) {
234
- return request("POST", path, params, callback);
246
+ function post(path, data, options) {
247
+ return request("POST", path, { body: data, ...options });
235
248
  }
236
- function put(path, params, callback) {
237
- return request("PUT", path, params, callback);
249
+ function put(path, data, options) {
250
+ return request("PUT", path, { body: data, ...options });
238
251
  }
239
- function patch(path, params, callback) {
240
- return request("PATCH", path, params, callback);
252
+ function patch(path, data, options) {
253
+ return request("PATCH", path, { body: data, ...options });
241
254
  }
242
- function del(path, params, callback) {
243
- return request("DELETE", path, params, callback);
255
+ function del(path, options) {
256
+ return request("DELETE", path, options);
244
257
  }
245
- function parseXML(body, script, callback) {
246
- return xmlParser(body, script, callback);
258
+ function parseXML(data, script) {
259
+ return xmlParser({ body: data }, script);
247
260
  }
248
261
 
249
262
  // src/index.js
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@ import { execute as commonExecute } from "@openfn/language-common";
43
43
  // src/util.js
44
44
  var util_exports = {};
45
45
  __export(util_exports, {
46
+ CONTENT_TYPES: () => CONTENT_TYPES,
46
47
  decode: () => decode,
47
48
  encode: () => encode,
48
49
  request: () => request,
@@ -96,7 +97,12 @@ var assertUrl = (pathOrUrl, baseUrl) => {
96
97
  throw e;
97
98
  }
98
99
  };
99
- function request(method, path, params, callback = (s) => s) {
100
+ var CONTENT_TYPES = {
101
+ xml: "application/xml",
102
+ json: "application/json",
103
+ string: "text/plain"
104
+ };
105
+ function request(method, path, params) {
100
106
  return (state) => {
101
107
  var _a;
102
108
  const [resolvedPath, resolvedParams = {}] = expandReferences(
@@ -104,15 +110,21 @@ function request(method, path, params, callback = (s) => s) {
104
110
  path,
105
111
  params
106
112
  );
107
- let { body, headers = {} } = resolvedParams;
108
- if (resolvedParams.json) {
109
- console.warn(
110
- "WARNING: The `json` option has been deprecated. Use `body` instead"
111
- );
112
- body = resolvedParams.json;
113
- }
114
- if (resolvedParams.form) {
115
- body = encodeFormBody(resolvedParams.form);
113
+ let { body, contentType = "json", headers = {} } = resolvedParams;
114
+ const contentTypeHeader = Object.keys(headers).find(
115
+ (key) => key.toLowerCase() === "content-type"
116
+ );
117
+ if (contentType === "form") {
118
+ delete headers[contentTypeHeader];
119
+ headers = { ...headers };
120
+ body = encodeFormBody(body);
121
+ } else if (contentTypeHeader) {
122
+ headers = { ...headers };
123
+ } else {
124
+ headers = {
125
+ ...headers,
126
+ "Content-Type": CONTENT_TYPES[contentType] || "application/json"
127
+ };
116
128
  }
117
129
  const baseUrl = (_a = state.configuration) == null ? void 0 : _a.baseUrl;
118
130
  assertUrl(resolvedPath, baseUrl);
@@ -135,18 +147,19 @@ function request(method, path, params, callback = (s) => s) {
135
147
  maxRedirections
136
148
  };
137
149
  return commonRequest(method, resolvedPath, options).then((response) => {
150
+ const { body: body2, ...responseWithoutBody } = response;
138
151
  logResponse(response);
139
152
  return {
140
- ...composeNextState(state, response.body),
141
- response
153
+ ...composeNextState(state, body2),
154
+ response: responseWithoutBody
142
155
  };
143
- }).then(callback).catch((err) => {
156
+ }).catch((err) => {
144
157
  logResponse(err);
145
158
  throw err;
146
159
  });
147
160
  };
148
161
  }
149
- function xmlParser(body, script, callback = (s) => s) {
162
+ function xmlParser(body, script) {
150
163
  return (state) => {
151
164
  const [resolvedBody] = expandReferences(state, body);
152
165
  const $ = cheerio.load(resolvedBody);
@@ -155,12 +168,12 @@ function xmlParser(body, script, callback = (s) => s) {
155
168
  const result = script($);
156
169
  try {
157
170
  const r = JSON.parse(result);
158
- return callback(composeNextState(state, r));
171
+ return composeNextState(state, r);
159
172
  } catch (e) {
160
- return callback(composeNextState(state, { body: result }));
173
+ return composeNextState(state, { body: result });
161
174
  }
162
175
  } else {
163
- return callback(composeNextState(state, { body: resolvedBody }));
176
+ return composeNextState(state, { body: resolvedBody });
164
177
  }
165
178
  };
166
179
  }
@@ -199,26 +212,26 @@ function execute(...operations) {
199
212
  return commonExecute(...operations)({ ...initialState, ...state });
200
213
  };
201
214
  }
202
- function request2(method, path, params, callback) {
203
- return request(method, path, params, callback);
215
+ function request2(method, path, options) {
216
+ return request(method, path, options);
204
217
  }
205
- function get(path, params, callback) {
206
- return request("GET", path, params, callback);
218
+ function get(path, options) {
219
+ return request("GET", path, options);
207
220
  }
208
- function post(path, params, callback) {
209
- return request("POST", path, params, callback);
221
+ function post(path, data, options) {
222
+ return request("POST", path, { body: data, ...options });
210
223
  }
211
- function put(path, params, callback) {
212
- return request("PUT", path, params, callback);
224
+ function put(path, data, options) {
225
+ return request("PUT", path, { body: data, ...options });
213
226
  }
214
- function patch(path, params, callback) {
215
- return request("PATCH", path, params, callback);
227
+ function patch(path, data, options) {
228
+ return request("PATCH", path, { body: data, ...options });
216
229
  }
217
- function del(path, params, callback) {
218
- return request("DELETE", path, params, callback);
230
+ function del(path, options) {
231
+ return request("DELETE", path, options);
219
232
  }
220
- function parseXML(body, script, callback) {
221
- return xmlParser(body, script, callback);
233
+ function parseXML(data, script) {
234
+ return xmlParser({ body: data }, script);
222
235
  }
223
236
 
224
237
  // src/index.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-http",
3
- "version": "6.5.4",
3
+ "version": "7.0.1",
4
4
  "description": "An HTTP request language package for use with Open Function",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -28,10 +28,9 @@
28
28
  "dependencies": {
29
29
  "cheerio": "1.0.0-rc.12",
30
30
  "cheerio-tableparser": "^1.0.1",
31
- "@openfn/language-common": "2.3.0"
31
+ "@openfn/language-common": "2.3.1"
32
32
  },
33
33
  "devDependencies": {
34
- "@openfn/simple-ast": "0.4.1",
35
34
  "chai": "^4.3.10",
36
35
  "deep-eql": "4.1.1",
37
36
  "esno": "^0.16.3",
@@ -2,9 +2,9 @@
2
2
  * Options provided to the HTTP request
3
3
  * @typedef {Object} RequestOptions
4
4
  * @public
5
- * @property {object|string} body - body data to append to the request. JSON will be converted to a string (but a content-type header will not be attached to the request).
6
5
  * @property {object} errors - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors for this code.
7
- * @property {object} form - Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.
6
+ * @property {string} contentType - Sets the `Content-Type` header on the request. Defaults to `json`. Supported values: `json`, `xml`, `string`, and `form` (for FormData).
7
+ * @property {object|string} body - body data to append to the request. JSON will be converted to a string (but a content-type header will not be attached to the request).This is only applicable to the request function
8
8
  * @property {object} query - An object of query parameters to be encoded into the URL.
9
9
  * @property {object} headers - An object of headers to append to the request.
10
10
  * @property {string} parseAs - Parse the response body as json, text or stream. By default will use the response headers.
@@ -36,7 +36,7 @@ export function execute(...operations: Operations): Operation;
36
36
  * @example
37
37
  * request(
38
38
  * 'GET',
39
- * '/myEndpoint',
39
+ * '/patient',
40
40
  * {
41
41
  * query: {foo: 'bar', a: 1},
42
42
  * headers: {'content-type': 'application/json'},
@@ -45,91 +45,89 @@ export function execute(...operations: Operations): Operation;
45
45
  * @function
46
46
  * @param {string} method - The HTTP method to use.
47
47
  * @param {string} path - Path to resource. Can be an absolute URL if baseURL is NOT set on `state.configuration`.
48
- * @param {RequestOptions} params - Query, Headers and Authentication parameters
49
- * @param {function} callback - (Optional) Callback function
48
+ * @param {RequestOptions} options - Body, Query, Headers and Authentication parameters
50
49
  * @state {HttpState}
51
50
  * @returns {Operation}
52
51
  */
53
- export function request(method: string, path: string, params: RequestOptions, callback: Function): Operation;
52
+ export function request(method: string, path: string, options: RequestOptions): Operation;
54
53
  /**
55
54
  * Make a GET request. If `configuration.baseUrl` is set, paths must be relative.
56
55
  * @public
57
56
  * @example
58
- * get('/myEndpoint', {
57
+ * get('/patient', {
59
58
  * query: {foo: 'bar', a: 1},
60
59
  * headers: {'content-type': 'application/json'},
61
60
  * })
62
61
  * @function
63
62
  * @param {string} path - Path to resource. Can be an absolute URL if baseURL is NOT set on `state.configuration`.
64
- * @param {RequestOptions} params - Query, Headers and Authentication parameters
65
- * @param {function} callback - (Optional) Callback function
63
+ * @param {RequestOptions} options - Body, Query, Headers and Authentication parameters
66
64
  * @state {HttpState}
67
65
  * @returns {Operation}
68
66
  */
69
- export function get(path: string, params: RequestOptions, callback: Function): Operation;
67
+ export function get(path: string, options: RequestOptions): Operation;
70
68
  /**
71
69
  * Make a POST request. If `configuration.baseUrl` is set, paths must be relative.
72
70
  * @public
73
- * @example
74
- * post('/myEndpoint', {
75
- * body: {'foo': 'bar'},
76
- * headers: {'content-type': 'application/json'},
77
- * })
71
+ * @example <caption>POST a resource with from state</caption>
72
+ * post('/patient' $.data)
73
+ @example <caption>POST a resource with custom headers</caption>
74
+ * post('/patient' $.data, {
75
+ * headers: { 'content-type': 'application/fhir+json' },
76
+ * })
78
77
  * @function
79
78
  * @param {string} path - Path to resource. Can be an absolute URL if baseURL is NOT set on `state.configuration`.
80
- * @param {RequestOptions} params - Body, Query, Headers and Authentication parameters
81
- * @param {function} callback - (Optional) Callback function
79
+ * @param {object} data - Body data to append to the request. JSON will be converted to a string.
80
+ * @param {RequestOptions} options - Query, Headers and Authentication parameters
82
81
  * @state {HttpState}
83
82
  * @returns {operation}
84
83
  */
85
- export function post(path: string, params: RequestOptions, callback: Function): operation;
84
+ export function post(path: string, data: object, options: RequestOptions): operation;
86
85
  /**
87
86
  * Make a PUT request. If `configuration.baseUrl` is set, paths must be relative.
88
87
  * @public
89
- * @example
90
- * put('/myEndpoint', {
91
- * body: {'foo': 'bar'},
92
- * headers: {'content-type': 'application/json'},
93
- * })
88
+ * @example <caption>PUT a resource from state</caption>
89
+ * put('/patient', $.data)
90
+ * @example <caption>PUT a resource with custom headers</caption>
91
+ * put('/patient', $.data, {
92
+ * headers: { 'content-type': 'application/fhir+json' },
93
+ * })
94
94
  * @function
95
95
  * @param {string} path - Path to resource. Can be an absolute URL if baseURL is NOT set on `state.configuration`.
96
- * @param {RequestOptions} params - Body, Query, Headers and Auth parameters
97
- * @param {function} callback - (Optional) Callback function
96
+ * @param {object} data - Body data to append to the request. JSON will be converted to a string.
97
+ * @param {RequestOptions} options- Query, Headers and Auth parameters
98
98
  * @state {HttpState}
99
99
  * @returns {Operation}
100
100
  */
101
- export function put(path: string, params: RequestOptions, callback: Function): Operation;
101
+ export function put(path: string, data: object, options: any): Operation;
102
102
  /**
103
103
  * Make a PATCH request. If `configuration.baseUrl` is set, paths must be relative.
104
104
  * @public
105
- * @example
106
- * patch('/myEndpoint', {
107
- * body: {'foo': 'bar'},
108
- * headers: {'content-type': 'application/json'},
109
- * })
105
+ * @example <caption>PATCH a resource from state</caption>
106
+ * patch('/patient', $.data)
107
+ * @example <caption>PATCH a resource with custom headers</caption>
108
+ * patch('/patient', $.data, {
109
+ * headers: { 'content-type': 'application/fhir+json' },
110
+ * })
110
111
  * @function
111
112
  * @param {string} path - Path to resource. Can be an absolute URL if baseURL is NOT set on `state.configuration`.
112
- * @param {RequestOptions} params - Body, Query, Headers and Auth parameters
113
- * @param {function} callback - (Optional) Callback function
113
+ * @param {object} data - Body data to append to the request. JSON will be converted to a string.
114
+ * @param {RequestOptions} options - Query, Headers and Auth parameters
114
115
  * @state {HttpState}
115
116
  * @returns {Operation}
116
117
  */
117
- export function patch(path: string, params: RequestOptions, callback: Function): Operation;
118
+ export function patch(path: string, data: object, options: RequestOptions): Operation;
118
119
  /**
119
120
  * Make a DELETE request. If `configuration.baseUrl` is set, paths must be relative.
120
121
  * @public
121
122
  * @example
122
- * del(`/myendpoint/${state => state.data.id}`, {
123
- * headers: {'content-type': 'application/json'}
124
- * })
123
+ * del(`/myendpoint/${$.data.id}`)
125
124
  * @function
126
125
  * @param {string} path - Path to resource. Can be an absolute URL if baseURL is NOT set on `state.configuration`.
127
- * @param {RequestOptions} params - Body, Query, Headers and Auth parameters
128
- * @param {function} callback - (Optional) Callback function
126
+ * @param {RequestOptions} options - Query, Headers and Auth parameters
129
127
  * @state {HttpState}
130
128
  * @returns {Operation}
131
129
  */
132
- export function del(path: string, params: RequestOptions, callback: Function): Operation;
130
+ export function del(path: string, options: RequestOptions): Operation;
133
131
  /**
134
132
  * Parse XML with the Cheerio parser
135
133
  * @public
@@ -146,17 +144,16 @@ export function del(path: string, params: RequestOptions, callback: Function): O
146
144
  * ($) => {
147
145
  * return $("table[class=your_table]").parsetable(true, true, true);
148
146
  * },
149
- * (next) => ({ ...next, results: next.data.body })
147
+ * (next) => ({ ...next, results: next.data.data })
150
148
  * );
151
149
  * @function
152
- * @param {String} body - data string to be parsed
150
+ * @param {String} data - Body string to be parsed
153
151
  * @param {function} script - script for extracting data
154
- * @param {function} callback - (Optional) Callback function
155
152
  * @state data - the parsed XML as a JSON object
156
153
  * @state references - an array of all previous data objects used in the Job
157
154
  * @returns {Operation}
158
155
  */
159
- export function parseXML(body: string, script: Function, callback: Function): Operation;
156
+ export function parseXML(data: string, script: Function): Operation;
160
157
  /**
161
158
  * Options provided to the HTTP request
162
159
  */
package/types/util.d.ts CHANGED
@@ -3,11 +3,16 @@
3
3
  * @function
4
4
  * @private
5
5
  */
6
- export function request(method: any, path: any, params: any, callback?: (s: any) => any): (state: any) => any;
6
+ export function request(method: any, path: any, params: any): (state: any) => any;
7
7
  /**
8
8
  * XML parser helper function
9
9
  * @function
10
10
  * @private
11
11
  */
12
- export function xmlParser(body: any, script: any, callback?: (s: any) => any): (state: any) => any;
12
+ export function xmlParser(body: any, script: any): (state: any) => State;
13
+ export namespace CONTENT_TYPES {
14
+ const xml: string;
15
+ const json: string;
16
+ const string: string;
17
+ }
13
18
  export { encode, decode, uuid };