@dnv-plant/typescriptpws 1.0.27 → 1.0.38-alpha.1846913

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.
@@ -0,0 +1,382 @@
1
+ /***********************************************************************
2
+ * This file has been auto-generated by a code generation tool.
3
+ * Version: 1.0.38
4
+ * Date/time: 24 Mar 2025 16:46:20
5
+ * Template: templates/typescriptpws/calculations.razor.
6
+ ***********************************************************************/
7
+
8
+
9
+
10
+ import * as Enums from "../enums";
11
+ import * as Entities from "../entities";
12
+ import * as EntitySchemas from "../entity-schemas";
13
+ import { getAnalyticsApiTarget, getClientAliasId, postRequest } from "../utilities";
14
+
15
+ import Joi from "joi";
16
+ import { AxiosResponse } from "axios";
17
+
18
+
19
+ class CalculationRequestBase {
20
+ /**
21
+ * Calculation request base class.
22
+ */
23
+ constructor() {
24
+ // Base class initialization code
25
+ }
26
+ }
27
+
28
+ class CalculationBase {
29
+ resultCode?: Enums.ResultCode = Enums.ResultCode.SUCCESS;
30
+ messages: string[] = [];
31
+ calculationElapsedTime?: number = 0.0;
32
+ operationId?: string = "";
33
+
34
+ /**
35
+ * Post JSON to URL and time the call
36
+ */
37
+ async postRequest(url: string, data: string): Promise<AxiosResponse> {
38
+ return postRequest(url, data);
39
+ }
40
+
41
+ /**
42
+ * Utility method to print the messages returned by the calculation.
43
+ */
44
+ printMessages(): void {
45
+ if (this.messages && this.messages.length > 0) {
46
+ this.messages.forEach((message) => console.log(message));
47
+ } else {
48
+ console.log("No messages");
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Utility method to handle a failed response.
54
+ */
55
+ handleFailedResponse(response: AxiosResponse): void {
56
+ try {
57
+ const validatedFailedResponse = new CalculationFailedResponseSchema().validate(response.data);
58
+
59
+ this.resultCode = validatedFailedResponse.resultCode;
60
+ this.messages.push(...(validatedFailedResponse.messages ?? []));
61
+ this.calculationElapsedTime = validatedFailedResponse.calculationElapsedTime;
62
+ this.operationId = validatedFailedResponse.operationId;
63
+ } catch (error) {
64
+ if (error instanceof Error) {
65
+ this.messages.push(`Failed to parse response: ${error.message}`);
66
+ } else {
67
+ this.messages.push("An unknown error occurred during response parsing.");
68
+ }
69
+ console.error("Failed to parse response:", error);
70
+ } finally {
71
+ this.messages.push(`${response.statusText} (Status code: ${response.status})`);
72
+ }
73
+ }
74
+ }
75
+
76
+ class CalculationResponseBase {
77
+ resultCode?: Enums.ResultCode;
78
+ messages?: string[];
79
+ calculationElapsedTime?: number;
80
+ operationId?: string;
81
+
82
+ /**
83
+ * Calculation response base class.
84
+ */
85
+ constructor(
86
+ resultCode?: Enums.ResultCode,
87
+ messages?: string[],
88
+ calculationElapsedTime?: number,
89
+ operationId?: string
90
+ ) {
91
+ this.resultCode = resultCode;
92
+ this.messages = messages;
93
+ this.calculationElapsedTime = calculationElapsedTime;
94
+ this.operationId = operationId;
95
+ }
96
+ }
97
+
98
+ class CalculationFailedResponse extends CalculationResponseBase {
99
+ /**
100
+ * Calculation failed response class.
101
+ */
102
+ constructor(
103
+ resultCode?: Enums.ResultCode,
104
+ messages: string[] = [],
105
+ calculationElapsedTime: number = 0,
106
+ operationId: string = ""
107
+ ) {
108
+ super(resultCode, messages, calculationElapsedTime, operationId);
109
+ }
110
+ }
111
+
112
+ class CalculationFailedResponseSchema {
113
+ schema: Joi.ObjectSchema;
114
+
115
+ /**
116
+ * Calculation failed response schema.
117
+ */
118
+ constructor() {
119
+ this.schema = Joi.object({
120
+ resultCode: Joi.string()
121
+ .valid(...Object.values(Enums.ResultCode))
122
+ .required(),
123
+ messages: Joi.array().items(Joi.string()).required(),
124
+ calculationElapsedTime: Joi.number().required(),
125
+ operationId: Joi.string().required(),
126
+ }).unknown(true);
127
+ }
128
+
129
+ validate(data: {
130
+ resultCode: Enums.ResultCode;
131
+ messages: string[];
132
+ calculationElapsedTime: number;
133
+ operationId: string;
134
+ }) {
135
+ const { error, value } = this.schema.validate(data);
136
+ if (error) throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
137
+ return this.makeCalculationFailedResponse(value);
138
+ }
139
+
140
+ makeCalculationFailedResponse(data: {
141
+ resultCode: Enums.ResultCode;
142
+ messages: string[];
143
+ calculationElapsedTime: number;
144
+ operationId: string;
145
+ }) {
146
+ return new CalculationFailedResponse(data.resultCode, data.messages, data.calculationElapsedTime, data.operationId);
147
+ }
148
+ }
149
+
150
+ export interface FlashCalculationRequestSchemaData {
151
+ material: Entities.Material;
152
+ materialState: Entities.State;
153
+ }
154
+
155
+ class FlashCalculationRequest extends CalculationRequestBase {
156
+ material: Entities.Material;
157
+ materialState: Entities.State;
158
+
159
+ /**
160
+ * Flash calculation request class.
161
+ *
162
+ * @param {Entities.Material} material - A Material entity.
163
+ * @param {Entities.State} materialState - A State entity.
164
+ */
165
+ constructor(
166
+ material: Entities.Material,
167
+ materialState: Entities.State
168
+ ) {
169
+ super();
170
+ this.material = material;
171
+ this.materialState = materialState;
172
+ }
173
+ }
174
+
175
+ export class FlashCalculationRequestSchema {
176
+ schema: Joi.ObjectSchema;
177
+ propertyTypes: Record<string, string>;
178
+
179
+ /**
180
+ * Schema for the Flash calculation request.
181
+ */
182
+ constructor() {
183
+ this.schema = Joi.object({
184
+ material: new EntitySchemas.MaterialSchema().schema,
185
+ materialState: new EntitySchemas.StateSchema().schema,
186
+ }).unknown(true);
187
+
188
+ this.propertyTypes = {
189
+ material: "Entities.Material",
190
+ materialState: "Entities.State",
191
+ };
192
+ }
193
+
194
+ validate(data: FlashCalculationRequestSchemaData): FlashCalculationRequest {
195
+ const { error, value } = this.schema.validate(data, { abortEarly: false });
196
+ if (error) {
197
+ throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
198
+ }
199
+ return this.makeCalculationRequest(value);
200
+ }
201
+
202
+ makeCalculationRequest(data: FlashCalculationRequestSchemaData): FlashCalculationRequest {
203
+ return new FlashCalculationRequest(
204
+ data.material,
205
+ data.materialState
206
+ );
207
+ }
208
+ }
209
+
210
+ export class FlashCalculation extends CalculationBase {
211
+ material: Entities.Material;
212
+ materialState: Entities.State;
213
+ flashResult?: Entities.FlashResult;
214
+
215
+ /**
216
+ * Calculates fluid properties for a user-defined material where the user has defined the pressure and temperature or pressure and liquid fraction. The model carries out a flash calculation
217
+ to obtain the fluid properties.
218
+ *
219
+ * @param {Entities.Material} material - A Material entity.
220
+ * @param {Entities.State} materialState - A State entity.
221
+ */
222
+ constructor(
223
+ material: Entities.Material,
224
+ materialState: Entities.State
225
+ ) {
226
+ super();
227
+ this.material = material;
228
+ this.materialState = materialState;
229
+ }
230
+
231
+ async run() {
232
+ try {
233
+ const request = new FlashCalculationRequest(
234
+ this.material,
235
+ this.materialState
236
+ );
237
+
238
+ const schema = new FlashCalculationRequestSchema();
239
+ const validatedRequest = schema.validate(request);
240
+
241
+ const requestJson = JSON.stringify(validatedRequest);
242
+ const url = `${getAnalyticsApiTarget()}calculateflash?clientId=${getClientAliasId()}`;
243
+
244
+ this.resultCode = Enums.ResultCode.UNEXPECTED_APPLICATION_ERROR;
245
+
246
+ const response = await this.postRequest(url, requestJson);
247
+
248
+ if (response.status >= 200 && response.status < 300) {
249
+ const schema = new FlashCalculationResponseSchema();
250
+ const validatedResponse = schema.validate(response.data);
251
+
252
+ this.resultCode = validatedResponse.resultCode;
253
+ if (this.resultCode === Enums.ResultCode.SUCCESS) {
254
+ this.flashResult = validatedResponse.flashResult;
255
+ this.resultCode = validatedResponse.resultCode;
256
+ this.messages = validatedResponse.messages ?? [];
257
+ this.calculationElapsedTime = validatedResponse.calculationElapsedTime;
258
+ this.operationId = validatedResponse.operationId;
259
+ } else {
260
+ this.messages.push(...(validatedResponse.messages ?? []));
261
+ }
262
+ } else {
263
+ this.handleFailedResponse(response);
264
+ }
265
+ } catch (error) {
266
+ if (error instanceof Error) {
267
+ this.messages.push(`Error: ${error.message}`);
268
+ } else {
269
+ this.messages.push(`Unexpected error: ${JSON.stringify(error)}`);
270
+ }
271
+ console.error(error);
272
+ this.resultCode = Enums.ResultCode.UNEXPECTED_APPLICATION_ERROR;
273
+ }
274
+
275
+ return this.resultCode;
276
+ }
277
+
278
+ toString() {
279
+ const parts = ["* Flash"];
280
+
281
+ parts.push(`flashResult: ${String(this.flashResult)}`);
282
+ parts.push(`resultCode: ${String(this.resultCode)}`);
283
+ parts.push("*** messages:");
284
+ parts.push(`messages: ${this.messages !== undefined ? this.messages : "(None)"}`);
285
+ parts.push(`calculationElapsedTime: ${this.calculationElapsedTime !== undefined ? this.calculationElapsedTime : "(None)"}`);
286
+ parts.push(`operationId: ${this.operationId !== undefined ? this.operationId : "(None)"}`);
287
+
288
+ return parts.join("\n");
289
+ }
290
+ }
291
+
292
+ export class FlashCalculationResponse extends CalculationResponseBase {
293
+ flashResult: Entities.FlashResult;
294
+
295
+ /**
296
+ * Flash calculation response class.
297
+ *
298
+ * @param {Entities.FlashResult} flashResult - Fluid properties at given conditions.
299
+ */
300
+ constructor(
301
+ flashResult: Entities.FlashResult,
302
+ resultCode: Enums.ResultCode,
303
+ messages: string[],
304
+ calculationElapsedTime: number,
305
+ operationId: string
306
+ ) {
307
+ super();
308
+ this.flashResult = flashResult;
309
+ this.resultCode = resultCode;
310
+ this.messages = messages;
311
+ this.calculationElapsedTime = calculationElapsedTime;
312
+ this.operationId = operationId;
313
+ }
314
+
315
+ initialiseFromDictionary(data: { [key: string]: unknown }) {
316
+ if (data.flashResult) {
317
+ this.flashResult = new Entities.FlashResult();
318
+ this.flashResult.initialiseFromDictionary(data.flashResult as { [key: string]: unknown });
319
+ }
320
+ if (data.resultCode !== undefined && (typeof data.resultCode === "string" || typeof data.resultCode === "number")) {
321
+ this.resultCode = data.resultCode as Enums.ResultCode;
322
+ }
323
+ this.messages = this.messages ?? [];
324
+ if (data.messages && Array.isArray(data.messages)) {
325
+ this.messages.push(...data.messages);
326
+ }
327
+ if (data.calculationElapsedTime !== undefined && typeof data.calculationElapsedTime === "number") {
328
+ this.calculationElapsedTime = data.calculationElapsedTime as number;
329
+ }
330
+ if (data.operationId !== undefined && typeof data.operationId === "string") {
331
+ this.operationId = data.operationId as string;
332
+ }
333
+ }
334
+ }
335
+
336
+ export interface FlashCalculationResponseSchemaData {
337
+ flashResult: Entities.FlashResult;
338
+ resultCode: Enums.ResultCode;
339
+ messages: string[];
340
+ calculationElapsedTime: number;
341
+ operationId: string;
342
+ }
343
+
344
+ export class FlashCalculationResponseSchema {
345
+ schema: Joi.ObjectSchema;
346
+ propertyTypes: Record<string, string>;
347
+
348
+ /**
349
+ * Schema for the Flash calculation response.
350
+ */
351
+ constructor() {
352
+ this.schema = Joi.object({
353
+ flashResult: new EntitySchemas.FlashResultSchema().schema,
354
+ resultCode: Joi.string().valid(...Object.values(Enums.ResultCode)),
355
+ messages: Joi.array().items(Joi.string()),
356
+ calculationElapsedTime: Joi.number().unsafe(),
357
+ operationId: Joi.string().uuid().allow(null),
358
+ }).unknown(true);
359
+
360
+ this.propertyTypes = {
361
+ flashResult: "Entities.FlashResult",
362
+ };
363
+ }
364
+
365
+ validate(data: FlashCalculationResponseSchemaData): FlashCalculationResponse {
366
+ const { error, value } = this.schema.validate(data, { abortEarly: false });
367
+ if (error) {
368
+ throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
369
+ }
370
+ return this.makeCalculationResponse(value);
371
+ }
372
+
373
+ makeCalculationResponse(data: FlashCalculationResponseSchemaData): FlashCalculationResponse {
374
+ return new FlashCalculationResponse(
375
+ data.flashResult,
376
+ data.resultCode,
377
+ data.messages,
378
+ data.calculationElapsedTime,
379
+ data.operationId
380
+ );
381
+ }
382
+ }