@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/dmart.model.ts +331 -0
- package/dmart.service.ts +486 -0
- package/index.ts +3 -776
- package/package.json +7 -7
- package/tsconfig.json +2 -1
package/dmart.service.ts
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import {
|
|
3
|
+
ActionRequest,
|
|
4
|
+
ActionResponse,
|
|
5
|
+
ApiQueryResponse,
|
|
6
|
+
ApiResponse,
|
|
7
|
+
ClientError,
|
|
8
|
+
ContentType,
|
|
9
|
+
headers,
|
|
10
|
+
LoginResponse,
|
|
11
|
+
ProfileResponse,
|
|
12
|
+
QueryRequest,
|
|
13
|
+
QueryType,
|
|
14
|
+
ResourceType,
|
|
15
|
+
ResponseEntry,
|
|
16
|
+
SortyType,
|
|
17
|
+
Status,
|
|
18
|
+
} from "./dmart.model";
|
|
19
|
+
|
|
20
|
+
axios.defaults.withCredentials = true;
|
|
21
|
+
axios.defaults.baseURL = "http://localhost:8282";
|
|
22
|
+
|
|
23
|
+
axios.interceptors.response.use(null, function (error) {
|
|
24
|
+
// need error.code (enum), error.status (same), error.message (axios)
|
|
25
|
+
// error.response.data (dmart), error.response.config. method, url,
|
|
26
|
+
const err: ClientError = {
|
|
27
|
+
code: error.code,
|
|
28
|
+
status: error.status,
|
|
29
|
+
message: error.message,
|
|
30
|
+
request: {url: error.response?.config?.url, method: error.response?.config?.method},
|
|
31
|
+
response: error.response?.data
|
|
32
|
+
}
|
|
33
|
+
return Promise.reject(err);
|
|
34
|
+
});
|
|
35
|
+
export const dmartClient = axios;
|
|
36
|
+
|
|
37
|
+
export class Dmart {
|
|
38
|
+
// static baseURL = "http://localhost:8282";
|
|
39
|
+
|
|
40
|
+
public static async login(shortname: string, password: string) {
|
|
41
|
+
try {
|
|
42
|
+
const response = await axios.post<LoginResponse>(
|
|
43
|
+
`user/login`,
|
|
44
|
+
{ shortname, password },
|
|
45
|
+
{ headers }
|
|
46
|
+
);
|
|
47
|
+
const data: LoginResponse = response.data;
|
|
48
|
+
if (data.status == Status.success && data.records.length > 0) {
|
|
49
|
+
headers["Authorization"] =
|
|
50
|
+
"Bearer " + data.records[0]?.attributes.access_token;
|
|
51
|
+
}
|
|
52
|
+
return data;
|
|
53
|
+
} catch (error: any) {
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public static async loginBy(credentials: any, password: string) {
|
|
59
|
+
try {
|
|
60
|
+
const response = await axios.post<LoginResponse>(
|
|
61
|
+
`user/login`,
|
|
62
|
+
{ ...credentials, password },
|
|
63
|
+
{ headers }
|
|
64
|
+
);
|
|
65
|
+
const data: LoginResponse = response.data;
|
|
66
|
+
if (data.status == Status.success && data.records.length > 0) {
|
|
67
|
+
headers["Authorization"] =
|
|
68
|
+
"Bearer " + data.records[0]?.attributes.access_token;
|
|
69
|
+
}
|
|
70
|
+
return data;
|
|
71
|
+
} catch (error: any) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public static async logout() {
|
|
77
|
+
try {
|
|
78
|
+
const { data } = await axios.post<ApiResponse>(
|
|
79
|
+
`user/logout`,
|
|
80
|
+
{},
|
|
81
|
+
{ headers }
|
|
82
|
+
);
|
|
83
|
+
return data;
|
|
84
|
+
} catch (error: any) {
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static async create_user(request: any) {
|
|
90
|
+
try {
|
|
91
|
+
const { data } = await axios.post<ActionResponse>(
|
|
92
|
+
`user/create`,
|
|
93
|
+
request,
|
|
94
|
+
{ headers }
|
|
95
|
+
);
|
|
96
|
+
return data;
|
|
97
|
+
} catch (error: any) {
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public static async update_user(request: any) {
|
|
103
|
+
try {
|
|
104
|
+
const { data } = await axios.post<ActionResponse>(
|
|
105
|
+
`user/profile`,
|
|
106
|
+
request,
|
|
107
|
+
{ headers }
|
|
108
|
+
);
|
|
109
|
+
return data;
|
|
110
|
+
} catch (error: any) {
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public static async check_existing(prop: string, value: string) {
|
|
116
|
+
try {
|
|
117
|
+
const { data } = await axios.get<ResponseEntry>(
|
|
118
|
+
`user/check-existing?${prop}=${value}`,
|
|
119
|
+
{ headers }
|
|
120
|
+
);
|
|
121
|
+
return data;
|
|
122
|
+
} catch (error: any) {
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public static async get_profile() {
|
|
128
|
+
try {
|
|
129
|
+
const { data } = await axios.get<ProfileResponse>(`user/profile`, {
|
|
130
|
+
headers,
|
|
131
|
+
});
|
|
132
|
+
if (typeof localStorage !== "undefined" && data.status === "success") {
|
|
133
|
+
localStorage.setItem(
|
|
134
|
+
"permissions",
|
|
135
|
+
JSON.stringify((data?.records ?? [{}])[0]?.attributes.permissions)
|
|
136
|
+
);
|
|
137
|
+
localStorage.setItem(
|
|
138
|
+
"roles",
|
|
139
|
+
JSON.stringify((data?.records ?? [{}])[0]?.attributes?.["roles"])
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return data;
|
|
143
|
+
} catch (error: any) {
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public static async query(
|
|
149
|
+
query: QueryRequest,
|
|
150
|
+
scope: string = "managed"
|
|
151
|
+
): Promise<ApiQueryResponse | null> {
|
|
152
|
+
try {
|
|
153
|
+
if (query.type != QueryType.spaces) {
|
|
154
|
+
query.sort_type = query.sort_type || SortyType.ascending;
|
|
155
|
+
query.sort_by = query.sort_by || "created_at";
|
|
156
|
+
}
|
|
157
|
+
query.subpath = query.subpath.replace(/\/+/g, "/");
|
|
158
|
+
const { data } = await axios.post<ApiQueryResponse>(
|
|
159
|
+
`${scope}/query`,
|
|
160
|
+
query,
|
|
161
|
+
{ headers, timeout: 3000 }
|
|
162
|
+
);
|
|
163
|
+
return data;
|
|
164
|
+
} catch (error: any) {
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public static async csv(query: any): Promise<ApiQueryResponse> {
|
|
170
|
+
try {
|
|
171
|
+
query.sort_type = query.sort_type || SortyType.ascending;
|
|
172
|
+
query.sort_by = "created_at";
|
|
173
|
+
query.subpath = query.subpath.replace(/\/+/g, "/");
|
|
174
|
+
const { data } = await axios.post<ApiQueryResponse>(
|
|
175
|
+
`managed/csv`,
|
|
176
|
+
query,
|
|
177
|
+
{ headers }
|
|
178
|
+
);
|
|
179
|
+
return data;
|
|
180
|
+
} catch (error: any) {
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public static async space(action: ActionRequest): Promise<ActionResponse> {
|
|
186
|
+
try {
|
|
187
|
+
const { data } = await axios.post<ActionResponse>(
|
|
188
|
+
`managed/space`,
|
|
189
|
+
action,
|
|
190
|
+
{ headers }
|
|
191
|
+
);
|
|
192
|
+
return data;
|
|
193
|
+
} catch (error: any) {
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public static async request(action: ActionRequest): Promise<ActionResponse> {
|
|
199
|
+
const res = await axios.post<ActionResponse>(`managed/request`, action, {
|
|
200
|
+
headers,
|
|
201
|
+
});
|
|
202
|
+
return res?.data;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public static async retrieve_entry(
|
|
206
|
+
resource_type: ResourceType,
|
|
207
|
+
space_name: string,
|
|
208
|
+
subpath: string,
|
|
209
|
+
shortname: string,
|
|
210
|
+
retrieve_json_payload: boolean = false,
|
|
211
|
+
retrieve_attachments: boolean = false,
|
|
212
|
+
validate_schema: boolean = true,
|
|
213
|
+
scope: string = "managed"
|
|
214
|
+
): Promise<ResponseEntry | null> {
|
|
215
|
+
try {
|
|
216
|
+
if (!subpath || subpath == "/") subpath = "__root__";
|
|
217
|
+
const url = `${scope}/entry/${resource_type}/${space_name}/${subpath}/${shortname}?retrieve_json_payload=${retrieve_json_payload}&retrieve_attachments=${retrieve_attachments}&validate_schema=${validate_schema}`;
|
|
218
|
+
const { data } = await axios.get<ResponseEntry>(
|
|
219
|
+
`${url.replace(/\/+/g, "/")}`,
|
|
220
|
+
{ headers }
|
|
221
|
+
);
|
|
222
|
+
return data;
|
|
223
|
+
} catch (error: any) {
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
public static async upload_with_payload(
|
|
229
|
+
space_name: string,
|
|
230
|
+
subpath: string,
|
|
231
|
+
shortname: string,
|
|
232
|
+
resource_type: ResourceType,
|
|
233
|
+
payload_file: File,
|
|
234
|
+
content_type?: ContentType,
|
|
235
|
+
schema_shortname?: string
|
|
236
|
+
): Promise<ApiResponse> {
|
|
237
|
+
const request_record_body: any = {
|
|
238
|
+
resource_type,
|
|
239
|
+
subpath,
|
|
240
|
+
shortname,
|
|
241
|
+
attributes: { is_active: true, payload: { body: {} } },
|
|
242
|
+
};
|
|
243
|
+
if (content_type) {
|
|
244
|
+
request_record_body.attributes.payload.content_type = content_type;
|
|
245
|
+
}
|
|
246
|
+
if (schema_shortname) {
|
|
247
|
+
request_record_body.attributes.payload.schema_shortname =
|
|
248
|
+
schema_shortname;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const request_record = new Blob([JSON.stringify(request_record_body)], {
|
|
252
|
+
type: "application/json",
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const form_data = new FormData();
|
|
256
|
+
form_data.append("space_name", space_name);
|
|
257
|
+
form_data.append("request_record", request_record);
|
|
258
|
+
form_data.append("payload_file", payload_file);
|
|
259
|
+
|
|
260
|
+
const headers = { "Content-Type": "multipart/form-data" };
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
const { data } = await axios.post<ApiResponse>(
|
|
264
|
+
`managed/resource_with_payload`,
|
|
265
|
+
form_data,
|
|
266
|
+
{ headers }
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return data;
|
|
270
|
+
} catch (error: any) {
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
public static async fetchDataAsset(
|
|
276
|
+
resourceType: string,
|
|
277
|
+
dataAssetType: string,
|
|
278
|
+
spaceName: string,
|
|
279
|
+
subpath: string,
|
|
280
|
+
shortname: string,
|
|
281
|
+
query_string?: string,
|
|
282
|
+
filter_data_assets?: string[],
|
|
283
|
+
branch_name?: string
|
|
284
|
+
) {
|
|
285
|
+
try {
|
|
286
|
+
const url = `managed/data-asset`;
|
|
287
|
+
const { data } = await axios.post(
|
|
288
|
+
url,
|
|
289
|
+
{
|
|
290
|
+
space_name: spaceName,
|
|
291
|
+
resource_type: resourceType,
|
|
292
|
+
data_asset_type: dataAssetType,
|
|
293
|
+
subpath,
|
|
294
|
+
shortname,
|
|
295
|
+
query_string: query_string ?? "SELECT * FROM file",
|
|
296
|
+
filter_data_assets,
|
|
297
|
+
branch_name,
|
|
298
|
+
},
|
|
299
|
+
{ headers }
|
|
300
|
+
);
|
|
301
|
+
return data;
|
|
302
|
+
} catch (error: any) {
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
public static async get_spaces(): Promise<ApiResponse | null> {
|
|
308
|
+
return await this.query({
|
|
309
|
+
type: QueryType.spaces,
|
|
310
|
+
space_name: "management",
|
|
311
|
+
subpath: "/",
|
|
312
|
+
search: "",
|
|
313
|
+
limit: 100,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
public static async get_children(
|
|
318
|
+
space_name: string,
|
|
319
|
+
subpath: string,
|
|
320
|
+
limit: number = 20,
|
|
321
|
+
offset: number = 0,
|
|
322
|
+
restrict_types: Array<ResourceType> = []
|
|
323
|
+
): Promise<ApiResponse | null> {
|
|
324
|
+
return await this.query({
|
|
325
|
+
type: QueryType.search,
|
|
326
|
+
space_name: space_name,
|
|
327
|
+
subpath: subpath,
|
|
328
|
+
filter_types: restrict_types,
|
|
329
|
+
exact_subpath: true,
|
|
330
|
+
search: "",
|
|
331
|
+
limit: limit,
|
|
332
|
+
offset: offset,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
public static get_attachment_url(
|
|
337
|
+
resource_type: ResourceType,
|
|
338
|
+
space_name: string,
|
|
339
|
+
subpath: string,
|
|
340
|
+
parent_shortname: string,
|
|
341
|
+
shortname: string,
|
|
342
|
+
ext: string | null = null,
|
|
343
|
+
scope: string = "managed"
|
|
344
|
+
) {
|
|
345
|
+
return `/${scope}/payload/${resource_type}/${space_name}/${subpath.replace(
|
|
346
|
+
/\/+$/,
|
|
347
|
+
""
|
|
348
|
+
)}/${parent_shortname}/${shortname}${ext === null ? "" : ext}`;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
public static async get_space_health(space_name: string) {
|
|
352
|
+
try {
|
|
353
|
+
const { data } = await axios.get<
|
|
354
|
+
ApiQueryResponse & { attributes: { folders_report: Object } }
|
|
355
|
+
>(`managed/health/${space_name}`, { headers });
|
|
356
|
+
return data;
|
|
357
|
+
} catch (error: any) {
|
|
358
|
+
throw error;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
public static async get_attachment_content(
|
|
363
|
+
resource_type: string,
|
|
364
|
+
space_name: string,
|
|
365
|
+
subpath: string,
|
|
366
|
+
shortname: string,
|
|
367
|
+
scope: string = "managed"
|
|
368
|
+
) {
|
|
369
|
+
try {
|
|
370
|
+
const { data } = await axios.get<any>(
|
|
371
|
+
`${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}`,
|
|
372
|
+
{ headers }
|
|
373
|
+
);
|
|
374
|
+
return data;
|
|
375
|
+
} catch (error: any) {
|
|
376
|
+
throw error;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
public static async get_payload(
|
|
381
|
+
resource_type: string,
|
|
382
|
+
space_name: string,
|
|
383
|
+
subpath: string,
|
|
384
|
+
shortname: string,
|
|
385
|
+
ext: string = ".json",
|
|
386
|
+
scope: string = "managed"
|
|
387
|
+
) {
|
|
388
|
+
try {
|
|
389
|
+
const { data } = await axios.get<any>(
|
|
390
|
+
`${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}${ext}`,
|
|
391
|
+
{ headers }
|
|
392
|
+
);
|
|
393
|
+
return data;
|
|
394
|
+
} catch (error: any) {
|
|
395
|
+
throw error;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
public static async get_payload_content(
|
|
400
|
+
resource_type: string,
|
|
401
|
+
space_name: string,
|
|
402
|
+
subpath: string,
|
|
403
|
+
shortname: string,
|
|
404
|
+
ext: string = ".json",
|
|
405
|
+
scope: string = "managed"
|
|
406
|
+
) {
|
|
407
|
+
try {
|
|
408
|
+
const { data } = await axios.get<any>(
|
|
409
|
+
`${scope}/payload/${resource_type}/${space_name}/${subpath}/${shortname}${ext}`,
|
|
410
|
+
{ headers }
|
|
411
|
+
);
|
|
412
|
+
return data;
|
|
413
|
+
} catch (error: any) {
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
public static async progress_ticket(
|
|
419
|
+
space_name: string,
|
|
420
|
+
subpath: string,
|
|
421
|
+
shortname: string,
|
|
422
|
+
action: string,
|
|
423
|
+
resolution?: string,
|
|
424
|
+
comment?: string
|
|
425
|
+
) {
|
|
426
|
+
try {
|
|
427
|
+
const payload: any = {};
|
|
428
|
+
if (resolution) {
|
|
429
|
+
payload.resolution = resolution;
|
|
430
|
+
}
|
|
431
|
+
if (comment) {
|
|
432
|
+
payload.comment = comment;
|
|
433
|
+
}
|
|
434
|
+
const { data } = await axios.put<
|
|
435
|
+
ApiQueryResponse & { attributes: { folders_report: Object } }
|
|
436
|
+
>(
|
|
437
|
+
`managed/progress-ticket/${space_name}/${subpath}/${shortname}/${action}`,
|
|
438
|
+
payload,
|
|
439
|
+
{ headers }
|
|
440
|
+
);
|
|
441
|
+
return data;
|
|
442
|
+
} catch (error: any) {
|
|
443
|
+
throw error;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
public static async submit(
|
|
448
|
+
spaceName: string,
|
|
449
|
+
schemaShortname: string,
|
|
450
|
+
subpath: string,
|
|
451
|
+
record: any
|
|
452
|
+
) {
|
|
453
|
+
try {
|
|
454
|
+
const { data } = await axios.post(
|
|
455
|
+
`public/submit/${spaceName}/${schemaShortname}/${subpath}`,
|
|
456
|
+
record,
|
|
457
|
+
{ headers }
|
|
458
|
+
);
|
|
459
|
+
return data;
|
|
460
|
+
} catch (error: any) {
|
|
461
|
+
throw error;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
public static async get_manifest() {
|
|
466
|
+
try {
|
|
467
|
+
const { data } = await axios.get<any>(`info/manifest`, {
|
|
468
|
+
headers,
|
|
469
|
+
});
|
|
470
|
+
return data;
|
|
471
|
+
} catch (error: any) {
|
|
472
|
+
throw error;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
public static async get_settings() {
|
|
477
|
+
try {
|
|
478
|
+
const { data } = await axios.get<any>(`info/settings`, {
|
|
479
|
+
headers,
|
|
480
|
+
});
|
|
481
|
+
return data;
|
|
482
|
+
} catch (error: any) {
|
|
483
|
+
throw error;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|