odsl-javascript-sdk 1.0.3 → 1.0.5
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/.node-xmlhttprequest-sync-116040 +0 -0
- package/.node-xmlhttprequest-sync-148376 +0 -0
- package/package.json +7 -4
- package/src/odsl.js +122 -0
- package/src/process.js +90 -0
- package/src/sdk.js +1 -1
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "odsl-javascript-sdk",
|
|
3
|
-
"type": "
|
|
4
|
-
"version": "1.0.
|
|
3
|
+
"type": "commonjs",
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"description": "Javascript SDK for OpenDataDSL",
|
|
6
6
|
"main": "src/sdk.js",
|
|
7
7
|
"scripts": {
|
|
@@ -26,11 +26,14 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/OpenDataDSL/odsl-javascript-sdk#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
+
"@azure/identity": "^4.13.0",
|
|
29
30
|
"@azure/msal-node": "^2.6.6",
|
|
31
|
+
"@azure/service-bus": "^7.9.5",
|
|
32
|
+
"node-fetch": "^3.3.2",
|
|
30
33
|
"xmlhttprequest": "^1.8.0"
|
|
31
34
|
},
|
|
32
35
|
"devDependencies": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
36
|
+
"dotenv": "^16.4.5",
|
|
37
|
+
"mocha": "^10.4.0"
|
|
35
38
|
}
|
|
36
39
|
}
|
package/src/odsl.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { ConfidentialClientApplication } = require("@azure/msal-node");
|
|
3
|
+
const { PublicClientApplication } = require("@azure/msal-browser");
|
|
4
|
+
const { XMLHttpRequest } = require("xmlhttprequest");
|
|
5
|
+
|
|
6
|
+
const msalConfig = {
|
|
7
|
+
"auth": {
|
|
8
|
+
"clientId": "d3742f5f-3d4d-4565-a80a-ebdefaab8d08",
|
|
9
|
+
"authority": "https://login.microsoft.com/common"
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
module.exports = class ODSL {
|
|
14
|
+
token = null;
|
|
15
|
+
// host = "https://odsl-dev.azurewebsites.net/api/";
|
|
16
|
+
host = "http://localhost:7071/api/";
|
|
17
|
+
constructor(loginResponse) {
|
|
18
|
+
this.token = loginResponse.accessToken;
|
|
19
|
+
}
|
|
20
|
+
static async login() {
|
|
21
|
+
try {
|
|
22
|
+
let msalInstance = new pcbrowser(msalConfig);
|
|
23
|
+
console.log("Initialising");
|
|
24
|
+
await msalInstance.initialize();
|
|
25
|
+
console.log("Logging In");
|
|
26
|
+
const loginResponse = await msalInstance.loginPopup(loginRequest);
|
|
27
|
+
return new ODSL(loginResponse);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.log("Token Acquisition Failed: " + err);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
static async loginWithSecret(config) {
|
|
33
|
+
try {
|
|
34
|
+
let loginRequest = {
|
|
35
|
+
scopes: ["api://opendatadsl/.default"]
|
|
36
|
+
}
|
|
37
|
+
let msalInstance = new ConfidentialClientApplication(config);
|
|
38
|
+
console.log("Logging In");
|
|
39
|
+
const loginResponse = await msalInstance.acquireTokenByClientCredential(loginRequest);
|
|
40
|
+
return new ODSL(loginResponse);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.log("Token Acquisition Failed: " + err);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
static loginWithToken(token) {
|
|
46
|
+
return new ODSL({accessToken:token});
|
|
47
|
+
}
|
|
48
|
+
get(service, source, id) {
|
|
49
|
+
try {
|
|
50
|
+
var xhttp = new XMLHttpRequest();
|
|
51
|
+
id = encodeURIComponent(id);
|
|
52
|
+
var url = new URL(this.host + service + "/v1/" + source + "/" + id);
|
|
53
|
+
xhttp.open("GET", url, false);
|
|
54
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
55
|
+
xhttp.responseType = "json";
|
|
56
|
+
xhttp.send();
|
|
57
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
58
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
59
|
+
}
|
|
60
|
+
return JSON.parse(xhttp.responseText);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.log("GET request Failed: " + err);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
getAsync(service, source, id, callback) {
|
|
66
|
+
try {
|
|
67
|
+
var xhttp = new XMLHttpRequest();
|
|
68
|
+
id = encodeURIComponent(id);
|
|
69
|
+
var url = new URL(this.host + service + "/v1/" + source + "/" + id);
|
|
70
|
+
xhttp.open("GET", url);
|
|
71
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
72
|
+
xhttp.responseType = "json";
|
|
73
|
+
xhttp.send();
|
|
74
|
+
xhttp.onload = function() {
|
|
75
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
76
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
77
|
+
} else {
|
|
78
|
+
callback(JSON.parse(xhttp.responseText));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.log("GET request Failed: " + err);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
list(service, source, params) {
|
|
86
|
+
try {
|
|
87
|
+
var xhttp = new XMLHttpRequest();
|
|
88
|
+
var url = new URL(this.host + service + "/v1/" + source);
|
|
89
|
+
if (params != undefined) {
|
|
90
|
+
for (let key of Object.keys(params)) {
|
|
91
|
+
url.searchParams.set(key, params[key]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
xhttp.open("GET", url, false);
|
|
95
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
96
|
+
xhttp.responseType = "json";
|
|
97
|
+
xhttp.send();
|
|
98
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
99
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
100
|
+
}
|
|
101
|
+
return JSON.parse(xhttp.responseText);
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.log("GET request Failed: " + err);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
update(service, source, body) {
|
|
107
|
+
try {
|
|
108
|
+
var xhttp = new XMLHttpRequest();
|
|
109
|
+
var url = new URL(this.host + service + "/v1/" + source);
|
|
110
|
+
xhttp.open("POST", url, false);
|
|
111
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
112
|
+
xhttp.responseType = "json";
|
|
113
|
+
xhttp.send(JSON.stringify(body));
|
|
114
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
115
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
116
|
+
}
|
|
117
|
+
return JSON.parse(xhttp.responseText);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
console.log("GET request Failed: " + err);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/process.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const { ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus");
|
|
2
|
+
|
|
3
|
+
module.exports = class PROCESS {
|
|
4
|
+
constructor(p, task) {
|
|
5
|
+
this.process = p;
|
|
6
|
+
this.task = task;
|
|
7
|
+
var cstr = process.env.ODSL_SB_CPM;
|
|
8
|
+
this.serviceBusClient = new ServiceBusClient(cstr);
|
|
9
|
+
this.started = false;
|
|
10
|
+
this.queue_name = process.env.ODSL_STAGE + "/process-execution";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async startProcess() {
|
|
14
|
+
if (!this.started) {
|
|
15
|
+
console.log("Starting process for task: " + this.task._id);
|
|
16
|
+
var message = new ProcessMessage(this.process, this.task);
|
|
17
|
+
message.status = "start";
|
|
18
|
+
message.message = "Initialising Process";
|
|
19
|
+
await this.updateProcess(message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async endProcess(status, mess) {
|
|
24
|
+
var message = new ProcessMessage(this.process, this.task);
|
|
25
|
+
message.status = status;
|
|
26
|
+
message.message = mess;
|
|
27
|
+
console.log(mess);
|
|
28
|
+
await this.updateProcess(message);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async startPhase(name) {
|
|
32
|
+
this.phase = new ProcessMessage(this.process, this.task);
|
|
33
|
+
this.phase.status = "running";
|
|
34
|
+
this.phase.message = "Initialising Phase: " + name;
|
|
35
|
+
this.phase.setPhase(name);
|
|
36
|
+
await this.updateProcess(this.phase);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async endPhase(status, mess) {
|
|
40
|
+
this.phase.status = status;
|
|
41
|
+
this.phase.message = mess;
|
|
42
|
+
this.phase.timestamp = new Date().toISOString().substring(0,19);
|
|
43
|
+
let level = status == "failed" ? "fatal" : "info"
|
|
44
|
+
let lm = new Date().toISOString() + " " + level + " " + mess;
|
|
45
|
+
this.phase.log.push(lm);
|
|
46
|
+
await this.updateProcess(this.phase);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async logMessage(level, mess) {
|
|
50
|
+
let lm = new Date().toISOString() + " " + level + " " + mess;
|
|
51
|
+
console.log(lm);
|
|
52
|
+
this.phase.log.push(lm);
|
|
53
|
+
await this.updateProcess(this.phase);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async updateProcess(message) {
|
|
57
|
+
var sbm = {
|
|
58
|
+
body: message,
|
|
59
|
+
applicationProperties:{
|
|
60
|
+
tenantid: this.task.tid,
|
|
61
|
+
environment: this.task.environment
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
var sender = this.serviceBusClient.createSender(this.queue_name);
|
|
65
|
+
await sender.sendMessages(sbm);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class ProcessMessage {
|
|
70
|
+
constructor(p, t) {
|
|
71
|
+
this.log = [];
|
|
72
|
+
this.timestamp = new Date().toISOString().substring(0,19);
|
|
73
|
+
this.type = "ProcessMessage";
|
|
74
|
+
this.id = t._id;
|
|
75
|
+
this.tenantid = t.tid;
|
|
76
|
+
this.service = p.service;
|
|
77
|
+
this.name = p.name;
|
|
78
|
+
this.stage = process.env.ODSL_STAGE;
|
|
79
|
+
this.environment = t.environment;
|
|
80
|
+
this.trigger = t.trigger;
|
|
81
|
+
this.triggerTime = t.triggerTime;
|
|
82
|
+
this.reason = t.reason;
|
|
83
|
+
this.status = "triggered";
|
|
84
|
+
this.message = "";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
setPhase(name) {
|
|
88
|
+
this.phase = name;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/sdk.js
CHANGED