@edraj/tsdmart 2.5.0 → 2.6.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/README.md +13 -1
- package/dmart.service.ts +58 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,8 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
A TypeScript implementation of the Dmart that depends on axios.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# Setup
|
|
6
|
+
|
|
7
|
+
make sure you define `axiosDmartInstance` with your axios instance before importing the Dmart class.
|
|
6
8
|
|
|
9
|
+
|
|
10
|
+
## APIs
|
|
11
|
+
* `setAxiosInstance(axiosInstance: AxiosInstance)` - Sets the axios instance to be used by the Dmart class.
|
|
12
|
+
* `getAxiosInstance() -> AxiosInstance` - Gets the axios instance used by the Dmart class.
|
|
13
|
+
* `getBaseUrl() -> string` - Gets the base URL of the Dmart instance.
|
|
14
|
+
* `setBaseUrl(baseUrl: string)` - Sets the base URL of the Dmart instance.
|
|
15
|
+
* `getHeaders()` - Gets the headers used by the Dmart instance.
|
|
16
|
+
* `setHeaders(headers: Record<string, string>)` - Sets the headers used by the Dmart instance.
|
|
17
|
+
* `getToken() -> string | null` - Gets the token used by the Dmart instance.
|
|
18
|
+
* `setToken(token: string)` - Sets the token used by the Dmart instance.
|
|
7
19
|
* `login(shortname: string, password: string) -> Promise<LoginResponse>` - Performs a login action (shortname).
|
|
8
20
|
* `loginBy(credentials: dict, password: string) -> Promise<LoginResponse>` - Performs a login action but altering the default identifier that you can customise.
|
|
9
21
|
* `logout() -> Promise<ApiResponse>` - Performs a logout action.
|
package/dmart.service.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import axios from "axios";
|
|
1
|
+
import axios, {AxiosInstance} from "axios";
|
|
2
2
|
import {
|
|
3
3
|
ActionRequest,
|
|
4
4
|
ActionResponse,
|
|
5
5
|
ApiQueryResponse,
|
|
6
6
|
ApiResponse,
|
|
7
|
-
ClientError,
|
|
8
7
|
ContentType,
|
|
9
8
|
headers,
|
|
10
9
|
LoginResponse,
|
|
@@ -17,31 +16,47 @@ import {
|
|
|
17
16
|
Status,
|
|
18
17
|
} from "./dmart.model";
|
|
19
18
|
|
|
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: {
|
|
31
|
-
url: error.response?.config?.url,
|
|
32
|
-
method: error.response?.config?.method,
|
|
33
|
-
},
|
|
34
|
-
response: error.response?.data,
|
|
35
|
-
};
|
|
36
|
-
return Promise.reject(err);
|
|
37
|
-
});
|
|
38
|
-
export const dmartClient = axios;
|
|
39
19
|
|
|
40
20
|
export class Dmart {
|
|
41
|
-
|
|
21
|
+
static axiosDmartInstance: AxiosInstance;
|
|
22
|
+
|
|
23
|
+
static setAxiosInstance(axiosInstance: AxiosInstance) {
|
|
24
|
+
Dmart.axiosDmartInstance = axiosInstance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static getAxiosInstance(): AxiosInstance {
|
|
28
|
+
if (!Dmart.axiosDmartInstance) {
|
|
29
|
+
throw new Error("Axios instance is not set. Please set it using setAxiosInstance method.");
|
|
30
|
+
}
|
|
31
|
+
return Dmart.axiosDmartInstance;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static getHeaders() {
|
|
35
|
+
return headers;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public static setHeaders(newHeaders: any) {
|
|
39
|
+
Object.assign(headers, newHeaders);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public static getBaseURL() {
|
|
43
|
+
return Dmart.axiosDmartInstance.defaults.baseURL;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static set setBaseURL(url: string) {
|
|
47
|
+
Dmart.axiosDmartInstance.defaults.baseURL = url;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public static getToken() {
|
|
51
|
+
return headers["Authorization"] ? headers["Authorization"].replace("Bearer ", "") : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public static setToken(token: string) {
|
|
55
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
56
|
+
}
|
|
42
57
|
|
|
43
58
|
public static async login(shortname: string, password: string) {
|
|
44
|
-
const response = await
|
|
59
|
+
const response = await Dmart.axiosDmartInstance.post<LoginResponse>(
|
|
45
60
|
`user/login`,
|
|
46
61
|
{ shortname, password },
|
|
47
62
|
{ headers }
|
|
@@ -56,7 +71,7 @@ export class Dmart {
|
|
|
56
71
|
|
|
57
72
|
public static async loginBy(credentials: any, password: string) {
|
|
58
73
|
try {
|
|
59
|
-
const response = await
|
|
74
|
+
const response = await Dmart.axiosDmartInstance.post<LoginResponse>(
|
|
60
75
|
`user/login`,
|
|
61
76
|
{ ...credentials, password },
|
|
62
77
|
{ headers }
|
|
@@ -74,7 +89,7 @@ export class Dmart {
|
|
|
74
89
|
|
|
75
90
|
public static async logout() {
|
|
76
91
|
try {
|
|
77
|
-
const { data } = await
|
|
92
|
+
const { data } = await Dmart.axiosDmartInstance.post<ApiResponse>(
|
|
78
93
|
`user/logout`,
|
|
79
94
|
{},
|
|
80
95
|
{ headers }
|
|
@@ -87,7 +102,7 @@ export class Dmart {
|
|
|
87
102
|
|
|
88
103
|
public static async create_user(request: any) {
|
|
89
104
|
try {
|
|
90
|
-
const { data } = await
|
|
105
|
+
const { data } = await Dmart.axiosDmartInstance.post<ActionResponse>(
|
|
91
106
|
`user/create`,
|
|
92
107
|
request,
|
|
93
108
|
{ headers }
|
|
@@ -100,7 +115,7 @@ export class Dmart {
|
|
|
100
115
|
|
|
101
116
|
public static async update_user(request: any) {
|
|
102
117
|
try {
|
|
103
|
-
const { data } = await
|
|
118
|
+
const { data } = await Dmart.axiosDmartInstance.post<ActionResponse>(
|
|
104
119
|
`user/profile`,
|
|
105
120
|
request,
|
|
106
121
|
{ headers }
|
|
@@ -113,7 +128,7 @@ export class Dmart {
|
|
|
113
128
|
|
|
114
129
|
public static async check_existing(prop: string, value: string) {
|
|
115
130
|
try {
|
|
116
|
-
const { data } = await
|
|
131
|
+
const { data } = await Dmart.axiosDmartInstance.get<ResponseEntry>(
|
|
117
132
|
`user/check-existing?${prop}=${value}`,
|
|
118
133
|
{ headers }
|
|
119
134
|
);
|
|
@@ -125,7 +140,7 @@ export class Dmart {
|
|
|
125
140
|
|
|
126
141
|
public static async get_profile() {
|
|
127
142
|
try {
|
|
128
|
-
const { data } = await
|
|
143
|
+
const { data } = await Dmart.axiosDmartInstance.get<ProfileResponse>(`user/profile`, {
|
|
129
144
|
headers,
|
|
130
145
|
});
|
|
131
146
|
if (typeof localStorage !== "undefined" && data.status === "success") {
|
|
@@ -154,7 +169,7 @@ export class Dmart {
|
|
|
154
169
|
query.sort_by = query.sort_by || "created_at";
|
|
155
170
|
}
|
|
156
171
|
query.subpath = query.subpath.replace(/\/+/g, "/");
|
|
157
|
-
const { data } = await
|
|
172
|
+
const { data } = await Dmart.axiosDmartInstance.post<ApiQueryResponse>(
|
|
158
173
|
`${scope}/query`,
|
|
159
174
|
query,
|
|
160
175
|
{ headers, timeout: 3000 }
|
|
@@ -170,7 +185,7 @@ export class Dmart {
|
|
|
170
185
|
query.sort_type = query.sort_type || SortyType.ascending;
|
|
171
186
|
query.sort_by = "created_at";
|
|
172
187
|
query.subpath = query.subpath.replace(/\/+/g, "/");
|
|
173
|
-
const { data } = await
|
|
188
|
+
const { data } = await Dmart.axiosDmartInstance.post<ApiQueryResponse>(
|
|
174
189
|
`managed/csv`,
|
|
175
190
|
query,
|
|
176
191
|
{ headers }
|
|
@@ -183,7 +198,7 @@ export class Dmart {
|
|
|
183
198
|
|
|
184
199
|
public static async space(action: ActionRequest): Promise<ActionResponse> {
|
|
185
200
|
try {
|
|
186
|
-
const { data } = await
|
|
201
|
+
const { data } = await Dmart.axiosDmartInstance.post<ActionResponse>(
|
|
187
202
|
`managed/space`,
|
|
188
203
|
action,
|
|
189
204
|
{ headers }
|
|
@@ -195,7 +210,7 @@ export class Dmart {
|
|
|
195
210
|
}
|
|
196
211
|
|
|
197
212
|
public static async request(action: ActionRequest): Promise<ActionResponse> {
|
|
198
|
-
const res = await
|
|
213
|
+
const res = await Dmart.axiosDmartInstance.post<ActionResponse>(`managed/request`, action, {
|
|
199
214
|
headers,
|
|
200
215
|
});
|
|
201
216
|
return res?.data;
|
|
@@ -214,7 +229,7 @@ export class Dmart {
|
|
|
214
229
|
try {
|
|
215
230
|
if (!subpath || subpath == "/") subpath = "__root__";
|
|
216
231
|
const url = `${scope}/entry/${resource_type}/${space_name}/${subpath}/${shortname}?retrieve_json_payload=${retrieve_json_payload}&retrieve_attachments=${retrieve_attachments}&validate_schema=${validate_schema}`;
|
|
217
|
-
const { data } = await
|
|
232
|
+
const { data } = await Dmart.axiosDmartInstance.get<ResponseEntry>(
|
|
218
233
|
`${url.replace(/\/+/g, "/")}`,
|
|
219
234
|
{ headers }
|
|
220
235
|
);
|
|
@@ -262,7 +277,7 @@ export class Dmart {
|
|
|
262
277
|
const headers = { "Content-Type": "multipart/form-data" };
|
|
263
278
|
|
|
264
279
|
try {
|
|
265
|
-
const { data } = await
|
|
280
|
+
const { data } = await Dmart.axiosDmartInstance.post<ApiResponse>(
|
|
266
281
|
`${scope}/resource_with_payload`,
|
|
267
282
|
form_data,
|
|
268
283
|
{ headers }
|
|
@@ -285,7 +300,7 @@ export class Dmart {
|
|
|
285
300
|
) {
|
|
286
301
|
try {
|
|
287
302
|
const url = `managed/data-asset`;
|
|
288
|
-
const { data } = await
|
|
303
|
+
const { data } = await Dmart.axiosDmartInstance.post(
|
|
289
304
|
url,
|
|
290
305
|
{
|
|
291
306
|
space_name: spaceName,
|
|
@@ -342,7 +357,7 @@ export class Dmart {
|
|
|
342
357
|
ext: string | null = null,
|
|
343
358
|
scope: string = "managed"
|
|
344
359
|
) {
|
|
345
|
-
return
|
|
360
|
+
return `${axios.defaults.baseURL}/${scope}/payload/${resource_type}/${space_name}/${subpath.replace(
|
|
346
361
|
/\/+$/,
|
|
347
362
|
""
|
|
348
363
|
)}/${parent_shortname}/${shortname}${ext === null ? "" : `.${ext}`}`;
|
|
@@ -350,7 +365,7 @@ export class Dmart {
|
|
|
350
365
|
|
|
351
366
|
public static async get_space_health(space_name: string) {
|
|
352
367
|
try {
|
|
353
|
-
const { data } = await
|
|
368
|
+
const { data } = await Dmart.axiosDmartInstance.get<
|
|
354
369
|
ApiQueryResponse & { attributes: { folders_report: Object } }
|
|
355
370
|
>(`managed/health/${space_name}`, { headers });
|
|
356
371
|
return data;
|
|
@@ -376,7 +391,7 @@ export class Dmart {
|
|
|
376
391
|
url += `.${schemaShortname}`
|
|
377
392
|
}
|
|
378
393
|
url += `.${ext}`
|
|
379
|
-
const { data } = await
|
|
394
|
+
const { data } = await Dmart.axiosDmartInstance.get<any>(
|
|
380
395
|
url,
|
|
381
396
|
{ headers }
|
|
382
397
|
);
|
|
@@ -402,7 +417,7 @@ export class Dmart {
|
|
|
402
417
|
if (comment) {
|
|
403
418
|
payload.comment = comment;
|
|
404
419
|
}
|
|
405
|
-
const { data } = await
|
|
420
|
+
const { data } = await Dmart.axiosDmartInstance.put<
|
|
406
421
|
ApiQueryResponse & { attributes: { folders_report: Object } }
|
|
407
422
|
>(
|
|
408
423
|
`managed/progress-ticket/${space_name}/${subpath}/${shortname}/${action}`,
|
|
@@ -432,7 +447,7 @@ export class Dmart {
|
|
|
432
447
|
url += `/${workflowShortname}`;
|
|
433
448
|
}
|
|
434
449
|
url += `/${schemaShortname}/${subpath}`;
|
|
435
|
-
const { data } = await
|
|
450
|
+
const { data } = await Dmart.axiosDmartInstance.post(
|
|
436
451
|
url,
|
|
437
452
|
record,
|
|
438
453
|
{ headers }
|
|
@@ -445,7 +460,7 @@ export class Dmart {
|
|
|
445
460
|
|
|
446
461
|
public static async get_manifest() {
|
|
447
462
|
try {
|
|
448
|
-
const { data } = await
|
|
463
|
+
const { data } = await Dmart.axiosDmartInstance.get<any>(`info/manifest`, {
|
|
449
464
|
headers,
|
|
450
465
|
});
|
|
451
466
|
return data;
|
|
@@ -456,7 +471,7 @@ export class Dmart {
|
|
|
456
471
|
|
|
457
472
|
public static async get_settings() {
|
|
458
473
|
try {
|
|
459
|
-
const { data } = await
|
|
474
|
+
const { data } = await Dmart.axiosDmartInstance.get<any>(`info/settings`, {
|
|
460
475
|
headers,
|
|
461
476
|
});
|
|
462
477
|
return data;
|