@openeo/js-client 2.5.1 → 2.6.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.
@@ -1,49 +1,70 @@
1
- const Environment = require('./env');
2
- const Utils = require('@openeo/js-commons/src/utils');
3
- const AuthProvider = require('./authprovider');
4
-
5
- /**
6
- * The Authentication Provider for HTTP Basic.
7
- *
8
- * @augments AuthProvider
9
- */
10
- class BasicProvider extends AuthProvider {
11
-
12
- /**
13
- * Creates a new BasicProvider instance to authenticate using HTTP Basic.
14
- *
15
- * @param {Connection} connection - A Connection object representing an established connection to an openEO back-end.
16
- */
17
- constructor(connection) {
18
- super("basic", connection, {
19
- id: null,
20
- title: "HTTP Basic",
21
- description: "Login with username and password using the method HTTP Basic."
22
- });
23
- }
24
-
25
- /**
26
- * Authenticate with HTTP Basic.
27
- *
28
- * @async
29
- * @param {string} username
30
- * @param {string} password
31
- * @returns {Promise<void>}
32
- * @throws {Error}
33
- */
34
- async login(username, password) {
35
- let response = await this.connection._send({
36
- method: 'get',
37
- responseType: 'json',
38
- url: '/credentials/basic',
39
- headers: {'Authorization': 'Basic ' + Environment.base64encode(username + ':' + password)}
40
- });
41
- if (!Utils.isObject(response.data) || typeof response.data.access_token !== 'string') {
42
- throw new Error("No access_token returned.");
43
- }
44
- this.setToken(response.data.access_token);
45
- }
46
-
47
- }
48
-
1
+ const Environment = require('./env');
2
+ const Utils = require('@openeo/js-commons/src/utils');
3
+ const AuthProvider = require('./authprovider');
4
+
5
+ /**
6
+ * The Authentication Provider for HTTP Basic.
7
+ *
8
+ * @augments AuthProvider
9
+ */
10
+ class BasicProvider extends AuthProvider {
11
+
12
+ /**
13
+ * Creates a new BasicProvider instance to authenticate using HTTP Basic.
14
+ *
15
+ * @param {Connection} connection - A Connection object representing an established connection to an openEO back-end.
16
+ */
17
+ constructor(connection) {
18
+ super("basic", connection, {
19
+ id: null,
20
+ title: "HTTP Basic",
21
+ description: "Login with username and password using the method HTTP Basic."
22
+ });
23
+ this.username = null;
24
+ }
25
+
26
+ /**
27
+ * Authenticate with HTTP Basic.
28
+ *
29
+ * @async
30
+ * @param {string} username
31
+ * @param {string} password
32
+ * @returns {Promise<void>}
33
+ * @throws {Error}
34
+ */
35
+ async login(username, password) {
36
+ let response = await this.connection._send({
37
+ method: 'get',
38
+ responseType: 'json',
39
+ url: '/credentials/basic',
40
+ headers: {'Authorization': 'Basic ' + Environment.base64encode(username + ':' + password)}
41
+ });
42
+ if (!Utils.isObject(response.data) || typeof response.data.access_token !== 'string') {
43
+ throw new Error("No access_token returned.");
44
+ }
45
+ this.username = username;
46
+ this.setToken(response.data.access_token);
47
+ }
48
+
49
+ /**
50
+ * Returns a display name for the authenticated user.
51
+ *
52
+ * @returns {string?} Name of the user or `null`
53
+ */
54
+ getDisplayName() {
55
+ return this.username;
56
+ }
57
+
58
+ /**
59
+ * Logout from the established session.
60
+ *
61
+ * @async
62
+ */
63
+ async logout() {
64
+ this.username = null;
65
+ await super.logout();
66
+ }
67
+
68
+ }
69
+
49
70
  module.exports = BasicProvider;
package/src/browser.js CHANGED
@@ -1,168 +1,168 @@
1
- /**
2
- * Platform dependant utilities for the openEO JS Client.
3
- *
4
- * Browser implementation, don't use in other environments.
5
- *
6
- * @hideconstructor
7
- */
8
- class Environment {
9
-
10
- /**
11
- * Returns the name of the Environment, here `Browser`.
12
- *
13
- * @returns {string}
14
- * @static
15
- */
16
- static getName() {
17
- return 'Browser';
18
- }
19
-
20
- /**
21
- * Returns the current URL of the browser window.
22
- *
23
- * @returns {string}
24
- * @static
25
- */
26
- static getUrl() {
27
- return window.location.toString();
28
- }
29
-
30
- /**
31
- * Sets the URL.
32
- *
33
- * Not supported in Browsers and only throws an Error!
34
- *
35
- * @param {string} uri
36
- * @static
37
- */
38
- static setUrl(uri) { // eslint-disable-line no-unused-vars
39
- throw new Error("setUrl is not supported in a browser environment.");
40
- }
41
-
42
- /**
43
- * Handles errors from the API that are returned as Blobs.
44
- *
45
- * @ignore
46
- * @static
47
- * @param {Blob} error
48
- * @returns {Promise<void>}
49
- */
50
- static handleErrorResponse(error) {
51
- return new Promise((resolve, reject) => {
52
- let fileReader = new FileReader();
53
- fileReader.onerror = event => {
54
- fileReader.abort();
55
- reject(event.target.error);
56
- };
57
- fileReader.onload = () => {
58
- // ArrayBuffer to String conversion is from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
59
- let res = fileReader.result instanceof ArrayBuffer ? String.fromCharCode.apply(null, new Uint16Array(fileReader.result)) : fileReader.result;
60
- let obj = typeof res === 'string' ? JSON.parse(res) : res;
61
- resolve(obj);
62
- };
63
- fileReader.readAsText(error.response.data);
64
- });
65
- }
66
-
67
- /**
68
- * Returns how binary responses from the servers are returned (`stream` or `blob`).
69
- *
70
- * @returns {string}
71
- * @static
72
- */
73
- static getResponseType() {
74
- return 'blob';
75
- }
76
-
77
- /**
78
- * Encodes a string into Base64 encoding.
79
- *
80
- * @static
81
- * @param {string} str - String to encode.
82
- * @returns {string} String encoded in Base64.
83
- */
84
- static base64encode(str) {
85
- // btoa is JS's ugly name for encodeBase64
86
- return btoa(str);
87
- }
88
-
89
- /**
90
- * Detect the file name for the given data source.
91
- *
92
- * @ignore
93
- * @static
94
- * @param {*} source - An object from a file upload form.
95
- * @returns {string}
96
- */
97
- static fileNameForUpload(source) {
98
- return source.name.split(/(\\|\/)/g).pop();
99
- }
100
-
101
- /**
102
- * Get the data from the source that should be uploaded.
103
- *
104
- * @ignore
105
- * @static
106
- * @param {*} source - An object from a file upload form.
107
- * @returns {*}
108
- */
109
- static dataForUpload(source) {
110
- return source;
111
- }
112
-
113
- /**
114
- * Downloads files to local storage and returns a list of file paths.
115
- *
116
- * Not supported in Browsers and only throws an Error!
117
- *
118
- * @static
119
- * @param {Connection} con
120
- * @param {Array.<object.<string, *>>} assets
121
- * @param {string} targetFolder
122
- * @throws {Error}
123
- */
124
- static async downloadResults(con, assets, targetFolder) { // eslint-disable-line no-unused-vars
125
- throw new Error("downloadResults is not supported in a browser environment.");
126
- }
127
-
128
- /**
129
- * Offers data to download in the browser.
130
- *
131
- * This method may fail with overly big data.
132
- *
133
- * @async
134
- * @static
135
- * @param {*} data - Data to download.
136
- * @param {string} filename - File name that is suggested to the user.
137
- * @returns {Promise<void>}
138
- * @see https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
139
- */
140
- static saveToFile(data, filename) {
141
- /* istanbul ignore next */
142
- return new Promise((resolve, reject) => {
143
- try {
144
- if (!(data instanceof Blob)) {
145
- data = new Blob([data], {type: 'application/octet-stream'});
146
- }
147
- let blobURL = window.URL.createObjectURL(data);
148
- let tempLink = document.createElement('a');
149
- tempLink.style.display = 'none';
150
- tempLink.href = blobURL;
151
- tempLink.setAttribute('download', filename || 'download');
152
- if (typeof tempLink.download === 'undefined') {
153
- tempLink.setAttribute('target', '_blank');
154
- }
155
- document.body.appendChild(tempLink);
156
- tempLink.click();
157
- document.body.removeChild(tempLink);
158
- window.URL.revokeObjectURL(blobURL);
159
- resolve();
160
- } catch (error) {
161
- console.error(error);
162
- reject(error);
163
- }
164
- });
165
- }
166
- }
167
-
168
- module.exports = Environment;
1
+ /**
2
+ * Platform dependant utilities for the openEO JS Client.
3
+ *
4
+ * Browser implementation, don't use in other environments.
5
+ *
6
+ * @hideconstructor
7
+ */
8
+ class Environment {
9
+
10
+ /**
11
+ * Returns the name of the Environment, here `Browser`.
12
+ *
13
+ * @returns {string}
14
+ * @static
15
+ */
16
+ static getName() {
17
+ return 'Browser';
18
+ }
19
+
20
+ /**
21
+ * Returns the current URL of the browser window.
22
+ *
23
+ * @returns {string}
24
+ * @static
25
+ */
26
+ static getUrl() {
27
+ return window.location.toString();
28
+ }
29
+
30
+ /**
31
+ * Sets the URL.
32
+ *
33
+ * Not supported in Browsers and only throws an Error!
34
+ *
35
+ * @param {string} uri
36
+ * @static
37
+ */
38
+ static setUrl(uri) { // eslint-disable-line no-unused-vars
39
+ throw new Error("setUrl is not supported in a browser environment.");
40
+ }
41
+
42
+ /**
43
+ * Handles errors from the API that are returned as Blobs.
44
+ *
45
+ * @ignore
46
+ * @static
47
+ * @param {Blob} error
48
+ * @returns {Promise<void>}
49
+ */
50
+ static handleErrorResponse(error) {
51
+ return new Promise((resolve, reject) => {
52
+ let fileReader = new FileReader();
53
+ fileReader.onerror = event => {
54
+ fileReader.abort();
55
+ reject(event.target.error);
56
+ };
57
+ fileReader.onload = () => {
58
+ // ArrayBuffer to String conversion is from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
59
+ let res = fileReader.result instanceof ArrayBuffer ? String.fromCharCode.apply(null, new Uint16Array(fileReader.result)) : fileReader.result;
60
+ let obj = typeof res === 'string' ? JSON.parse(res) : res;
61
+ resolve(obj);
62
+ };
63
+ fileReader.readAsText(error.response.data);
64
+ });
65
+ }
66
+
67
+ /**
68
+ * Returns how binary responses from the servers are returned (`stream` or `blob`).
69
+ *
70
+ * @returns {string}
71
+ * @static
72
+ */
73
+ static getResponseType() {
74
+ return 'blob';
75
+ }
76
+
77
+ /**
78
+ * Encodes a string into Base64 encoding.
79
+ *
80
+ * @static
81
+ * @param {string} str - String to encode.
82
+ * @returns {string} String encoded in Base64.
83
+ */
84
+ static base64encode(str) {
85
+ // btoa is JS's ugly name for encodeBase64
86
+ return btoa(str);
87
+ }
88
+
89
+ /**
90
+ * Detect the file name for the given data source.
91
+ *
92
+ * @ignore
93
+ * @static
94
+ * @param {*} source - An object from a file upload form.
95
+ * @returns {string}
96
+ */
97
+ static fileNameForUpload(source) {
98
+ return source.name.split(/(\\|\/)/g).pop();
99
+ }
100
+
101
+ /**
102
+ * Get the data from the source that should be uploaded.
103
+ *
104
+ * @ignore
105
+ * @static
106
+ * @param {*} source - An object from a file upload form.
107
+ * @returns {*}
108
+ */
109
+ static dataForUpload(source) {
110
+ return source;
111
+ }
112
+
113
+ /**
114
+ * Downloads files to local storage and returns a list of file paths.
115
+ *
116
+ * Not supported in Browsers and only throws an Error!
117
+ *
118
+ * @static
119
+ * @param {Connection} con
120
+ * @param {Array.<object.<string, *>>} assets
121
+ * @param {string} targetFolder
122
+ * @throws {Error}
123
+ */
124
+ static async downloadResults(con, assets, targetFolder) { // eslint-disable-line no-unused-vars
125
+ throw new Error("downloadResults is not supported in a browser environment.");
126
+ }
127
+
128
+ /**
129
+ * Offers data to download in the browser.
130
+ *
131
+ * This method may fail with overly big data.
132
+ *
133
+ * @async
134
+ * @static
135
+ * @param {*} data - Data to download.
136
+ * @param {string} filename - File name that is suggested to the user.
137
+ * @returns {Promise<void>}
138
+ * @see https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
139
+ */
140
+ static saveToFile(data, filename) {
141
+ /* istanbul ignore next */
142
+ return new Promise((resolve, reject) => {
143
+ try {
144
+ if (!(data instanceof Blob)) {
145
+ data = new Blob([data], {type: 'application/octet-stream'});
146
+ }
147
+ let blobURL = window.URL.createObjectURL(data);
148
+ let tempLink = document.createElement('a');
149
+ tempLink.style.display = 'none';
150
+ tempLink.href = blobURL;
151
+ tempLink.setAttribute('download', filename || 'download');
152
+ if (typeof tempLink.download === 'undefined') {
153
+ tempLink.setAttribute('target', '_blank');
154
+ }
155
+ document.body.appendChild(tempLink);
156
+ tempLink.click();
157
+ document.body.removeChild(tempLink);
158
+ window.URL.revokeObjectURL(blobURL);
159
+ resolve();
160
+ } catch (error) {
161
+ console.error(error);
162
+ reject(error);
163
+ }
164
+ });
165
+ }
166
+ }
167
+
168
+ module.exports = Environment;