@edraj/tsdmart 1.0.17 → 2.1.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/index.ts CHANGED
@@ -1,778 +1,5 @@
1
- import axios from "axios";
1
+ // can and needs to be redone
2
2
 
3
+ export * from "./dmart.model";
4
+ export * from "./dmart.service";
3
5
 
4
- axios.defaults.withCredentials = true;
5
-
6
- export enum Status {
7
- success = "success",
8
- failed = "failed",
9
- }
10
-
11
- export type Error = {
12
- type: string;
13
- code: number;
14
- message: string;
15
- info: any;
16
- };
17
-
18
- export type ApiResponseRecord = {
19
- resource_type: string;
20
- shortname: string;
21
- branch_name?: string;
22
- subpath: string;
23
- attributes: Record<string, any>;
24
- };
25
-
26
- export type ApiResponse = {
27
- status: Status;
28
- error?: Error;
29
- records: Array<ApiResponseRecord>;
30
- };
31
-
32
- export type Translation = {
33
- ar: string;
34
- en: string;
35
- kd: string;
36
- };
37
-
38
- export enum UserType {
39
- web = "web",
40
- mobile = "mobile",
41
- bot = "bot",
42
- }
43
-
44
- export type LoginResponseRecord = ApiResponseRecord & {
45
- attributes: {
46
- access_token: string;
47
- type: UserType;
48
- displayname: Translation;
49
- };
50
- };
51
-
52
- type LoginResponse = ApiResponse & { records : Array<LoginResponseRecord> };
53
-
54
- export type Permission = {
55
- allowed_actions: Array<ActionType>;
56
- conditions: Array<string>;
57
- restricted_fields: Array<any>;
58
- allowed_fields_values: Map<string, any>;
59
- };
60
-
61
- export enum Language {
62
- arabic = "arabic",
63
- english = "engligh",
64
- kurdish = "kurdish",
65
- french = "french",
66
- turkish = "turkish",
67
- }
68
-
69
- export type ProfileResponseRecord = ApiResponseRecord & {
70
- attributes: {
71
- email: string;
72
- displayname: Translation;
73
- type: string;
74
- language: Language;
75
- is_email_verified: boolean;
76
- is_msisdn_verified: boolean;
77
- force_password_change: boolean;
78
- permissions: Record<string, Permission>;
79
- };
80
- };
81
-
82
- export enum ActionType {
83
- query = "query",
84
- view = "view",
85
- update = "update",
86
- create = "create",
87
- delete = "delete",
88
- attach = "attach",
89
- move = "move",
90
- progress_ticket = "progress_ticket",
91
- }
92
-
93
- export type ProfileResponse = ApiResponse & {
94
- records: Array<ProfileResponseRecord>;
95
- };
96
-
97
- export let headers: { [key: string]: string } = {
98
- "Content-type": "application/json",
99
- "Authorization": ""
100
- };
101
-
102
- export type AggregationReducer = {
103
- name: string;
104
- alias: string;
105
- args: Array<string>;
106
- };
107
-
108
- export type AggregationType = {
109
- load: Array<string>;
110
- group_by: Array<string>;
111
- reducers: Array<AggregationReducer> | Array<string>;
112
- };
113
-
114
- export enum QueryType {
115
- aggregation = "aggregation",
116
- search = "search",
117
- subpath = "subpath",
118
- events = "events",
119
- history = "history",
120
- tags = "tags",
121
- spaces = "spaces",
122
- counters = "counters",
123
- reports = "reports",
124
- attachments = "attachments",
125
- attachments_aggregation = "attachments_aggregation"
126
- }
127
-
128
- export enum SortyType {
129
- ascending = "ascending",
130
- descending = "descending",
131
- }
132
-
133
- // enum NotificationPriority {
134
- // high = "high",
135
- // medium = "medium",
136
- // low = "low"
137
- // };
138
-
139
- export type QueryRequest = {
140
- type: QueryType;
141
- space_name: string;
142
- subpath: string;
143
- filter_types?: Array<ResourceType>;
144
- filter_schema_names?: Array<string>;
145
- filter_shortnames?: Array<string>;
146
- search: string;
147
- from_date?: string;
148
- to_date?: string;
149
- sort_by?: string;
150
- sort_type?: SortyType;
151
- retrieve_json_payload?: boolean;
152
- retrieve_attachments?: boolean;
153
- validate_schema?: boolean;
154
- jq_filter?: string;
155
- exact_subpath?: boolean;
156
- limit?: number;
157
- offset?: number;
158
- aggregation_data?: AggregationType;
159
- };
160
-
161
- export enum RequestType {
162
- create = "create",
163
- update = "update",
164
- replace = "replace",
165
- delete = "delete",
166
- move = "move",
167
- updateACL = "update_acl",
168
- assign = "assign",
169
- }
170
-
171
- export enum ResourceAttachmentType {
172
- json = "json",
173
- comment = "comment",
174
- media = "media",
175
- relationship = "relationship",
176
- alteration = "alteration",
177
- csv = "csv",
178
- parquet = "parquet",
179
- jsonl = "jsonl",
180
- sqlite = "sqlite",
181
- }
182
-
183
- export enum ResourceType {
184
- user = "user",
185
- group = "group",
186
- folder = "folder",
187
- schema = "schema",
188
- content = "content",
189
- acl = "acl",
190
- comment = "comment",
191
- reaction = "reaction",
192
- media = "media",
193
- locator = "locator",
194
- relationship = "relationship",
195
- alteration = "alteration",
196
- history = "history",
197
- space = "space",
198
- branch = "branch",
199
- permission = "permission",
200
- role = "role",
201
- ticket = "ticket",
202
- json = "json",
203
- post = "post",
204
- plugin_wrapper = "plugin_wrapper",
205
- notification = "notification",
206
- jsonl = "jsonl",
207
- csv = "csv",
208
- sqlite = "sqlite",
209
- parquet = "parquet",
210
- }
211
-
212
- export enum ContentType {
213
- text = "text",
214
- html = "html",
215
- markdown = "markdown",
216
- json = "json",
217
- image = "image",
218
- python = "python",
219
- pdf = "pdf",
220
- audio = "audio",
221
- video = "video",
222
- jsonl = "jsonl",
223
- csv = "csv",
224
- sqlite = "sqlite",
225
- parquet = "parquet",
226
- }
227
-
228
- export enum ContentTypeMedia {
229
- text = "text",
230
- html = "html",
231
- markdown = "markdown",
232
- image = "image",
233
- python = "python",
234
- pdf = "pdf",
235
- audio = "audio",
236
- video = "video",
237
- }
238
-
239
- export type Payload = {
240
- content_type: ContentType;
241
- schema_shortname?: string;
242
- checksum: string;
243
- body: string | Record<string, any> | any;
244
- last_validated: string;
245
- validation_status: "valid" | "invalid";
246
- };
247
-
248
- export type MetaExtended = {
249
- email: string;
250
- msisdn: string;
251
- is_email_verified: boolean;
252
- is_msisdn_verified: boolean;
253
- force_password_change: boolean;
254
- password: string;
255
- workflow_shortname: string;
256
- state: string;
257
- is_open: boolean;
258
- };
259
-
260
- export type ResponseEntry = MetaExtended & {
261
- uuid: string;
262
- shortname: string;
263
- subpath: string;
264
- is_active: boolean;
265
- displayname: Translation;
266
- description: Translation;
267
- tags: Set<string>;
268
- created_at: string;
269
- updated_at: string;
270
- owner_shortname: string;
271
- payload?: Payload;
272
- relationships?: any;
273
- attachments?: Object;
274
- workflow_shortname?: string;
275
- state?: string;
276
- };
277
-
278
- export type ResponseRecord = {
279
- resource_type: ResourceType;
280
- uuid: string;
281
- shortname: string;
282
- subpath: string;
283
- attributes: {
284
- is_active: boolean;
285
- displayname: Translation;
286
- description: Translation;
287
- tags: Set<string>;
288
- created_at: string;
289
- updated_at: string;
290
- owner_shortname: string;
291
- payload?: Payload;
292
- };
293
- };
294
-
295
- export type ActionResponse = ApiResponse & {
296
- records: Array<
297
- ResponseRecord & {
298
- attachments: {
299
- media: Array<ResponseRecord>;
300
- json: Array<ResponseRecord>;
301
- };
302
- }
303
- >;
304
- };
305
-
306
- export type ActionRequestRecord = {
307
- resource_type: ResourceType;
308
- uuid?: string;
309
- shortname: string;
310
- subpath: string;
311
- attributes: Record<string, any>;
312
- attachments?: Record<ResourceType, Array<any>>;
313
- };
314
-
315
- export type ActionRequest = {
316
- space_name: string;
317
- request_type: RequestType;
318
- records: Array<ActionRequestRecord>;
319
- };
320
-
321
-
322
- export type ApiQueryResponse = ApiResponse & {
323
- attributes: { total: number; returned: number };
324
- };
325
-
326
- export default class Dmart {
327
- static baseURL = "http://localhost:8282";
328
-
329
- public static async login(shortname: string, password: string) {
330
- try {
331
- const response = await axios.post<LoginResponse>(`${this.baseURL}/user/login`, {shortname, password}, {headers});
332
- const data: LoginResponse = response.data;
333
- if (data.status == Status.success && data.records.length > 0) {
334
- headers['Authorization'] = "Bearer " + data.records[0].attributes.access_token;
335
- }
336
- return data;
337
- } catch (error: any) {
338
- throw error.response.data
339
- }
340
- }
341
-
342
- public static async loginBy(credentials: any, password: string) {
343
- try {
344
- const response = await axios.post<LoginResponse>(
345
- `${this.baseURL}/user/login`, {...credentials, password}, {headers}
346
- );
347
- const data: LoginResponse = response.data;
348
- if (data.status == Status.success && data.records.length > 0) {
349
- headers['Authorization'] = "Bearer " + data.records[0].attributes.access_token;
350
- }
351
- return data;
352
- } catch (error: any) {
353
- throw error.response.data
354
- }
355
- }
356
-
357
- public static async logout() {
358
- try {
359
- const {data} = await axios.post<ApiResponse>(
360
- `${this.baseURL}/user/logout`,
361
- {},
362
- {headers}
363
- );
364
- return data;
365
- } catch (error: any) {
366
- throw error.response.data
367
- }
368
- }
369
-
370
- public static async create_user(request: any) {
371
- try {
372
- const {data} = await axios.post<ActionResponse>(
373
- `${this.baseURL}/user/create`,
374
- request,
375
- {headers}
376
- );
377
- return data;
378
- } catch (error: any) {
379
- throw error.response.data
380
- }
381
- }
382
-
383
- public static async update_user(request: any) {
384
- try {
385
- const {data} = await axios.post<ActionResponse>(
386
- `${this.baseURL}/user/profile`,
387
- request,
388
- {headers}
389
- );
390
- return data;
391
- } catch (error: any) {
392
- throw error.response.data
393
- }
394
- }
395
-
396
- public static async check_existing(prop: string, value: string) {
397
- try {
398
- const {data} = await axios.get<ResponseEntry>(
399
- `${this.baseURL}/user/check-existing?${prop}=${value}`,
400
- {headers}
401
- );
402
- return data;
403
- } catch (error: any) {
404
- throw error.response.data
405
- }
406
- }
407
-
408
- public static async get_profile() {
409
- try {
410
- const {data} = await axios.get<ProfileResponse>(
411
- `${this.baseURL}/user/profile`,
412
- {
413
- headers,
414
- }
415
- );
416
- if (typeof localStorage !== "undefined" && data.status === "success") {
417
- localStorage.setItem(
418
- "permissions",
419
- JSON.stringify((data?.records ?? [{}])[0]?.attributes.permissions)
420
- );
421
- localStorage.setItem(
422
- "roles",
423
- JSON.stringify((data?.records ?? [{}])[0]?.attributes?.["roles"])
424
- );
425
- }
426
- return data;
427
- } catch (error: any) {
428
- throw error.response.data
429
- }
430
- }
431
-
432
- public static async query(query: QueryRequest, scope: string = "managed"): Promise<ApiQueryResponse | null> {
433
- try {
434
- if (query.type != QueryType.spaces) {
435
- query.sort_type = query.sort_type || SortyType.ascending;
436
- query.sort_by = query.sort_by || "created_at";
437
- }
438
- query.subpath = query.subpath.replace(/\/+/g, "/");
439
- const {data} = await axios.post<ApiQueryResponse>(
440
- `${this.baseURL}/${scope}/query`,
441
- query,
442
- {headers, timeout: 3000}
443
- );
444
- return data;
445
- } catch (error: any) {
446
- throw error.response.data
447
- }
448
- }
449
-
450
- public static async csv(query: any): Promise<ApiQueryResponse> {
451
- try {
452
- query.sort_type = query.sort_type || SortyType.ascending;
453
- query.sort_by = "created_at";
454
- query.subpath = query.subpath.replace(/\/+/g, "/");
455
- const {data} = await axios.post<ApiQueryResponse>(
456
- `${this.baseURL}/managed/csv`,
457
- query,
458
- {headers}
459
- );
460
- return data;
461
- } catch (error: any) {
462
- throw error.response.data
463
- }
464
- }
465
-
466
- public static async space(action: ActionRequest): Promise<ActionResponse> {
467
- try {
468
- const {data} = await axios.post<ActionResponse>(
469
- `${this.baseURL}/managed/space`,
470
- action,
471
- {headers}
472
- );
473
- return data;
474
- } catch (error: any) {
475
- throw error.response.data
476
- }
477
- }
478
-
479
- public static async request(action: ActionRequest): Promise<ActionResponse> {
480
- try {
481
- const {data} = await axios.post<ActionResponse>(
482
- `${this.baseURL}/managed/request`,
483
- action,
484
- {headers}
485
- );
486
- return data;
487
- } catch (error: any) {
488
- throw error.response.data
489
- }
490
- }
491
-
492
- public static async retrieve_entry(
493
- resource_type: ResourceType,
494
- space_name: string,
495
- subpath: string,
496
- shortname: string,
497
- retrieve_json_payload: boolean = false,
498
- retrieve_attachments: boolean = false,
499
- validate_schema: boolean = true,
500
- scope: string = "managed"
501
- ): Promise<ResponseEntry | null> {
502
- try {
503
- if (!subpath || subpath == "/") subpath = "__root__";
504
- const url = `${scope}/entry/${resource_type}/${space_name}/${subpath}/${shortname}?retrieve_json_payload=${retrieve_json_payload}&retrieve_attachments=${retrieve_attachments}&validate_schema=${validate_schema}`;
505
- const {data} = await axios.get<ResponseEntry>(
506
- `${this.baseURL}/${url.replace(/\/+/g, "/")}`,
507
- {headers}
508
- );
509
- return data;
510
- } catch (error: any) {
511
- throw error.response.data
512
- }
513
- }
514
-
515
- public static async upload_with_payload(
516
- space_name: string,
517
- subpath: string,
518
- shortname: string,
519
- resource_type: ResourceType,
520
- payload_file: File,
521
- content_type?: ContentType,
522
- schema_shortname?: string
523
- ): Promise<ApiResponse> {
524
- const request_record_body: any = {
525
- resource_type,
526
- subpath,
527
- shortname,
528
- attributes: {is_active: true, payload: {body: {}}},
529
- };
530
- if (content_type) {
531
- request_record_body.attributes.payload.content_type = content_type;
532
- }
533
- if (schema_shortname) {
534
- request_record_body.attributes.payload.schema_shortname = schema_shortname;
535
- }
536
-
537
- const request_record = new Blob(
538
- [
539
- JSON.stringify(request_record_body),
540
- ],
541
- {type: "application/json"}
542
- );
543
-
544
- const form_data = new FormData();
545
- form_data.append("space_name", space_name);
546
- form_data.append("request_record", request_record);
547
- form_data.append("payload_file", payload_file);
548
-
549
- const headers = {"Content-Type": "multipart/form-data"};
550
-
551
- try {
552
- const {data} = await axios.post<ApiResponse>(
553
- `${this.baseURL}/managed/resource_with_payload`,
554
- form_data,
555
- {headers}
556
- );
557
-
558
- return data;
559
- } catch (error: any) {
560
- throw error.response.data
561
- }
562
- }
563
-
564
-
565
- public static async fetchDataAsset(
566
- resourceType: string,
567
- dataAssetType: string,
568
- spaceName: string,
569
- subpath: string,
570
- shortname: string,
571
- query_string?: string,
572
- filter_data_assets?: string[],
573
- branch_name?: string
574
- ) {
575
- try {
576
- const url = `${this.baseURL}/managed/data-asset`;
577
- const {data} = await axios.post(
578
- url,
579
- {
580
- space_name: spaceName,
581
- resource_type: resourceType,
582
- data_asset_type: dataAssetType,
583
- subpath,
584
- shortname,
585
- query_string: query_string ?? "SELECT * FROM file",
586
- filter_data_assets,
587
- branch_name,
588
- },
589
- {headers}
590
- );
591
- return data;
592
- } catch (error: any) {
593
- throw error.response.data
594
- }
595
- }
596
-
597
- public static async get_spaces(): Promise<ApiResponse | null> {
598
- return await this.query({
599
- type: QueryType.spaces,
600
- space_name: "management",
601
- subpath: "/",
602
- search: "",
603
- limit: 100,
604
- });
605
- }
606
-
607
- public static async get_children(
608
- space_name: string,
609
- subpath: string,
610
- limit: number = 20,
611
- offset: number = 0,
612
- restrict_types: Array<ResourceType> = []
613
- ): Promise<ApiResponse | null> {
614
- return await this.query({
615
- type: QueryType.search,
616
- space_name: space_name,
617
- subpath: subpath,
618
- filter_types: restrict_types,
619
- exact_subpath: true,
620
- search: "",
621
- limit: limit,
622
- offset: offset,
623
- });
624
- }
625
-
626
- public static get_attachment_url(
627
- resource_type: ResourceType,
628
- space_name: string,
629
- subpath: string,
630
- parent_shortname: string,
631
- shortname: string,
632
- ext: string|null = null,
633
- scope: string = "managed"
634
- ) {
635
- return (
636
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath.replace(
637
- /\/+$/,
638
- ""
639
- )}/${parent_shortname}/${shortname}${ext===null?"":ext}`
640
- );
641
- }
642
-
643
- public static async get_space_health(space_name: string) {
644
- try {
645
- const {data} = await axios.get<
646
- ApiQueryResponse & { attributes: { folders_report: Object } }
647
- >(`${this.baseURL}/managed/health/${space_name}`, {headers});
648
- return data;
649
- } catch (error: any) {
650
- throw error.response.data
651
- }
652
- }
653
-
654
- public static async get_attachment_content(
655
- resource_type: string,
656
- space_name: string,
657
- subpath: string,
658
- shortname: string,
659
- scope: string = "managed"
660
- ) {
661
- try {
662
- const {data} = await axios.get<any>(
663
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}`,
664
- {headers}
665
- );
666
- return data;
667
- } catch (error: any) {
668
- throw error.response.data
669
- }
670
- }
671
-
672
- public static async get_payload(
673
- resource_type: string,
674
- space_name: string,
675
- subpath: string,
676
- shortname: string,
677
- ext: string = ".json",
678
- scope: string = "managed"
679
- ) {
680
- try {
681
- const {data} = await axios.get<any>(
682
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}${ext}`,
683
- {headers}
684
- );
685
- return data;
686
- } catch (error: any) {
687
- throw error.response.data
688
- }
689
- }
690
-
691
- public static async get_payload_content(
692
- resource_type: string,
693
- space_name: string,
694
- subpath: string,
695
- shortname: string,
696
- ext: string = ".json",
697
- scope: string = "managed"
698
- ) {
699
- try {
700
- const {data} = await axios.get<any>(
701
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}${ext}`,
702
- {headers}
703
- );
704
- return data;
705
- }catch (error: any) {
706
- throw error.response.data
707
- }
708
- }
709
-
710
- public static async progress_ticket(
711
- space_name: string,
712
- subpath: string,
713
- shortname: string,
714
- action: string,
715
- resolution?: string,
716
- comment?: string
717
- ) {
718
- try {
719
- const payload: any = {}
720
- if (resolution) {
721
- payload.resolution = resolution;
722
- }
723
- if (comment) {
724
- payload.comment = comment;
725
- }
726
- const {data} = await axios.put<
727
- ApiQueryResponse & { attributes: { folders_report: Object } }
728
- >(
729
- `${this.baseURL}/managed/progress-ticket/${space_name}/${subpath}/${shortname}/${action}`,
730
- payload,
731
- {headers}
732
- );
733
- return data;
734
- } catch (error: any) {
735
- throw error.response.data
736
- }
737
- }
738
-
739
- public static async submit(
740
- spaceName: string,
741
- schemaShortname: string,
742
- subpath: string,
743
- record: any
744
- ) {
745
- try {
746
- const {data} = await axios.post(
747
- `${this.baseURL}/public/submit/${spaceName}/${schemaShortname}/${subpath}`,
748
- record,
749
- {headers}
750
- );
751
- return data;
752
- } catch (error: any) {
753
- throw error.response.data
754
- }
755
- }
756
-
757
- public static async get_manifest() {
758
- try {
759
- const {data} = await axios.get<any>(`${this.baseURL}/info/manifest`, {
760
- headers,
761
- });
762
- return data;
763
- } catch (error: any) {
764
- throw error.response.data
765
- }
766
- }
767
-
768
- public static async get_settings() {
769
- try{
770
- const {data} = await axios.get<any>(`${this.baseURL}/info/settings`, {
771
- headers,
772
- });
773
- return data;
774
- } catch (error: any) {
775
- throw error.response.data
776
- }
777
- }
778
- }