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