@dnv-plant/typescriptpws 1.0.16 → 1.0.18-alpha.1805813
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/index.ts +3 -3
- package/package.json +2 -2
- package/src/calculations.ts +364 -2344
- package/src/constants.ts +2 -2
- package/src/entities.ts +976 -649
- package/src/entity-schemas.ts +232 -671
- package/src/enums.ts +2 -2
- package/src/materials.ts +53 -154
- package/src/utilities.ts +11 -32
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.
|
|
4
|
-
* Date/time:
|
|
3
|
+
* Version: 1.0.18
|
|
4
|
+
* Date/time: 18 Feb 2025 14:37:48
|
|
5
5
|
* Template: templates/typescriptpws/enums.razor.
|
|
6
6
|
***********************************************************************/
|
|
7
7
|
|
package/src/materials.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
/***********************************************************************
|
|
2
2
|
* This file has been auto-generated by a code generation tool.
|
|
3
|
-
* Version: 1.0.
|
|
4
|
-
* Date/time:
|
|
3
|
+
* Version: 1.0.18
|
|
4
|
+
* Date/time: 18 Feb 2025 14:37:47
|
|
5
5
|
* Template: templates/typescriptpws/materials.razor.
|
|
6
6
|
***********************************************************************/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
getRequest,
|
|
10
|
-
postRequest,
|
|
11
|
-
putRequest,
|
|
12
|
-
getMaterialsApiTarget,
|
|
13
|
-
getClientAliasId,
|
|
14
|
-
} from "./utilities";
|
|
8
|
+
import { getRequest, postRequest, putRequest, getMaterialsApiTarget, getClientAliasId } from "./utilities";
|
|
15
9
|
import { MaterialSchema, MaterialComponentDataSchema } from "./entity-schemas";
|
|
16
10
|
import { Material, MaterialComponentData } from "./entities";
|
|
17
11
|
|
|
@@ -28,7 +22,10 @@ export class MaterialInfo {
|
|
|
28
22
|
* @param {string} id - A unique identifier of the material.
|
|
29
23
|
* @param {string} name - The name of the material.
|
|
30
24
|
*/
|
|
31
|
-
constructor(
|
|
25
|
+
constructor(
|
|
26
|
+
readonly id: string,
|
|
27
|
+
readonly name: string
|
|
28
|
+
) {}
|
|
32
29
|
}
|
|
33
30
|
|
|
34
31
|
export class MaterialCasIdInfo {
|
|
@@ -88,11 +85,7 @@ export class MaterialCasIdInfoSchema {
|
|
|
88
85
|
* @returns A MaterialCasIdInfo instance.
|
|
89
86
|
* @throws Error if validation fails.
|
|
90
87
|
*/
|
|
91
|
-
makeMaterialInfo(data: {
|
|
92
|
-
id: string;
|
|
93
|
-
name: string;
|
|
94
|
-
casId: string;
|
|
95
|
-
}): MaterialCasIdInfo {
|
|
88
|
+
makeMaterialInfo(data: { id: string; name: string; casId: string }): MaterialCasIdInfo {
|
|
96
89
|
const { error, value } = this.schema.validate(data);
|
|
97
90
|
if (error) {
|
|
98
91
|
throw new Error(`Validation error: ${error.details[0].message}`);
|
|
@@ -151,54 +144,34 @@ export class MaterialEntityDescriptorSchema {
|
|
|
151
144
|
delete value.extendedProperties;
|
|
152
145
|
|
|
153
146
|
// Return a new instance of the MaterialEntityDescriptor class.
|
|
154
|
-
return new MaterialEntityDescriptor(
|
|
155
|
-
value.id,
|
|
156
|
-
value.displayName,
|
|
157
|
-
value.cas_id
|
|
158
|
-
);
|
|
147
|
+
return new MaterialEntityDescriptor(value.id, value.displayName, value.cas_id);
|
|
159
148
|
}
|
|
160
149
|
}
|
|
161
150
|
|
|
162
|
-
async function getDataFromUrl<T>(
|
|
163
|
-
url: string,
|
|
164
|
-
schemaClass: SchemaClass,
|
|
165
|
-
many: boolean = false
|
|
166
|
-
): Promise<T | T[]> {
|
|
151
|
+
async function getDataFromUrl<T>(url: string, schemaClass: SchemaClass, many: boolean = false): Promise<T | T[]> {
|
|
167
152
|
try {
|
|
168
153
|
const response = await getRequest(url);
|
|
169
154
|
|
|
170
155
|
if (response.status !== 200) {
|
|
171
|
-
throw new Error(
|
|
172
|
-
`Failed to get data: ${response.status} ${response.statusText}`
|
|
173
|
-
);
|
|
156
|
+
throw new Error(`Failed to get data: ${response.status} ${response.statusText}`);
|
|
174
157
|
}
|
|
175
158
|
|
|
176
159
|
const data = response.data;
|
|
177
160
|
|
|
178
161
|
// Access the Joi schema from the schema class
|
|
179
|
-
const validationSchema = many
|
|
180
|
-
? Joi.array().items(schemaClass.schema)
|
|
181
|
-
: schemaClass.schema;
|
|
162
|
+
const validationSchema = many ? Joi.array().items(schemaClass.schema) : schemaClass.schema;
|
|
182
163
|
|
|
183
164
|
// Validate the data
|
|
184
|
-
const { error, value } = validationSchema.validate(data, {
|
|
185
|
-
abortEarly: false,
|
|
186
|
-
});
|
|
165
|
+
const { error, value } = validationSchema.validate(data, { abortEarly: false });
|
|
187
166
|
|
|
188
167
|
if (error) {
|
|
189
|
-
const errorMessages = error.details
|
|
190
|
-
.map((e: { message: any }) => e.message)
|
|
191
|
-
.join(", ");
|
|
168
|
+
const errorMessages = error.details.map((e: { message: any }) => e.message).join(", ");
|
|
192
169
|
throw new Error(`Error retrieving data: ${errorMessages}`);
|
|
193
170
|
}
|
|
194
171
|
|
|
195
172
|
return value;
|
|
196
173
|
} catch (err: unknown) {
|
|
197
|
-
throw new Error(
|
|
198
|
-
`Error retrieving data: ${
|
|
199
|
-
err instanceof Error ? err.message : "Unknown error"
|
|
200
|
-
}`
|
|
201
|
-
);
|
|
174
|
+
throw new Error(`Error retrieving data: ${err instanceof Error ? err.message : "Unknown error"}`);
|
|
202
175
|
}
|
|
203
176
|
}
|
|
204
177
|
|
|
@@ -210,12 +183,9 @@ export async function getAllCasIds(): Promise<MaterialCasIdInfo[]> {
|
|
|
210
183
|
const url = `${apiTarget}cas?clientId=${clientAliasId}`;
|
|
211
184
|
|
|
212
185
|
// Fetch and validate the data
|
|
213
|
-
return getDataFromUrl(url, new MaterialCasIdInfoSchema(), true) as Promise<
|
|
214
|
-
MaterialCasIdInfo[]
|
|
215
|
-
>;
|
|
186
|
+
return getDataFromUrl(url, new MaterialCasIdInfoSchema(), true) as Promise<MaterialCasIdInfo[]>;
|
|
216
187
|
} catch (error: unknown) {
|
|
217
|
-
const errorMessage =
|
|
218
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
188
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
219
189
|
throw new Error(`Error retrieving CAS IDs: ${errorMessage}`);
|
|
220
190
|
}
|
|
221
191
|
}
|
|
@@ -230,17 +200,12 @@ export async function getMaterialByCasId(casId: string): Promise<Material> {
|
|
|
230
200
|
// Fetch and validate the data
|
|
231
201
|
return getDataFromUrl(url, new MaterialSchema()) as Promise<Material>;
|
|
232
202
|
} catch (error: unknown) {
|
|
233
|
-
const errorMessage =
|
|
234
|
-
|
|
235
|
-
throw new Error(
|
|
236
|
-
`Error retrieving material with CAS ID ${casId}: ${errorMessage}`
|
|
237
|
-
);
|
|
203
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
204
|
+
throw new Error(`Error retrieving material with CAS ID ${casId}: ${errorMessage}`);
|
|
238
205
|
}
|
|
239
206
|
}
|
|
240
207
|
|
|
241
|
-
export async function getComponents(
|
|
242
|
-
source: number
|
|
243
|
-
): Promise<MaterialComponentData[]> {
|
|
208
|
+
export async function getComponents(source: number): Promise<MaterialComponentData[]> {
|
|
244
209
|
try {
|
|
245
210
|
// Generate the URL
|
|
246
211
|
const apiTarget = getMaterialsApiTarget();
|
|
@@ -248,14 +213,9 @@ export async function getComponents(
|
|
|
248
213
|
const url = `${apiTarget}components?clientId=${clientAliasId}&sources=${source}`;
|
|
249
214
|
|
|
250
215
|
// Fetch and validate the data
|
|
251
|
-
return getDataFromUrl(
|
|
252
|
-
url,
|
|
253
|
-
new MaterialEntityDescriptorSchema(),
|
|
254
|
-
true
|
|
255
|
-
) as Promise<MaterialComponentData[]>;
|
|
216
|
+
return getDataFromUrl(url, new MaterialEntityDescriptorSchema(), true) as Promise<MaterialComponentData[]>;
|
|
256
217
|
} catch (error: unknown) {
|
|
257
|
-
const errorMessage =
|
|
258
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
218
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
259
219
|
throw new Error(`Error retrieving components: ${errorMessage}`);
|
|
260
220
|
}
|
|
261
221
|
}
|
|
@@ -272,9 +232,7 @@ export async function getUserComponents(): Promise<MaterialComponentData[]> {
|
|
|
272
232
|
return getComponents(3);
|
|
273
233
|
}
|
|
274
234
|
|
|
275
|
-
export async function getComponentById(
|
|
276
|
-
id: string
|
|
277
|
-
): Promise<MaterialComponentData> {
|
|
235
|
+
export async function getComponentById(id: string): Promise<MaterialComponentData> {
|
|
278
236
|
try {
|
|
279
237
|
// Generate the URL
|
|
280
238
|
const apiTarget = getMaterialsApiTarget();
|
|
@@ -282,41 +240,25 @@ export async function getComponentById(
|
|
|
282
240
|
const url = `${apiTarget}components/id=${id}?clientId=${clientAliasId}`;
|
|
283
241
|
|
|
284
242
|
// Fetch and validate the data
|
|
285
|
-
return getDataFromUrl(
|
|
286
|
-
url,
|
|
287
|
-
new MaterialComponentDataSchema()
|
|
288
|
-
) as Promise<MaterialComponentData>;
|
|
243
|
+
return getDataFromUrl(url, new MaterialComponentDataSchema()) as Promise<MaterialComponentData>;
|
|
289
244
|
} catch (error: unknown) {
|
|
290
|
-
const errorMessage =
|
|
291
|
-
|
|
292
|
-
throw new Error(
|
|
293
|
-
`Error retrieving component with ID ${id}: ${errorMessage}`
|
|
294
|
-
);
|
|
245
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
246
|
+
throw new Error(`Error retrieving component with ID ${id}: ${errorMessage}`);
|
|
295
247
|
}
|
|
296
248
|
}
|
|
297
249
|
|
|
298
|
-
export async function getComponentByName(
|
|
299
|
-
name: string
|
|
300
|
-
): Promise<MaterialComponentData> {
|
|
250
|
+
export async function getComponentByName(name: string): Promise<MaterialComponentData> {
|
|
301
251
|
try {
|
|
302
252
|
// Generate the URL
|
|
303
253
|
const apiTarget = getMaterialsApiTarget();
|
|
304
254
|
const clientAliasId = getClientAliasId();
|
|
305
|
-
const url = `${apiTarget}components/name=${encodeURIComponent(
|
|
306
|
-
name
|
|
307
|
-
)}?clientId=${clientAliasId}`;
|
|
255
|
+
const url = `${apiTarget}components/name=${encodeURIComponent(name)}?clientId=${clientAliasId}`;
|
|
308
256
|
|
|
309
257
|
// Fetch and validate the data
|
|
310
|
-
return getDataFromUrl(
|
|
311
|
-
url,
|
|
312
|
-
new MaterialComponentDataSchema()
|
|
313
|
-
) as Promise<MaterialComponentData>;
|
|
258
|
+
return getDataFromUrl(url, new MaterialComponentDataSchema()) as Promise<MaterialComponentData>;
|
|
314
259
|
} catch (error: unknown) {
|
|
315
|
-
const errorMessage =
|
|
316
|
-
|
|
317
|
-
throw new Error(
|
|
318
|
-
`Error retrieving component with name ${name}: ${errorMessage}`
|
|
319
|
-
);
|
|
260
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
261
|
+
throw new Error(`Error retrieving component with name ${name}: ${errorMessage}`);
|
|
320
262
|
}
|
|
321
263
|
}
|
|
322
264
|
|
|
@@ -325,29 +267,17 @@ export async function getComponentByCasId(casId: string) {
|
|
|
325
267
|
// Generate the URL
|
|
326
268
|
const apiTarget = getMaterialsApiTarget();
|
|
327
269
|
const clientAliasId = getClientAliasId();
|
|
328
|
-
const url = `${apiTarget}components/casid=${encodeURIComponent(
|
|
329
|
-
casId
|
|
330
|
-
)}?clientId=${clientAliasId}`;
|
|
270
|
+
const url = `${apiTarget}components/casid=${encodeURIComponent(casId)}?clientId=${clientAliasId}`;
|
|
331
271
|
|
|
332
272
|
// Fetch and validate the data
|
|
333
|
-
return getDataFromUrl(
|
|
334
|
-
url,
|
|
335
|
-
new MaterialComponentDataSchema()
|
|
336
|
-
) as Promise<MaterialComponentData>;
|
|
273
|
+
return getDataFromUrl(url, new MaterialComponentDataSchema()) as Promise<MaterialComponentData>;
|
|
337
274
|
} catch (error: unknown) {
|
|
338
|
-
const errorMessage =
|
|
339
|
-
|
|
340
|
-
throw new Error(
|
|
341
|
-
`Error retrieving component with CAS ID ${casId}: ${errorMessage}`
|
|
342
|
-
);
|
|
275
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
276
|
+
throw new Error(`Error retrieving component with CAS ID ${casId}: ${errorMessage}`);
|
|
343
277
|
}
|
|
344
278
|
}
|
|
345
279
|
|
|
346
|
-
export async function storeDataAndGetResponse<T>(
|
|
347
|
-
url: string,
|
|
348
|
-
data: any,
|
|
349
|
-
schemaClass: SchemaClass
|
|
350
|
-
): Promise<T> {
|
|
280
|
+
export async function storeDataAndGetResponse<T>(url: string, data: any, schemaClass: SchemaClass): Promise<T> {
|
|
351
281
|
// Serialize the data using the schema
|
|
352
282
|
|
|
353
283
|
const { error, value } = schemaClass.schema.validate(data);
|
|
@@ -379,14 +309,10 @@ export async function storeDataAndGetResponse<T>(
|
|
|
379
309
|
}
|
|
380
310
|
return value;
|
|
381
311
|
} else {
|
|
382
|
-
throw new Error(
|
|
383
|
-
`Failed to retrieve data: ${getResponse.status} ${getResponse.statusText}`
|
|
384
|
-
);
|
|
312
|
+
throw new Error(`Failed to retrieve data: ${getResponse.status} ${getResponse.statusText}`);
|
|
385
313
|
}
|
|
386
314
|
} else {
|
|
387
|
-
throw new Error(
|
|
388
|
-
`Failed to store data: ${postResponse.status} ${postResponse.statusText}`
|
|
389
|
-
);
|
|
315
|
+
throw new Error(`Failed to store data: ${postResponse.status} ${postResponse.statusText}`);
|
|
390
316
|
}
|
|
391
317
|
}
|
|
392
318
|
|
|
@@ -400,14 +326,9 @@ export async function storeMaterialComponentData(
|
|
|
400
326
|
const url = `${apiTarget}components?clientId=${clientAliasId}`;
|
|
401
327
|
|
|
402
328
|
// Store the data and return the response
|
|
403
|
-
return storeDataAndGetResponse(
|
|
404
|
-
url,
|
|
405
|
-
materialComponentData,
|
|
406
|
-
new MaterialComponentDataSchema()
|
|
407
|
-
);
|
|
329
|
+
return storeDataAndGetResponse(url, materialComponentData, new MaterialComponentDataSchema());
|
|
408
330
|
} catch (error: unknown) {
|
|
409
|
-
const errorMessage =
|
|
410
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
331
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
411
332
|
throw new Error(`Error storing material component data: ${errorMessage}`);
|
|
412
333
|
}
|
|
413
334
|
}
|
|
@@ -422,23 +343,14 @@ export async function storeMaterialComponentAndCreateMaterial(
|
|
|
422
343
|
const url = `${apiTarget}components/material?clientId=${clientAliasId}`;
|
|
423
344
|
|
|
424
345
|
// Store the data and return the response
|
|
425
|
-
return storeDataAndGetResponse(
|
|
426
|
-
url,
|
|
427
|
-
materialComponentData,
|
|
428
|
-
new MaterialSchema()
|
|
429
|
-
);
|
|
346
|
+
return storeDataAndGetResponse(url, materialComponentData, new MaterialSchema());
|
|
430
347
|
} catch (error: unknown) {
|
|
431
|
-
const errorMessage =
|
|
432
|
-
|
|
433
|
-
throw new Error(
|
|
434
|
-
`Error storing material component and creating material: ${errorMessage}`
|
|
435
|
-
);
|
|
348
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
349
|
+
throw new Error(`Error storing material component and creating material: ${errorMessage}`);
|
|
436
350
|
}
|
|
437
351
|
}
|
|
438
352
|
|
|
439
|
-
export async function updateMaterialComponent(
|
|
440
|
-
materialComponentData: MaterialComponentData
|
|
441
|
-
): Promise<boolean> {
|
|
353
|
+
export async function updateMaterialComponent(materialComponentData: MaterialComponentData): Promise<boolean> {
|
|
442
354
|
try {
|
|
443
355
|
// Generate the URL
|
|
444
356
|
const apiTarget = getMaterialsApiTarget();
|
|
@@ -455,13 +367,10 @@ export async function updateMaterialComponent(
|
|
|
455
367
|
if (response.status === 204) {
|
|
456
368
|
return true;
|
|
457
369
|
} else {
|
|
458
|
-
throw new Error(
|
|
459
|
-
`Failed to update material component: ${response.status} ${response.statusText}`
|
|
460
|
-
);
|
|
370
|
+
throw new Error(`Failed to update material component: ${response.status} ${response.statusText}`);
|
|
461
371
|
}
|
|
462
372
|
} catch (error: unknown) {
|
|
463
|
-
const errorMessage =
|
|
464
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
373
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
465
374
|
throw new Error(`Error updating material component: ${errorMessage}`);
|
|
466
375
|
}
|
|
467
376
|
}
|
|
@@ -474,12 +383,9 @@ export async function getMaterials(): Promise<Material[]> {
|
|
|
474
383
|
const url = `${apiTarget}materials?clientId=${clientAliasId}`;
|
|
475
384
|
|
|
476
385
|
// Fetch and validate the data
|
|
477
|
-
return getDataFromUrl(url, new MaterialSchema(), true) as Promise<
|
|
478
|
-
Material[]
|
|
479
|
-
>;
|
|
386
|
+
return getDataFromUrl(url, new MaterialSchema(), true) as Promise<Material[]>;
|
|
480
387
|
} catch (error: unknown) {
|
|
481
|
-
const errorMessage =
|
|
482
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
388
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
483
389
|
throw new Error(`Error retrieving materials: ${errorMessage}`);
|
|
484
390
|
}
|
|
485
391
|
}
|
|
@@ -494,8 +400,7 @@ export async function getMaterialById(id: string): Promise<Material> {
|
|
|
494
400
|
// Fetch and validate the data
|
|
495
401
|
return getDataFromUrl(url, new MaterialSchema()) as Promise<Material>;
|
|
496
402
|
} catch (error: unknown) {
|
|
497
|
-
const errorMessage =
|
|
498
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
403
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
499
404
|
throw new Error(`Error retrieving material with ID ${id}: ${errorMessage}`);
|
|
500
405
|
}
|
|
501
406
|
}
|
|
@@ -510,11 +415,8 @@ export async function getMaterialByName(name: string): Promise<Material> {
|
|
|
510
415
|
// Fetch and validate the data
|
|
511
416
|
return getDataFromUrl(url, new MaterialSchema()) as Promise<Material>;
|
|
512
417
|
} catch (error: unknown) {
|
|
513
|
-
const errorMessage =
|
|
514
|
-
|
|
515
|
-
throw new Error(
|
|
516
|
-
`Error retrieving material with name ${name}: ${errorMessage}`
|
|
517
|
-
);
|
|
418
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
419
|
+
throw new Error(`Error retrieving material with name ${name}: ${errorMessage}`);
|
|
518
420
|
}
|
|
519
421
|
}
|
|
520
422
|
|
|
@@ -526,12 +428,9 @@ export async function getMaterialNames() {
|
|
|
526
428
|
const url = `${apiTarget}materials/descriptors?clientId=${clientAliasId}`;
|
|
527
429
|
|
|
528
430
|
// Fetch and validate the data
|
|
529
|
-
return getDataFromUrl(url, new MaterialInfoSchema(), true) as Promise<
|
|
530
|
-
MaterialInfo[]
|
|
531
|
-
>;
|
|
431
|
+
return getDataFromUrl(url, new MaterialInfoSchema(), true) as Promise<MaterialInfo[]>;
|
|
532
432
|
} catch (error: unknown) {
|
|
533
|
-
const errorMessage =
|
|
534
|
-
error instanceof Error ? error.message : "Unknown error";
|
|
433
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
535
434
|
throw new Error(`Error retrieving material names: ${errorMessage}`);
|
|
536
435
|
}
|
|
537
436
|
}
|
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.
|
|
4
|
-
* Date/time:
|
|
3
|
+
* Version: 1.0.18
|
|
4
|
+
* Date/time: 18 Feb 2025 14:37:47
|
|
5
5
|
* Template: templates/typescriptpws/utilities.razor.
|
|
6
6
|
***********************************************************************/
|
|
7
7
|
|
|
@@ -19,10 +19,7 @@ function getHeaders(accessToken: string) {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
// Post JSON to URL and time the call
|
|
22
|
-
export async function postRequest(
|
|
23
|
-
url: string,
|
|
24
|
-
data: string
|
|
25
|
-
): Promise<AxiosResponse> {
|
|
22
|
+
export async function postRequest(url: string, data: string): Promise<AxiosResponse> {
|
|
26
23
|
try {
|
|
27
24
|
const accessToken = getAccessToken();
|
|
28
25
|
|
|
@@ -33,9 +30,7 @@ export async function postRequest(
|
|
|
33
30
|
const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
|
|
34
31
|
|
|
35
32
|
if (!hasExpired) {
|
|
36
|
-
const response = await axios.post(url, data, {
|
|
37
|
-
headers: getHeaders(accessToken),
|
|
38
|
-
});
|
|
33
|
+
const response = await axios.post(url, data, { headers: getHeaders(accessToken) });
|
|
39
34
|
return response;
|
|
40
35
|
} else {
|
|
41
36
|
console.log(`Your access token has expired: ${expiryDate}`);
|
|
@@ -43,10 +38,7 @@ export async function postRequest(
|
|
|
43
38
|
}
|
|
44
39
|
} catch (error: unknown) {
|
|
45
40
|
if (axios.isAxiosError(error)) {
|
|
46
|
-
console.error(
|
|
47
|
-
"Error during HTTP request:",
|
|
48
|
-
error.response?.data || error.message
|
|
49
|
-
);
|
|
41
|
+
console.error("Error during HTTP request:", error.response?.data || error.message);
|
|
50
42
|
throw error;
|
|
51
43
|
} else {
|
|
52
44
|
console.error("Unexpected error:", error);
|
|
@@ -67,9 +59,7 @@ export async function putRequest(url: string, data: string) {
|
|
|
67
59
|
const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
|
|
68
60
|
|
|
69
61
|
if (!hasExpired) {
|
|
70
|
-
const response = await axios.put(url, data, {
|
|
71
|
-
headers: getHeaders(accessToken),
|
|
72
|
-
});
|
|
62
|
+
const response = await axios.put(url, data, { headers: getHeaders(accessToken) });
|
|
73
63
|
return response;
|
|
74
64
|
} else {
|
|
75
65
|
console.log(`Your access token has expired: ${expiryDate}`);
|
|
@@ -77,10 +67,7 @@ export async function putRequest(url: string, data: string) {
|
|
|
77
67
|
}
|
|
78
68
|
} catch (error: unknown) {
|
|
79
69
|
if (axios.isAxiosError(error)) {
|
|
80
|
-
console.error(
|
|
81
|
-
"Error during HTTP request:",
|
|
82
|
-
error.response?.data || error.message
|
|
83
|
-
);
|
|
70
|
+
console.error("Error during HTTP request:", error.response?.data || error.message);
|
|
84
71
|
throw error;
|
|
85
72
|
} else {
|
|
86
73
|
console.error("Unexpected error:", error);
|
|
@@ -101,9 +88,7 @@ export async function getRequest(url: string) {
|
|
|
101
88
|
const [, expiryDate, hasExpired] = getAccessTokenInfo(accessToken);
|
|
102
89
|
|
|
103
90
|
if (!hasExpired) {
|
|
104
|
-
const response = await axios.get(url, {
|
|
105
|
-
headers: getHeaders(accessToken),
|
|
106
|
-
});
|
|
91
|
+
const response = await axios.get(url, { headers: getHeaders(accessToken) });
|
|
107
92
|
return response;
|
|
108
93
|
} else {
|
|
109
94
|
console.log(`Your access token has expired: ${expiryDate}`);
|
|
@@ -111,10 +96,7 @@ export async function getRequest(url: string) {
|
|
|
111
96
|
}
|
|
112
97
|
} catch (error: unknown) {
|
|
113
98
|
if (axios.isAxiosError(error)) {
|
|
114
|
-
console.error(
|
|
115
|
-
"Error during HTTP request:",
|
|
116
|
-
error.response?.data || error.message
|
|
117
|
-
);
|
|
99
|
+
console.error("Error during HTTP request:", error.response?.data || error.message);
|
|
118
100
|
throw error;
|
|
119
101
|
} else {
|
|
120
102
|
console.error("Unexpected error:", error);
|
|
@@ -127,8 +109,7 @@ export async function getRequest(url: string) {
|
|
|
127
109
|
function getAccessToken() {
|
|
128
110
|
const debug = process.env.PYPWS_DEBUG;
|
|
129
111
|
|
|
130
|
-
let accessToken =
|
|
131
|
-
process.env.PWS_ACCESS_TOKEN || process.env.PYPWS_ACCESS_TOKEN;
|
|
112
|
+
let accessToken = process.env.PWS_ACCESS_TOKEN || process.env.PYPWS_ACCESS_TOKEN;
|
|
132
113
|
|
|
133
114
|
if (!accessToken) {
|
|
134
115
|
console.log(
|
|
@@ -151,9 +132,7 @@ function getAccessToken() {
|
|
|
151
132
|
const tokenInfo = getAccessTokenInfo(accessToken);
|
|
152
133
|
if (tokenInfo) {
|
|
153
134
|
const [platform, expiryDate, hasExpired] = tokenInfo;
|
|
154
|
-
console.log(
|
|
155
|
-
`Platform: ${platform}. Expiry date: ${expiryDate}. Expired: ${hasExpired}`
|
|
156
|
-
);
|
|
135
|
+
console.log(`Platform: ${platform}. Expiry date: ${expiryDate}. Expired: ${hasExpired}`);
|
|
157
136
|
}
|
|
158
137
|
}
|
|
159
138
|
|