@openfn/language-godata 3.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.js ADDED
@@ -0,0 +1,641 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/Adaptor.js
8
+ var Adaptor_exports = {};
9
+ __export(Adaptor_exports, {
10
+ alterState: () => alterState,
11
+ axios: () => axios,
12
+ dataPath: () => dataPath,
13
+ dataValue: () => dataValue,
14
+ each: () => each,
15
+ execute: () => execute,
16
+ field: () => field,
17
+ fields: () => fields,
18
+ getCase: () => getCase,
19
+ getContact: () => getContact,
20
+ getLocation: () => getLocation,
21
+ getOutbreak: () => getOutbreak,
22
+ getReferenceData: () => getReferenceData,
23
+ lastReferenceValue: () => lastReferenceValue,
24
+ listCases: () => listCases,
25
+ listContacts: () => listContacts,
26
+ listLocations: () => listLocations,
27
+ listOutbreaks: () => listOutbreaks,
28
+ listReferenceData: () => listReferenceData,
29
+ merge: () => merge,
30
+ sourceValue: () => sourceValue,
31
+ upsertCase: () => upsertCase,
32
+ upsertContact: () => upsertContact,
33
+ upsertLocation: () => upsertLocation,
34
+ upsertOutbreak: () => upsertOutbreak,
35
+ upsertReferenceData: () => upsertReferenceData
36
+ });
37
+ import {
38
+ execute as commonExecute,
39
+ composeNextState,
40
+ expandReferences
41
+ } from "@openfn/language-common";
42
+ import axios from "axios";
43
+ import {
44
+ alterState,
45
+ dataPath,
46
+ dataValue,
47
+ each,
48
+ field,
49
+ fields,
50
+ lastReferenceValue,
51
+ merge,
52
+ sourceValue
53
+ } from "@openfn/language-common";
54
+ function execute(...operations) {
55
+ const initialState = {
56
+ references: [],
57
+ data: null
58
+ };
59
+ return (state) => {
60
+ return commonExecute(
61
+ login,
62
+ ...operations
63
+ )({ ...initialState, ...state }).catch((e) => {
64
+ console.error(e);
65
+ process.exit(1);
66
+ });
67
+ };
68
+ }
69
+ function login(state) {
70
+ const { apiUrl, password, email } = state.configuration;
71
+ return axios({
72
+ method: "post",
73
+ url: `${apiUrl}/users/login`,
74
+ data: {
75
+ email,
76
+ password
77
+ }
78
+ }).then((response) => {
79
+ console.log("Authentication succeeded.");
80
+ const { id } = response.data;
81
+ return {
82
+ ...state,
83
+ configuration: { ...state.configuration, apiUrl, access_token: id }
84
+ };
85
+ });
86
+ }
87
+ function listContacts(id, callback) {
88
+ return (state) => {
89
+ const { apiUrl, access_token } = state.configuration;
90
+ return axios({
91
+ method: "GET",
92
+ baseURL: apiUrl,
93
+ url: `/outbreaks/${id}/contacts`,
94
+ params: {
95
+ access_token
96
+ }
97
+ }).then((response) => {
98
+ const nextState = composeNextState(state, response.data);
99
+ if (callback)
100
+ return callback(nextState);
101
+ return nextState;
102
+ }).catch((error) => {
103
+ return error;
104
+ });
105
+ };
106
+ }
107
+ function getContact(id, query, callback) {
108
+ return (state) => {
109
+ const { apiUrl, access_token } = state.configuration;
110
+ const filter = JSON.stringify(query);
111
+ return axios({
112
+ baseURL: apiUrl,
113
+ url: `/outbreaks/${id}/contacts`,
114
+ method: "GET",
115
+ params: {
116
+ filter,
117
+ access_token
118
+ }
119
+ }).then((response) => {
120
+ const nextState = composeNextState(state, response.data);
121
+ if (callback)
122
+ return callback(nextState);
123
+ return nextState;
124
+ }).catch((error) => {
125
+ return error;
126
+ });
127
+ };
128
+ }
129
+ function upsertContact(id, externalId, goDataContact, callback) {
130
+ return (state) => {
131
+ const { apiUrl, access_token } = state.configuration;
132
+ const data = expandReferences(goDataContact)(state);
133
+ const query = { where: {} };
134
+ query.where[externalId] = data[externalId];
135
+ const filter = JSON.stringify(query);
136
+ return axios({
137
+ baseURL: apiUrl,
138
+ url: `/outbreaks/${id}/contacts`,
139
+ method: "GET",
140
+ params: {
141
+ filter,
142
+ access_token
143
+ }
144
+ }).then((response) => {
145
+ if (response.data.length > 1) {
146
+ console.log("Multiple contacts found. Aborting upsert.");
147
+ console.log(response.data.length, "contacts");
148
+ } else if (response.data.length === 1) {
149
+ console.log("Contact found. Performing update.");
150
+ const contactId = response.data[0].id;
151
+ return axios({
152
+ method: "PUT",
153
+ baseURL: apiUrl,
154
+ url: `/outbreaks/${id}/contacts/${contactId}`,
155
+ params: {
156
+ access_token
157
+ },
158
+ data
159
+ }).then((response2) => {
160
+ const nextState = composeNextState(state, response2.data);
161
+ if (callback)
162
+ return callback(nextState);
163
+ return nextState;
164
+ }).catch((error) => {
165
+ return error;
166
+ });
167
+ } else {
168
+ console.log("No contact found. Performing create.");
169
+ return axios({
170
+ method: "POST",
171
+ baseURL: apiUrl,
172
+ url: `/outbreaks/${id}/contacts/`,
173
+ params: {
174
+ access_token
175
+ },
176
+ data
177
+ }).then((response2) => {
178
+ const nextState = composeNextState(state, response2.data);
179
+ if (callback)
180
+ return callback(nextState);
181
+ return nextState;
182
+ }).catch((error) => {
183
+ return error;
184
+ });
185
+ }
186
+ }).catch((error) => {
187
+ return error;
188
+ });
189
+ };
190
+ }
191
+ function listOutbreaks(callback) {
192
+ return (state) => {
193
+ const { apiUrl, access_token } = state.configuration;
194
+ return axios({
195
+ method: "GET",
196
+ baseURL: apiUrl,
197
+ url: "/outbreaks",
198
+ params: {
199
+ access_token
200
+ }
201
+ }).then((response) => {
202
+ const nextState = composeNextState(state, response.data);
203
+ if (callback)
204
+ return callback(nextState);
205
+ return nextState;
206
+ }).catch((error) => {
207
+ return error;
208
+ });
209
+ };
210
+ }
211
+ function getOutbreak(query, callback) {
212
+ return (state) => {
213
+ const { apiUrl, access_token } = state.configuration;
214
+ const filter = JSON.stringify(query);
215
+ return axios({
216
+ method: "GET",
217
+ baseURL: apiUrl,
218
+ url: "/outbreaks",
219
+ params: {
220
+ filter,
221
+ access_token
222
+ }
223
+ }).then((response) => {
224
+ const nextState = composeNextState(state, response.data);
225
+ if (callback)
226
+ return callback(nextState);
227
+ return nextState;
228
+ }).catch((error) => {
229
+ return error;
230
+ });
231
+ };
232
+ }
233
+ function upsertOutbreak(outbreak, callback) {
234
+ return (state) => {
235
+ const { apiUrl, access_token } = state.configuration;
236
+ const { externalId, data } = expandReferences(outbreak)(state);
237
+ const filter = JSON.stringify({ where: { id: externalId } });
238
+ return axios({
239
+ method: "GET",
240
+ baseURL: apiUrl,
241
+ url: "/outbreaks",
242
+ params: {
243
+ filter,
244
+ access_token
245
+ }
246
+ }).then((response) => {
247
+ if (response.data.length > 0) {
248
+ console.log("Outbreak found. Performing update.");
249
+ const outbreakId = response.data[0].id;
250
+ return axios({
251
+ method: "PUT",
252
+ baseURL: apiUrl,
253
+ url: `/outbreaks/${outbreakId}`,
254
+ params: {
255
+ access_token
256
+ },
257
+ data
258
+ }).then((response2) => {
259
+ const nextState = composeNextState(state, response2.data);
260
+ if (callback)
261
+ return callback(nextState);
262
+ return nextState;
263
+ }).catch((error) => {
264
+ return error;
265
+ });
266
+ } else {
267
+ console.log("No outbreak found. Performing create.");
268
+ return axios({
269
+ method: "POST",
270
+ baseURL: apiUrl,
271
+ url: "/outbreaks",
272
+ params: {
273
+ access_token
274
+ },
275
+ data
276
+ }).then((response2) => {
277
+ const nextState = composeNextState(state, response2.data);
278
+ if (callback)
279
+ return callback(nextState);
280
+ return nextState;
281
+ }).catch((error) => {
282
+ return error;
283
+ });
284
+ }
285
+ }).catch((error) => {
286
+ return error;
287
+ });
288
+ };
289
+ }
290
+ function listCases(id, callback) {
291
+ return (state) => {
292
+ const { apiUrl, access_token } = state.configuration;
293
+ return axios({
294
+ method: "GET",
295
+ baseURL: apiUrl,
296
+ url: `/outbreaks/${id}/cases`,
297
+ params: {
298
+ access_token
299
+ }
300
+ }).then((response) => {
301
+ const nextState = composeNextState(state, response.data);
302
+ if (callback)
303
+ return callback(nextState);
304
+ return nextState;
305
+ }).catch((error) => {
306
+ return error;
307
+ });
308
+ };
309
+ }
310
+ function getCase(id, query, callback) {
311
+ return (state) => {
312
+ const { apiUrl, access_token } = state.configuration;
313
+ const filter = JSON.stringify(query);
314
+ return axios({
315
+ baseURL: apiUrl,
316
+ url: `/outbreaks/${id}/cases`,
317
+ method: "GET",
318
+ params: {
319
+ filter,
320
+ access_token
321
+ }
322
+ }).then((response) => {
323
+ const nextState = composeNextState(state, response.data);
324
+ if (callback)
325
+ return callback(nextState);
326
+ return nextState;
327
+ }).catch((error) => {
328
+ return error;
329
+ });
330
+ };
331
+ }
332
+ function upsertCase(id, externalId, goDataCase, callback) {
333
+ return (state) => {
334
+ const { apiUrl, access_token } = state.configuration;
335
+ const data = expandReferences(goDataCase)(state);
336
+ const query = { where: {} };
337
+ query.where[externalId] = data[externalId];
338
+ const filter = JSON.stringify(query);
339
+ return axios({
340
+ baseURL: apiUrl,
341
+ url: `/outbreaks/${id}/cases`,
342
+ method: "GET",
343
+ params: {
344
+ filter,
345
+ access_token
346
+ }
347
+ }).then((response) => {
348
+ if (response.data.length > 1) {
349
+ console.log(response.data.length, "cases found; aborting upsert.");
350
+ } else if (response.data.length === 1) {
351
+ console.log("Case found. Performing update.");
352
+ const caseId = response.data[0].id;
353
+ data["id"] = caseId;
354
+ delete data.visualId;
355
+ return axios({
356
+ method: "PUT",
357
+ baseURL: apiUrl,
358
+ url: `/outbreaks/${id}/cases/${caseId}`,
359
+ params: {
360
+ access_token
361
+ },
362
+ data
363
+ }).then((response2) => {
364
+ const nextState = composeNextState(state, response2.data);
365
+ if (callback)
366
+ return callback(nextState);
367
+ return nextState;
368
+ }).catch((error) => {
369
+ return error;
370
+ });
371
+ } else {
372
+ console.log("No case found. Performing create.");
373
+ return axios({
374
+ method: "POST",
375
+ baseURL: apiUrl,
376
+ url: `/outbreaks/${id}/cases/`,
377
+ params: {
378
+ access_token
379
+ },
380
+ data
381
+ }).then((response2) => {
382
+ const nextState = composeNextState(state, response2.data);
383
+ if (callback)
384
+ return callback(nextState);
385
+ return nextState;
386
+ }).catch((error) => {
387
+ return error;
388
+ });
389
+ }
390
+ }).catch((error) => {
391
+ return error;
392
+ });
393
+ };
394
+ }
395
+ function listLocations(callback) {
396
+ return (state) => {
397
+ const { apiUrl, access_token } = state.configuration;
398
+ return axios({
399
+ method: "GET",
400
+ baseURL: apiUrl,
401
+ url: "/locations",
402
+ params: {
403
+ access_token
404
+ }
405
+ }).then((response) => {
406
+ const nextState = composeNextState(state, response.data);
407
+ if (callback)
408
+ return callback(nextState);
409
+ return nextState;
410
+ }).catch((error) => {
411
+ return error;
412
+ });
413
+ };
414
+ }
415
+ function getLocation(query, callback) {
416
+ return (state) => {
417
+ const { apiUrl, access_token } = state.configuration;
418
+ const filter = JSON.stringify(query);
419
+ return axios({
420
+ method: "GET",
421
+ baseURL: apiUrl,
422
+ url: "/locations",
423
+ params: {
424
+ filter,
425
+ access_token
426
+ }
427
+ }).then((response) => {
428
+ const nextState = composeNextState(state, response.data);
429
+ if (callback)
430
+ return callback(nextState);
431
+ return nextState;
432
+ }).catch((error) => {
433
+ return error;
434
+ });
435
+ };
436
+ }
437
+ function upsertLocation(externalId, goDataLocation, callback) {
438
+ return (state) => {
439
+ const { apiUrl, access_token } = state.configuration;
440
+ const data = expandReferences(goDataLocation)(state);
441
+ const query = { where: {} };
442
+ query.where[externalId] = data[externalId];
443
+ const filter = JSON.stringify(query);
444
+ return axios({
445
+ method: "GET",
446
+ baseURL: apiUrl,
447
+ url: "/locations",
448
+ params: {
449
+ filter,
450
+ access_token
451
+ }
452
+ }).then((response) => {
453
+ if (response.data.length > 1) {
454
+ console.log(
455
+ response.data.length,
456
+ "locations found; aborting upsert."
457
+ );
458
+ return response;
459
+ } else if (response.data.length === 1) {
460
+ console.log("Location found. Performing update.");
461
+ const locationId = response.data[0].id;
462
+ return axios({
463
+ method: "PUT",
464
+ baseURL: apiUrl,
465
+ url: `/locations/${locationId}`,
466
+ params: {
467
+ access_token
468
+ },
469
+ data
470
+ }).then((response2) => {
471
+ const nextState = composeNextState(state, response2.data);
472
+ if (callback)
473
+ return callback(nextState);
474
+ return nextState;
475
+ }).catch((error) => {
476
+ return error;
477
+ });
478
+ } else {
479
+ console.log("No location found. Performing create.");
480
+ return axios({
481
+ method: "POST",
482
+ baseURL: apiUrl,
483
+ url: "/locations",
484
+ params: {
485
+ access_token
486
+ },
487
+ data
488
+ }).then((response2) => {
489
+ const nextState = composeNextState(state, response2.data);
490
+ if (callback)
491
+ return callback(nextState);
492
+ return nextState;
493
+ }).catch((error) => {
494
+ return error;
495
+ });
496
+ }
497
+ }).catch((error) => {
498
+ return error;
499
+ });
500
+ };
501
+ }
502
+ function listReferenceData(callback) {
503
+ return (state) => {
504
+ const { apiUrl, access_token } = state.configuration;
505
+ return axios({
506
+ method: "GET",
507
+ baseURL: apiUrl,
508
+ url: "/reference-data",
509
+ params: {
510
+ access_token
511
+ }
512
+ }).then((response) => {
513
+ const nextState = composeNextState(state, response.data);
514
+ if (callback)
515
+ return callback(nextState);
516
+ return nextState;
517
+ }).catch((error) => {
518
+ return error;
519
+ });
520
+ };
521
+ }
522
+ function getReferenceData(query, callback) {
523
+ return (state) => {
524
+ const { apiUrl, access_token } = state.configuration;
525
+ const filter = JSON.stringify(query);
526
+ return axios({
527
+ method: "GET",
528
+ baseURL: apiUrl,
529
+ url: "/reference-data",
530
+ params: {
531
+ filter,
532
+ access_token
533
+ }
534
+ }).then((response) => {
535
+ const nextState = composeNextState(state, response.data);
536
+ if (callback)
537
+ return callback(nextState);
538
+ return nextState;
539
+ }).catch((error) => {
540
+ return error;
541
+ });
542
+ };
543
+ }
544
+ function upsertReferenceData(externalId, goDataReferenceData, callback) {
545
+ return (state) => {
546
+ const { apiUrl, access_token } = state.configuration;
547
+ const data = expandReferences(goDataReferenceData)(state);
548
+ const query = { where: {} };
549
+ query.where[externalId] = data[externalId];
550
+ const filter = JSON.stringify(query);
551
+ return axios({
552
+ method: "GET",
553
+ baseURL: apiUrl,
554
+ url: "/reference-data",
555
+ params: {
556
+ filter,
557
+ access_token
558
+ }
559
+ }).then((response) => {
560
+ if (response.data.length > 1) {
561
+ console.log(
562
+ response.data.length,
563
+ "reference data found; aborting upsert."
564
+ );
565
+ return response;
566
+ } else if (response.data.length === 1) {
567
+ console.log("Reference data found. Performing update.");
568
+ const referenceDataId = response.data[0].id;
569
+ console.log("referenceDataId", referenceDataId);
570
+ return axios({
571
+ method: "PUT",
572
+ baseURL: apiUrl,
573
+ url: `/reference-data/${referenceDataId}`,
574
+ params: {
575
+ access_token
576
+ },
577
+ data
578
+ }).then((response2) => {
579
+ const nextState = composeNextState(state, response2.data);
580
+ if (callback)
581
+ return callback(nextState);
582
+ return nextState;
583
+ }).catch((error) => {
584
+ return error;
585
+ });
586
+ } else {
587
+ console.log("No reference data found. Performing create.");
588
+ return axios({
589
+ method: "POST",
590
+ baseURL: apiUrl,
591
+ url: "/reference-data",
592
+ params: {
593
+ access_token
594
+ },
595
+ data
596
+ }).then((response2) => {
597
+ const nextState = composeNextState(state, response2.data);
598
+ if (callback)
599
+ return callback(nextState);
600
+ return nextState;
601
+ }).catch((error) => {
602
+ return error;
603
+ });
604
+ }
605
+ }).catch((error) => {
606
+ return error;
607
+ });
608
+ };
609
+ }
610
+
611
+ // src/index.js
612
+ var src_default = Adaptor_exports;
613
+ export {
614
+ alterState,
615
+ axios,
616
+ dataPath,
617
+ dataValue,
618
+ src_default as default,
619
+ each,
620
+ execute,
621
+ field,
622
+ fields,
623
+ getCase,
624
+ getContact,
625
+ getLocation,
626
+ getOutbreak,
627
+ getReferenceData,
628
+ lastReferenceValue,
629
+ listCases,
630
+ listContacts,
631
+ listLocations,
632
+ listOutbreaks,
633
+ listReferenceData,
634
+ merge,
635
+ sourceValue,
636
+ upsertCase,
637
+ upsertContact,
638
+ upsertLocation,
639
+ upsertOutbreak,
640
+ upsertReferenceData
641
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@openfn/language-godata",
3
+ "version": "3.2.0",
4
+ "description": "An OpenFn adaptor for use with the WHO's GoData API",
5
+ "main": "dist/index.cjs",
6
+ "author": "Open Function Group",
7
+ "license": "LGPLv3",
8
+ "files": [
9
+ "dist/",
10
+ "types/",
11
+ "ast.json",
12
+ "configuration-schema.json"
13
+ ],
14
+ "dependencies": {
15
+ "axios": "^0.21.2",
16
+ "@openfn/language-common": "^1.7.5"
17
+ },
18
+ "devDependencies": {
19
+ "assertion-error": "^1.0.1",
20
+ "chai": "^3.4.0",
21
+ "deep-eql": "^0.1.3",
22
+ "mocha": "^7.1.1",
23
+ "nock": "^12.0.3",
24
+ "sinon": "^1.17.2",
25
+ "esno": "^0.16.3",
26
+ "@openfn/buildtools": "^1.0.1",
27
+ "@openfn/simple-ast": "0.4.1",
28
+ "rimraf": "^3.0.2"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/openfn/adaptors.git"
33
+ },
34
+ "type": "module",
35
+ "exports": {
36
+ ".": {
37
+ "import": "./dist/index.js",
38
+ "require": "./dist/index.cjs"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "types": "types/index.d.ts",
43
+ "scripts": {
44
+ "build": "pnpm clean && build-adaptor godata",
45
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
46
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
47
+ "clean": "rimraf dist types docs",
48
+ "pack": "pnpm pack --pack-destination ../../dist"
49
+ }
50
+ }