@dnv-plant/typescriptpws 1.0.77 → 1.0.78

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.
package/src/constants.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * This file has been auto-generated by a code generation tool.
3
- * Version: 1.0.77
4
- * Date/time: 18 Jun 2025 15:16:45
3
+ * Version: 1.0.78
4
+ * Date/time: 20 Jun 2025 16:35:06
5
5
  * Template: templates/typescriptpws/constants.razor.
6
6
  ***********************************************************************/
7
7
 
package/src/entities.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * This file has been auto-generated by a code generation tool.
3
- * Version: 1.0.77
4
- * Date/time: 18 Jun 2025 15:16:46
3
+ * Version: 1.0.78
4
+ * Date/time: 20 Jun 2025 16:35:07
5
5
  * Template: templates/typescriptpws/entities.razor.
6
6
  ***********************************************************************/
7
7
 
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * This file has been auto-generated by a code generation tool.
3
- * Version: 1.0.77
4
- * Date/time: 18 Jun 2025 15:16:47
3
+ * Version: 1.0.78
4
+ * Date/time: 20 Jun 2025 16:35:08
5
5
  * Template: templates/typescriptpws/entityschemas.razor.
6
6
  ***********************************************************************/
7
7
 
package/src/enums.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * This file has been auto-generated by a code generation tool.
3
- * Version: 1.0.77
4
- * Date/time: 18 Jun 2025 15:16:46
3
+ * Version: 1.0.78
4
+ * Date/time: 20 Jun 2025 16:35:07
5
5
  * Template: templates/typescriptpws/enums.razor.
6
6
  ***********************************************************************/
7
7
 
package/src/materials.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * This file has been auto-generated by a code generation tool.
3
- * Version: 1.0.77
4
- * Date/time: 18 Jun 2025 15:16:45
3
+ * Version: 1.0.78
4
+ * Date/time: 20 Jun 2025 16:35:06
5
5
  * Template: templates/typescriptpws/materials.razor.
6
6
  ***********************************************************************/
7
7
 
@@ -149,30 +149,20 @@ export class MaterialEntityDescriptorSchema {
149
149
  }
150
150
 
151
151
  async function getDataFromUrl<T>(url: string, schemaClass: SchemaClass, many: boolean = false): Promise<T | T[]> {
152
- try {
153
- const response = await getRequest(url);
154
-
155
- if (response.status !== 200) {
156
- throw new Error(`Failed to get data: ${response.status} ${response.statusText}`);
157
- }
158
-
159
- const data = response.data;
152
+ const { data} = await getRequest(url);
160
153
 
161
- // Access the Joi schema from the schema class
162
- const validationSchema = many ? Joi.array().items(schemaClass.schema) : schemaClass.schema;
154
+ // Access the Joi schema from the schema class
155
+ const validationSchema = many ? Joi.array().items(schemaClass.schema) : schemaClass.schema;
163
156
 
164
- // Validate the data
165
- const { error, value } = validationSchema.validate(data, { abortEarly: false });
166
-
167
- if (error) {
168
- const errorMessages = error.details.map((e: { message: any }) => e.message).join(", ");
169
- throw new Error(`Error retrieving data: ${errorMessages}`);
170
- }
157
+ // Validate the data
158
+ const { error, value } = validationSchema.validate(data, { abortEarly: false });
171
159
 
172
- return value;
173
- } catch (err: unknown) {
174
- throw new Error(`Error retrieving data: ${err instanceof Error ? err.message : "Unknown error"}`);
160
+ if (error) {
161
+ const errorMessages = error.details.map((e: { message: any }) => e.message).join(", ");
162
+ throw new Error(`Error retrieving data: ${errorMessages}`);
175
163
  }
164
+
165
+ return value;
176
166
  }
177
167
 
178
168
  export async function getAllCasIds(): Promise<MaterialCasIdInfo[]> {
@@ -279,41 +269,33 @@ export async function getComponentByCasId(casId: number) {
279
269
 
280
270
  export async function storeDataAndGetResponse<T>(url: string, data: any, schemaClass: SchemaClass): Promise<T> {
281
271
  // Serialize the data using the schema
282
-
283
- const { error, value } = schemaClass.schema.validate(data);
284
- if (error) {
285
- throw new Error(`Validation error: ${error.details[0].message}`);
272
+ const { error: reqErr, value: payload } = schemaClass.schema.validate(data);
273
+ if (reqErr) {
274
+ throw new Error(`Validation error (request): ${reqErr.details[0].message}`);
286
275
  }
287
276
 
288
- const jsonText = JSON.stringify(value);
277
+ const { status: postStatus, headers } = await postRequest(
278
+ url,
279
+ JSON.stringify(payload)
280
+ );
289
281
 
290
- // Send a POST request
291
- const postResponse = await postRequest(url, jsonText);
282
+ if (postStatus !== 201) {
283
+ throw new Error(`Expected 201 from POST, got ${postStatus}`);
284
+ }
292
285
 
293
- // Check if the response status is 201 (created)
294
- if (postResponse.status === 201) {
295
- const materialUrl = postResponse.headers["location"];
296
- if (!materialUrl) {
297
- throw new Error("Failed to store data: no location header in response");
298
- }
286
+ const location = headers.location ?? headers.Location; // header name can vary
287
+ if (!location) {
288
+ throw new Error("POST succeeded but no Location header returned");
289
+ }
299
290
 
300
- // Send a GET request to the material URL
301
- const getResponse = await getRequest(materialUrl);
302
-
303
- // Check if the GET request is successful
304
- if (getResponse.status === 200) {
305
- // Validate and parse the response data using the schema
306
- const { error, value } = schemaClass.schema.validate(getResponse.data);
307
- if (error) {
308
- throw new Error(`Validation error: ${error.details[0].message}`);
309
- }
310
- return value;
311
- } else {
312
- throw new Error(`Failed to retrieve data: ${getResponse.status} ${getResponse.statusText}`);
313
- }
314
- } else {
315
- throw new Error(`Failed to store data: ${postResponse.status} ${postResponse.statusText}`);
291
+ const { data: fetched } = await getRequest(location);
292
+
293
+ const { error: respErr, value: parsed } = schemaClass.schema.validate(fetched);
294
+ if (respErr) {
295
+ throw new Error(`Validation error (response): ${respErr.details[0].message}`);
316
296
  }
297
+
298
+ return parsed as T;
317
299
  }
318
300
 
319
301
  export async function storeMaterialComponentData(
@@ -350,29 +332,15 @@ export async function storeMaterialComponentAndCreateMaterial(
350
332
  }
351
333
  }
352
334
 
353
- export async function updateMaterialComponent(materialComponentData: MaterialComponentData): Promise<boolean> {
354
- try {
355
- // Generate the URL
356
- const apiTarget = getMaterialsApiTarget();
357
- const clientAliasId = getClientAliasId();
358
- const url = `${apiTarget}components?clientId=${clientAliasId}`;
359
-
360
- // Serialize the data
361
- const jsonText = JSON.stringify(materialComponentData);
335
+ export async function updateMaterialComponent(data: MaterialComponentData): Promise<boolean> {
336
+ const url = `${getMaterialsApiTarget()}components?clientId=${getClientAliasId()}`;
337
+ const { status, statusText } = await putRequest(url, JSON.stringify(data));
362
338
 
363
- // Send a PUT request
364
- const response = await putRequest(url, jsonText);
365
-
366
- // Check if the response status is 204 (no content)
367
- if (response.status === 204) {
368
- return true;
369
- } else {
370
- throw new Error(`Failed to update material component: ${response.status} ${response.statusText}`);
371
- }
372
- } catch (error: unknown) {
373
- const errorMessage = error instanceof Error ? error.message : "Unknown error";
374
- throw new Error(`Error updating material component: ${errorMessage}`);
375
- }
339
+ // Check if the response status is 204 (no content)
340
+ if (status !== 204) {
341
+ throw new Error(`Failed to update material component: ${status} ${statusText}`);
342
+ }
343
+ return true;
376
344
  }
377
345
 
378
346
  export async function getMaterials(): Promise<Material[]> {
package/src/utilities.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * This file has been auto-generated by a code generation tool.
3
- * Version: 1.0.77
4
- * Date/time: 18 Jun 2025 15:16:44
3
+ * Version: 1.0.78
4
+ * Date/time: 20 Jun 2025 16:35:05
5
5
  * Template: templates/typescriptpws/utilities.razor.
6
6
  ***********************************************************************/
7
7
 
@@ -56,108 +56,49 @@ function getHeaders(accessToken: string) {
56
56
  }
57
57
 
58
58
  // Post JSON to URL and time the call
59
- export async function postRequest(
60
- url: string,
61
- data: string
62
- ): Promise<AxiosResponse> {
63
- try {
64
- const accessToken = getAccessToken();
65
-
66
- if (!accessToken) {
67
- throw new Error("Access token not found");
68
- }
69
-
70
- const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
71
-
72
- if (!hasExpired) {
73
- const response = await axios.post(url, data, {
74
- headers: getHeaders(accessToken),
75
- });
76
- return response;
77
- } else {
78
- console.log(`Your access token has expired: ${expiryDate}`);
79
- throw new Error("Expired access token");
80
- }
81
- } catch (error: unknown) {
82
- if (axios.isAxiosError(error)) {
83
- console.error(
84
- "Error during HTTP request:",
85
- error.response?.data || error.message
86
- );
87
- throw error;
88
- } else {
89
- console.error("Unexpected error:", error);
90
- throw new Error("Unexpected error occurred");
91
- }
59
+ export async function postRequest<T>(url: string, data: string): Promise<AxiosResponse<T>> {
60
+ const accessToken = getAccessToken();
61
+ if (!accessToken) {
62
+ throw new Error("Access token not found");
63
+ }
64
+
65
+ const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
66
+ if (hasExpired) {
67
+ throw new Error(`Expired access token (${expiryDate})`);
92
68
  }
69
+
70
+ return axios.post(url, data, { headers: getHeaders(accessToken) });
93
71
  }
94
72
 
95
73
  // Put JSON to URL and time the call
96
- export async function putRequest(url: string, data: string) {
97
- try {
98
- const accessToken = getAccessToken();
99
-
100
- if (!accessToken) {
101
- throw new Error("Access token not found");
102
- }
103
-
104
- const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
105
-
106
- if (!hasExpired) {
107
- const response = await axios.put(url, data, {
108
- headers: getHeaders(accessToken),
109
- });
110
- return response;
111
- } else {
112
- console.log(`Your access token has expired: ${expiryDate}`);
113
- throw new Error("Expired access token");
114
- }
115
- } catch (error: unknown) {
116
- if (axios.isAxiosError(error)) {
117
- console.error(
118
- "Error during HTTP request:",
119
- error.response?.data || error.message
120
- );
121
- throw error;
122
- } else {
123
- console.error("Unexpected error:", error);
124
- throw new Error("Unexpected error occurred");
125
- }
74
+ export async function putRequest<T>(url: string, data: string): Promise<AxiosResponse<T>> {
75
+ const accessToken = getAccessToken();
76
+ if (!accessToken) {
77
+ throw new Error("Access token not found");
78
+ }
79
+
80
+ const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
81
+ if (hasExpired) {
82
+ throw new Error(`Expired access token (${expiryDate})`);
126
83
  }
84
+
85
+ return axios.put(url, data, { headers: getHeaders(accessToken) });
127
86
  }
128
87
 
129
88
  // Get data from URL
130
- export async function getRequest(url: string) {
131
- try {
132
- const accessToken = getAccessToken();
133
-
134
- if (!accessToken) {
135
- throw new Error("Access token not found");
136
- }
137
-
138
- const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
139
-
140
- if (!hasExpired) {
141
- const response = await axios.get(url, {
142
- headers: getHeaders(accessToken),
143
- });
144
- return response;
145
- } else {
146
- console.log(`Your access token has expired: ${expiryDate}`);
147
- throw new Error("Expired access token");
148
- }
149
- } catch (error: unknown) {
150
- if (axios.isAxiosError(error)) {
151
- console.error(
152
- "Error during HTTP request:",
153
- error.response?.data || error.message
154
- );
155
- throw error;
156
- } else {
157
- console.error("Unexpected error:", error);
158
- throw new Error("Unexpected error occurred");
159
- }
89
+ export async function getRequest<T>(url: string): Promise<AxiosResponse<T>> {
90
+ const accessToken = getAccessToken();
91
+ if (!accessToken) {
92
+ throw new Error("Access token not found");
160
93
  }
94
+
95
+ const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
96
+ if (hasExpired) {
97
+ console.log(`Your access token has expired: ${expiryDate}`);
98
+ throw new Error("Expired access token");
99
+ }
100
+
101
+ return axios.get(url, { headers: getHeaders(accessToken) });
161
102
  }
162
103
 
163
104
  // Get platform, expiry date and token expiration status