@openeo/js-client 2.5.1 → 2.7.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,268 +1,268 @@
1
- const Utils = require("@openeo/js-commons/src/utils");
2
- const Parameter = require("./parameter");
3
-
4
- /**
5
- * A class that represents a process node and also a result from a process.
6
- */
7
- class BuilderNode {
8
-
9
- /**
10
- * Creates a new process node for the builder.
11
- *
12
- * @param {Builder} parent
13
- * @param {string} processId
14
- * @param {object.<string, *>} [processArgs={}]
15
- * @param {?string} [processDescription=null]
16
- * @param {?string} [processNamespace=null]
17
- */
18
- constructor(parent, processId, processArgs = {}, processDescription = null, processNamespace = null) {
19
- /**
20
- * The parent builder.
21
- * @type {Builder}
22
- */
23
- this.parent = parent;
24
-
25
- /**
26
- * The specification of the process associated with this node.
27
- * @type {Process}
28
- * @readonly
29
- */
30
- this.spec = this.parent.spec(processId, processNamespace);
31
- if (!this.spec) {
32
- throw new Error("Process doesn't exist: " + processId);
33
- }
34
-
35
- /**
36
- * The unique identifier for the node (not the process ID!).
37
- * @type {string}
38
- */
39
- this.id = parent.generateId(processId);
40
- /**
41
- * The namespace of the process - EXPERIMENTAL!
42
- * @type {string}
43
- */
44
- this.namespace = processNamespace;
45
- /**
46
- * The arguments for the process.
47
- * @type {object.<string, *>}
48
- */
49
- this.arguments = Array.isArray(processArgs) ? this.namedArguments(processArgs) : processArgs;
50
- /**
51
- * @ignore
52
- */
53
- this._description = processDescription;
54
- /**
55
- * Is this the result node?
56
- * @type {boolean}
57
- */
58
- this.result = false;
59
-
60
- this.addParametersToProcess(this.arguments);
61
- }
62
-
63
- /**
64
- * Converts a sorted array of arguments to an object with the respective parameter names.
65
- *
66
- * @param {Array.<object.<string, *>>} processArgs
67
- * @returns {object.<string, *>}
68
- * @throws {Error}
69
- */
70
- namedArguments(processArgs) {
71
- if (processArgs.length > (this.spec.parameters || []).length) {
72
- throw new Error("More arguments specified than parameters available.");
73
- }
74
- let obj = {};
75
- if (Array.isArray(this.spec.parameters)) {
76
- for(let i = 0; i < this.spec.parameters.length; i++) {
77
- obj[this.spec.parameters[i].name] = processArgs[i];
78
- }
79
- }
80
- return obj;
81
- }
82
-
83
- /**
84
- * Checks the arguments given for parameters and add them to the process.
85
- *
86
- * @param {object.<string, *>|Array} processArgs
87
- */
88
- addParametersToProcess(processArgs) {
89
- for(let key in processArgs) {
90
- let arg = processArgs[key];
91
- if (arg instanceof Parameter) {
92
- if (Utils.isObject(arg.spec.schema)) {
93
- this.parent.addParameter(arg.spec);
94
- }
95
- }
96
- else if (arg instanceof BuilderNode) {
97
- this.addParametersToProcess(arg.arguments);
98
- }
99
- else if (Array.isArray(arg) || Utils.isObject(arg)) {
100
- this.addParametersToProcess(arg);
101
- }
102
- }
103
- }
104
-
105
- /**
106
- * Gets/Sets a description for the node.
107
- *
108
- * Can be used in a variety of ways:
109
- *
110
- * By default, this is a function:
111
- * `node.description()` - Returns the description.
112
- * `node.description("foo")` - Sets the description to "foo". Returns the node itself for method chaining.
113
- *
114
- * You can also "replace" the function (not supported in TypeScript!),
115
- * then it acts as normal property and the function is not available any longer:
116
- * `node.description = "foo"` - Sets the description to "foo".
117
- * Afterwards you can call `node.description` as normal object property.
118
- *
119
- * @param {string|undefined} description - Optional: If given, set the value.
120
- * @returns {string|BuilderNode}
121
- */
122
- description(description) {
123
- if (typeof description === 'undefined') {
124
- return this._description;
125
- }
126
- else {
127
- this._description = description;
128
- return this;
129
- }
130
- }
131
-
132
- /**
133
- * Converts the given argument into something serializable...
134
- *
135
- * @protected
136
- * @param {*} arg - Argument
137
- * @param {string} name - Parameter name
138
- * @returns {*}
139
- */
140
- exportArgument(arg, name) {
141
- const Formula = require('./formula');
142
- if (Utils.isObject(arg)) {
143
- if (arg instanceof BuilderNode || arg instanceof Parameter) {
144
- return arg.ref();
145
- }
146
- else if (arg instanceof Formula) {
147
- let builder = this.createBuilder(this, name);
148
- arg.setBuilder(builder);
149
- arg.generate();
150
- return builder.toJSON();
151
- }
152
- else if (arg instanceof Date) {
153
- return arg.toISOString();
154
- }
155
- else if (typeof arg.toJSON === 'function') {
156
- return arg.toJSON();
157
- }
158
- else {
159
- let obj = {};
160
- for(let key in arg) {
161
- if (typeof arg[key] !== 'undefined') {
162
- obj[key] = this.exportArgument(arg[key], name);
163
- }
164
- }
165
- return obj;
166
- }
167
- }
168
- else if (Array.isArray(arg)) {
169
- return arg.map(element => this.exportArgument(element), name);
170
- }
171
- // export child process graph
172
- else if (typeof arg === 'function') {
173
- return this.exportCallback(arg, name);
174
- }
175
- else {
176
- return arg;
177
- }
178
- }
179
-
180
- /**
181
- * Creates a new Builder, usually for a callback.
182
- *
183
- * @protected
184
- * @param {?BuilderNode} [parentNode=null]
185
- * @param {?string} [parentParameter=null]
186
- * @returns {BuilderNode}
187
- */
188
- createBuilder(parentNode = null, parentParameter = null) {
189
- const Builder = require('./builder');
190
- let builder = new Builder(this.parent.processes, this.parent);
191
- if (parentNode !== null && parentParameter !== null) {
192
- builder.setParent(parentNode, parentParameter);
193
- }
194
- return builder;
195
- }
196
-
197
- /**
198
- * Returns the serializable process for the callback function given.
199
- *
200
- * @protected
201
- * @param {Function} arg - callback function
202
- * @param {string} name - Parameter name
203
- * @returns {object.<string, *>}
204
- * @throws {Error}
205
- */
206
- exportCallback(arg, name) {
207
- let builder = this.createBuilder(this, name);
208
- let params = builder.getParentCallbackParameters();
209
- // Bind builder to this, so that this.xxx can be used for processes
210
- // Also pass builder as last parameter so that we can grab it in arrow functions
211
- let node = arg.bind(builder)(...params, builder);
212
- if (Array.isArray(node) && builder.supports('array_create')) {
213
- node = builder.array_create(node);
214
- }
215
- else if (!Utils.isObject(node) && builder.supports('constant')) {
216
- node = builder.constant(node);
217
- }
218
- if (node instanceof BuilderNode) {
219
- node.result = true;
220
- return builder.toJSON();
221
- }
222
- else {
223
- throw new Error("Callback must return BuilderNode");
224
- }
225
- }
226
-
227
- /**
228
- * Returns a JSON serializable representation of the data that is API compliant.
229
- *
230
- * @returns {object.<string, *>}
231
- */
232
- toJSON() {
233
- let obj = {
234
- process_id: this.spec.id,
235
- arguments: {}
236
- };
237
- if (this.namespace) {
238
- obj.namespace = this.namespace;
239
- }
240
- for(let name in this.arguments) {
241
- if (typeof this.arguments[name] !== 'undefined') {
242
- obj.arguments[name] = this.exportArgument(this.arguments[name], name);
243
- }
244
- }
245
- if (typeof this.description !== 'function') {
246
- obj.description = this.description;
247
- }
248
- else if (typeof this._description === 'string') {
249
- obj.description = this._description;
250
- }
251
- if (this.result) {
252
- obj.result = true;
253
- }
254
- return obj;
255
- }
256
-
257
- /**
258
- * Returns the reference object for this node.
259
- *
260
- * @returns {FromNode}
261
- */
262
- ref() {
263
- return { from_node: this.id };
264
- }
265
-
266
- }
267
-
268
- module.exports = BuilderNode;
1
+ const Utils = require("@openeo/js-commons/src/utils");
2
+ const Parameter = require("./parameter");
3
+
4
+ /**
5
+ * A class that represents a process node and also a result from a process.
6
+ */
7
+ class BuilderNode {
8
+
9
+ /**
10
+ * Creates a new process node for the builder.
11
+ *
12
+ * @param {Builder} parent
13
+ * @param {string} processId
14
+ * @param {object.<string, *>} [processArgs={}]
15
+ * @param {?string} [processDescription=null]
16
+ * @param {?string} [processNamespace=null]
17
+ */
18
+ constructor(parent, processId, processArgs = {}, processDescription = null, processNamespace = null) {
19
+ /**
20
+ * The parent builder.
21
+ * @type {Builder}
22
+ */
23
+ this.parent = parent;
24
+
25
+ /**
26
+ * The specification of the process associated with this node.
27
+ * @type {Process}
28
+ * @readonly
29
+ */
30
+ this.spec = this.parent.spec(processId, processNamespace);
31
+ if (!this.spec) {
32
+ throw new Error("Process doesn't exist: " + processId);
33
+ }
34
+
35
+ /**
36
+ * The unique identifier for the node (not the process ID!).
37
+ * @type {string}
38
+ */
39
+ this.id = parent.generateId(processId);
40
+ /**
41
+ * The namespace of the process - EXPERIMENTAL!
42
+ * @type {string}
43
+ */
44
+ this.namespace = processNamespace;
45
+ /**
46
+ * The arguments for the process.
47
+ * @type {object.<string, *>}
48
+ */
49
+ this.arguments = Array.isArray(processArgs) ? this.namedArguments(processArgs) : processArgs;
50
+ /**
51
+ * @ignore
52
+ */
53
+ this._description = processDescription;
54
+ /**
55
+ * Is this the result node?
56
+ * @type {boolean}
57
+ */
58
+ this.result = false;
59
+
60
+ this.addParametersToProcess(this.arguments);
61
+ }
62
+
63
+ /**
64
+ * Converts a sorted array of arguments to an object with the respective parameter names.
65
+ *
66
+ * @param {Array.<object.<string, *>>} processArgs
67
+ * @returns {object.<string, *>}
68
+ * @throws {Error}
69
+ */
70
+ namedArguments(processArgs) {
71
+ if (processArgs.length > (this.spec.parameters || []).length) {
72
+ throw new Error("More arguments specified than parameters available.");
73
+ }
74
+ let obj = {};
75
+ if (Array.isArray(this.spec.parameters)) {
76
+ for(let i = 0; i < this.spec.parameters.length; i++) {
77
+ obj[this.spec.parameters[i].name] = processArgs[i];
78
+ }
79
+ }
80
+ return obj;
81
+ }
82
+
83
+ /**
84
+ * Checks the arguments given for parameters and add them to the process.
85
+ *
86
+ * @param {object.<string, *>|Array} processArgs
87
+ */
88
+ addParametersToProcess(processArgs) {
89
+ for(let key in processArgs) {
90
+ let arg = processArgs[key];
91
+ if (arg instanceof Parameter) {
92
+ if (Utils.isObject(arg.spec.schema)) {
93
+ this.parent.addParameter(arg.spec);
94
+ }
95
+ }
96
+ else if (arg instanceof BuilderNode) {
97
+ this.addParametersToProcess(arg.arguments);
98
+ }
99
+ else if (Array.isArray(arg) || Utils.isObject(arg)) {
100
+ this.addParametersToProcess(arg);
101
+ }
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Gets/Sets a description for the node.
107
+ *
108
+ * Can be used in a variety of ways:
109
+ *
110
+ * By default, this is a function:
111
+ * `node.description()` - Returns the description.
112
+ * `node.description("foo")` - Sets the description to "foo". Returns the node itself for method chaining.
113
+ *
114
+ * You can also "replace" the function (not supported in TypeScript!),
115
+ * then it acts as normal property and the function is not available any longer:
116
+ * `node.description = "foo"` - Sets the description to "foo".
117
+ * Afterwards you can call `node.description` as normal object property.
118
+ *
119
+ * @param {string|undefined} description - Optional: If given, set the value.
120
+ * @returns {string|BuilderNode}
121
+ */
122
+ description(description) {
123
+ if (typeof description === 'undefined') {
124
+ return this._description;
125
+ }
126
+ else {
127
+ this._description = description;
128
+ return this;
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Converts the given argument into something serializable...
134
+ *
135
+ * @protected
136
+ * @param {*} arg - Argument
137
+ * @param {string} name - Parameter name
138
+ * @returns {*}
139
+ */
140
+ exportArgument(arg, name) {
141
+ const Formula = require('./formula');
142
+ if (Utils.isObject(arg)) {
143
+ if (arg instanceof BuilderNode || arg instanceof Parameter) {
144
+ return arg.ref();
145
+ }
146
+ else if (arg instanceof Formula) {
147
+ let builder = this.createBuilder(this, name);
148
+ arg.setBuilder(builder);
149
+ arg.generate();
150
+ return builder.toJSON();
151
+ }
152
+ else if (arg instanceof Date) {
153
+ return arg.toISOString();
154
+ }
155
+ else if (typeof arg.toJSON === 'function') {
156
+ return arg.toJSON();
157
+ }
158
+ else {
159
+ let obj = {};
160
+ for(let key in arg) {
161
+ if (typeof arg[key] !== 'undefined') {
162
+ obj[key] = this.exportArgument(arg[key], name);
163
+ }
164
+ }
165
+ return obj;
166
+ }
167
+ }
168
+ else if (Array.isArray(arg)) {
169
+ return arg.map(element => this.exportArgument(element), name);
170
+ }
171
+ // export child process graph
172
+ else if (typeof arg === 'function') {
173
+ return this.exportCallback(arg, name);
174
+ }
175
+ else {
176
+ return arg;
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Creates a new Builder, usually for a callback.
182
+ *
183
+ * @protected
184
+ * @param {?BuilderNode} [parentNode=null]
185
+ * @param {?string} [parentParameter=null]
186
+ * @returns {BuilderNode}
187
+ */
188
+ createBuilder(parentNode = null, parentParameter = null) {
189
+ const Builder = require('./builder');
190
+ let builder = new Builder(this.parent.processes, this.parent);
191
+ if (parentNode !== null && parentParameter !== null) {
192
+ builder.setParent(parentNode, parentParameter);
193
+ }
194
+ return builder;
195
+ }
196
+
197
+ /**
198
+ * Returns the serializable process for the callback function given.
199
+ *
200
+ * @protected
201
+ * @param {Function} arg - callback function
202
+ * @param {string} name - Parameter name
203
+ * @returns {object.<string, *>}
204
+ * @throws {Error}
205
+ */
206
+ exportCallback(arg, name) {
207
+ let builder = this.createBuilder(this, name);
208
+ let params = builder.getParentCallbackParameters();
209
+ // Bind builder to this, so that this.xxx can be used for processes
210
+ // Also pass builder as last parameter so that we can grab it in arrow functions
211
+ let node = arg.bind(builder)(...params, builder);
212
+ if (Array.isArray(node) && builder.supports('array_create')) {
213
+ node = builder.array_create(node);
214
+ }
215
+ else if (!Utils.isObject(node) && builder.supports('constant')) {
216
+ node = builder.constant(node);
217
+ }
218
+ if (node instanceof BuilderNode) {
219
+ node.result = true;
220
+ return builder.toJSON();
221
+ }
222
+ else {
223
+ throw new Error("Callback must return BuilderNode");
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Returns a JSON serializable representation of the data that is API compliant.
229
+ *
230
+ * @returns {object.<string, *>}
231
+ */
232
+ toJSON() {
233
+ let obj = {
234
+ process_id: this.spec.id,
235
+ arguments: {}
236
+ };
237
+ if (this.namespace) {
238
+ obj.namespace = this.namespace;
239
+ }
240
+ for(let name in this.arguments) {
241
+ if (typeof this.arguments[name] !== 'undefined') {
242
+ obj.arguments[name] = this.exportArgument(this.arguments[name], name);
243
+ }
244
+ }
245
+ if (typeof this.description !== 'function') {
246
+ obj.description = this.description;
247
+ }
248
+ else if (typeof this._description === 'string') {
249
+ obj.description = this._description;
250
+ }
251
+ if (this.result) {
252
+ obj.result = true;
253
+ }
254
+ return obj;
255
+ }
256
+
257
+ /**
258
+ * Returns the reference object for this node.
259
+ *
260
+ * @returns {FromNode}
261
+ */
262
+ ref() {
263
+ return { from_node: this.id };
264
+ }
265
+
266
+ }
267
+
268
+ module.exports = BuilderNode;