@edraj/tsdmart 5.0.4 → 5.2.0

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/dmart.service.ts CHANGED
@@ -1,796 +1,676 @@
1
- import {AxiosInstance} from "axios";
2
- import {
3
- ActionRequest,
4
- ActionRequestRecord,
5
- ActionResponse,
6
- ApiQueryResponse,
7
- ApiResponse,
8
- ConfirmOTPRequest, DmartScope,
9
- FetchDataAssetRequest,
10
- GetAttachmentURLRequest,
11
- GetChildrenRequest,
12
- GetPayloadRequest,
13
- headers,
14
- LoginResponse,
15
- PasswordResetRequest,
16
- ProfileResponse, ProgressTicketRequest,
17
- QueryRequest,
18
- QueryType,
19
- ResourcesFromCSVRequest,
20
- ResponseEntry,
21
- RetrieveEntryRequest,
22
- SendOTPRequest,
23
- SortyType,
24
- Status, SubmitRequest,
25
- UploadWithPayloadRequest,
26
- } from "./dmart.model";
27
-
28
-
29
- export class Dmart {
30
- static axiosDmartInstance: AxiosInstance;
31
-
32
- /**
33
- * Sets the Axios instance to be used for all HTTP requests
34
- * @param axiosInstance - The Axios instance to use for API calls
35
- */
36
- static setAxiosInstance(axiosInstance: AxiosInstance) {
37
- Dmart.axiosDmartInstance = axiosInstance;
38
- }
39
-
40
- /**
41
- * Gets the current Axios instance
42
- * @returns The configured Axios instance
43
- * @throws Error if no Axios instance has been set
44
- */
45
- public static getAxiosInstance(): AxiosInstance {
46
- if (!Dmart.axiosDmartInstance) {
47
- throw new Error("Axios instance is not set. Please set it using setAxiosInstance method.");
48
- }
49
- return Dmart.axiosDmartInstance;
50
- }
51
-
52
- /**
53
- * Gets the current headers object used for API requests
54
- * @returns The headers object containing request headers
55
- */
56
- public static getHeaders() {
57
- return headers;
58
- }
59
-
60
- /**
61
- * Updates the headers object with new headers
62
- * @param newHeaders - Object containing headers to merge with existing headers
63
- */
64
- public static setHeaders(newHeaders: any) {
65
- Object.assign(headers, newHeaders);
66
- }
67
-
68
- /**
69
- * Gets the base URL from the Axios instance
70
- * @returns The base URL string
71
- */
72
- public static getBaseURL() {
73
- return Dmart.axiosDmartInstance.defaults.baseURL;
74
- }
75
-
76
- /**
77
- * Sets the base URL for the Axios instance
78
- * @param url - The base URL to set
79
- */
80
- public static set setBaseURL(url: string) {
81
- Dmart.axiosDmartInstance.defaults.baseURL = url;
82
- }
83
-
84
- /**
85
- * Gets the current authentication token
86
- * @returns The token string without the "Bearer " prefix, or null if not set
87
- */
88
- public static getToken() {
89
- return headers["Authorization"] ? headers["Authorization"].replace("Bearer ", "") : null;
90
- }
91
-
92
- /**
93
- * Sets the authentication token in the headers
94
- * @param token - The JWT token to use for authentication
95
- */
96
- public static setToken(token: string) {
97
- headers["Authorization"] = `Bearer ${token}`;
98
- }
99
-
100
- /**
101
- * Authenticates a user with shortname and password
102
- * @param shortname - The user's shortname (username)
103
- * @param password - The user's password
104
- * @returns Promise resolving to LoginResponse containing authentication data and access token
105
- */
106
- public static async login(shortname: string, password: string) {
107
- const response = await Dmart.axiosDmartInstance.post<LoginResponse>(
108
- 'user/login',
109
- {shortname, password},
110
- {headers}
111
- );
112
- const data: LoginResponse = response.data;
113
- if (data.status == Status.success && data.records.length > 0) {
114
- headers["Authorization"] = "Bearer " + data.records[0]?.attributes.access_token;
115
- }
116
- return data;
117
- }
118
-
119
- /**
120
- * Authenticates a user with custom credentials and password
121
- * @param credentials - Object containing login credentials (e.g., email, phone, etc.)
122
- * @param password - The user's password
123
- * @returns Promise resolving to LoginResponse containing authentication data and access token
124
- * @throws Error if login request fails
125
- */
126
- public static async loginBy(credentials: any, password: string) {
127
- try {
128
- const response = await Dmart.axiosDmartInstance.post<LoginResponse>(
129
- 'user/login',
130
- {...credentials, password},
131
- {headers}
132
- );
133
- const data: LoginResponse = response.data;
134
- if (data.status == Status.success && data.records.length > 0) {
135
- headers["Authorization"] =
136
- "Bearer " + data.records[0]?.attributes.access_token;
137
- }
138
- return data;
139
- } catch (error: any) {
140
- throw error;
141
- }
142
- }
143
-
144
- /**
145
- * Logs out the current user session
146
- * @returns Promise resolving to ApiResponse indicating logout status
147
- * @throws Error if logout request fails
148
- */
149
- public static async logout() {
150
- try {
151
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
152
- 'user/logout',
153
- {},
154
- {headers}
155
- );
156
- return data;
157
- } catch (error: any) {
158
- throw error;
159
- }
160
- }
161
-
162
- /**
163
- * Creates a new user account
164
- * @param request - ActionRequestRecord containing user creation data
165
- * @returns Promise resolving to ActionResponse with creation result
166
- * @throws Error if user creation fails
167
- */
168
- public static async createUser(request: ActionRequestRecord) {
169
- try {
170
- const {data} = await Dmart.axiosDmartInstance.post<ActionResponse>(
171
- 'user/create',
172
- request,
173
- {headers}
174
- );
175
- return data;
176
- } catch (error: any) {
177
- throw error;
178
- }
179
- }
180
-
181
- /**
182
- * Updates an existing user's profile information
183
- * @param request - ActionRequestRecord containing user update data
184
- * @returns Promise resolving to ActionResponse with update result
185
- * @throws Error if user update fails
186
- */
187
- public static async updateUser(request: ActionRequestRecord) {
188
- try {
189
- const {data} = await Dmart.axiosDmartInstance.post<ActionResponse>(
190
- 'user/profile',
191
- request,
192
- {headers}
193
- );
194
- return data;
195
- } catch (error: any) {
196
- throw error;
197
- }
198
- }
199
-
200
- /**
201
- * Checks if a user property value already exists in the system
202
- * @param prop - The property name to check (e.g., 'email', 'shortname')
203
- * @param value - The value to check for existence
204
- * @returns Promise resolving to ResponseEntry indicating if the value exists
205
- * @throws Error if check request fails
206
- */
207
- public static async checkExisting(prop: string, value: string) {
208
- try {
209
- const {data} = await Dmart.axiosDmartInstance.get<ResponseEntry>(
210
- `user/check-existing?${prop}=${value}`,
211
- {headers}
212
- );
213
- return data;
214
- } catch (error: any) {
215
- throw error;
216
- }
217
- }
218
-
219
- /**
220
- * Retrieves the current user's profile information
221
- * @returns Promise resolving to ProfileResponse containing user profile data, permissions, and roles
222
- * @throws Error if profile retrieval fails
223
- */
224
- public static async getProfile() {
225
- try {
226
- const {data} = await Dmart.axiosDmartInstance.get<ProfileResponse>('user/profile', {
227
- headers,
228
- });
229
- if (typeof localStorage !== "undefined" && data.status === "success") {
230
- localStorage.setItem(
231
- "permissions",
232
- JSON.stringify((data?.records ?? [{}])[0]?.attributes.permissions)
233
- );
234
- localStorage.setItem(
235
- "roles",
236
- JSON.stringify((data?.records ?? [{}])[0]?.attributes?.["roles"])
237
- );
238
- }
239
- return data;
240
- } catch (error: any) {
241
- throw error;
242
- }
243
- }
244
-
245
- /**
246
- * Executes a query against the Dmart API to retrieve data
247
- * @param query - QueryRequest object containing query parameters, filters, and sorting options
248
- * @param scope - The scope for the query (default: DmartScope.managed)
249
- * @returns Promise resolving to ApiQueryResponse with query results or null
250
- * @throws Error if query execution fails
251
- */
252
- public static async query(
253
- query: QueryRequest,
254
- scope: string = DmartScope.managed
255
- ): Promise<ApiQueryResponse | null> {
256
- try {
257
- if (query.type != QueryType.spaces) {
258
- query.sort_type = query.sort_type || SortyType.ascending;
259
- query.sort_by = query.sort_by || "created_at";
260
- }
261
- query.subpath = query.subpath.replace(/\/+/g, "/");
262
- const {data} = await Dmart.axiosDmartInstance.post<ApiQueryResponse>(
263
- `${scope}/query`,
264
- query,
265
- {headers}
266
- );
267
- return data;
268
- } catch (error: any) {
269
- throw error;
270
- }
271
- }
272
-
273
- /**
274
- * Exports query results as CSV format
275
- * @param query - Query object with parameters for CSV export
276
- * @returns Promise resolving to ApiQueryResponse containing CSV data
277
- * @throws Error if CSV generation fails
278
- */
279
- public static async csv(query: any): Promise<ApiQueryResponse> {
280
- try {
281
- query.sort_type = query.sort_type || SortyType.ascending;
282
- query.sort_by = "created_at";
283
- query.subpath = query.subpath.replace(/\/+/g, "/");
284
- const {data} = await Dmart.axiosDmartInstance.post<ApiQueryResponse>(
285
- 'managed/csv',
286
- query,
287
- {headers}
288
- );
289
- return data;
290
- } catch (error: any) {
291
- throw error;
292
- }
293
- }
294
-
295
- /**
296
- * Creates resources from an uploaded CSV file
297
- * @param request - ResourcesFromCSVRequest containing file data and resource configuration
298
- * @returns Promise resolving to ApiResponse with creation results, or error object if failed
299
- */
300
- public static async resourcesFromCsv(
301
- request: ResourcesFromCSVRequest
302
- ) {
303
- try {
304
- let csvUrl = `/managed/resources_from_csv/${request.resourceType}/${request.space_name}/${request.subpath}`;
305
- const params: any = {};
306
- if (request.schema) {
307
- csvUrl += `/${request.schema}`;
308
- }
309
-
310
- let formdata = new FormData();
311
- formdata.append("resources_file", request.payload);
312
-
313
- if(request.isUpdate){
314
- params['is_update'] = request.isUpdate;
315
- }
316
-
317
- const headers = {"Content-Type": "multipart/form-data"};
318
-
319
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
320
- csvUrl,
321
- formdata,
322
- {headers, params}
323
- );
324
-
325
- return data;
326
- } catch (error) {
327
- return error;
328
- }
329
- }
330
-
331
- // /** LEGACY, DEPRECATED CODE, DO NOT USE
332
- // * Performs space-level operations (create, update, delete spaces)
333
- // * @param action - ActionRequest containing the operation details for space management
334
- // * @returns Promise resolving to ActionResponse with operation result
335
- // * @throws Error if space operation fails
336
- // */
337
- // public static async space(action: ActionRequest): Promise<ActionResponse> {
338
- // try {
339
- // const {data} = await Dmart.axiosDmartInstance.post<ActionResponse>(
340
- // 'managed/space',
341
- // action,
342
- // {headers}
343
- // );
344
- // return data;
345
- // } catch (error: any) {
346
- // throw error;
347
- // }
348
- // }
349
-
350
- /**
351
- * Executes a general request action against the Dmart API
352
- * @param action - ActionRequest containing the request details and parameters
353
- * @returns Promise resolving to ActionResponse with request result
354
- */
355
- public static async request(action: ActionRequest): Promise<ActionResponse> {
356
- const res = await Dmart.axiosDmartInstance.post<ActionResponse>('managed/request', action, {
357
- headers,
358
- });
359
- return res?.data;
360
- }
361
-
362
- /**
363
- * Retrieves a specific entry from the Dmart system
364
- * @param request - RetrieveEntryRequest containing entry identification and retrieval options
365
- * @param scope - The scope for the retrieval (default: DmartScope.managed)
366
- * @returns Promise resolving to ResponseEntry with entry data or null if not found
367
- * @throws Error if entry retrieval fails
368
- */
369
- public static async retrieveEntry(
370
- request: RetrieveEntryRequest,
371
- scope: string = DmartScope.managed
372
- ): Promise<ResponseEntry | null> {
373
- try {
374
- if (request.validate_schema === null) {
375
- request.validate_schema = true;
376
- }
377
- if (!request.subpath || request.subpath == "/") request.subpath = "__root__";
378
- const url = `${scope}/entry/${request.resource_type}/${request.space_name}/${request.subpath}/${request.shortname}?retrieve_json_payload=${request.retrieve_json_payload}&retrieve_attachments=${request.retrieve_attachments}&validate_schema=${request.validate_schema}`;
379
- const {data} = await Dmart.axiosDmartInstance.get<ResponseEntry>(
380
- `${url.replace(/\/+/g, "/")}`,
381
- {headers}
382
- );
383
- return data;
384
- } catch (error: any) {
385
- throw error;
386
- }
387
- }
388
-
389
-
390
- /**
391
- * Uploads a resource with an attached payload file
392
- * @param request - UploadWithPayloadRequest containing resource data and payload file
393
- * @param scope - The scope for the upload operation (default: DmartScope.managed)
394
- * @returns Promise resolving to ApiResponse with upload result
395
- * @throws Error if upload fails
396
- */
397
- public static async uploadWithPayload(
398
- request: UploadWithPayloadRequest,
399
- scope: string = DmartScope.managed
400
- ) {
401
- const request_record_body: any = {
402
- resource_type: request.resource_type,
403
- subpath: request.subpath,
404
- shortname: request.shortname,
405
- attributes: request.attributes,
406
- };
407
-
408
- if (request.attributes !== null && Object.keys(request.attributes!).length === 0) {
409
- request_record_body.attributes = {is_active: true, payload: {body: {}}};
410
- } else {
411
- if (!Object.keys(request.attributes!).includes('is_active')) {
412
- request_record_body.attributes.is_active = true;
413
- }
414
- }
415
-
416
- const request_record = new Blob([JSON.stringify(request_record_body)], {
417
- type: "application/json",
418
- });
419
-
420
- const form_data = new FormData();
421
- form_data.append("space_name", request.space_name);
422
- form_data.append("request_record", request_record);
423
- form_data.append("payload_file", request.payload_file);
424
-
425
- const _headers = {...headers, "Content-Type": "multipart/form-data"};
426
-
427
- try {
428
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
429
- `${scope}/resource_with_payload`,
430
- form_data,
431
- {headers: _headers}
432
- );
433
-
434
- return data;
435
- } catch (error: any) {
436
- throw error;
437
- }
438
- }
439
-
440
- /**
441
- * Fetches data assets from the Dmart system with optional SQL query filtering
442
- * @param request - FetchDataAssetRequest containing asset identification and query parameters
443
- * @returns Promise resolving to data asset response
444
- * @throws Error if data asset fetch fails
445
- */
446
- public static async fetchDataAsset(
447
- request: FetchDataAssetRequest,
448
- ) {
449
- try {
450
- const {data} = await Dmart.axiosDmartInstance.post(
451
- 'managed/data-asset',
452
- {
453
- space_name: request.spaceName,
454
- resource_type: request.resourceType,
455
- data_asset_type: request.dataAssetType,
456
- subpath: request.subpath,
457
- shortname: request.shortname,
458
- query_string: request.query_string ?? "SELECT * FROM file",
459
- filter_data_assets: request.filter_data_assets,
460
- },
461
- {headers}
462
- );
463
- return data;
464
- } catch (error: any) {
465
- throw error;
466
- }
467
- }
468
-
469
- /**
470
- * Retrieves a list of all available spaces in the system
471
- * @returns Promise resolving to ApiResponse containing spaces data or null
472
- */
473
- public static async getSpaces(): Promise<ApiResponse | null> {
474
- return await this.query({
475
- type: QueryType.spaces,
476
- space_name: "management",
477
- subpath: "/",
478
- search: "",
479
- limit: 100,
480
- });
481
- }
482
-
483
- /**
484
- * Gets child resources within a specified space and subpath
485
- * @param request - GetChildrenRequest containing search parameters and filters
486
- * @returns Promise resolving to ApiResponse with child resources or null
487
- */
488
- public static async getChildren(
489
- request: GetChildrenRequest
490
- ): Promise<ApiResponse | null> {
491
- if(request.limit === null){
492
- request.limit = 20
493
- }
494
- if(request.offset === null){
495
- request.offset = 0
496
- }
497
- if(request.restrict_types === null){
498
- request.restrict_types = []
499
- }
500
- return await this.query({
501
- type: QueryType.search,
502
- space_name: request.space_name,
503
- subpath: request.subpath,
504
- filter_types: request.restrict_types,
505
- exact_subpath: true,
506
- search: request.search,
507
- limit: request.limit,
508
- offset: request.offset,
509
- });
510
- }
511
-
512
- /**
513
- * Generates a URL for accessing attachment resources
514
- * @param request - GetAttachmentURLRequest containing attachment identification parameters
515
- * @param scope - The scope for the attachment URL (default: DmartScope.managed)
516
- * @returns String URL for accessing the attachment
517
- */
518
- public static getAttachmentUrl(
519
- request: GetAttachmentURLRequest,
520
- scope: string = DmartScope.managed
521
- ) {
522
- const subpath = request.subpath.replace(
523
- /\/+$/,
524
- ""
525
- );
526
- return `${Dmart.axiosDmartInstance.defaults.baseURL}`+`/${scope}/payload/${request.resource_type}/${request.space_name}/${subpath}/${request.parent_shortname}/${request.shortname}${request.ext === null ? "" : `.${request.ext}`}`.replaceAll('//', '/');
527
- }
528
-
529
- /**
530
- * Retrieves health information and statistics for a specific space
531
- * @param space_name - The name of the space to check health for
532
- * @returns Promise resolving to ApiQueryResponse with health data and folders report
533
- * @throws Error if health check fails
534
- */
535
- public static async getSpaceHealth(space_name: string) {
536
- try {
537
- const {data} = await Dmart.axiosDmartInstance.get<
538
- ApiQueryResponse & { attributes: { folders_report: Object } }
539
- >(`managed/health/${space_name}`, {headers});
540
- return data;
541
- } catch (error: any) {
542
- throw error;
543
- }
544
- }
545
-
546
- /**
547
- * Retrieves payload data for a specific resource
548
- * @param request - GetPayloadRequest containing payload identification parameters
549
- * @param scope - The scope for the payload retrieval (default: DmartScope.managed)
550
- * @returns Promise resolving to payload data
551
- * @throws Error if payload retrieval fails
552
- */
553
- public static async getPayload(
554
- request: GetPayloadRequest,
555
- scope: string = DmartScope.managed
556
- ) {
557
- try {
558
- let url = `${scope}/payload/${request.resource_type}/${request.space_name}/${request.subpath}/${request.shortname}`;
559
-
560
- if (request.schemaShortname) {
561
- url += `.${request.schemaShortname}`
562
- }
563
- url += `.${request.ext}`
564
- const {data} = await Dmart.axiosDmartInstance.get<any>(
565
- url,
566
- {headers}
567
- );
568
- return data;
569
- } catch (error: any) {
570
- throw error;
571
- }
572
- }
573
-
574
- /**
575
- * Updates the progress of a ticket with resolution and comments
576
- * @param request - ProgressTicketRequest containing ticket identification and update data
577
- * @returns Promise resolving to ApiQueryResponse with progress update result
578
- * @throws Error if ticket progress update fails
579
- */
580
- public static async progressTicket(
581
- request: ProgressTicketRequest
582
- ) {
583
- try {
584
- const payload: any = {};
585
- if (request.resolution) {
586
- payload.resolution = request.resolution;
587
- }
588
- if (request.comment) {
589
- payload.comment = request.comment;
590
- }
591
- const {data} = await Dmart.axiosDmartInstance.put<
592
- ApiQueryResponse & { attributes: { folders_report: Object } }
593
- >(
594
- `managed/progress-ticket/${request.space_name}/${request.subpath}/${request.shortname}/${request.action}`,
595
- payload,
596
- {headers}
597
- );
598
- return data;
599
- } catch (error: any) {
600
- throw error;
601
- }
602
- }
603
-
604
- /**
605
- * Submits data to a public endpoint with optional workflow processing
606
- * @param request - SubmitRequest containing submission data and routing information
607
- * @returns Promise resolving to submission response data
608
- * @throws Error if submission fails
609
- */
610
- public static async submit(
611
- request: SubmitRequest
612
- ) {
613
- try {
614
- var url = `public/submit/${request.spaceName}`;
615
- if (request.resourceType) {
616
- url += `/${request.resourceType}`;
617
- }
618
- if (request.workflowShortname) {
619
- url += `/${request.workflowShortname}`;
620
- }
621
- url += `/${request.schemaShortname}/${request.subpath}`;
622
- const {data} = await Dmart.axiosDmartInstance.post(
623
- url,
624
- request.record,
625
- {headers}
626
- );
627
- return data;
628
- } catch (error: any) {
629
- throw error;
630
- }
631
- }
632
-
633
- /**
634
- * Retrieves the system manifest containing configuration and metadata
635
- * @returns Promise resolving to manifest data
636
- * @throws Error if manifest retrieval fails
637
- */
638
- public static async getManifest() {
639
- try {
640
- const {data} = await Dmart.axiosDmartInstance.get<any>('info/manifest', {
641
- headers,
642
- });
643
- return data;
644
- } catch (error: any) {
645
- throw error;
646
- }
647
- }
648
-
649
- /**
650
- * Retrieves the system settings and configuration
651
- * @returns Promise resolving to settings data
652
- * @throws Error if settings retrieval fails
653
- */
654
- public static async getSettings() {
655
- try {
656
- const {data} = await Dmart.axiosDmartInstance.get<any>('info/settings', {
657
- headers,
658
- });
659
- return data;
660
- } catch (error: any) {
661
- throw error;
662
- }
663
- }
664
-
665
- /**
666
- * Sends an OTP (One-Time Password) request to a user
667
- * @param request - SendOTPRequest containing recipient information
668
- * @param acceptLanguage - Optional language preference for the OTP message (default: null)
669
- * @returns Promise resolving to ApiResponse with OTP request result
670
- * @throws Error if OTP request fails
671
- */
672
- public static async otpRequest(
673
- request: SendOTPRequest,
674
- acceptLanguage: string | null = null
675
- ) {
676
- try {
677
- const requestHeaders = {...headers};
678
- if (acceptLanguage) {
679
- requestHeaders['Accept-Language'] = acceptLanguage;
680
- }
681
-
682
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
683
- 'user/otp-request',
684
- request,
685
- {headers: requestHeaders}
686
- );
687
- return data;
688
- } catch (error: any) {
689
- throw error;
690
- }
691
- }
692
-
693
- /**
694
- * Sends an OTP (One-Time Password) request for login purposes
695
- * @param request - SendOTPRequest containing recipient information for login OTP
696
- * @param acceptLanguage - Optional language preference for the OTP message (default: null)
697
- * @returns Promise resolving to ApiResponse with OTP login request result
698
- * @throws Error if OTP login request fails
699
- */
700
- public static async otpRequestLogin(
701
- request: SendOTPRequest,
702
- acceptLanguage: string | null = null
703
- ) {
704
- try {
705
- const requestHeaders = {...headers};
706
- if (acceptLanguage) {
707
- requestHeaders['Accept-Language'] = acceptLanguage;
708
- }
709
-
710
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
711
- 'user/otp-request-login',
712
- request,
713
- {headers: requestHeaders}
714
- );
715
- return data;
716
- } catch (error: any) {
717
- throw error;
718
- }
719
- }
720
-
721
- /**
722
- * Initiates a password reset request for a user
723
- * @param request - PasswordResetRequest containing user identification for password reset
724
- * @returns Promise resolving to ApiResponse with password reset request result
725
- * @throws Error if password reset request fails
726
- */
727
- public static async passwordResetRequest(request: PasswordResetRequest) {
728
- try {
729
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
730
- 'user/password-reset-request',
731
- request,
732
- {headers}
733
- );
734
- return data;
735
- } catch (error: any) {
736
- throw error;
737
- }
738
- }
739
-
740
- /**
741
- * Confirms an OTP (One-Time Password) code provided by the user
742
- * @param request - ConfirmOTPRequest containing the OTP code and verification details
743
- * @returns Promise resolving to ApiResponse with OTP confirmation result
744
- * @throws Error if OTP confirmation fails
745
- */
746
- public static async confirmOtp(request: ConfirmOTPRequest) {
747
- try {
748
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
749
- 'user/otp-confirm',
750
- request,
751
- {headers}
752
- );
753
- return data;
754
- } catch (error: any) {
755
- throw error;
756
- }
757
- }
758
-
759
- /**
760
- * Resets a user's account or session state
761
- * @param shortname - The shortname (username) of the user to reset
762
- * @returns Promise resolving to ApiResponse with user reset result
763
- * @throws Error if user reset fails
764
- */
765
- public static async userReset(shortname: string) {
766
- try {
767
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
768
- 'user/reset',
769
- {shortname},
770
- {headers}
771
- );
772
- return data;
773
- } catch (error: any) {
774
- throw error;
775
- }
776
- }
777
-
778
- /**
779
- * Validates a password against system password policies and requirements
780
- * @param password - The password string to validate
781
- * @returns Promise resolving to ApiResponse with validation result
782
- * @throws Error if password validation fails
783
- */
784
- public static async validatePassword(password: string) {
785
- try {
786
- const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
787
- 'user/validate_password',
788
- {password},
789
- {headers}
790
- );
791
- return data;
792
- } catch (error: any) {
793
- throw error;
794
- }
795
- }
796
- }
1
+ import {AxiosInstance} from "axios";
2
+ import {
3
+ ActionRequest,
4
+ ActionRequestRecord,
5
+ ActionResponse,
6
+ ApiQueryResponse,
7
+ ApiResponse,
8
+ ConfirmOTPRequest,
9
+ DmartScope,
10
+ FetchDataAssetRequest,
11
+ GetAttachmentURLRequest,
12
+ GetChildrenRequest,
13
+ GetPayloadRequest,
14
+ headers,
15
+ LoginResponse,
16
+ PasswordResetRequest,
17
+ ProfileResponse,
18
+ ProgressTicketRequest,
19
+ QueryRequest,
20
+ QueryType,
21
+ ResourcesFromCSVRequest,
22
+ ResponseEntry,
23
+ RetrieveEntryRequest,
24
+ SendOTPRequest,
25
+ SortType,
26
+ Status,
27
+ SubmitRequest,
28
+ UploadWithPayloadRequest,
29
+ } from "./dmart.model";
30
+
31
+
32
+ export class Dmart {
33
+ static axiosDmartInstance: AxiosInstance;
34
+
35
+ /**
36
+ * Sets the Axios instance to be used for all HTTP requests
37
+ * @param axiosInstance - The Axios instance to use for API calls
38
+ */
39
+ static setAxiosInstance(axiosInstance: AxiosInstance) {
40
+ Dmart.axiosDmartInstance = axiosInstance;
41
+ }
42
+
43
+ /**
44
+ * Gets the current Axios instance
45
+ * @returns The configured Axios instance
46
+ * @throws Error if no Axios instance has been set
47
+ */
48
+ public static getAxiosInstance(): AxiosInstance {
49
+ if (!Dmart.axiosDmartInstance) {
50
+ throw new Error("Axios instance is not set. Please set it using setAxiosInstance method.");
51
+ }
52
+ return Dmart.axiosDmartInstance;
53
+ }
54
+
55
+ /**
56
+ * Gets the current headers object used for API requests
57
+ * @returns The headers object containing request headers
58
+ */
59
+ public static getHeaders() {
60
+ return headers;
61
+ }
62
+
63
+ /**
64
+ * Updates the headers object with new headers
65
+ * @param newHeaders - Object containing headers to merge with existing headers
66
+ */
67
+ public static setHeaders(newHeaders: Record<string, string>) {
68
+ Object.assign(headers, newHeaders);
69
+ }
70
+
71
+ /**
72
+ * Gets the base URL from the Axios instance
73
+ * @returns The base URL string
74
+ * @throws Error if no Axios instance has been set
75
+ */
76
+ public static getBaseURL() {
77
+ return Dmart.getAxiosInstance().defaults.baseURL;
78
+ }
79
+
80
+ /**
81
+ * Sets the base URL for the Axios instance
82
+ * @param url - The base URL to set
83
+ */
84
+ public static setBaseURL(url: string) {
85
+ Dmart.getAxiosInstance().defaults.baseURL = url;
86
+ }
87
+
88
+ /**
89
+ * Gets the current authentication token
90
+ * @returns The token string without the "Bearer " prefix, or null if not set
91
+ */
92
+ public static getToken() {
93
+ return headers["Authorization"] ? headers["Authorization"].replace("Bearer ", "") : null;
94
+ }
95
+
96
+ /**
97
+ * Sets the authentication token in the headers
98
+ * @param token - The JWT token to use for authentication
99
+ */
100
+ public static setToken(token: string) {
101
+ headers["Authorization"] = `Bearer ${token}`;
102
+ }
103
+
104
+ /**
105
+ * Authenticates a user with shortname and password
106
+ * @param shortname - The user's shortname (username)
107
+ * @param password - The user's password
108
+ * @returns Promise resolving to LoginResponse containing authentication data and access token
109
+ */
110
+ public static async login(shortname: string, password: string) {
111
+ const response = await Dmart.axiosDmartInstance.post<LoginResponse>(
112
+ 'user/login',
113
+ {shortname, password},
114
+ {headers}
115
+ );
116
+ const data: LoginResponse = response.data;
117
+ if (data.status === Status.success && data.records.length > 0) {
118
+ headers["Authorization"] = "Bearer " + data.records[0]?.attributes.access_token;
119
+ }
120
+ return data;
121
+ }
122
+
123
+ /**
124
+ * Authenticates a user with custom credentials and password
125
+ * @param credentials - Object containing login credentials (e.g., email, phone, etc.)
126
+ * @param password - The user's password
127
+ * @returns Promise resolving to LoginResponse containing authentication data and access token
128
+ */
129
+ public static async loginBy(credentials: Record<string, string>, password: string) {
130
+ const response = await Dmart.axiosDmartInstance.post<LoginResponse>(
131
+ 'user/login',
132
+ {...credentials, password},
133
+ {headers}
134
+ );
135
+ const data: LoginResponse = response.data;
136
+ if (data.status === Status.success && data.records.length > 0) {
137
+ headers["Authorization"] =
138
+ "Bearer " + data.records[0]?.attributes.access_token;
139
+ }
140
+ return data;
141
+ }
142
+
143
+ /**
144
+ * Logs out the current user session
145
+ * @returns Promise resolving to ApiResponse indicating logout status
146
+ */
147
+ public static async logout() {
148
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
149
+ 'user/logout',
150
+ {},
151
+ {headers}
152
+ );
153
+ return data;
154
+ }
155
+
156
+ /**
157
+ * Creates a new user account
158
+ * @param request - ActionRequestRecord containing user creation data
159
+ * @returns Promise resolving to ActionResponse with creation result
160
+ */
161
+ public static async createUser(request: ActionRequestRecord) {
162
+ const {data} = await Dmart.axiosDmartInstance.post<ActionResponse>(
163
+ 'user/create',
164
+ request,
165
+ {headers}
166
+ );
167
+ return data;
168
+ }
169
+
170
+ /**
171
+ * Updates an existing user's profile information
172
+ * @param request - ActionRequestRecord containing user update data
173
+ * @returns Promise resolving to ActionResponse with update result
174
+ */
175
+ public static async updateUser(request: ActionRequestRecord) {
176
+ const {data} = await Dmart.axiosDmartInstance.post<ActionResponse>(
177
+ 'user/profile',
178
+ request,
179
+ {headers}
180
+ );
181
+ return data;
182
+ }
183
+
184
+ /**
185
+ * Checks if a user property value already exists in the system
186
+ * @param prop - The property name to check (e.g., 'email', 'shortname')
187
+ * @param value - The value to check for existence
188
+ * @returns Promise resolving to ResponseEntry indicating if the value exists
189
+ */
190
+ public static async checkExisting(prop: string, value: string) {
191
+ const {data} = await Dmart.axiosDmartInstance.get<ResponseEntry>(
192
+ `user/check-existing?${prop}=${value}`,
193
+ {headers}
194
+ );
195
+ return data;
196
+ }
197
+
198
+ /**
199
+ * Retrieves the current user's profile information
200
+ * @returns Promise resolving to ProfileResponse containing user profile data, permissions, and roles
201
+ */
202
+ public static async getProfile() {
203
+ const {data} = await Dmart.axiosDmartInstance.get<ProfileResponse>('user/profile', {
204
+ headers,
205
+ });
206
+ if (typeof localStorage !== "undefined" && data.status === "success") {
207
+ localStorage.setItem(
208
+ "permissions",
209
+ JSON.stringify((data?.records ?? [{}])[0]?.attributes.permissions)
210
+ );
211
+ localStorage.setItem(
212
+ "roles",
213
+ JSON.stringify((data?.records ?? [{}])[0]?.attributes?.["roles"])
214
+ );
215
+ }
216
+ return data;
217
+ }
218
+
219
+ /**
220
+ * Executes a query against the Dmart API to retrieve data
221
+ * @param query - QueryRequest object containing query parameters, filters, and sorting options
222
+ * @param scope - The scope for the query (default: DmartScope.managed)
223
+ * @returns Promise resolving to ApiQueryResponse with query results or null
224
+ */
225
+ public static async query(
226
+ query: QueryRequest,
227
+ scope: DmartScope = DmartScope.managed
228
+ ): Promise<ApiQueryResponse | null> {
229
+ if (query.type !== QueryType.spaces) {
230
+ query.sort_type = query.sort_type || SortType.ascending;
231
+ query.sort_by = query.sort_by || "created_at";
232
+ }
233
+ query.subpath = query.subpath.replace(/\/+/g, "/");
234
+ const {data} = await Dmart.axiosDmartInstance.post<ApiQueryResponse>(
235
+ `${scope}/query`,
236
+ query,
237
+ {headers}
238
+ );
239
+ return data;
240
+ }
241
+
242
+ /**
243
+ * Exports query results as CSV format
244
+ * @param query - Query object with parameters for CSV export
245
+ * @returns Promise resolving to ApiQueryResponse containing CSV data
246
+ */
247
+ public static async csv(query: QueryRequest): Promise<ApiQueryResponse> {
248
+ query.sort_type = query.sort_type || SortType.ascending;
249
+ query.sort_by = "created_at";
250
+ query.subpath = query.subpath.replace(/\/+/g, "/");
251
+ const {data} = await Dmart.axiosDmartInstance.post<ApiQueryResponse>(
252
+ 'managed/csv',
253
+ query,
254
+ {headers}
255
+ );
256
+ return data;
257
+ }
258
+
259
+ /**
260
+ * Creates resources from an uploaded CSV file
261
+ * @param request - ResourcesFromCSVRequest containing file data and resource configuration
262
+ * @returns Promise resolving to ApiResponse with creation results
263
+ */
264
+ public static async resourcesFromCsv(
265
+ request: ResourcesFromCSVRequest
266
+ ) {
267
+ let csvUrl = `/managed/resources_from_csv/${request.resourceType}/${request.space_name}/${request.subpath}`;
268
+ const params: Record<string, boolean> = {};
269
+ if (request.schema) {
270
+ csvUrl += `/${request.schema}`;
271
+ }
272
+
273
+ const formdata = new FormData();
274
+ formdata.append("resources_file", request.payload);
275
+
276
+ if (request.isUpdate) {
277
+ params['is_update'] = request.isUpdate;
278
+ }
279
+
280
+ const multipartHeaders = {...headers, "Content-Type": "multipart/form-data"};
281
+
282
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
283
+ csvUrl,
284
+ formdata,
285
+ {headers: multipartHeaders, params}
286
+ );
287
+
288
+ return data;
289
+ }
290
+
291
+ // /** LEGACY, DEPRECATED CODE, DO NOT USE
292
+ // * Performs space-level operations (create, update, delete spaces)
293
+ // * @param action - ActionRequest containing the operation details for space management
294
+ // * @returns Promise resolving to ActionResponse with operation result
295
+ // */
296
+ // public static async space(action: ActionRequest): Promise<ActionResponse> {
297
+ // const {data} = await Dmart.axiosDmartInstance.post<ActionResponse>(
298
+ // 'managed/space',
299
+ // action,
300
+ // {headers}
301
+ // );
302
+ // return data;
303
+ // }
304
+
305
+ /**
306
+ * Executes a general request action against the Dmart API
307
+ * @param action - ActionRequest containing the request details and parameters
308
+ * @returns Promise resolving to ActionResponse with request result
309
+ */
310
+ public static async request(action: ActionRequest): Promise<ActionResponse> {
311
+ const res = await Dmart.axiosDmartInstance.post<ActionResponse>('managed/request', action, {
312
+ headers,
313
+ });
314
+ return res?.data;
315
+ }
316
+
317
+ /**
318
+ * Retrieves a specific entry from the Dmart system
319
+ * @param request - RetrieveEntryRequest containing entry identification and retrieval options
320
+ * @param scope - The scope for the retrieval (default: DmartScope.managed)
321
+ * @returns Promise resolving to ResponseEntry with entry data or null if not found
322
+ */
323
+ public static async retrieveEntry(
324
+ request: RetrieveEntryRequest,
325
+ scope: DmartScope = DmartScope.managed
326
+ ): Promise<ResponseEntry | null> {
327
+ if (request.validate_schema === null) {
328
+ request.validate_schema = true;
329
+ }
330
+ if (!request.subpath || request.subpath === "/") request.subpath = "__root__";
331
+ const url = `${scope}/entry/${request.resource_type}/${request.space_name}/${request.subpath}/${request.shortname}?retrieve_json_payload=${request.retrieve_json_payload}&retrieve_attachments=${request.retrieve_attachments}&validate_schema=${request.validate_schema}`;
332
+ const {data} = await Dmart.axiosDmartInstance.get<ResponseEntry>(
333
+ `${url.replace(/\/+/g, "/")}`,
334
+ {headers}
335
+ );
336
+ return data;
337
+ }
338
+
339
+
340
+ /**
341
+ * Uploads a resource with an attached payload file
342
+ * @param request - UploadWithPayloadRequest containing resource data and payload file
343
+ * @param scope - The scope for the upload operation (default: DmartScope.managed)
344
+ * @returns Promise resolving to ApiResponse with upload result
345
+ */
346
+ public static async uploadWithPayload(
347
+ request: UploadWithPayloadRequest,
348
+ scope: DmartScope = DmartScope.managed
349
+ ) {
350
+ const request_record_body: Record<string, any> = {
351
+ resource_type: request.resource_type,
352
+ subpath: request.subpath,
353
+ shortname: request.shortname,
354
+ attributes: request.attributes ?? {},
355
+ };
356
+
357
+ if (!request.attributes || Object.keys(request.attributes).length === 0) {
358
+ request_record_body['attributes'] = {is_active: true, payload: {body: {}}};
359
+ } else {
360
+ if (!Object.keys(request.attributes).includes('is_active')) {
361
+ request_record_body['attributes'].is_active = true;
362
+ }
363
+ }
364
+
365
+ const request_record = new Blob([JSON.stringify(request_record_body)], {
366
+ type: "application/json",
367
+ });
368
+
369
+ const form_data = new FormData();
370
+ form_data.append("space_name", request.space_name);
371
+ form_data.append("request_record", request_record);
372
+ form_data.append("payload_file", request.payload_file);
373
+
374
+ const _headers = {...headers, "Content-Type": "multipart/form-data"};
375
+
376
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
377
+ `${scope}/resource_with_payload`,
378
+ form_data,
379
+ {headers: _headers}
380
+ );
381
+
382
+ return data;
383
+ }
384
+
385
+ /**
386
+ * Fetches data assets from the Dmart system with optional SQL query filtering
387
+ * @param request - FetchDataAssetRequest containing asset identification and query parameters
388
+ * @returns Promise resolving to data asset response
389
+ */
390
+ public static async fetchDataAsset(
391
+ request: FetchDataAssetRequest,
392
+ ) {
393
+ const {data} = await Dmart.axiosDmartInstance.post(
394
+ 'managed/data-asset',
395
+ {
396
+ space_name: request.spaceName,
397
+ resource_type: request.resourceType,
398
+ data_asset_type: request.dataAssetType,
399
+ subpath: request.subpath,
400
+ shortname: request.shortname,
401
+ query_string: request.query_string ?? "SELECT * FROM file",
402
+ filter_data_assets: request.filter_data_assets,
403
+ },
404
+ {headers}
405
+ );
406
+ return data;
407
+ }
408
+
409
+ /**
410
+ * Retrieves a list of all available spaces in the system
411
+ * @returns Promise resolving to ApiResponse containing spaces data or null
412
+ */
413
+ public static async getSpaces(): Promise<ApiResponse | null> {
414
+ return await this.query({
415
+ type: QueryType.spaces,
416
+ space_name: "management",
417
+ subpath: "/",
418
+ search: "",
419
+ limit: 100,
420
+ });
421
+ }
422
+
423
+ /**
424
+ * Gets child resources within a specified space and subpath
425
+ * @param request - GetChildrenRequest containing search parameters and filters
426
+ * @returns Promise resolving to ApiResponse with child resources or null
427
+ */
428
+ public static async getChildren(
429
+ request: GetChildrenRequest
430
+ ): Promise<ApiResponse | null> {
431
+ if (request.limit === null) {
432
+ request.limit = 20;
433
+ }
434
+ if (request.offset === null) {
435
+ request.offset = 0;
436
+ }
437
+ if (request.restrict_types === null) {
438
+ request.restrict_types = [];
439
+ }
440
+ return await this.query({
441
+ type: QueryType.search,
442
+ space_name: request.space_name,
443
+ subpath: request.subpath,
444
+ filter_types: request.restrict_types,
445
+ exact_subpath: true,
446
+ search: request.search,
447
+ limit: request.limit,
448
+ offset: request.offset,
449
+ });
450
+ }
451
+
452
+ /**
453
+ * Generates a URL for accessing attachment resources
454
+ * @param request - GetAttachmentURLRequest containing attachment identification parameters
455
+ * @param scope - The scope for the attachment URL (default: DmartScope.managed)
456
+ * @returns String URL for accessing the attachment
457
+ */
458
+ public static getAttachmentUrl(
459
+ request: GetAttachmentURLRequest,
460
+ scope: DmartScope = DmartScope.managed
461
+ ) {
462
+ const subpath = request.subpath.replace(
463
+ /\/+$/,
464
+ ""
465
+ );
466
+ return `${Dmart.getAxiosInstance().defaults.baseURL}` + `/${scope}/payload/${request.resource_type}/${request.space_name}/${subpath}/${request.parent_shortname}/${request.shortname}${request.ext === null ? "" : `.${request.ext}`}`.replaceAll('//', '/');
467
+ }
468
+
469
+ /**
470
+ * Retrieves health information and statistics for a specific space
471
+ * @param space_name - The name of the space to check health for
472
+ * @returns Promise resolving to ApiQueryResponse with health data and folders report
473
+ */
474
+ public static async getSpaceHealth(space_name: string) {
475
+ const {data} = await Dmart.axiosDmartInstance.get<
476
+ ApiQueryResponse & { attributes: { folders_report: Record<string, unknown> } }
477
+ >(`managed/health/${space_name}`, {headers});
478
+ return data;
479
+ }
480
+
481
+ /**
482
+ * Retrieves payload data for a specific resource
483
+ * @param request - GetPayloadRequest containing payload identification parameters
484
+ * @param scope - The scope for the payload retrieval (default: DmartScope.managed)
485
+ * @returns Promise resolving to payload data
486
+ */
487
+ public static async getPayload(
488
+ request: GetPayloadRequest,
489
+ scope: DmartScope = DmartScope.managed
490
+ ) {
491
+ let url = `${scope}/payload/${request.resource_type}/${request.space_name}/${request.subpath}/${request.shortname}`;
492
+
493
+ if (request.schemaShortname) {
494
+ url += `.${request.schemaShortname}`;
495
+ }
496
+ url += `.${request.ext}`;
497
+ const {data} = await Dmart.axiosDmartInstance.get<any>(
498
+ url,
499
+ {headers}
500
+ );
501
+ return data;
502
+ }
503
+
504
+ /**
505
+ * Updates the progress of a ticket with resolution and comments
506
+ * @param request - ProgressTicketRequest containing ticket identification and update data
507
+ * @returns Promise resolving to ApiQueryResponse with progress update result
508
+ */
509
+ public static async progressTicket(
510
+ request: ProgressTicketRequest
511
+ ) {
512
+ const payload: Record<string, string> = {};
513
+ if (request.resolution) {
514
+ payload['resolution'] = request.resolution;
515
+ }
516
+ if (request.comment) {
517
+ payload['comment'] = request.comment;
518
+ }
519
+ const {data} = await Dmart.axiosDmartInstance.put<
520
+ ApiQueryResponse & { attributes: { folders_report: Record<string, unknown> } }
521
+ >(
522
+ `managed/progress-ticket/${request.space_name}/${request.subpath}/${request.shortname}/${request.action}`,
523
+ payload,
524
+ {headers}
525
+ );
526
+ return data;
527
+ }
528
+
529
+ /**
530
+ * Submits data to a public endpoint with optional workflow processing
531
+ * @param request - SubmitRequest containing submission data and routing information
532
+ * @returns Promise resolving to submission response data
533
+ */
534
+ public static async submit(
535
+ request: SubmitRequest
536
+ ) {
537
+ let url = `public/submit/${request.spaceName}`;
538
+ if (request.resourceType) {
539
+ url += `/${request.resourceType}`;
540
+ }
541
+ if (request.workflowShortname) {
542
+ url += `/${request.workflowShortname}`;
543
+ }
544
+ url += `/${request.schemaShortname}/${request.subpath}`;
545
+ const {data} = await Dmart.axiosDmartInstance.post(
546
+ url,
547
+ request.record,
548
+ {headers}
549
+ );
550
+ return data;
551
+ }
552
+
553
+ /**
554
+ * Retrieves the system manifest containing configuration and metadata
555
+ * @returns Promise resolving to manifest data
556
+ */
557
+ public static async getManifest() {
558
+ const {data} = await Dmart.axiosDmartInstance.get<any>('info/manifest', {
559
+ headers,
560
+ });
561
+ return data;
562
+ }
563
+
564
+ /**
565
+ * Retrieves the system settings and configuration
566
+ * @returns Promise resolving to settings data
567
+ */
568
+ public static async getSettings() {
569
+ const {data} = await Dmart.axiosDmartInstance.get<any>('info/settings', {
570
+ headers,
571
+ });
572
+ return data;
573
+ }
574
+
575
+ /**
576
+ * Sends an OTP (One-Time Password) request to a user
577
+ * @param request - SendOTPRequest containing recipient information
578
+ * @param acceptLanguage - Optional language preference for the OTP message (default: null)
579
+ * @returns Promise resolving to ApiResponse with OTP request result
580
+ */
581
+ public static async otpRequest(
582
+ request: SendOTPRequest,
583
+ acceptLanguage: string | null = null
584
+ ) {
585
+ const requestHeaders = {...headers};
586
+ if (acceptLanguage) {
587
+ requestHeaders['Accept-Language'] = acceptLanguage;
588
+ }
589
+
590
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
591
+ 'user/otp-request',
592
+ request,
593
+ {headers: requestHeaders}
594
+ );
595
+ return data;
596
+ }
597
+
598
+ /**
599
+ * Sends an OTP (One-Time Password) request for login purposes
600
+ * @param request - SendOTPRequest containing recipient information for login OTP
601
+ * @param acceptLanguage - Optional language preference for the OTP message (default: null)
602
+ * @returns Promise resolving to ApiResponse with OTP login request result
603
+ */
604
+ public static async otpRequestLogin(
605
+ request: SendOTPRequest,
606
+ acceptLanguage: string | null = null
607
+ ) {
608
+ const requestHeaders = {...headers};
609
+ if (acceptLanguage) {
610
+ requestHeaders['Accept-Language'] = acceptLanguage;
611
+ }
612
+
613
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
614
+ 'user/otp-request-login',
615
+ request,
616
+ {headers: requestHeaders}
617
+ );
618
+ return data;
619
+ }
620
+
621
+ /**
622
+ * Initiates a password reset request for a user
623
+ * @param request - PasswordResetRequest containing user identification for password reset
624
+ * @returns Promise resolving to ApiResponse with password reset request result
625
+ */
626
+ public static async passwordResetRequest(request: PasswordResetRequest) {
627
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
628
+ 'user/password-reset-request',
629
+ request,
630
+ {headers}
631
+ );
632
+ return data;
633
+ }
634
+
635
+ /**
636
+ * Confirms an OTP (One-Time Password) code provided by the user
637
+ * @param request - ConfirmOTPRequest containing the OTP code and verification details
638
+ * @returns Promise resolving to ApiResponse with OTP confirmation result
639
+ */
640
+ public static async confirmOtp(request: ConfirmOTPRequest) {
641
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
642
+ 'user/otp-confirm',
643
+ request,
644
+ {headers}
645
+ );
646
+ return data;
647
+ }
648
+
649
+ /**
650
+ * Resets a user's account or session state
651
+ * @param shortname - The shortname (username) of the user to reset
652
+ * @returns Promise resolving to ApiResponse with user reset result
653
+ */
654
+ public static async userReset(shortname: string) {
655
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
656
+ 'user/reset',
657
+ {shortname},
658
+ {headers}
659
+ );
660
+ return data;
661
+ }
662
+
663
+ /**
664
+ * Validates a password against system password policies and requirements
665
+ * @param password - The password string to validate
666
+ * @returns Promise resolving to ApiResponse with validation result
667
+ */
668
+ public static async validatePassword(password: string) {
669
+ const {data} = await Dmart.axiosDmartInstance.post<ApiResponse>(
670
+ 'user/validate_password',
671
+ {password},
672
+ {headers}
673
+ );
674
+ return data;
675
+ }
676
+ }