odsl-javascript-sdk 1.0.1 → 1.0.3

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.
Files changed (3) hide show
  1. package/README.md +11 -12
  2. package/package.json +3 -3
  3. package/src/sdk.js +48 -24
package/README.md CHANGED
@@ -4,7 +4,7 @@ The Javascript SDK for the [OpenDataDSL](https://opendatadsl.com) data managemen
4
4
  ## Installation
5
5
  You can install the ODSL Javascript SDK from [npm](https://www.npmjs.com/package/odsl-javascript-sdk):
6
6
 
7
- npm i odsl-javascipt-sdk
7
+ npm i odsl-javascript-sdk
8
8
 
9
9
  ## About
10
10
  This javascript SDK for OpenDataDSL has the following features:
@@ -40,36 +40,35 @@ ODSL.loginWithSecret(config).then(function(odsl) {
40
40
  ### Finding master data
41
41
 
42
42
  ```js
43
- objects = ODSL.list('object', 'public', 'source=ECB')
44
- # objects is a JSON object wrapper containing status, statusText and body
45
- print(objects.body[0])
43
+ objects = ODSL.list('object', 'public', {"source":"ECB"})
44
+ print(objects[0])
46
45
  ```
47
46
 
48
47
  ### Getting master data
49
48
 
50
49
  ```js
51
50
  obj = ODSL.get('object', 'public', '#ECB')
52
- print(obj.body['description'])
51
+ print(obj['description'])
53
52
  ```
54
53
 
55
54
  ### Getting a timeseries
56
55
  ```js
57
56
  ts = ODSL.get('data', 'public', '#ABN_FX.EURUSD:SPOT')
58
- print(ts.body)
57
+ print(ts)
59
58
  ```
60
59
 
61
60
  ### Updating some private master data
62
61
  ```js
63
- var = {
64
- '_id': 'AAA.PYTHON',
65
- 'name': 'Python Example'
62
+ var body = {
63
+ "_id":"AAA.JS",
64
+ "name": "Javascript Update Object"
66
65
  }
67
- ODSL.update('object', 'private', var)
66
+ ODSL.update('object', 'private', body)
68
67
  ```
69
68
 
70
69
  ### Reading and updating some private master data
71
70
  ```js
72
71
  po = ODSL.get('object', 'private', 'AAA.TEST')
73
- po.body['description'] = 'Updated from Javascript'
74
- ODSL.update('object', 'private', po.body)
72
+ po['description'] = 'Updated from Javascript'
73
+ ODSL.update('object', 'private', po)
75
74
  ```
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "odsl-javascript-sdk",
3
3
  "type": "module",
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "description": "Javascript SDK for OpenDataDSL",
6
6
  "main": "src/sdk.js",
7
7
  "scripts": {
8
8
  "test": "mocha test/test.js",
9
+ "testUpdate": "mocha test/testUpdate.js",
9
10
  "npm:publish": "npm publish"
10
11
  },
11
12
  "repository": {
@@ -25,12 +26,11 @@
25
26
  },
26
27
  "homepage": "https://github.com/OpenDataDSL/odsl-javascript-sdk#readme",
27
28
  "dependencies": {
28
- "@azure/msal-browser": "^3.11.1",
29
29
  "@azure/msal-node": "^2.6.6",
30
- "mocha": "^10.4.0",
31
30
  "xmlhttprequest": "^1.8.0"
32
31
  },
33
32
  "devDependencies": {
33
+ "mocha": "^10.4.0",
34
34
  "dotenv": "^16.4.5"
35
35
  }
36
36
  }
package/src/sdk.js CHANGED
@@ -42,26 +42,22 @@ export default class ODSL {
42
42
  console.log("Token Acquisition Failed: " + err);
43
43
  }
44
44
  }
45
+ static async loginWithToken(token) {
46
+ return new ODSL({accessToken:token});
47
+ }
45
48
  get(service, source, id) {
46
49
  try {
47
50
  var xhttp = new XMLHttpRequest();
48
- var url = this.host + service + "/v1/" + source + "/" + id;
51
+ id = encodeURIComponent(id);
52
+ var url = new URL(this.host + service + "/v1/" + source + "/" + id);
49
53
  xhttp.open("GET", url, false);
50
54
  xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
51
55
  xhttp.responseType = "json";
52
56
  xhttp.send();
53
- if (xhttp.status != 200) {
54
- return {
55
- status: xhttp.status,
56
- statusText: xhttp.getResponseHeader("x-odsl-error"),
57
- body: undefined
58
- };
57
+ if (xhttp.status < 200 || xhttp.status >= 300) {
58
+ throw xhttp.getResponseHeader("x-odsl-error");
59
59
  }
60
- return {
61
- status: xhttp.status,
62
- statusText: xhttp.statusText,
63
- body: JSON.parse(xhttp.responseText)
64
- };
60
+ return JSON.parse(xhttp.responseText);
65
61
  } catch (err) {
66
62
  console.log("GET request Failed: " + err);
67
63
  }
@@ -69,28 +65,56 @@ export default class ODSL {
69
65
  getAsync(service, source, id, callback) {
70
66
  try {
71
67
  var xhttp = new XMLHttpRequest();
72
- var url = this.host + service + "/v1/" + source + "/" + id;
68
+ id = encodeURIComponent(id);
69
+ var url = new URL(this.host + service + "/v1/" + source + "/" + id);
73
70
  xhttp.open("GET", url);
74
71
  xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
75
72
  xhttp.responseType = "json";
76
73
  xhttp.send();
77
74
  xhttp.onload = function() {
78
- if (xhttp.status != 200) {
79
- callback({
80
- status: xhttp.status,
81
- statusText: xhttp.getResponseHeader("x-odsl-error"),
82
- body: undefined
83
- });
75
+ if (xhttp.status < 200 || xhttp.status >= 300) {
76
+ throw xhttp.getResponseHeader("x-odsl-error");
84
77
  } else {
85
- callback({
86
- status: xhttp.status,
87
- statusText: xhttp.statusText,
88
- body: JSON.parse(xhttp.responseText)
89
- });
78
+ callback(JSON.parse(xhttp.responseText));
90
79
  }
91
80
  }
92
81
  } catch (err) {
93
82
  console.log("GET request Failed: " + err);
94
83
  }
95
84
  }
85
+ list(service, source, filter) {
86
+ try {
87
+ var xhttp = new XMLHttpRequest();
88
+ var url = new URL(this.host + service + "/v1/" + source);
89
+ if (filter != undefined) {
90
+ url.searchParams.set("_filter", JSON.stringify(filter));
91
+ }
92
+ xhttp.open("GET", url, false);
93
+ xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
94
+ xhttp.responseType = "json";
95
+ xhttp.send();
96
+ if (xhttp.status < 200 || xhttp.status >= 300) {
97
+ throw xhttp.getResponseHeader("x-odsl-error");
98
+ }
99
+ return JSON.parse(xhttp.responseText);
100
+ } catch (err) {
101
+ console.log("GET request Failed: " + err);
102
+ }
103
+ }
104
+ update(service, source, body) {
105
+ try {
106
+ var xhttp = new XMLHttpRequest();
107
+ var url = new URL(this.host + service + "/v1/" + source);
108
+ xhttp.open("POST", url, false);
109
+ xhttp.setRequestHeader("Authorization", "Bearer " + this.token);
110
+ xhttp.responseType = "json";
111
+ xhttp.send(JSON.stringify(body));
112
+ if (xhttp.status < 200 || xhttp.status >= 300) {
113
+ throw xhttp.getResponseHeader("x-odsl-error");
114
+ }
115
+ return JSON.parse(xhttp.responseText);
116
+ } catch (err) {
117
+ console.log("GET request Failed: " + err);
118
+ }
119
+ }
96
120
  }