@openfn/language-ghana-bdr 0.1.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.
@@ -0,0 +1,29 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "properties": {
4
+ "baseUrl": {
5
+ "title": "Base URL",
6
+ "type": "string",
7
+ "description": "The base URL (http://www.example.com)",
8
+ "format": "uri",
9
+ "minLength": 1,
10
+ "examples": ["https://example.com"]
11
+ },
12
+ "username": {
13
+ "title": "Username",
14
+ "type": "string",
15
+ "description": "Username",
16
+ "examples": ["test@openfn.org"]
17
+ },
18
+ "password": {
19
+ "title": "Password",
20
+ "type": "string",
21
+ "description": "Password",
22
+ "writeOnly": true,
23
+ "examples": ["@some(!)Str0ngp4ss0w0rd"]
24
+ }
25
+ },
26
+ "type": "object",
27
+ "additionalProperties": true,
28
+ "required": ["password", "username", "baseUrl"]
29
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,301 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+
25
+ // src/index.js
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ cursor: () => import_language_common3.cursor,
29
+ dataPath: () => import_language_common3.dataPath,
30
+ dataValue: () => import_language_common3.dataValue,
31
+ dateFns: () => import_language_common3.dateFns,
32
+ default: () => src_default,
33
+ each: () => import_language_common3.each,
34
+ execute: () => execute,
35
+ field: () => import_language_common3.field,
36
+ fields: () => import_language_common3.fields,
37
+ fn: () => import_language_common3.fn,
38
+ fnIf: () => import_language_common3.fnIf,
39
+ get: () => get,
40
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
41
+ merge: () => import_language_common3.merge,
42
+ post: () => post,
43
+ sendBirthNotification: () => sendBirthNotification,
44
+ sourceValue: () => import_language_common3.sourceValue
45
+ });
46
+ module.exports = __toCommonJS(src_exports);
47
+
48
+ // src/Adaptor.js
49
+ var Adaptor_exports = {};
50
+ __export(Adaptor_exports, {
51
+ cursor: () => import_language_common3.cursor,
52
+ dataPath: () => import_language_common3.dataPath,
53
+ dataValue: () => import_language_common3.dataValue,
54
+ dateFns: () => import_language_common3.dateFns,
55
+ each: () => import_language_common3.each,
56
+ execute: () => execute,
57
+ field: () => import_language_common3.field,
58
+ fields: () => import_language_common3.fields,
59
+ fn: () => import_language_common3.fn,
60
+ fnIf: () => import_language_common3.fnIf,
61
+ get: () => get,
62
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
63
+ merge: () => import_language_common3.merge,
64
+ post: () => post,
65
+ sendBirthNotification: () => sendBirthNotification,
66
+ sourceValue: () => import_language_common3.sourceValue
67
+ });
68
+ var import_language_common2 = require("@openfn/language-common");
69
+
70
+ // src/util.js
71
+ var import_undici = __toESM(require("undici"), 1);
72
+ var import_node_path = __toESM(require("path"), 1);
73
+ var import_util = require("@openfn/language-common/util");
74
+ var import_language_common = require("@openfn/language-common");
75
+ var client;
76
+ var getClient = (baseUrl) => {
77
+ if (baseUrl) {
78
+ const url = new URL(baseUrl);
79
+ client = new import_undici.default.Client(url.origin);
80
+ }
81
+ return client;
82
+ };
83
+ var setMockClient = (mockClient) => {
84
+ client = mockClient;
85
+ };
86
+ var prepareNextState = async (state, response) => {
87
+ const { body, ...responseWithoutBody } = response;
88
+ const result = await body.json();
89
+ if (!state.references) {
90
+ state.references = [];
91
+ }
92
+ const nextState = {
93
+ ...(0, import_language_common.composeNextState)(state, result),
94
+ response: responseWithoutBody
95
+ };
96
+ return nextState;
97
+ };
98
+ var request = (path, options) => {
99
+ return async (state) => {
100
+ const { baseUrl, username, password } = state.configuration;
101
+ getClient(baseUrl);
102
+ const basePath = baseUrl ? new URL(baseUrl).pathname : "/";
103
+ const [resolvedPath, resolvedoptions] = (0, import_util.expandReferences)(
104
+ state,
105
+ path,
106
+ options
107
+ );
108
+ const {
109
+ data,
110
+ headers = { "content-type": "application/json" },
111
+ method = "POST",
112
+ query,
113
+ ...otherOptions
114
+ } = resolvedoptions;
115
+ const addAuth = (0, import_util.makeBasicAuthHeader)(username, password);
116
+ const safePath = resolvedPath ? import_node_path.default.join(basePath, resolvedPath) : basePath;
117
+ const args = {
118
+ path: safePath,
119
+ data,
120
+ headers: {
121
+ ...headers,
122
+ ...addAuth
123
+ },
124
+ method,
125
+ query,
126
+ ...otherOptions
127
+ };
128
+ if (data) {
129
+ args.body = JSON.stringify(data);
130
+ }
131
+ const response = await client.request(args);
132
+ if (response.statusCode >= 400) {
133
+ (0, import_util.throwError)("BDR_ERROR", {
134
+ code: response.statusCode,
135
+ description: response.statusMessage,
136
+ body: await response.body.text()
137
+ });
138
+ }
139
+ (0, import_util.logResponse)(response);
140
+ return prepareNextState(state, response);
141
+ };
142
+ };
143
+
144
+ // src/mock.js
145
+ var import_undici2 = require("undici");
146
+ var birthNotificationResponse = {
147
+ registry_code: "1",
148
+ child: {
149
+ first_name: "Nana",
150
+ middle_name: "Nana",
151
+ surname: "Kumasi",
152
+ birth_date: "2023/10/22",
153
+ national_id_number: "90282901892",
154
+ place_of_delivery_code: "2",
155
+ place_of_delivery_other: null,
156
+ birth_type_code: "1",
157
+ gender_code: "2",
158
+ attendant_at_birth_code: "1",
159
+ attendant_at_birth_other: null,
160
+ birth_attendant_full_name: "Dr Lawrence Kwame",
161
+ birth_attendant_phone: "0556969609"
162
+ },
163
+ mother: {
164
+ first_name: "Nana",
165
+ middle_name: "Nana",
166
+ maiden_name: "Nana",
167
+ surname_or_marriage_name: "Nana",
168
+ phone: "05673673673",
169
+ age: 35,
170
+ national_id_number: "90-030-003",
171
+ nationality_code: "131",
172
+ house_number: "No 4536",
173
+ street_name: "Accra city Ghana",
174
+ district_code: "10",
175
+ region_code: "10",
176
+ town_or_village: "Ajiringanor",
177
+ number_of_children_ever_born_alive: 5,
178
+ born_alive_are_living: 5,
179
+ live_birth_order: 5,
180
+ born_alive_are_dead: 0,
181
+ education_level_code: "6",
182
+ education_level_other: null,
183
+ occupation_code: "13",
184
+ occupation_other: null
185
+ },
186
+ father: {
187
+ first_name: "Kufor",
188
+ middle_name: "Kufor",
189
+ surname: "Mensa",
190
+ nationality_code: "131",
191
+ national_id_number: "88-9292-92092",
192
+ phone: "05673673737",
193
+ age: 38,
194
+ education_level_code: "13",
195
+ occupation_code: "13",
196
+ religion_code: "1",
197
+ education_level_other: null,
198
+ occupation_other: null,
199
+ religion_other: null,
200
+ is_gainfully_employed_code: "1"
201
+ },
202
+ health_facility: {
203
+ name: "Korlebu General Hospital",
204
+ house_number: "No 4536",
205
+ street_name: "Adjiringano",
206
+ town_code: "12",
207
+ town_other: null,
208
+ district_code: "11",
209
+ region_code: "12"
210
+ },
211
+ birth_informant: {
212
+ relationship_code: "2",
213
+ relationship_other: null,
214
+ full_name: "Kufor Mensa",
215
+ phone: "055646874",
216
+ digital_address: "No. 64 Ajiringanor",
217
+ national_id_number: "993993939-0303"
218
+ }
219
+ };
220
+ function createServer(url = "http://tracker.chimgh.org") {
221
+ const agent = new import_undici2.MockAgent();
222
+ agent.disableNetConnect();
223
+ const mockPool = agent.get(url);
224
+ const sendBirthNotification2 = (req) => {
225
+ return {
226
+ statusCode: 200,
227
+ responseOptions: {
228
+ headers: { "Content-Type": "application/json" }
229
+ },
230
+ data: JSON.stringify(birthNotificationResponse)
231
+ };
232
+ };
233
+ mockPool.intercept({ method: "post", path: /api\/notification/ }).reply(sendBirthNotification2).persist();
234
+ return {
235
+ agent,
236
+ request: ({ method, path, data, ...rest }) => {
237
+ const opts = {
238
+ method,
239
+ path,
240
+ origin: url,
241
+ headers: {},
242
+ ...rest
243
+ };
244
+ if (data) {
245
+ opts.body = JSON.stringify(data);
246
+ }
247
+ return mockPool.request(opts);
248
+ }
249
+ };
250
+ }
251
+
252
+ // src/Adaptor.js
253
+ var import_language_common3 = require("@openfn/language-common");
254
+ function execute(...operations) {
255
+ const initialState = {
256
+ references: [],
257
+ data: null,
258
+ configuration: {}
259
+ };
260
+ return (state) => {
261
+ if (!state.configuration.baseUrl) {
262
+ const client2 = createServer();
263
+ setMockClient(client2);
264
+ }
265
+ return (0, import_language_common2.execute)(...operations)({ ...initialState, ...state });
266
+ };
267
+ }
268
+ function get(path, query) {
269
+ return request(path, { query, method: "GET" });
270
+ }
271
+ function post(path, data) {
272
+ return request(path, { data, method: "POST" });
273
+ }
274
+ function sendBirthNotification(data) {
275
+ return request("api/notification", {
276
+ method: "POST",
277
+ data
278
+ });
279
+ }
280
+
281
+ // src/index.js
282
+ var src_default = Adaptor_exports;
283
+ // Annotate the CommonJS export names for ESM import in node:
284
+ 0 && (module.exports = {
285
+ cursor,
286
+ dataPath,
287
+ dataValue,
288
+ dateFns,
289
+ each,
290
+ execute,
291
+ field,
292
+ fields,
293
+ fn,
294
+ fnIf,
295
+ get,
296
+ lastReferenceValue,
297
+ merge,
298
+ post,
299
+ sendBirthNotification,
300
+ sourceValue
301
+ });
package/dist/index.js ADDED
@@ -0,0 +1,278 @@
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
+ cursor: () => cursor,
11
+ dataPath: () => dataPath,
12
+ dataValue: () => dataValue,
13
+ dateFns: () => dateFns,
14
+ each: () => each,
15
+ execute: () => execute,
16
+ field: () => field,
17
+ fields: () => fields,
18
+ fn: () => fn,
19
+ fnIf: () => fnIf,
20
+ get: () => get,
21
+ lastReferenceValue: () => lastReferenceValue,
22
+ merge: () => merge,
23
+ post: () => post,
24
+ sendBirthNotification: () => sendBirthNotification,
25
+ sourceValue: () => sourceValue
26
+ });
27
+ import { execute as commonExecute } from "@openfn/language-common";
28
+
29
+ // src/util.js
30
+ import undici from "undici";
31
+ import nodepath from "path";
32
+ import {
33
+ throwError,
34
+ logResponse,
35
+ expandReferences,
36
+ makeBasicAuthHeader
37
+ } from "@openfn/language-common/util";
38
+ import { composeNextState } from "@openfn/language-common";
39
+ var client;
40
+ var getClient = (baseUrl) => {
41
+ if (baseUrl) {
42
+ const url = new URL(baseUrl);
43
+ client = new undici.Client(url.origin);
44
+ }
45
+ return client;
46
+ };
47
+ var setMockClient = (mockClient) => {
48
+ client = mockClient;
49
+ };
50
+ var prepareNextState = async (state, response) => {
51
+ const { body, ...responseWithoutBody } = response;
52
+ const result = await body.json();
53
+ if (!state.references) {
54
+ state.references = [];
55
+ }
56
+ const nextState = {
57
+ ...composeNextState(state, result),
58
+ response: responseWithoutBody
59
+ };
60
+ return nextState;
61
+ };
62
+ var request = (path, options) => {
63
+ return async (state) => {
64
+ const { baseUrl, username, password } = state.configuration;
65
+ getClient(baseUrl);
66
+ const basePath = baseUrl ? new URL(baseUrl).pathname : "/";
67
+ const [resolvedPath, resolvedoptions] = expandReferences(
68
+ state,
69
+ path,
70
+ options
71
+ );
72
+ const {
73
+ data,
74
+ headers = { "content-type": "application/json" },
75
+ method = "POST",
76
+ query,
77
+ ...otherOptions
78
+ } = resolvedoptions;
79
+ const addAuth = makeBasicAuthHeader(username, password);
80
+ const safePath = resolvedPath ? nodepath.join(basePath, resolvedPath) : basePath;
81
+ const args = {
82
+ path: safePath,
83
+ data,
84
+ headers: {
85
+ ...headers,
86
+ ...addAuth
87
+ },
88
+ method,
89
+ query,
90
+ ...otherOptions
91
+ };
92
+ if (data) {
93
+ args.body = JSON.stringify(data);
94
+ }
95
+ const response = await client.request(args);
96
+ if (response.statusCode >= 400) {
97
+ throwError("BDR_ERROR", {
98
+ code: response.statusCode,
99
+ description: response.statusMessage,
100
+ body: await response.body.text()
101
+ });
102
+ }
103
+ logResponse(response);
104
+ return prepareNextState(state, response);
105
+ };
106
+ };
107
+
108
+ // src/mock.js
109
+ import { MockAgent } from "undici";
110
+ var birthNotificationResponse = {
111
+ registry_code: "1",
112
+ child: {
113
+ first_name: "Nana",
114
+ middle_name: "Nana",
115
+ surname: "Kumasi",
116
+ birth_date: "2023/10/22",
117
+ national_id_number: "90282901892",
118
+ place_of_delivery_code: "2",
119
+ place_of_delivery_other: null,
120
+ birth_type_code: "1",
121
+ gender_code: "2",
122
+ attendant_at_birth_code: "1",
123
+ attendant_at_birth_other: null,
124
+ birth_attendant_full_name: "Dr Lawrence Kwame",
125
+ birth_attendant_phone: "0556969609"
126
+ },
127
+ mother: {
128
+ first_name: "Nana",
129
+ middle_name: "Nana",
130
+ maiden_name: "Nana",
131
+ surname_or_marriage_name: "Nana",
132
+ phone: "05673673673",
133
+ age: 35,
134
+ national_id_number: "90-030-003",
135
+ nationality_code: "131",
136
+ house_number: "No 4536",
137
+ street_name: "Accra city Ghana",
138
+ district_code: "10",
139
+ region_code: "10",
140
+ town_or_village: "Ajiringanor",
141
+ number_of_children_ever_born_alive: 5,
142
+ born_alive_are_living: 5,
143
+ live_birth_order: 5,
144
+ born_alive_are_dead: 0,
145
+ education_level_code: "6",
146
+ education_level_other: null,
147
+ occupation_code: "13",
148
+ occupation_other: null
149
+ },
150
+ father: {
151
+ first_name: "Kufor",
152
+ middle_name: "Kufor",
153
+ surname: "Mensa",
154
+ nationality_code: "131",
155
+ national_id_number: "88-9292-92092",
156
+ phone: "05673673737",
157
+ age: 38,
158
+ education_level_code: "13",
159
+ occupation_code: "13",
160
+ religion_code: "1",
161
+ education_level_other: null,
162
+ occupation_other: null,
163
+ religion_other: null,
164
+ is_gainfully_employed_code: "1"
165
+ },
166
+ health_facility: {
167
+ name: "Korlebu General Hospital",
168
+ house_number: "No 4536",
169
+ street_name: "Adjiringano",
170
+ town_code: "12",
171
+ town_other: null,
172
+ district_code: "11",
173
+ region_code: "12"
174
+ },
175
+ birth_informant: {
176
+ relationship_code: "2",
177
+ relationship_other: null,
178
+ full_name: "Kufor Mensa",
179
+ phone: "055646874",
180
+ digital_address: "No. 64 Ajiringanor",
181
+ national_id_number: "993993939-0303"
182
+ }
183
+ };
184
+ function createServer(url = "http://tracker.chimgh.org") {
185
+ const agent = new MockAgent();
186
+ agent.disableNetConnect();
187
+ const mockPool = agent.get(url);
188
+ const sendBirthNotification2 = (req) => {
189
+ return {
190
+ statusCode: 200,
191
+ responseOptions: {
192
+ headers: { "Content-Type": "application/json" }
193
+ },
194
+ data: JSON.stringify(birthNotificationResponse)
195
+ };
196
+ };
197
+ mockPool.intercept({ method: "post", path: /api\/notification/ }).reply(sendBirthNotification2).persist();
198
+ return {
199
+ agent,
200
+ request: ({ method, path, data, ...rest }) => {
201
+ const opts = {
202
+ method,
203
+ path,
204
+ origin: url,
205
+ headers: {},
206
+ ...rest
207
+ };
208
+ if (data) {
209
+ opts.body = JSON.stringify(data);
210
+ }
211
+ return mockPool.request(opts);
212
+ }
213
+ };
214
+ }
215
+
216
+ // src/Adaptor.js
217
+ import {
218
+ fn,
219
+ fnIf,
220
+ each,
221
+ merge,
222
+ field,
223
+ fields,
224
+ cursor,
225
+ dateFns,
226
+ dataPath,
227
+ dataValue,
228
+ sourceValue,
229
+ lastReferenceValue
230
+ } from "@openfn/language-common";
231
+ function execute(...operations) {
232
+ const initialState = {
233
+ references: [],
234
+ data: null,
235
+ configuration: {}
236
+ };
237
+ return (state) => {
238
+ if (!state.configuration.baseUrl) {
239
+ const client2 = createServer();
240
+ setMockClient(client2);
241
+ }
242
+ return commonExecute(...operations)({ ...initialState, ...state });
243
+ };
244
+ }
245
+ function get(path, query) {
246
+ return request(path, { query, method: "GET" });
247
+ }
248
+ function post(path, data) {
249
+ return request(path, { data, method: "POST" });
250
+ }
251
+ function sendBirthNotification(data) {
252
+ return request("api/notification", {
253
+ method: "POST",
254
+ data
255
+ });
256
+ }
257
+
258
+ // src/index.js
259
+ var src_default = Adaptor_exports;
260
+ export {
261
+ cursor,
262
+ dataPath,
263
+ dataValue,
264
+ dateFns,
265
+ src_default as default,
266
+ each,
267
+ execute,
268
+ field,
269
+ fields,
270
+ fn,
271
+ fnIf,
272
+ get,
273
+ lastReferenceValue,
274
+ merge,
275
+ post,
276
+ sendBirthNotification,
277
+ sourceValue
278
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@openfn/language-ghana-bdr",
3
+ "version": "0.1.0",
4
+ "description": "OpenFn ghana-bdr adaptor",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "types": "./types/index.d.ts",
10
+ "require": "./dist/index.cjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "author": "Open Function Group",
15
+ "license": "LGPLv3",
16
+ "files": [
17
+ "dist/",
18
+ "types/",
19
+ "ast.json",
20
+ "configuration-schema.json"
21
+ ],
22
+ "dependencies": {
23
+ "undici": "^5.22.1",
24
+ "@openfn/language-common": "2.3.0"
25
+ },
26
+ "devDependencies": {
27
+ "assertion-error": "2.0.0",
28
+ "chai": "4.3.6",
29
+ "deep-eql": "4.1.1",
30
+ "esno": "^0.16.3",
31
+ "mocha": "^10.7.3",
32
+ "rimraf": "3.0.2",
33
+ "undici": "^5.22.1"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/openfn/adaptors.git"
38
+ },
39
+ "types": "types/index.d.ts",
40
+ "main": "dist/index.cjs",
41
+ "scripts": {
42
+ "build": "pnpm clean && build-adaptor ghana-bdr",
43
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
44
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
45
+ "clean": "rimraf dist types docs",
46
+ "pack": "pnpm pack --pack-destination ../../dist",
47
+ "lint": "eslint src"
48
+ }
49
+ }