@dnv-plant/typescriptpws 1.0.97 → 1.0.98-alpha.2397542

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
@@ -6,9 +6,9 @@
6
6
  * Editing it may lead to inconsistent results and limit DNV's ability to provide support.
7
7
  * Please contact DNV if you believe changes are required.
8
8
  *
9
- * Version: 1.0.97
10
- * Date/time: 13 Apr 2026 17:02:52
11
- * Template: templates/typescriptpws/utilities.razor.
9
+ * Version: 1.0.0
10
+ * Date/time: 23 Jun 2026 12:11:11
11
+ * Template: TYPE_SCRIPT_PWS/utilities.sbn.
12
12
  ***********************************************************************/
13
13
 
14
14
  import jwt from 'jsonwebtoken';
@@ -18,10 +18,12 @@ import { REST_API_URI, PWS_CLIENT_ID, REST_API_VERSION } from './constants';
18
18
 
19
19
  const accessTokenKey = '__pwsAccessToken';
20
20
  const usedInDnvKey = '__pwsUsedInDnvKey';
21
+ const isAnalytics2Key = '__pwsIsAnalytics2';
21
22
  const clientAliasKey = '__pwsClientAliasId';
22
23
 
23
24
  (globalThis as any)[accessTokenKey] = (globalThis as any)[accessTokenKey] ?? '';
24
25
  (globalThis as any)[usedInDnvKey] = (globalThis as any)[usedInDnvKey] ?? false;
26
+ (globalThis as any)[isAnalytics2Key] = (globalThis as any)[isAnalytics2Key] ?? false;
25
27
  (globalThis as any)[clientAliasKey] = (globalThis as any)[clientAliasKey] ?? PWS_CLIENT_ID;
26
28
 
27
29
  export function getAccessToken(): string {
@@ -42,6 +44,16 @@ export function setUsedInDnvProduct(value: boolean) {
42
44
  (globalThis as any)[usedInDnvKey] = value;
43
45
  }
44
46
 
47
+ // For DNV use only
48
+ function isAnalytics2(): boolean {
49
+ return (globalThis as any)[isAnalytics2Key];
50
+ }
51
+
52
+ // For DNV use only
53
+ export function setAnalytics2(value: boolean) {
54
+ (globalThis as any)[isAnalytics2Key] = value;
55
+ }
56
+
45
57
  // Get client alias ID
46
58
  export function getClientAliasId() {
47
59
  return (globalThis as any)[clientAliasKey];
@@ -55,8 +67,8 @@ export function setClientAliasId(clientAliasId: string) {
55
67
  // Get headers for requests
56
68
  function getHeaders(accessToken: string) {
57
69
  return {
58
- 'Content-Type': 'application/json',
59
- Authorization: `Bearer ${accessToken}`,
70
+ 'Content-Type': 'application/json',
71
+ Authorization: `Bearer ${accessToken}`,
60
72
  };
61
73
  }
62
74
 
@@ -66,17 +78,17 @@ const axiosInstance = axios.create({});
66
78
  export async function postRequest<T>(url: string, data: string, controller?: AbortController): Promise<AxiosResponse<T>> {
67
79
  const accessToken = getAccessToken();
68
80
  if (!accessToken) {
69
- throw new Error('Access token not found');
81
+ throw new Error('Access token not found');
70
82
  }
71
83
 
72
84
  const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
73
85
  if (hasExpired) {
74
- throw new Error(`Expired access token (${expiryDate})`);
86
+ throw new Error(`Expired access token (${expiryDate})`);
75
87
  }
76
88
 
77
89
  const response = await axiosInstance.post<T>(url, data, {
78
- headers: getHeaders(accessToken),
79
- signal: controller?.signal,
90
+ headers: getHeaders(accessToken),
91
+ signal: controller?.signal,
80
92
  });
81
93
  return response;
82
94
  }
@@ -85,17 +97,17 @@ export async function postRequest<T>(url: string, data: string, controller?: Abo
85
97
  export async function putRequest<T>(url: string, data: string, controller?: AbortController): Promise<AxiosResponse<T>> {
86
98
  const accessToken = getAccessToken();
87
99
  if (!accessToken) {
88
- throw new Error('Access token not found');
100
+ throw new Error('Access token not found');
89
101
  }
90
102
 
91
103
  const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
92
104
  if (hasExpired) {
93
- throw new Error(`Expired access token (${expiryDate})`);
105
+ throw new Error(`Expired access token (${expiryDate})`);
94
106
  }
95
107
 
96
108
  const response = await axiosInstance.put<T>(url, data, {
97
- headers: getHeaders(accessToken),
98
- signal: controller?.signal,
109
+ headers: getHeaders(accessToken),
110
+ signal: controller?.signal,
99
111
  });
100
112
  return response;
101
113
  }
@@ -104,18 +116,18 @@ export async function putRequest<T>(url: string, data: string, controller?: Abor
104
116
  export async function getRequest<T>(url: string, controller?: AbortController): Promise<AxiosResponse<T>> {
105
117
  const accessToken = getAccessToken();
106
118
  if (!accessToken) {
107
- throw new Error('Access token not found');
119
+ throw new Error('Access token not found');
108
120
  }
109
121
 
110
122
  const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
111
123
  if (hasExpired) {
112
- console.log(`Your access token has expired: ${expiryDate}`);
113
- throw new Error('Expired access token');
124
+ console.log(`Your access token has expired: ${expiryDate}`);
125
+ throw new Error('Expired access token');
114
126
  }
115
127
 
116
128
  const response = await axiosInstance.get<T>(url, {
117
- headers: getHeaders(accessToken),
118
- signal: controller?.signal,
129
+ headers: getHeaders(accessToken),
130
+ signal: controller?.signal,
119
131
  });
120
132
  return response;
121
133
  }
@@ -126,14 +138,14 @@ function getAccessTokenInfo(accessToken: string): [string, string, boolean] {
126
138
 
127
139
  // Ensure the token is valid and contains the expected properties
128
140
  if (!decoded || typeof decoded !== 'object' || !('exp' in decoded)) {
129
- throw new Error('Invalid or malformed token');
141
+ throw new Error('Invalid or malformed token');
130
142
  }
131
143
 
132
144
  const exp = decoded.exp as number;
133
145
  const aud = decoded.aud as string | undefined;
134
146
 
135
147
  if (typeof exp !== 'number') {
136
- throw new Error('Token does not have a valid expiration time');
148
+ throw new Error('Token does not have a valid expiration time');
137
149
  }
138
150
 
139
151
  const platform = aud || 'unknown';
@@ -150,16 +162,16 @@ function getApiRoot() {
150
162
  const isDnvProduct = isUsedInDnvProduct();
151
163
 
152
164
  if (isDnvProduct) {
153
- return '/api/';
165
+ return '/api/';
154
166
  }
155
167
 
156
168
  if (accessToken) {
157
- const [platform] = getAccessTokenInfo(accessToken);
169
+ const [platform] = getAccessTokenInfo(accessToken);
158
170
 
159
- // Determine API root based on devMode
160
- const apiTarget = devMode ? '/api/' : `${platform}/api/`;
171
+ // Determine API root based on devMode
172
+ const apiTarget = devMode ? '/api/' : `${platform}/api/`;
161
173
 
162
- return apiTarget;
174
+ return apiTarget;
163
175
  }
164
176
 
165
177
  return REST_API_URI;
@@ -172,7 +184,8 @@ function getApiVersion() {
172
184
 
173
185
  // Get analytics API target URL
174
186
  export function getAnalyticsApiTarget() {
175
- return `${getApiRoot()}analytics/v${getApiVersion()}/`;
187
+ const analyticsName = isAnalytics2() ? 'analytics2' : 'analytics';
188
+ return `${getApiRoot()}${analyticsName}/v${getApiVersion()}/`;
176
189
  }
177
190
 
178
191
  // Get materials API target URL
@@ -188,4 +201,4 @@ export function convertSnakeCaseToCamelCase(snakeStr: string) {
188
201
  // Convert camelCase to snake_case
189
202
  export function convertCamelCaseToSnakeCase(camelStr: string) {
190
203
  return camelStr.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
191
- }
204
+ }