@dnv-plant/typescriptpws 1.0.95 → 1.0.96

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/utilities.ts CHANGED
@@ -1,29 +1,28 @@
1
1
  /***********************************************************************
2
- * This file has been auto-generated by a code generation tool.
3
- *
4
- * DO NOT MODIFY THIS FILE
5
- * This file is maintained by DNV.
6
- * Editing it may lead to inconsistent results and limit DNV's ability to provide support.
7
- * Please contact DNV if you believe changes are required.
8
- *
9
- * Version: 1.0.95
10
- * Date/time: 19 Jan 2026 12:11:52
11
- * Template: templates/typescriptpws/utilities.razor.
12
- ***********************************************************************/
13
-
14
- import jwt from "jsonwebtoken";
15
- import axios, { AxiosResponse } from "axios";
16
-
17
- import { REST_API_URI, PWS_CLIENT_ID, REST_API_VERSION } from "./constants";
18
-
19
- const accessTokenKey = "__pwsAccessToken";
20
- const usedInDnvKey = "__pwsUsedInDnvKey";
21
- const clientAliasKey = "__pwsClientAliasId";
22
-
23
- (globalThis as any)[accessTokenKey] = (globalThis as any)[accessTokenKey] ?? "";
2
+ * This file has been auto-generated by a code generation tool.
3
+ *
4
+ * DO NOT MODIFY THIS FILE
5
+ * This file is maintained by DNV.
6
+ * Editing it may lead to inconsistent results and limit DNV's ability to provide support.
7
+ * Please contact DNV if you believe changes are required.
8
+ *
9
+ * Version: 1.0.96
10
+ * Date/time: 4 Apr 2026 08:17:52
11
+ * Template: templates/typescriptpws/utilities.razor.
12
+ ***********************************************************************/
13
+
14
+ import jwt from 'jsonwebtoken';
15
+ import axios, { AxiosResponse } from 'axios';
16
+
17
+ import { REST_API_URI, PWS_CLIENT_ID, REST_API_VERSION } from './constants';
18
+
19
+ const accessTokenKey = '__pwsAccessToken';
20
+ const usedInDnvKey = '__pwsUsedInDnvKey';
21
+ const clientAliasKey = '__pwsClientAliasId';
22
+
23
+ (globalThis as any)[accessTokenKey] = (globalThis as any)[accessTokenKey] ?? '';
24
24
  (globalThis as any)[usedInDnvKey] = (globalThis as any)[usedInDnvKey] ?? false;
25
- (globalThis as any)[clientAliasKey] =
26
- (globalThis as any)[clientAliasKey] ?? PWS_CLIENT_ID;
25
+ (globalThis as any)[clientAliasKey] = (globalThis as any)[clientAliasKey] ?? PWS_CLIENT_ID;
27
26
 
28
27
  export function getAccessToken(): string {
29
28
  return (globalThis as any)[accessTokenKey];
@@ -56,7 +55,7 @@ export function setClientAliasId(clientAliasId: string) {
56
55
  // Get headers for requests
57
56
  function getHeaders(accessToken: string) {
58
57
  return {
59
- "Content-Type": "application/json",
58
+ 'Content-Type': 'application/json',
60
59
  Authorization: `Bearer ${accessToken}`,
61
60
  };
62
61
  }
@@ -64,14 +63,10 @@ function getHeaders(accessToken: string) {
64
63
  const axiosInstance = axios.create({});
65
64
 
66
65
  // Post JSON to URL and time the call
67
- export async function postRequest<T>(
68
- url: string,
69
- data: string,
70
- controller?: AbortController
71
- ): Promise<AxiosResponse<T>> {
66
+ export async function postRequest<T>(url: string, data: string, controller?: AbortController): Promise<AxiosResponse<T>> {
72
67
  const accessToken = getAccessToken();
73
68
  if (!accessToken) {
74
- throw new Error("Access token not found");
69
+ throw new Error('Access token not found');
75
70
  }
76
71
 
77
72
  const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
@@ -81,20 +76,16 @@ export async function postRequest<T>(
81
76
 
82
77
  const response = await axiosInstance.post<T>(url, data, {
83
78
  headers: getHeaders(accessToken),
84
- signal: controller?.signal
79
+ signal: controller?.signal,
85
80
  });
86
81
  return response;
87
82
  }
88
83
 
89
84
  // Put JSON to URL and time the call
90
- export async function putRequest<T>(
91
- url: string,
92
- data: string,
93
- controller?: AbortController
94
- ): Promise<AxiosResponse<T>> {
85
+ export async function putRequest<T>(url: string, data: string, controller?: AbortController): Promise<AxiosResponse<T>> {
95
86
  const accessToken = getAccessToken();
96
87
  if (!accessToken) {
97
- throw new Error("Access token not found");
88
+ throw new Error('Access token not found');
98
89
  }
99
90
 
100
91
  const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
@@ -104,7 +95,7 @@ export async function putRequest<T>(
104
95
 
105
96
  const response = await axiosInstance.put<T>(url, data, {
106
97
  headers: getHeaders(accessToken),
107
- signal: controller?.signal
98
+ signal: controller?.signal,
108
99
  });
109
100
  return response;
110
101
  }
@@ -113,18 +104,18 @@ export async function putRequest<T>(
113
104
  export async function getRequest<T>(url: string, controller?: AbortController): Promise<AxiosResponse<T>> {
114
105
  const accessToken = getAccessToken();
115
106
  if (!accessToken) {
116
- throw new Error("Access token not found");
107
+ throw new Error('Access token not found');
117
108
  }
118
109
 
119
110
  const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
120
111
  if (hasExpired) {
121
112
  console.log(`Your access token has expired: ${expiryDate}`);
122
- throw new Error("Expired access token");
113
+ throw new Error('Expired access token');
123
114
  }
124
115
 
125
116
  const response = await axiosInstance.get<T>(url, {
126
117
  headers: getHeaders(accessToken),
127
- signal: controller?.signal
118
+ signal: controller?.signal,
128
119
  });
129
120
  return response;
130
121
  }
@@ -134,18 +125,18 @@ function getAccessTokenInfo(accessToken: string): [string, string, boolean] {
134
125
  const decoded = jwt.decode(accessToken);
135
126
 
136
127
  // Ensure the token is valid and contains the expected properties
137
- if (!decoded || typeof decoded !== "object" || !("exp" in decoded)) {
138
- throw new Error("Invalid or malformed token");
128
+ if (!decoded || typeof decoded !== 'object' || !('exp' in decoded)) {
129
+ throw new Error('Invalid or malformed token');
139
130
  }
140
131
 
141
132
  const exp = decoded.exp as number;
142
133
  const aud = decoded.aud as string | undefined;
143
134
 
144
- if (typeof exp !== "number") {
145
- throw new Error("Token does not have a valid expiration time");
135
+ if (typeof exp !== 'number') {
136
+ throw new Error('Token does not have a valid expiration time');
146
137
  }
147
138
 
148
- const platform = aud || "unknown";
139
+ const platform = aud || 'unknown';
149
140
  const expiryDate = new Date(exp * 1000).toISOString();
150
141
  const hasExpired = Date.now() >= exp * 1000;
151
142
 
@@ -159,14 +150,14 @@ function getApiRoot() {
159
150
  const isDnvProduct = isUsedInDnvProduct();
160
151
 
161
152
  if (isDnvProduct) {
162
- return "/api/";
153
+ return '/api/';
163
154
  }
164
155
 
165
156
  if (accessToken) {
166
157
  const [platform] = getAccessTokenInfo(accessToken);
167
158
 
168
159
  // Determine API root based on devMode
169
- const apiTarget = devMode ? "/api/" : `${platform}/api/`;
160
+ const apiTarget = devMode ? '/api/' : `${platform}/api/`;
170
161
 
171
162
  return apiTarget;
172
163
  }
@@ -196,5 +187,5 @@ export function convertSnakeCaseToCamelCase(snakeStr: string) {
196
187
 
197
188
  // Convert camelCase to snake_case
198
189
  export function convertCamelCaseToSnakeCase(camelStr: string) {
199
- return camelStr.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
190
+ return camelStr.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
200
191
  }