@openfn/language-satusehat 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,271 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.js
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ alterState: () => import_language_common3.alterState,
23
+ arrayToString: () => import_language_common3.arrayToString,
24
+ combine: () => import_language_common3.combine,
25
+ dataPath: () => import_language_common3.dataPath,
26
+ dataValue: () => import_language_common3.dataValue,
27
+ default: () => src_default,
28
+ each: () => import_language_common3.each,
29
+ execute: () => execute,
30
+ field: () => import_language_common3.field,
31
+ fields: () => import_language_common3.fields,
32
+ fn: () => import_language_common3.fn,
33
+ get: () => get,
34
+ http: () => import_language_common3.http,
35
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
36
+ merge: () => import_language_common3.merge,
37
+ patch: () => patch,
38
+ post: () => post,
39
+ put: () => put,
40
+ sourceValue: () => import_language_common3.sourceValue
41
+ });
42
+ module.exports = __toCommonJS(src_exports);
43
+
44
+ // src/Adaptor.js
45
+ var Adaptor_exports = {};
46
+ __export(Adaptor_exports, {
47
+ alterState: () => import_language_common3.alterState,
48
+ arrayToString: () => import_language_common3.arrayToString,
49
+ combine: () => import_language_common3.combine,
50
+ dataPath: () => import_language_common3.dataPath,
51
+ dataValue: () => import_language_common3.dataValue,
52
+ each: () => import_language_common3.each,
53
+ execute: () => execute,
54
+ field: () => import_language_common3.field,
55
+ fields: () => import_language_common3.fields,
56
+ fn: () => import_language_common3.fn,
57
+ get: () => get,
58
+ http: () => import_language_common3.http,
59
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
60
+ merge: () => import_language_common3.merge,
61
+ patch: () => patch,
62
+ post: () => post,
63
+ put: () => put,
64
+ sourceValue: () => import_language_common3.sourceValue
65
+ });
66
+ var import_language_common2 = require("@openfn/language-common");
67
+ var import_util2 = require("@openfn/language-common/util");
68
+
69
+ // src/Utils.js
70
+ var import_language_common = require("@openfn/language-common");
71
+ var import_util = require("@openfn/language-common/util");
72
+ var generateUserAgent = () => {
73
+ return `nodejs/${process.version} @openfn/language-satusehat`;
74
+ };
75
+ var authorize = (state) => {
76
+ const auth = state.configuration;
77
+ if (auth.access_token) {
78
+ return state;
79
+ }
80
+ const clientId = auth.clientId;
81
+ const clientSecret = auth.clientSecret;
82
+ const headers = {};
83
+ if (clientId && clientSecret) {
84
+ Object.assign(headers, {
85
+ "Content-Type": "application/x-www-form-urlencoded",
86
+ "User-Agent": generateUserAgent()
87
+ });
88
+ const body = new URLSearchParams();
89
+ body.append("client_id", clientId);
90
+ body.append("client_secret", clientSecret);
91
+ const options = {
92
+ body: body.toString(),
93
+ headers,
94
+ method: "POST",
95
+ parseAs: "json",
96
+ baseUrl: auth.baseUrl,
97
+ query: {
98
+ grant_type: "client_credentials"
99
+ },
100
+ maxRedirections: 1
101
+ };
102
+ return (0, import_util.request)("POST", "/oauth2/v1/accesstoken", options).then(
103
+ (response) => {
104
+ return {
105
+ ...state,
106
+ configuration: {
107
+ ...state.configuration,
108
+ access_token: response.body.access_token
109
+ }
110
+ };
111
+ }
112
+ );
113
+ } else {
114
+ throw new Error(
115
+ "Invalid authorization credentials. Include clientId and clientSecret in state.configuration"
116
+ );
117
+ }
118
+ };
119
+ var prepareNextState = (state, response, callback = (s) => s) => {
120
+ const { body, ...responseWithoutBody } = response;
121
+ const nextState = {
122
+ ...(0, import_language_common.composeNextState)(state, response.body),
123
+ response: responseWithoutBody
124
+ };
125
+ return callback(nextState);
126
+ };
127
+ async function request(configuration, path, opts) {
128
+ const { baseUrl, access_token } = configuration;
129
+ const {
130
+ method,
131
+ data,
132
+ params = {},
133
+ parseAs = "json",
134
+ contentType = "application/json"
135
+ } = opts;
136
+ const headers = {
137
+ Authorization: `Bearer ${access_token}`,
138
+ "User-Agent": generateUserAgent(),
139
+ "content-type": contentType
140
+ };
141
+ const options = {
142
+ body: data,
143
+ headers,
144
+ query: params,
145
+ parseAs,
146
+ maxRedirections: 1,
147
+ baseUrl: `${baseUrl}/fhir-r4/v1/`
148
+ };
149
+ return (0, import_util.request)(method, path, options).then(import_util.logResponse);
150
+ }
151
+
152
+ // src/Adaptor.js
153
+ var import_language_common3 = require("@openfn/language-common");
154
+ function execute(...operations) {
155
+ const initialState = {
156
+ references: [],
157
+ data: null
158
+ };
159
+ return (state) => {
160
+ return (0, import_language_common2.execute)(
161
+ authorize,
162
+ ...operations
163
+ )({
164
+ ...initialState,
165
+ ...state
166
+ });
167
+ };
168
+ }
169
+ function get(path, params = {}, callback = (s) => s) {
170
+ return async (state) => {
171
+ const [resolvedPath, resolvedParams] = (0, import_util2.expandReferences)(
172
+ state,
173
+ path,
174
+ params
175
+ );
176
+ try {
177
+ const response = await request(state.configuration, `/${resolvedPath}`, {
178
+ method: "GET",
179
+ params: resolvedParams
180
+ });
181
+ return prepareNextState(state, response, callback);
182
+ } catch (e) {
183
+ throw e.body ?? e;
184
+ }
185
+ };
186
+ }
187
+ function post(path, data, params = {}, callback = (s) => s) {
188
+ return async (state) => {
189
+ const [resolvedPath, resolvedData, resolvedParams] = (0, import_util2.expandReferences)(
190
+ state,
191
+ path,
192
+ data,
193
+ params
194
+ );
195
+ try {
196
+ const response = await request(state.configuration, `/${resolvedPath}`, {
197
+ method: "POST",
198
+ data: resolvedData,
199
+ params: resolvedParams
200
+ });
201
+ return prepareNextState(state, response, callback);
202
+ } catch (e) {
203
+ throw e.body.error ?? e;
204
+ }
205
+ };
206
+ }
207
+ function put(path, data, params = {}, callback = (s) => s) {
208
+ return async (state) => {
209
+ const [resolvedPath, resolvedData, resolvedParams] = (0, import_util2.expandReferences)(
210
+ state,
211
+ path,
212
+ data,
213
+ params
214
+ );
215
+ try {
216
+ const response = await request(state.configuration, `/${resolvedPath}`, {
217
+ method: "PUT",
218
+ data: resolvedData,
219
+ params: resolvedParams
220
+ });
221
+ return prepareNextState(state, response, callback);
222
+ } catch (e) {
223
+ throw e.body.error ?? e;
224
+ }
225
+ };
226
+ }
227
+ function patch(path, data, params = {}, callback = (s) => s) {
228
+ return async (state) => {
229
+ const [resolvedPath, resolvedData, resolvedParams] = (0, import_util2.expandReferences)(
230
+ state,
231
+ path,
232
+ data,
233
+ params
234
+ );
235
+ try {
236
+ const response = await request(state.configuration, `/${resolvedPath}`, {
237
+ method: "PATCH",
238
+ data: JSON.stringify(resolvedData),
239
+ params: resolvedParams,
240
+ contentType: "application/json-patch+json"
241
+ });
242
+ return prepareNextState(state, response, callback);
243
+ } catch (e) {
244
+ throw e.body ?? e;
245
+ }
246
+ };
247
+ }
248
+
249
+ // src/index.js
250
+ var src_default = Adaptor_exports;
251
+ // Annotate the CommonJS export names for ESM import in node:
252
+ 0 && (module.exports = {
253
+ alterState,
254
+ arrayToString,
255
+ combine,
256
+ dataPath,
257
+ dataValue,
258
+ each,
259
+ execute,
260
+ field,
261
+ fields,
262
+ fn,
263
+ get,
264
+ http,
265
+ lastReferenceValue,
266
+ merge,
267
+ patch,
268
+ post,
269
+ put,
270
+ sourceValue
271
+ });
package/dist/index.js ADDED
@@ -0,0 +1,251 @@
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
+ arrayToString: () => arrayToString,
12
+ combine: () => combine,
13
+ dataPath: () => dataPath,
14
+ dataValue: () => dataValue,
15
+ each: () => each,
16
+ execute: () => execute,
17
+ field: () => field,
18
+ fields: () => fields,
19
+ fn: () => fn,
20
+ get: () => get,
21
+ http: () => http,
22
+ lastReferenceValue: () => lastReferenceValue,
23
+ merge: () => merge,
24
+ patch: () => patch,
25
+ post: () => post,
26
+ put: () => put,
27
+ sourceValue: () => sourceValue
28
+ });
29
+ import { execute as commonExecute } from "@openfn/language-common";
30
+ import { expandReferences } from "@openfn/language-common/util";
31
+
32
+ // src/Utils.js
33
+ import { composeNextState } from "@openfn/language-common";
34
+ import {
35
+ request as commonRequest,
36
+ logResponse
37
+ } from "@openfn/language-common/util";
38
+ var generateUserAgent = () => {
39
+ return `nodejs/${process.version} @openfn/language-satusehat`;
40
+ };
41
+ var authorize = (state) => {
42
+ const auth = state.configuration;
43
+ if (auth.access_token) {
44
+ return state;
45
+ }
46
+ const clientId = auth.clientId;
47
+ const clientSecret = auth.clientSecret;
48
+ const headers = {};
49
+ if (clientId && clientSecret) {
50
+ Object.assign(headers, {
51
+ "Content-Type": "application/x-www-form-urlencoded",
52
+ "User-Agent": generateUserAgent()
53
+ });
54
+ const body = new URLSearchParams();
55
+ body.append("client_id", clientId);
56
+ body.append("client_secret", clientSecret);
57
+ const options = {
58
+ body: body.toString(),
59
+ headers,
60
+ method: "POST",
61
+ parseAs: "json",
62
+ baseUrl: auth.baseUrl,
63
+ query: {
64
+ grant_type: "client_credentials"
65
+ },
66
+ maxRedirections: 1
67
+ };
68
+ return commonRequest("POST", "/oauth2/v1/accesstoken", options).then(
69
+ (response) => {
70
+ return {
71
+ ...state,
72
+ configuration: {
73
+ ...state.configuration,
74
+ access_token: response.body.access_token
75
+ }
76
+ };
77
+ }
78
+ );
79
+ } else {
80
+ throw new Error(
81
+ "Invalid authorization credentials. Include clientId and clientSecret in state.configuration"
82
+ );
83
+ }
84
+ };
85
+ var prepareNextState = (state, response, callback = (s) => s) => {
86
+ const { body, ...responseWithoutBody } = response;
87
+ const nextState = {
88
+ ...composeNextState(state, response.body),
89
+ response: responseWithoutBody
90
+ };
91
+ return callback(nextState);
92
+ };
93
+ async function request(configuration, path, opts) {
94
+ const { baseUrl, access_token } = configuration;
95
+ const {
96
+ method,
97
+ data,
98
+ params = {},
99
+ parseAs = "json",
100
+ contentType = "application/json"
101
+ } = opts;
102
+ const headers = {
103
+ Authorization: `Bearer ${access_token}`,
104
+ "User-Agent": generateUserAgent(),
105
+ "content-type": contentType
106
+ };
107
+ const options = {
108
+ body: data,
109
+ headers,
110
+ query: params,
111
+ parseAs,
112
+ maxRedirections: 1,
113
+ baseUrl: `${baseUrl}/fhir-r4/v1/`
114
+ };
115
+ return commonRequest(method, path, options).then(logResponse);
116
+ }
117
+
118
+ // src/Adaptor.js
119
+ import {
120
+ fn,
121
+ alterState,
122
+ arrayToString,
123
+ combine,
124
+ dataPath,
125
+ dataValue,
126
+ each,
127
+ field,
128
+ fields,
129
+ http,
130
+ lastReferenceValue,
131
+ merge,
132
+ sourceValue
133
+ } from "@openfn/language-common";
134
+ function execute(...operations) {
135
+ const initialState = {
136
+ references: [],
137
+ data: null
138
+ };
139
+ return (state) => {
140
+ return commonExecute(
141
+ authorize,
142
+ ...operations
143
+ )({
144
+ ...initialState,
145
+ ...state
146
+ });
147
+ };
148
+ }
149
+ function get(path, params = {}, callback = (s) => s) {
150
+ return async (state) => {
151
+ const [resolvedPath, resolvedParams] = expandReferences(
152
+ state,
153
+ path,
154
+ params
155
+ );
156
+ try {
157
+ const response = await request(state.configuration, `/${resolvedPath}`, {
158
+ method: "GET",
159
+ params: resolvedParams
160
+ });
161
+ return prepareNextState(state, response, callback);
162
+ } catch (e) {
163
+ throw e.body ?? e;
164
+ }
165
+ };
166
+ }
167
+ function post(path, data, params = {}, callback = (s) => s) {
168
+ return async (state) => {
169
+ const [resolvedPath, resolvedData, resolvedParams] = expandReferences(
170
+ state,
171
+ path,
172
+ data,
173
+ params
174
+ );
175
+ try {
176
+ const response = await request(state.configuration, `/${resolvedPath}`, {
177
+ method: "POST",
178
+ data: resolvedData,
179
+ params: resolvedParams
180
+ });
181
+ return prepareNextState(state, response, callback);
182
+ } catch (e) {
183
+ throw e.body.error ?? e;
184
+ }
185
+ };
186
+ }
187
+ function put(path, data, params = {}, callback = (s) => s) {
188
+ return async (state) => {
189
+ const [resolvedPath, resolvedData, resolvedParams] = expandReferences(
190
+ state,
191
+ path,
192
+ data,
193
+ params
194
+ );
195
+ try {
196
+ const response = await request(state.configuration, `/${resolvedPath}`, {
197
+ method: "PUT",
198
+ data: resolvedData,
199
+ params: resolvedParams
200
+ });
201
+ return prepareNextState(state, response, callback);
202
+ } catch (e) {
203
+ throw e.body.error ?? e;
204
+ }
205
+ };
206
+ }
207
+ function patch(path, data, params = {}, callback = (s) => s) {
208
+ return async (state) => {
209
+ const [resolvedPath, resolvedData, resolvedParams] = expandReferences(
210
+ state,
211
+ path,
212
+ data,
213
+ params
214
+ );
215
+ try {
216
+ const response = await request(state.configuration, `/${resolvedPath}`, {
217
+ method: "PATCH",
218
+ data: JSON.stringify(resolvedData),
219
+ params: resolvedParams,
220
+ contentType: "application/json-patch+json"
221
+ });
222
+ return prepareNextState(state, response, callback);
223
+ } catch (e) {
224
+ throw e.body ?? e;
225
+ }
226
+ };
227
+ }
228
+
229
+ // src/index.js
230
+ var src_default = Adaptor_exports;
231
+ export {
232
+ alterState,
233
+ arrayToString,
234
+ combine,
235
+ dataPath,
236
+ dataValue,
237
+ src_default as default,
238
+ each,
239
+ execute,
240
+ field,
241
+ fields,
242
+ fn,
243
+ get,
244
+ http,
245
+ lastReferenceValue,
246
+ merge,
247
+ patch,
248
+ post,
249
+ put,
250
+ sourceValue
251
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@openfn/language-satusehat",
3
+ "version": "1.0.0",
4
+ "description": "OpenFn satusehat adaptor",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "require": "./dist/index.cjs"
10
+ },
11
+ "./package.json": "./package.json"
12
+ },
13
+ "author": "Open Function Group",
14
+ "license": "LGPLv3",
15
+ "files": [
16
+ "dist/",
17
+ "types/",
18
+ "ast.json",
19
+ "configuration-schema.json"
20
+ ],
21
+ "dependencies": {
22
+ "@openfn/language-common": "1.13.4"
23
+ },
24
+ "devDependencies": {
25
+ "assertion-error": "2.0.0",
26
+ "chai": "4.3.6",
27
+ "deep-eql": "4.1.1",
28
+ "esno": "^0.16.3",
29
+ "mocha": "9.2.2",
30
+ "rimraf": "3.0.2",
31
+ "undici": "^5.22.1"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/openfn/adaptors.git"
36
+ },
37
+ "types": "types/index.d.ts",
38
+ "main": "dist/index.cjs",
39
+ "scripts": {
40
+ "build": "pnpm clean && build-adaptor satusehat",
41
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
42
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
43
+ "clean": "rimraf dist types docs",
44
+ "pack": "pnpm pack --pack-destination ../../dist",
45
+ "lint": "eslint src"
46
+ }
47
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Execute a sequence of operations.
3
+ * Wraps `language-common/execute` to make working with this API easier.
4
+ * @example
5
+ * execute(
6
+ * create('foo'),
7
+ * delete('bar')
8
+ * )(state)
9
+ * @private
10
+ * @param {Operations} operations - Operations to be performed.
11
+ * @returns {Operation}
12
+ */
13
+ export function execute(...operations: Operations): Operation;
14
+ /**
15
+ * Make a get request to any satusehat endpoint
16
+ * @public
17
+ * @example
18
+ * get("Organization", {"name": "somename"})
19
+ * @function
20
+ * @param {string} path - Path to resource
21
+ * @param {object} params - Optional request params such as name.
22
+ * @param {function} callback - An optional callback to handle the response
23
+ * @returns {Operation}
24
+ */
25
+ export function get(path: string, params?: object, callback?: Function): Operation;
26
+ /**
27
+ * Make a post request to satusehat
28
+ * @example
29
+ * post(
30
+ * "Organization",
31
+ * { "resourceType": "Organization", "active": true,
32
+ * }
33
+ * );
34
+ * @function
35
+ * @param {string} path - Path to resource
36
+ * @param {object} data - Object or JSON which defines data that will be used to create a given instance of resource
37
+ * @param {Object} params - Optional request params.
38
+ * @param {function} [callback] - Optional callback to handle the response
39
+ * @returns {Operation}
40
+ */
41
+ export function post(path: string, data: object, params?: any, callback?: Function): Operation;
42
+ /**
43
+ * Make a put request to satusehat
44
+ * @example
45
+ * put(
46
+ * "Organization/123",
47
+ * { "resourceType": "Organization", "active": false,
48
+ * }
49
+ * );
50
+ * @function
51
+ * @param {string} path - Path to resource and exact item to be updated
52
+ * @param {object} data - Object or JSON which defines data that will be used to update a given instance of resource
53
+ * @param {Object} params - Optional request params.
54
+ * @param {function} [callback] - Optional callback to handle the response
55
+ * @returns {Operation}
56
+ */
57
+ export function put(path: string, data: object, params?: any, callback?: Function): Operation;
58
+ /**
59
+ * Make a patch request to satusehat
60
+ * @example
61
+ * patch(
62
+ * "Organization/123",
63
+ * [{
64
+ * "op": "replace", // Operation - `replace` is the only one used to change a specific property or element
65
+ * "path": "/language", // Path - The name of property/element of resource to be replaced
66
+ * "value": "id" // Value- The value to be replaced
67
+ *}]
68
+ *
69
+ * );
70
+ * @function
71
+ * @param {string} path - Path to resource and exact item to be partially updated
72
+ * @param {Array} data - An array of objects which defines data that will be used to partially update a given instance of resource
73
+ * @param {Object} params - Optional request params.
74
+ * @param {function} [callback] - Optional callback to handle the response
75
+ * @returns {Operation}
76
+ */
77
+ export function patch(path: string, data: any[], params?: any, callback?: Function): Operation;
78
+ export { fn, alterState, arrayToString, combine, dataPath, dataValue, each, field, fields, http, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";
@@ -0,0 +1,3 @@
1
+ export function request(configuration: any, path: any, opts: any): Promise<any>;
2
+ export function authorize(state: any): any;
3
+ export function prepareNextState(state: any, response: any, callback?: (s: any) => any): any;
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";