@openeo/js-client 2.5.0 → 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,220 +1,265 @@
1
- const Utils = require('@openeo/js-commons/src/utils');
2
-
3
- /**
4
- * Capabilities of a back-end.
5
- */
6
- class Capabilities {
7
-
8
- /**
9
- * Creates a new Capabilities object from an API-compatible JSON response.
10
- *
11
- * @param {object.<string, *>} data - A capabilities response compatible to the API specification for `GET /`.
12
- * @throws {Error}
13
- */
14
- constructor(data) {
15
- if(!Utils.isObject(data)) {
16
- throw new Error("No capabilities retrieved.");
17
- }
18
- if(!data.api_version) {
19
- throw new Error("Invalid capabilities: No API version retrieved");
20
- }
21
- if(!Array.isArray(data.endpoints)) {
22
- throw new Error("Invalid capabilities: No endpoints retrieved");
23
- }
24
-
25
- /**
26
- * @private
27
- * @type {object.<string, *>}
28
- */
29
- this.data = data;
30
-
31
- /**
32
- * @private
33
- * @type {Array.<string>}
34
- */
35
- this.features = this.data.endpoints
36
- // Flatten features to be compatible with the feature map.
37
- .map(e => e.methods.map(method => (method + ' ' + e.path).toLowerCase()))
38
- .reduce((flat, next) => flat.concat(next), []); // .flat(1) once browser support for ECMAscript 10/2019 gets better
39
-
40
- /**
41
- * @private
42
- * @ignore
43
- * @type {object.<string, string>}
44
- */
45
- this.featureMap = {
46
- // Discovery
47
- capabilities: true,
48
- listFileTypes: 'get /file_formats',
49
- listServiceTypes: 'get /service_types',
50
- listUdfRuntimes: 'get /udf_runtimes',
51
- // Collections
52
- listCollections: 'get /collections',
53
- describeCollection: 'get /collections/{collection_id}',
54
- listCollectionItems: 'get /collections/{collection_id}/items',
55
- // Processes
56
- listProcesses: 'get /processes',
57
- describeProcess: 'get /processes',
58
- // Auth / Account
59
- listAuthProviders: true,
60
- authenticateOIDC: 'get /credentials/oidc',
61
- authenticateBasic: 'get /credentials/basic',
62
- describeAccount: 'get /me',
63
- // Files
64
- listFiles: 'get /files',
65
- getFile: 'get /files', // getFile is a virtual function and doesn't request an endpoint, but get /files should be available nevertheless.
66
- uploadFile: 'put /files/{path}',
67
- downloadFile: 'get /files/{path}',
68
- deleteFile: 'delete /files/{path}',
69
- // User-Defined Processes
70
- validateProcess: 'post /validation',
71
- listUserProcesses: 'get /process_graphs',
72
- describeUserProcess: 'get /process_graphs/{process_graph_id}',
73
- getUserProcess: 'get /process_graphs/{process_graph_id}',
74
- setUserProcess: 'put /process_graphs/{process_graph_id}',
75
- replaceUserProcess: 'put /process_graphs/{process_graph_id}',
76
- deleteUserProcess: 'delete /process_graphs/{process_graph_id}',
77
- // Processing
78
- computeResult: 'post /result',
79
- listJobs: 'get /jobs',
80
- createJob: 'post /jobs',
81
- listServices: 'get /services',
82
- createService: 'post /services',
83
- getJob: 'get /jobs/{job_id}',
84
- describeJob: 'get /jobs/{job_id}',
85
- updateJob: 'patch /jobs/{job_id}',
86
- deleteJob: 'delete /jobs/{job_id}',
87
- estimateJob: 'get /jobs/{job_id}/estimate',
88
- debugJob: 'get /jobs/{job_id}/logs',
89
- startJob: 'post /jobs/{job_id}/results',
90
- stopJob: 'delete /jobs/{job_id}/results',
91
- listResults: 'get /jobs/{job_id}/results',
92
- downloadResults: 'get /jobs/{job_id}/results',
93
- // Web services
94
- describeService: 'get /services/{service_id}',
95
- getService: 'get /services/{service_id}',
96
- updateService: 'patch /services/{service_id}',
97
- deleteService: 'delete /services/{service_id}',
98
- debugService: 'get /services/{service_id}/logs',
99
- };
100
- }
101
-
102
- /**
103
- * Returns the capabilities response as a JSON serializable representation of the data that is API compliant.
104
- *
105
- * @returns {object.<string, *>} - A reference to the capabilities response.
106
- */
107
- toJSON() {
108
- return this.data;
109
- }
110
-
111
- /**
112
- * Returns the openEO API version implemented by the back-end.
113
- *
114
- * @returns {string} openEO API version number.
115
- */
116
- apiVersion() {
117
- return this.data.api_version;
118
- }
119
-
120
- /**
121
- * Returns the back-end version number.
122
- *
123
- * @returns {string} openEO back-end version number.
124
- */
125
- backendVersion() {
126
- return this.data.backend_version;
127
- }
128
-
129
- /**
130
- * Returns the back-end title.
131
- *
132
- * @returns {string} Title
133
- */
134
- title() {
135
- return typeof this.data.title === 'string' ? this.data.title : "";
136
- }
137
-
138
- /**
139
- * Returns the back-end description.
140
- *
141
- * @returns {string} Description
142
- */
143
- description() {
144
- return typeof this.data.description === 'string' ? this.data.description : "";
145
- }
146
-
147
- /**
148
- * Is the back-end suitable for use in production?
149
- *
150
- * @returns {boolean} true = stable/production, false = unstable
151
- */
152
- isStable() {
153
- return this.data.production === true;
154
- }
155
-
156
- /**
157
- * Returns the links.
158
- *
159
- * @returns {Array.<Link>} Array of link objects (href, title, rel, type)
160
- */
161
- links() {
162
- return Array.isArray(this.data.links) ? this.data.links : [];
163
- }
164
-
165
- /**
166
- * Lists all supported features.
167
- *
168
- * @returns {Array.<string>} An array of supported features.
169
- */
170
- listFeatures() {
171
- let features = [];
172
- for(let feature in this.featureMap) {
173
- if (this.featureMap[feature] === true || this.features.includes(this.featureMap[feature])) {
174
- features.push(feature);
175
- }
176
- }
177
- return features.sort();
178
- }
179
-
180
- /**
181
- * Check whether a feature is supported by the back-end.
182
- *
183
- * @param {string} methodName - A feature name (corresponds to the JS client method names, see also the feature map for allowed values).
184
- * @returns {boolean} `true` if the feature is supported, otherwise `false`.
185
- */
186
- hasFeature(methodName) {
187
- return this.featureMap[methodName] === true || this.features.some(e => e === this.featureMap[methodName]);
188
- }
189
-
190
- /**
191
- * Get the billing currency.
192
- *
193
- * @returns {string | null} The billing currency or `null` if not available.
194
- */
195
- currency() {
196
- return (Utils.isObject(this.data.billing) && typeof this.data.billing.currency === 'string' ? this.data.billing.currency : null);
197
- }
198
-
199
- /**
200
- * List all billing plans.
201
- *
202
- * @returns {Array.<BillingPlan>} Billing plans
203
- */
204
- listPlans() {
205
- if (Utils.isObject(this.data.billing) && Array.isArray(this.data.billing.plans)) {
206
- let defaultPlan = typeof this.data.billing.default_plan === 'string' ? this.data.billing.default_plan.toLowerCase() : null;
207
- return this.data.billing.plans.map(plan => {
208
- let addition = {
209
- default: (defaultPlan === plan.name.toLowerCase())
210
- };
211
- return Object.assign({}, plan, addition);
212
- });
213
- }
214
- else {
215
- return [];
216
- }
217
- }
218
- }
219
-
220
- module.exports = Capabilities;
1
+ const Utils = require('@openeo/js-commons/src/utils');
2
+
3
+ const FEATURE_MAP = {
4
+ // Discovery
5
+ capabilities: true,
6
+ listFileTypes: 'get /file_formats',
7
+ listServiceTypes: 'get /service_types',
8
+ listUdfRuntimes: 'get /udf_runtimes',
9
+ // Collections
10
+ listCollections: 'get /collections',
11
+ describeCollection: 'get /collections/{}',
12
+ listCollectionItems: 'get /collections/{}/items',
13
+ describeCollectionItem: 'get /collections/{}/items/{}',
14
+ describeCollectionQueryables: 'get /collections/{}/queryables',
15
+ // Processes
16
+ listProcesses: 'get /processes',
17
+ describeProcess: 'get /processes',
18
+ // Auth / Account
19
+ listAuthProviders: true,
20
+ authenticateOIDC: 'get /credentials/oidc',
21
+ authenticateBasic: 'get /credentials/basic',
22
+ describeAccount: 'get /me',
23
+ // Files
24
+ listFiles: 'get /files',
25
+ getFile: 'get /files', // getFile is a virtual function and doesn't request an endpoint, but get /files should be available nevertheless.
26
+ uploadFile: 'put /files/{}',
27
+ downloadFile: 'get /files/{}',
28
+ deleteFile: 'delete /files/{}',
29
+ // User-Defined Processes
30
+ validateProcess: 'post /validation',
31
+ listUserProcesses: 'get /process_graphs',
32
+ describeUserProcess: 'get /process_graphs/{}',
33
+ getUserProcess: 'get /process_graphs/{}',
34
+ setUserProcess: 'put /process_graphs/{}',
35
+ replaceUserProcess: 'put /process_graphs/{}',
36
+ deleteUserProcess: 'delete /process_graphs/{}',
37
+ // Processing
38
+ computeResult: 'post /result',
39
+ listJobs: 'get /jobs',
40
+ createJob: 'post /jobs',
41
+ listServices: 'get /services',
42
+ createService: 'post /services',
43
+ getJob: 'get /jobs/{}',
44
+ describeJob: 'get /jobs/{}',
45
+ updateJob: 'patch /jobs/{}',
46
+ deleteJob: 'delete /jobs/{}',
47
+ estimateJob: 'get /jobs/{}/estimate',
48
+ debugJob: 'get /jobs/{}/logs',
49
+ startJob: 'post /jobs/{}/results',
50
+ stopJob: 'delete /jobs/{}/results',
51
+ listResults: 'get /jobs/{}/results',
52
+ downloadResults: 'get /jobs/{}/results',
53
+ // Web services
54
+ describeService: 'get /services/{}',
55
+ getService: 'get /services/{}',
56
+ updateService: 'patch /services/{}',
57
+ deleteService: 'delete /services/{}',
58
+ debugService: 'get /services/{}/logs',
59
+ };
60
+
61
+ /**
62
+ * Capabilities of a back-end.
63
+ */
64
+ class Capabilities {
65
+
66
+ /**
67
+ * Creates a new Capabilities object from an API-compatible JSON response.
68
+ *
69
+ * @param {object.<string, *>} data - A capabilities response compatible to the API specification for `GET /`.
70
+ * @throws {Error}
71
+ */
72
+ constructor(data) {
73
+
74
+ /**
75
+ * @private
76
+ * @type {object.<string, *>}
77
+ */
78
+ this.data = data;
79
+
80
+ /**
81
+ * @private
82
+ * @ignore
83
+ * @type {object.<string, string>}
84
+ */
85
+ this.featureMap = FEATURE_MAP;
86
+
87
+ /**
88
+ * @private
89
+ * @type {Array.<string>}
90
+ */
91
+ this.features = [];
92
+
93
+ this.validate();
94
+ this.init();
95
+ }
96
+
97
+ /**
98
+ * Validates the capabilities.
99
+ *
100
+ * Throws an error in case of an issue, otherwise just passes.
101
+ *
102
+ * @protected
103
+ * @throws {Error}
104
+ */
105
+ validate() {
106
+ if(!Utils.isObject(this.data)) {
107
+ throw new Error("No capabilities retrieved.");
108
+ }
109
+ else if(!this.data.api_version) {
110
+ throw new Error("Invalid capabilities: No API version retrieved");
111
+ }
112
+ else if(!Array.isArray(this.data.endpoints)) {
113
+ throw new Error("Invalid capabilities: No endpoints retrieved");
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Initializes the class.
119
+ *
120
+ * @protected
121
+ */
122
+ init() {
123
+ this.features = this.data.endpoints
124
+ // Flatten features and simplify variables to be compatible with the feature map.
125
+ .map(e => e.methods.map(method => {
126
+ const path = e.path.replace(/\{[^}]+\}/g, '{}');
127
+ return `${method} ${path}`.toLowerCase();
128
+ }))
129
+ .reduce((flat, next) => flat.concat(next), []); // .flat(1) once browser support for ECMAscript 10/2019 gets better
130
+ }
131
+
132
+ /**
133
+ * Returns the capabilities response as a JSON serializable representation of the data that is API compliant.
134
+ *
135
+ * @returns {object.<string, *>} - A reference to the capabilities response.
136
+ */
137
+ toJSON() {
138
+ return this.data;
139
+ }
140
+
141
+ /**
142
+ * Returns the openEO API version implemented by the back-end.
143
+ *
144
+ * @returns {string} openEO API version number.
145
+ */
146
+ apiVersion() {
147
+ return this.data.api_version;
148
+ }
149
+
150
+ /**
151
+ * Returns the back-end version number.
152
+ *
153
+ * @returns {string} openEO back-end version number.
154
+ */
155
+ backendVersion() {
156
+ return this.data.backend_version;
157
+ }
158
+
159
+ /**
160
+ * Returns the back-end title.
161
+ *
162
+ * @returns {string} Title
163
+ */
164
+ title() {
165
+ return typeof this.data.title === 'string' ? this.data.title : "";
166
+ }
167
+
168
+ /**
169
+ * Returns the back-end description.
170
+ *
171
+ * @returns {string} Description
172
+ */
173
+ description() {
174
+ return typeof this.data.description === 'string' ? this.data.description : "";
175
+ }
176
+
177
+ /**
178
+ * Is the back-end suitable for use in production?
179
+ *
180
+ * @returns {boolean} true = stable/production, false = unstable
181
+ */
182
+ isStable() {
183
+ return this.data.production === true;
184
+ }
185
+
186
+ /**
187
+ * Returns the links.
188
+ *
189
+ * @returns {Array.<Link>} Array of link objects (href, title, rel, type)
190
+ */
191
+ links() {
192
+ return Array.isArray(this.data.links) ? this.data.links : [];
193
+ }
194
+
195
+ /**
196
+ * Lists all supported features.
197
+ *
198
+ * @returns {Array.<string>} An array of supported features.
199
+ */
200
+ listFeatures() {
201
+ let features = [];
202
+ for(let feature in this.featureMap) {
203
+ if (this.featureMap[feature] === true || this.features.includes(this.featureMap[feature])) {
204
+ features.push(feature);
205
+ }
206
+ }
207
+ return features.sort();
208
+ }
209
+
210
+ /**
211
+ * Check whether a feature is supported by the back-end.
212
+ *
213
+ * @param {string} methodName - A feature name (corresponds to the JS client method names, see also the feature map for allowed values).
214
+ * @returns {boolean} `true` if the feature is supported, otherwise `false`.
215
+ */
216
+ hasFeature(methodName) {
217
+ let feature = this.featureMap[methodName];
218
+ if (typeof feature === 'string') {
219
+ feature = feature.toLowerCase();
220
+ }
221
+ return feature === true || this.features.some(e => e === feature);
222
+ }
223
+
224
+ /**
225
+ * Get the billing currency.
226
+ *
227
+ * @returns {string | null} The billing currency or `null` if not available.
228
+ */
229
+ currency() {
230
+ return (Utils.isObject(this.data.billing) && typeof this.data.billing.currency === 'string' ? this.data.billing.currency : null);
231
+ }
232
+
233
+ /**
234
+ * List all billing plans.
235
+ *
236
+ * @returns {Array.<BillingPlan>} Billing plans
237
+ */
238
+ listPlans() {
239
+ if (Utils.isObject(this.data.billing) && Array.isArray(this.data.billing.plans)) {
240
+ let defaultPlan = typeof this.data.billing.default_plan === 'string' ? this.data.billing.default_plan.toLowerCase() : null;
241
+ return this.data.billing.plans.map(plan => {
242
+ let addition = {
243
+ default: (defaultPlan === plan.name.toLowerCase())
244
+ };
245
+ return Object.assign({}, plan, addition);
246
+ });
247
+ }
248
+ else {
249
+ return [];
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Migrates a response, if required.
255
+ *
256
+ * @param {AxiosResponse} response
257
+ * @protected
258
+ * @returns {AxiosResponse}
259
+ */
260
+ migrate(response) { // eslint-disable-line no-unused-vars
261
+ return response;
262
+ }
263
+ }
264
+
265
+ module.exports = Capabilities;