@edraj/tsdmart 1.0.16 → 2.0.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,772 +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
- 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 {data} = await axios.post<
332
- ApiResponse & { records: Array<LoginResponseRecord> }
333
- >(`${this.baseURL}/user/login`, {shortname, password}, {headers});
334
- return data;
335
- } catch (error: any) {
336
- throw error.response.data
337
- }
338
- }
339
-
340
- public static async loginBy(credentials: any, password: string) {
341
- try {
342
- const {data} = await axios.post<
343
- ApiResponse & { records: Array<LoginResponseRecord> }
344
- >(`${this.baseURL}/user/login`, {...credentials, password}, {headers});
345
- return data;
346
- } catch (error: any) {
347
- throw error.response.data
348
- }
349
- }
350
-
351
- public static async logout() {
352
- try {
353
- const {data} = await axios.post<ApiResponse>(
354
- `${this.baseURL}/user/logout`,
355
- {},
356
- {headers}
357
- );
358
- return data;
359
- } catch (error: any) {
360
- throw error.response.data
361
- }
362
- }
363
-
364
- public static async create_user(request: any) {
365
- try {
366
- const {data} = await axios.post<ActionResponse>(
367
- `${this.baseURL}/user/create`,
368
- request,
369
- {headers}
370
- );
371
- return data;
372
- } catch (error: any) {
373
- throw error.response.data
374
- }
375
- }
376
-
377
- public static async update_user(request: any) {
378
- try {
379
- const {data} = await axios.post<ActionResponse>(
380
- `${this.baseURL}/user/profile`,
381
- request,
382
- {headers}
383
- );
384
- return data;
385
- } catch (error: any) {
386
- throw error.response.data
387
- }
388
- }
389
-
390
- public static async check_existing(prop: string, value: string) {
391
- try {
392
- const {data} = await axios.get<ResponseEntry>(
393
- `${this.baseURL}/user/check-existing?${prop}=${value}`,
394
- {headers}
395
- );
396
- return data;
397
- } catch (error: any) {
398
- throw error.response.data
399
- }
400
- }
401
-
402
- public static async get_profile() {
403
- try {
404
- const {data} = await axios.get<ProfileResponse>(
405
- `${this.baseURL}/user/profile`,
406
- {
407
- headers,
408
- }
409
- );
410
- if (typeof localStorage !== "undefined" && data.status === "success") {
411
- localStorage.setItem(
412
- "permissions",
413
- JSON.stringify((data?.records ?? [{}])[0]?.attributes.permissions)
414
- );
415
- localStorage.setItem(
416
- "roles",
417
- JSON.stringify((data?.records ?? [{}])[0]?.attributes?.["roles"])
418
- );
419
- }
420
- return data;
421
- } catch (error: any) {
422
- throw error.response.data
423
- }
424
- }
425
-
426
- public static async query(query: QueryRequest, scope: string = "managed"): Promise<ApiQueryResponse | null> {
427
- try {
428
- if (query.type != QueryType.spaces) {
429
- query.sort_type = query.sort_type || SortyType.ascending;
430
- query.sort_by = query.sort_by || "created_at";
431
- }
432
- query.subpath = query.subpath.replace(/\/+/g, "/");
433
- const {data} = await axios.post<ApiQueryResponse>(
434
- `${this.baseURL}/${scope}/query`,
435
- query,
436
- {headers, timeout: 3000}
437
- );
438
- return data;
439
- } catch (error: any) {
440
- throw error.response.data
441
- }
442
- }
443
-
444
- public static async csv(query: any): Promise<ApiQueryResponse> {
445
- try {
446
- query.sort_type = query.sort_type || SortyType.ascending;
447
- query.sort_by = "created_at";
448
- query.subpath = query.subpath.replace(/\/+/g, "/");
449
- const {data} = await axios.post<ApiQueryResponse>(
450
- `${this.baseURL}/managed/csv`,
451
- query,
452
- {headers}
453
- );
454
- return data;
455
- } catch (error: any) {
456
- throw error.response.data
457
- }
458
- }
459
-
460
- public static async space(action: ActionRequest): Promise<ActionResponse> {
461
- try {
462
- const {data} = await axios.post<ActionResponse>(
463
- `${this.baseURL}/managed/space`,
464
- action,
465
- {headers}
466
- );
467
- return data;
468
- } catch (error: any) {
469
- throw error.response.data
470
- }
471
- }
472
-
473
- public static async request(action: ActionRequest): Promise<ActionResponse> {
474
- try {
475
- const {data} = await axios.post<ActionResponse>(
476
- `${this.baseURL}/managed/request`,
477
- action,
478
- {headers}
479
- );
480
- return data;
481
- } catch (error: any) {
482
- throw error.response.data
483
- }
484
- }
485
-
486
- public static async retrieve_entry(
487
- resource_type: ResourceType,
488
- space_name: string,
489
- subpath: string,
490
- shortname: string,
491
- retrieve_json_payload: boolean = false,
492
- retrieve_attachments: boolean = false,
493
- validate_schema: boolean = true,
494
- scope: string = "managed"
495
- ): Promise<ResponseEntry | null> {
496
- try {
497
- if (!subpath || subpath == "/") subpath = "__root__";
498
- const url = `${scope}/entry/${resource_type}/${space_name}/${subpath}/${shortname}?retrieve_json_payload=${retrieve_json_payload}&retrieve_attachments=${retrieve_attachments}&validate_schema=${validate_schema}`;
499
- const {data} = await axios.get<ResponseEntry>(
500
- `${this.baseURL}/${url.replace(/\/+/g, "/")}`,
501
- {headers}
502
- );
503
- return data;
504
- } catch (error: any) {
505
- throw error.response.data
506
- }
507
- }
508
-
509
- public static async upload_with_payload(
510
- space_name: string,
511
- subpath: string,
512
- shortname: string,
513
- resource_type: ResourceType,
514
- payload_file: File,
515
- content_type?: ContentType,
516
- schema_shortname?: string
517
- ): Promise<ApiResponse> {
518
- const request_record_body: any = {
519
- resource_type,
520
- subpath,
521
- shortname,
522
- attributes: {is_active: true, payload: {body: {}}},
523
- };
524
- if (content_type) {
525
- request_record_body.attributes.payload.content_type = content_type;
526
- }
527
- if (schema_shortname) {
528
- request_record_body.attributes.payload.schema_shortname = schema_shortname;
529
- }
530
-
531
- const request_record = new Blob(
532
- [
533
- JSON.stringify(request_record_body),
534
- ],
535
- {type: "application/json"}
536
- );
537
-
538
- const form_data = new FormData();
539
- form_data.append("space_name", space_name);
540
- form_data.append("request_record", request_record);
541
- form_data.append("payload_file", payload_file);
542
-
543
- const headers = {"Content-Type": "multipart/form-data"};
544
-
545
- try {
546
- const {data} = await axios.post<ApiResponse>(
547
- `${this.baseURL}/managed/resource_with_payload`,
548
- form_data,
549
- {headers}
550
- );
551
-
552
- return data;
553
- } catch (error: any) {
554
- throw error.response.data
555
- }
556
- }
557
-
558
-
559
- public static async fetchDataAsset(
560
- resourceType: string,
561
- dataAssetType: string,
562
- spaceName: string,
563
- subpath: string,
564
- shortname: string,
565
- query_string?: string,
566
- filter_data_assets?: string[],
567
- branch_name?: string
568
- ) {
569
- try {
570
- const url = `${this.baseURL}/managed/data-asset`;
571
- const {data} = await axios.post(
572
- url,
573
- {
574
- space_name: spaceName,
575
- resource_type: resourceType,
576
- data_asset_type: dataAssetType,
577
- subpath,
578
- shortname,
579
- query_string: query_string ?? "SELECT * FROM file",
580
- filter_data_assets,
581
- branch_name,
582
- },
583
- {headers}
584
- );
585
- return data;
586
- } catch (error: any) {
587
- throw error.response.data
588
- }
589
- }
590
-
591
- public static async get_spaces(): Promise<ApiResponse | null> {
592
- return await this.query({
593
- type: QueryType.spaces,
594
- space_name: "management",
595
- subpath: "/",
596
- search: "",
597
- limit: 100,
598
- });
599
- }
600
-
601
- public static async get_children(
602
- space_name: string,
603
- subpath: string,
604
- limit: number = 20,
605
- offset: number = 0,
606
- restrict_types: Array<ResourceType> = []
607
- ): Promise<ApiResponse | null> {
608
- return await this.query({
609
- type: QueryType.search,
610
- space_name: space_name,
611
- subpath: subpath,
612
- filter_types: restrict_types,
613
- exact_subpath: true,
614
- search: "",
615
- limit: limit,
616
- offset: offset,
617
- });
618
- }
619
-
620
- public static get_attachment_url(
621
- resource_type: ResourceType,
622
- space_name: string,
623
- subpath: string,
624
- parent_shortname: string,
625
- shortname: string,
626
- ext: string|null = null,
627
- scope: string = "managed"
628
- ) {
629
- return (
630
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath.replace(
631
- /\/+$/,
632
- ""
633
- )}/${parent_shortname}/${shortname}${ext===null?"":ext}`
634
- );
635
- }
636
-
637
- public static async get_space_health(space_name: string) {
638
- try {
639
- const {data} = await axios.get<
640
- ApiQueryResponse & { attributes: { folders_report: Object } }
641
- >(`${this.baseURL}/managed/health/${space_name}`, {headers});
642
- return data;
643
- } catch (error: any) {
644
- throw error.response.data
645
- }
646
- }
647
-
648
- public static async get_attachment_content(
649
- resource_type: string,
650
- space_name: string,
651
- subpath: string,
652
- shortname: string,
653
- scope: string = "managed"
654
- ) {
655
- try {
656
- const {data} = await axios.get<any>(
657
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}`,
658
- {headers}
659
- );
660
- return data;
661
- } catch (error: any) {
662
- throw error.response.data
663
- }
664
- }
665
-
666
- public static async get_payload(
667
- resource_type: string,
668
- space_name: string,
669
- subpath: string,
670
- shortname: string,
671
- ext: string = ".json",
672
- scope: string = "managed"
673
- ) {
674
- try {
675
- const {data} = await axios.get<any>(
676
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}${ext}`,
677
- {headers}
678
- );
679
- return data;
680
- } catch (error: any) {
681
- throw error.response.data
682
- }
683
- }
684
-
685
- public static async get_payload_content(
686
- resource_type: string,
687
- space_name: string,
688
- subpath: string,
689
- shortname: string,
690
- ext: string = ".json",
691
- scope: string = "managed"
692
- ) {
693
- try {
694
- const {data} = await axios.get<any>(
695
- `${this.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}${ext}`,
696
- {headers}
697
- );
698
- return data;
699
- }catch (error: any) {
700
- throw error.response.data
701
- }
702
- }
703
-
704
- public static async progress_ticket(
705
- space_name: string,
706
- subpath: string,
707
- shortname: string,
708
- action: string,
709
- resolution?: string,
710
- comment?: string
711
- ) {
712
- try {
713
- const payload: any = {}
714
- if (resolution) {
715
- payload.resolution = resolution;
716
- }
717
- if (comment) {
718
- payload.comment = comment;
719
- }
720
- const {data} = await axios.put<
721
- ApiQueryResponse & { attributes: { folders_report: Object } }
722
- >(
723
- `${this.baseURL}/managed/progress-ticket/${space_name}/${subpath}/${shortname}/${action}`,
724
- payload,
725
- {headers}
726
- );
727
- return data;
728
- } catch (error: any) {
729
- throw error.response.data
730
- }
731
- }
732
-
733
- public static async submit(
734
- spaceName: string,
735
- schemaShortname: string,
736
- subpath: string,
737
- record: any
738
- ) {
739
- try {
740
- const {data} = await axios.post(
741
- `${this.baseURL}/public/submit/${spaceName}/${schemaShortname}/${subpath}`,
742
- record,
743
- {headers}
744
- );
745
- return data;
746
- } catch (error: any) {
747
- throw error.response.data
748
- }
749
- }
750
-
751
- public static async get_manifest() {
752
- try {
753
- const {data} = await axios.get<any>(`${this.baseURL}/info/manifest`, {
754
- headers,
755
- });
756
- return data;
757
- } catch (error: any) {
758
- throw error.response.data
759
- }
760
- }
761
-
762
- public static async get_settings() {
763
- try{
764
- const {data} = await axios.get<any>(`${this.baseURL}/info/settings`, {
765
- headers,
766
- });
767
- return data;
768
- } catch (error: any) {
769
- throw error.response.data
770
- }
771
- }
772
- }