odsl-javascript-sdk 1.0.0 → 1.0.2
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/README.md +73 -1
- package/package.json +5 -4
- package/src/sdk.js +45 -24
- package/.vscode/launch.json +0 -15
- package/test/test.js +0 -33
package/README.md
CHANGED
|
@@ -1,2 +1,74 @@
|
|
|
1
1
|
# odsl-javascript-sdk
|
|
2
|
-
Javascript SDK for OpenDataDSL
|
|
2
|
+
The Javascript SDK for the [OpenDataDSL](https://opendatadsl.com) data management platform
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
You can install the ODSL Javascript SDK from [npm](https://www.npmjs.com/package/odsl-javascript-sdk):
|
|
6
|
+
|
|
7
|
+
npm i odsl-javascript-sdk
|
|
8
|
+
|
|
9
|
+
## About
|
|
10
|
+
This javascript SDK for OpenDataDSL has the following features:
|
|
11
|
+
|
|
12
|
+
* Find any data in OpenDataDSL using the ```list``` command
|
|
13
|
+
* Retrieve any data using the ```get``` command
|
|
14
|
+
* Update any data (if you have permission) using the ```update``` command
|
|
15
|
+
|
|
16
|
+
Check out our [demo repository](https://github.com/OpenDataDSL/odsl-javascript-sdk-demo) for examples of real-world usage.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Logging in and getting started
|
|
21
|
+
|
|
22
|
+
#### Logging in using a client credentials flow (client secret)
|
|
23
|
+
```js
|
|
24
|
+
import ODSL from '../src/sdk.js'
|
|
25
|
+
import "dotenv/config";
|
|
26
|
+
|
|
27
|
+
var config = {
|
|
28
|
+
auth: {
|
|
29
|
+
clientId: process.env.clientId,
|
|
30
|
+
authority: process.env.authority,
|
|
31
|
+
clientSecret: process.env.clientSecret
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
ODSL.loginWithSecret(config).then(function(odsl) {
|
|
36
|
+
# Do Something
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Finding master data
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
objects = ODSL.list('object', 'public', {"source":"ECB"})
|
|
44
|
+
print(objects[0])
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Getting master data
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
obj = ODSL.get('object', 'public', '#ECB')
|
|
51
|
+
print(obj['description'])
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Getting a timeseries
|
|
55
|
+
```js
|
|
56
|
+
ts = ODSL.get('data', 'public', '#ABN_FX.EURUSD:SPOT')
|
|
57
|
+
print(ts)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Updating some private master data
|
|
61
|
+
```js
|
|
62
|
+
var body = {
|
|
63
|
+
"_id":"AAA.JS",
|
|
64
|
+
"name": "Javascript Update Object"
|
|
65
|
+
}
|
|
66
|
+
ODSL.update('object', 'private', body)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Reading and updating some private master data
|
|
70
|
+
```js
|
|
71
|
+
po = ODSL.get('object', 'private', 'AAA.TEST')
|
|
72
|
+
po['description'] = 'Updated from Javascript'
|
|
73
|
+
ODSL.update('object', 'private', po)
|
|
74
|
+
```
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "odsl-javascript-sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"description": "Javascript SDK for OpenDataDSL",
|
|
6
6
|
"main": "src/sdk.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "mocha test/test.js"
|
|
8
|
+
"test": "mocha test/test.js",
|
|
9
|
+
"testUpdate": "mocha test/testUpdate.js",
|
|
10
|
+
"npm:publish": "npm publish"
|
|
9
11
|
},
|
|
10
12
|
"repository": {
|
|
11
13
|
"type": "git",
|
|
@@ -24,12 +26,11 @@
|
|
|
24
26
|
},
|
|
25
27
|
"homepage": "https://github.com/OpenDataDSL/odsl-javascript-sdk#readme",
|
|
26
28
|
"dependencies": {
|
|
27
|
-
"@azure/msal-browser": "^3.11.1",
|
|
28
29
|
"@azure/msal-node": "^2.6.6",
|
|
29
|
-
"mocha": "^10.4.0",
|
|
30
30
|
"xmlhttprequest": "^1.8.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
+
"mocha": "^10.4.0",
|
|
33
34
|
"dotenv": "^16.4.5"
|
|
34
35
|
}
|
|
35
36
|
}
|
package/src/sdk.js
CHANGED
|
@@ -45,23 +45,16 @@ export default class ODSL {
|
|
|
45
45
|
get(service, source, id) {
|
|
46
46
|
try {
|
|
47
47
|
var xhttp = new XMLHttpRequest();
|
|
48
|
-
|
|
48
|
+
id = encodeURIComponent(id);
|
|
49
|
+
var url = new URL(this.host + service + "/v1/" + source + "/" + id);
|
|
49
50
|
xhttp.open("GET", url, false);
|
|
50
51
|
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
51
52
|
xhttp.responseType = "json";
|
|
52
53
|
xhttp.send();
|
|
53
|
-
if (xhttp.status
|
|
54
|
-
|
|
55
|
-
status: xhttp.status,
|
|
56
|
-
statusText: xhttp.getResponseHeader("x-odsl-error"),
|
|
57
|
-
body: undefined
|
|
58
|
-
};
|
|
54
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
55
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
59
56
|
}
|
|
60
|
-
return
|
|
61
|
-
status: xhttp.status,
|
|
62
|
-
statusText: xhttp.statusText,
|
|
63
|
-
body: JSON.parse(xhttp.responseText)
|
|
64
|
-
};
|
|
57
|
+
return JSON.parse(xhttp.responseText);
|
|
65
58
|
} catch (err) {
|
|
66
59
|
console.log("GET request Failed: " + err);
|
|
67
60
|
}
|
|
@@ -69,28 +62,56 @@ export default class ODSL {
|
|
|
69
62
|
getAsync(service, source, id, callback) {
|
|
70
63
|
try {
|
|
71
64
|
var xhttp = new XMLHttpRequest();
|
|
72
|
-
|
|
65
|
+
id = encodeURIComponent(id);
|
|
66
|
+
var url = new URL(this.host + service + "/v1/" + source + "/" + id);
|
|
73
67
|
xhttp.open("GET", url);
|
|
74
68
|
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
75
69
|
xhttp.responseType = "json";
|
|
76
70
|
xhttp.send();
|
|
77
71
|
xhttp.onload = function() {
|
|
78
|
-
if (xhttp.status
|
|
79
|
-
|
|
80
|
-
status: xhttp.status,
|
|
81
|
-
statusText: xhttp.getResponseHeader("x-odsl-error"),
|
|
82
|
-
body: undefined
|
|
83
|
-
});
|
|
72
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
73
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
84
74
|
} else {
|
|
85
|
-
callback(
|
|
86
|
-
status: xhttp.status,
|
|
87
|
-
statusText: xhttp.statusText,
|
|
88
|
-
body: JSON.parse(xhttp.responseText)
|
|
89
|
-
});
|
|
75
|
+
callback(JSON.parse(xhttp.responseText));
|
|
90
76
|
}
|
|
91
77
|
}
|
|
92
78
|
} catch (err) {
|
|
93
79
|
console.log("GET request Failed: " + err);
|
|
94
80
|
}
|
|
95
81
|
}
|
|
82
|
+
list(service, source, filter) {
|
|
83
|
+
try {
|
|
84
|
+
var xhttp = new XMLHttpRequest();
|
|
85
|
+
var url = new URL(this.host + service + "/v1/" + source);
|
|
86
|
+
if (filter != undefined) {
|
|
87
|
+
url.searchParams.set("_filter", JSON.stringify(filter));
|
|
88
|
+
}
|
|
89
|
+
xhttp.open("GET", url, false);
|
|
90
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
91
|
+
xhttp.responseType = "json";
|
|
92
|
+
xhttp.send();
|
|
93
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
94
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
95
|
+
}
|
|
96
|
+
return JSON.parse(xhttp.responseText);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
console.log("GET request Failed: " + err);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
update(service, source, body) {
|
|
102
|
+
try {
|
|
103
|
+
var xhttp = new XMLHttpRequest();
|
|
104
|
+
var url = new URL(this.host + service + "/v1/" + source);
|
|
105
|
+
xhttp.open("POST", url, false);
|
|
106
|
+
xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
107
|
+
xhttp.responseType = "json";
|
|
108
|
+
xhttp.send(JSON.stringify(body));
|
|
109
|
+
if (xhttp.status < 200 || xhttp.status >= 300) {
|
|
110
|
+
throw xhttp.getResponseHeader("x-odsl-error");
|
|
111
|
+
}
|
|
112
|
+
return JSON.parse(xhttp.responseText);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.log("GET request Failed: " + err);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
96
117
|
}
|
package/.vscode/launch.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Use IntelliSense to learn about possible attributes.
|
|
3
|
-
// Hover to view descriptions of existing attributes.
|
|
4
|
-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"type": "chrome",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"name": "Launch Chrome against localhost",
|
|
11
|
-
"url": "http://localhost:8080",
|
|
12
|
-
"webRoot": "${workspaceFolder}/test/web"
|
|
13
|
-
}
|
|
14
|
-
]
|
|
15
|
-
}
|
package/test/test.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import ODSL from '../src/sdk.js'
|
|
4
|
-
import "dotenv/config";
|
|
5
|
-
|
|
6
|
-
var config = {
|
|
7
|
-
auth: {
|
|
8
|
-
clientId: process.env.clientId,
|
|
9
|
-
authority: process.env.authority,
|
|
10
|
-
clientSecret: process.env.clientSecret
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
console.log("Asynchronous GET");
|
|
15
|
-
ODSL.loginWithSecret(config).then(function(odsl) {
|
|
16
|
-
odsl.getAsync('object', 'private', 'AAA', function(r) {
|
|
17
|
-
if (r.status == 200) {
|
|
18
|
-
console.log(r.body);
|
|
19
|
-
} else {
|
|
20
|
-
console.log(r.status + ": " + r.statusText);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
console.log("Synchronous GET");
|
|
26
|
-
ODSL.loginWithSecret(config).then(function(odsl) {
|
|
27
|
-
var r = odsl.get('object', 'private', 'AAA');
|
|
28
|
-
if (r.status == 200) {
|
|
29
|
-
console.log(r.body);
|
|
30
|
-
} else {
|
|
31
|
-
console.log(r.status + ": " + r.statusText);
|
|
32
|
-
}
|
|
33
|
-
});
|