@openfn/language-mogli 0.3.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/LICENSE +674 -0
- package/LICENSE.LESSER +165 -0
- package/README.md +52 -0
- package/ast.json +781 -0
- package/configuration-schema.json +48 -0
- package/dist/index.cjs +394 -0
- package/dist/index.js +380 -0
- package/package.json +55 -0
- package/types/Adaptor.d.ts +28 -0
- package/types/FakeAdaptor.d.ts +9 -0
- package/types/index.d.ts +5 -0
- package/types/sourceHelpers.d.ts +24 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"properties": {
|
|
4
|
+
"loginUrl": {
|
|
5
|
+
"title": "Login URL",
|
|
6
|
+
"type": "string",
|
|
7
|
+
"description": "Mogli instance login URL",
|
|
8
|
+
"format": "uri",
|
|
9
|
+
"minLength": 1
|
|
10
|
+
},
|
|
11
|
+
"username": {
|
|
12
|
+
"title": "Username",
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "Mogli instance username",
|
|
15
|
+
"minLength": 1
|
|
16
|
+
},
|
|
17
|
+
"password": {
|
|
18
|
+
"title": "Password",
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "Mogli instance password",
|
|
21
|
+
"writeOnly": true,
|
|
22
|
+
"minLength": 1
|
|
23
|
+
},
|
|
24
|
+
"securityToken": {
|
|
25
|
+
"title": "Security Token",
|
|
26
|
+
"type": "string",
|
|
27
|
+
"description": "Mogli instance security token",
|
|
28
|
+
"writeOnly": true,
|
|
29
|
+
"minLength": 1
|
|
30
|
+
},
|
|
31
|
+
"secret": {
|
|
32
|
+
"title": "Secret",
|
|
33
|
+
"type": "string",
|
|
34
|
+
"description": "Mogli instance secret",
|
|
35
|
+
"writeOnly": true,
|
|
36
|
+
"minLength": 1
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"type": "object",
|
|
40
|
+
"additionalProperties": true,
|
|
41
|
+
"required": [
|
|
42
|
+
"loginUrl",
|
|
43
|
+
"username",
|
|
44
|
+
"password",
|
|
45
|
+
"securityToken",
|
|
46
|
+
"secret"
|
|
47
|
+
]
|
|
48
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
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
|
+
FakeAdaptor: () => FakeAdaptor_exports,
|
|
29
|
+
alterState: () => import_language_common3.alterState,
|
|
30
|
+
arrayToString: () => import_language_common3.arrayToString,
|
|
31
|
+
beta: () => import_language_common3.beta,
|
|
32
|
+
combine: () => import_language_common3.combine,
|
|
33
|
+
createSMS: () => createSMS,
|
|
34
|
+
dataPath: () => import_language_common3.dataPath,
|
|
35
|
+
dataValue: () => import_language_common3.dataValue,
|
|
36
|
+
default: () => src_default,
|
|
37
|
+
each: () => import_language_common3.each,
|
|
38
|
+
execute: () => execute,
|
|
39
|
+
field: () => import_language_common3.field,
|
|
40
|
+
fields: () => import_language_common3.fields,
|
|
41
|
+
index: () => import_language_common3.index,
|
|
42
|
+
join: () => import_language_common3.join,
|
|
43
|
+
lastReferenceValue: () => import_language_common3.lastReferenceValue,
|
|
44
|
+
lookup: () => lookup,
|
|
45
|
+
map: () => import_language_common3.map,
|
|
46
|
+
merge: () => import_language_common3.merge,
|
|
47
|
+
reference: () => reference,
|
|
48
|
+
referencePath: () => import_language_common3.referencePath,
|
|
49
|
+
relationship: () => relationship,
|
|
50
|
+
source: () => import_language_common3.source,
|
|
51
|
+
sourceValue: () => import_language_common3.sourceValue,
|
|
52
|
+
steps: () => steps,
|
|
53
|
+
toArray: () => import_language_common3.toArray,
|
|
54
|
+
updateSMS: () => updateSMS
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(src_exports);
|
|
57
|
+
|
|
58
|
+
// src/Adaptor.js
|
|
59
|
+
var Adaptor_exports = {};
|
|
60
|
+
__export(Adaptor_exports, {
|
|
61
|
+
alterState: () => import_language_common3.alterState,
|
|
62
|
+
arrayToString: () => import_language_common3.arrayToString,
|
|
63
|
+
beta: () => import_language_common3.beta,
|
|
64
|
+
combine: () => import_language_common3.combine,
|
|
65
|
+
createSMS: () => createSMS,
|
|
66
|
+
dataPath: () => import_language_common3.dataPath,
|
|
67
|
+
dataValue: () => import_language_common3.dataValue,
|
|
68
|
+
each: () => import_language_common3.each,
|
|
69
|
+
execute: () => execute,
|
|
70
|
+
field: () => import_language_common3.field,
|
|
71
|
+
fields: () => import_language_common3.fields,
|
|
72
|
+
index: () => import_language_common3.index,
|
|
73
|
+
join: () => import_language_common3.join,
|
|
74
|
+
lastReferenceValue: () => import_language_common3.lastReferenceValue,
|
|
75
|
+
lookup: () => lookup,
|
|
76
|
+
map: () => import_language_common3.map,
|
|
77
|
+
merge: () => import_language_common3.merge,
|
|
78
|
+
reference: () => reference,
|
|
79
|
+
referencePath: () => import_language_common3.referencePath,
|
|
80
|
+
relationship: () => relationship,
|
|
81
|
+
source: () => import_language_common3.source,
|
|
82
|
+
sourceValue: () => import_language_common3.sourceValue,
|
|
83
|
+
steps: () => steps,
|
|
84
|
+
toArray: () => import_language_common3.toArray,
|
|
85
|
+
updateSMS: () => updateSMS
|
|
86
|
+
});
|
|
87
|
+
var import_language_common2 = require("@openfn/language-common");
|
|
88
|
+
var import_jsforce = __toESM(require("jsforce"), 1);
|
|
89
|
+
var import_request = __toESM(require("request"), 1);
|
|
90
|
+
var import_lodash_fp = __toESM(require("lodash-fp"), 1);
|
|
91
|
+
|
|
92
|
+
// src/sourceHelpers.js
|
|
93
|
+
var import_language_common = require("@openfn/language-common");
|
|
94
|
+
function lookup(relationshipName, externalId, path) {
|
|
95
|
+
return (0, import_language_common.field)(relationshipName, (state) => {
|
|
96
|
+
return { [externalId]: (0, import_language_common.sourceValue)(path)(state) };
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function relationship(relationshipName, externalId, dataSource) {
|
|
100
|
+
return (0, import_language_common.field)(relationshipName, (state) => {
|
|
101
|
+
if (typeof dataSource == "function") {
|
|
102
|
+
return { [externalId]: dataSource(state) };
|
|
103
|
+
}
|
|
104
|
+
return { [externalId]: dataSource };
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/Adaptor.js
|
|
109
|
+
var import_language_common3 = require("@openfn/language-common");
|
|
110
|
+
var { curry, mapValues, flatten } = import_lodash_fp.default;
|
|
111
|
+
var createSMS = curry(function(params, state) {
|
|
112
|
+
function assembleError({ response, error }) {
|
|
113
|
+
if (response && [200, 201, 202].indexOf(response.statusCode) > -1)
|
|
114
|
+
return false;
|
|
115
|
+
if (error)
|
|
116
|
+
return error;
|
|
117
|
+
return new Error(`Server responded with ${response.statusCode}`);
|
|
118
|
+
}
|
|
119
|
+
let { connection, references } = state;
|
|
120
|
+
const { loginUrl, secret } = state.configuration;
|
|
121
|
+
const body = expandReferences(state, params);
|
|
122
|
+
body.type = "Inbound";
|
|
123
|
+
console.log("Creating new inbound message in Mogli:");
|
|
124
|
+
console.log("======================================");
|
|
125
|
+
console.log(" URL: " + url + '" \n');
|
|
126
|
+
console.log(" Sender phone number: " + body.sender);
|
|
127
|
+
console.log(' Message content: "' + body.message + '" \n');
|
|
128
|
+
const url = connection.instanceUrl.concat(
|
|
129
|
+
"/services/apexrest/Mogli_SMS/v1/sms"
|
|
130
|
+
);
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
import_request.default.post(
|
|
133
|
+
{
|
|
134
|
+
url,
|
|
135
|
+
auth: {
|
|
136
|
+
bearer: connection.accessToken
|
|
137
|
+
},
|
|
138
|
+
headers: {
|
|
139
|
+
"x-api-key": secret
|
|
140
|
+
},
|
|
141
|
+
json: body
|
|
142
|
+
},
|
|
143
|
+
function(error, response, postResponseBody) {
|
|
144
|
+
error = assembleError({ error, response });
|
|
145
|
+
if (error) {
|
|
146
|
+
console.error("POST failed.");
|
|
147
|
+
reject(error);
|
|
148
|
+
} else {
|
|
149
|
+
console.log("POST succeeded.");
|
|
150
|
+
resolve(body);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
}).then(function(recordResult) {
|
|
155
|
+
console.log("Result : " + JSON.stringify(recordResult));
|
|
156
|
+
return {
|
|
157
|
+
...state,
|
|
158
|
+
references: [recordResult, ...state.references]
|
|
159
|
+
};
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
var updateSMS = curry(function(params, state) {
|
|
163
|
+
function assembleError({ response, error }) {
|
|
164
|
+
if (response && [200, 201, 202].indexOf(response.statusCode) > -1)
|
|
165
|
+
return false;
|
|
166
|
+
if (error)
|
|
167
|
+
return error;
|
|
168
|
+
return new Error(`Server responded with ${response.statusCode}`);
|
|
169
|
+
}
|
|
170
|
+
let { connection, references } = state;
|
|
171
|
+
const { loginUrl } = state.configuration;
|
|
172
|
+
const body = expandReferences(state, params);
|
|
173
|
+
console.log("Updating SMS message status in Mogli:");
|
|
174
|
+
console.log("=====================================");
|
|
175
|
+
console.log(" URL: " + url + '" \n');
|
|
176
|
+
console.log(" Message Id: " + body.Id);
|
|
177
|
+
console.log(' Message Status: "' + body.status + '" \n');
|
|
178
|
+
const url = connection.instanceUrl.concat(
|
|
179
|
+
"/services/apexrest/Mogli_SMS/v1/sms"
|
|
180
|
+
);
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
import_request.default.put(
|
|
183
|
+
{
|
|
184
|
+
url,
|
|
185
|
+
auth: {
|
|
186
|
+
bearer: connection.accessToken
|
|
187
|
+
},
|
|
188
|
+
json: body
|
|
189
|
+
},
|
|
190
|
+
function(error, response, postResponseBody) {
|
|
191
|
+
error = assembleError({ error, response });
|
|
192
|
+
if (error) {
|
|
193
|
+
console.error("POST failed.");
|
|
194
|
+
reject(error);
|
|
195
|
+
} else {
|
|
196
|
+
console.log("POST succeeded.");
|
|
197
|
+
resolve(body);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
);
|
|
201
|
+
}).then(function(recordResult) {
|
|
202
|
+
console.log("Result : " + JSON.stringify(recordResult));
|
|
203
|
+
return {
|
|
204
|
+
...state,
|
|
205
|
+
references: [recordResult, ...state.references]
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
var reference = curry(function(position, { references }) {
|
|
210
|
+
return references[position].id;
|
|
211
|
+
});
|
|
212
|
+
function createConnection(state) {
|
|
213
|
+
const { loginUrl } = state.configuration;
|
|
214
|
+
if (!loginUrl) {
|
|
215
|
+
throw new Error("loginUrl missing from configuration.");
|
|
216
|
+
}
|
|
217
|
+
return { ...state, connection: new import_jsforce.default.Connection({ loginUrl }) };
|
|
218
|
+
}
|
|
219
|
+
function login(state) {
|
|
220
|
+
const { username, password, securityToken } = state.configuration;
|
|
221
|
+
let { connection } = state;
|
|
222
|
+
console.info(`Logging in as ${username}.`);
|
|
223
|
+
return connection.login(username, password + securityToken).then(() => state);
|
|
224
|
+
}
|
|
225
|
+
function execute(...operations) {
|
|
226
|
+
const initialState = {
|
|
227
|
+
logger: {
|
|
228
|
+
info: console.info.bind(console),
|
|
229
|
+
debug: console.log.bind(console)
|
|
230
|
+
},
|
|
231
|
+
references: [],
|
|
232
|
+
data: null,
|
|
233
|
+
configuration: {}
|
|
234
|
+
};
|
|
235
|
+
return (state) => {
|
|
236
|
+
return (0, import_language_common2.execute)(
|
|
237
|
+
createConnection,
|
|
238
|
+
login,
|
|
239
|
+
...flatten(operations),
|
|
240
|
+
cleanupState
|
|
241
|
+
)({ ...initialState, ...state });
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function cleanupState(state) {
|
|
245
|
+
delete state.connection;
|
|
246
|
+
return state;
|
|
247
|
+
}
|
|
248
|
+
function steps(...operations) {
|
|
249
|
+
return flatten(operations);
|
|
250
|
+
}
|
|
251
|
+
function expandReferences(state, attrs) {
|
|
252
|
+
return mapValues(function(value) {
|
|
253
|
+
return typeof value == "function" ? value(state) : value;
|
|
254
|
+
})(attrs);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// src/FakeAdaptor.js
|
|
258
|
+
var FakeAdaptor_exports = {};
|
|
259
|
+
__export(FakeAdaptor_exports, {
|
|
260
|
+
arrayToString: () => import_language_common5.arrayToString,
|
|
261
|
+
beta: () => import_language_common5.beta,
|
|
262
|
+
combine: () => import_language_common5.combine,
|
|
263
|
+
create: () => create,
|
|
264
|
+
dataPath: () => import_language_common5.dataPath,
|
|
265
|
+
dataValue: () => import_language_common5.dataValue,
|
|
266
|
+
each: () => import_language_common5.each,
|
|
267
|
+
execute: () => execute2,
|
|
268
|
+
field: () => import_language_common5.field,
|
|
269
|
+
fields: () => import_language_common5.fields,
|
|
270
|
+
index: () => import_language_common5.index,
|
|
271
|
+
join: () => import_language_common5.join,
|
|
272
|
+
lastReferenceValue: () => import_language_common5.lastReferenceValue,
|
|
273
|
+
lookup: () => lookup,
|
|
274
|
+
map: () => import_language_common5.map,
|
|
275
|
+
merge: () => import_language_common5.merge,
|
|
276
|
+
reference: () => reference2,
|
|
277
|
+
referencePath: () => import_language_common5.referencePath,
|
|
278
|
+
relationship: () => relationship,
|
|
279
|
+
source: () => import_language_common5.source,
|
|
280
|
+
sourceValue: () => import_language_common5.sourceValue,
|
|
281
|
+
steps: () => steps2,
|
|
282
|
+
toArray: () => import_language_common5.toArray,
|
|
283
|
+
update: () => update,
|
|
284
|
+
upsert: () => upsert
|
|
285
|
+
});
|
|
286
|
+
var import_language_common4 = require("@openfn/language-common");
|
|
287
|
+
var import_lodash_fp2 = __toESM(require("lodash-fp"), 1);
|
|
288
|
+
var import_language_common5 = require("@openfn/language-common");
|
|
289
|
+
var { curry: curry2, mapValues: mapValues2, flatten: flatten2 } = import_lodash_fp2.default;
|
|
290
|
+
function steps2(...operations) {
|
|
291
|
+
return flatten2(operations);
|
|
292
|
+
}
|
|
293
|
+
function expandReferences2(attrs, state) {
|
|
294
|
+
return mapValues2(function(value) {
|
|
295
|
+
return typeof value == "function" ? value(state) : value;
|
|
296
|
+
})(attrs);
|
|
297
|
+
}
|
|
298
|
+
function create(sObject, fields3) {
|
|
299
|
+
return (state) => {
|
|
300
|
+
state.logger.debug(`Creating ${sObject}`);
|
|
301
|
+
state.logger.debug(JSON.stringify(state.data, null, 2));
|
|
302
|
+
state.logger.debug("===================");
|
|
303
|
+
let id = state.references.length + 1;
|
|
304
|
+
let result = { sObject, fields: expandReferences2(fields3, state), id };
|
|
305
|
+
return {
|
|
306
|
+
...state,
|
|
307
|
+
references: [result, ...state.references]
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function update(sObject, fields3) {
|
|
312
|
+
return (state) => {
|
|
313
|
+
state.logger.debug(`Updating ${sObject}`);
|
|
314
|
+
state.logger.debug(JSON.stringify(state.data, null, 2));
|
|
315
|
+
state.logger.debug("===================");
|
|
316
|
+
let id = state.references.length + 1;
|
|
317
|
+
let result = { sObject, fields: expandReferences2(fields3, state), id };
|
|
318
|
+
return {
|
|
319
|
+
...state,
|
|
320
|
+
references: [result, ...state.references]
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function upsert(sObject, externalId, fields3) {
|
|
325
|
+
return (state) => {
|
|
326
|
+
state.logger.debug(`Upserting ${sObject} with externalId:`, externalId);
|
|
327
|
+
state.logger.debug(JSON.stringify(state.data, null, 2));
|
|
328
|
+
state.logger.debug("===================");
|
|
329
|
+
let id = state.references.length + 1;
|
|
330
|
+
let result = { sObject, fields: expandReferences2(fields3, state), id };
|
|
331
|
+
return {
|
|
332
|
+
...state,
|
|
333
|
+
references: [result, ...state.references]
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
var reference2 = curry2(function(position, { references }) {
|
|
338
|
+
return references[position].id;
|
|
339
|
+
});
|
|
340
|
+
function execute2(...operations) {
|
|
341
|
+
const initialState = {
|
|
342
|
+
logger: {
|
|
343
|
+
info: console.info.bind(console),
|
|
344
|
+
debug: console.log.bind(console)
|
|
345
|
+
},
|
|
346
|
+
references: [],
|
|
347
|
+
data: null
|
|
348
|
+
};
|
|
349
|
+
return (state) => {
|
|
350
|
+
return (0, import_language_common4.execute)(...flatten2(operations), function(state2) {
|
|
351
|
+
delete state2.connection;
|
|
352
|
+
return state2;
|
|
353
|
+
})({ ...initialState, ...state }).then(function(state2) {
|
|
354
|
+
state2.logger.info(JSON.stringify(state2.references, null, 2));
|
|
355
|
+
console.info("Finished Successfully");
|
|
356
|
+
return state2;
|
|
357
|
+
}).catch(function(err) {
|
|
358
|
+
console.error(err.stack);
|
|
359
|
+
console.info("Job failed.");
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/index.js
|
|
365
|
+
var src_default = Adaptor_exports;
|
|
366
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
367
|
+
0 && (module.exports = {
|
|
368
|
+
FakeAdaptor,
|
|
369
|
+
alterState,
|
|
370
|
+
arrayToString,
|
|
371
|
+
beta,
|
|
372
|
+
combine,
|
|
373
|
+
createSMS,
|
|
374
|
+
dataPath,
|
|
375
|
+
dataValue,
|
|
376
|
+
each,
|
|
377
|
+
execute,
|
|
378
|
+
field,
|
|
379
|
+
fields,
|
|
380
|
+
index,
|
|
381
|
+
join,
|
|
382
|
+
lastReferenceValue,
|
|
383
|
+
lookup,
|
|
384
|
+
map,
|
|
385
|
+
merge,
|
|
386
|
+
reference,
|
|
387
|
+
referencePath,
|
|
388
|
+
relationship,
|
|
389
|
+
source,
|
|
390
|
+
sourceValue,
|
|
391
|
+
steps,
|
|
392
|
+
toArray,
|
|
393
|
+
updateSMS
|
|
394
|
+
});
|