@edraj/tsdmart 2.1.2 → 2.2.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/client.ts +112 -0
- package/config.ts +9 -0
- package/dmart.service.ts +17 -17
- package/package.json +1 -1
- package/services/auth.ts +45 -0
- package/services/query.ts +53 -0
- package/services/request.ts +29 -0
- package/services/upload.ts +46 -0
- package/services/user.ts +12 -0
- package/test.ts +70 -46
package/client.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
|
|
2
|
+
import { Config } from "./config";
|
|
3
|
+
import {
|
|
4
|
+
ActionRequest,
|
|
5
|
+
ClientError,
|
|
6
|
+
ContentType,
|
|
7
|
+
QueryRequest,
|
|
8
|
+
ResourceType,
|
|
9
|
+
} from "./dmart.model";
|
|
10
|
+
import { get_profile, login, logout } from "./services/auth";
|
|
11
|
+
import { query, retrieve_entry } from "./services/query";
|
|
12
|
+
import { request, submit } from "./services/request";
|
|
13
|
+
import { upload_with_payload } from "./services/upload";
|
|
14
|
+
|
|
15
|
+
export class DmartClient {
|
|
16
|
+
client: AxiosInstance;
|
|
17
|
+
|
|
18
|
+
constructor(config: CreateAxiosDefaults) {
|
|
19
|
+
this.client = axios.create({ ...Config, ...config });
|
|
20
|
+
|
|
21
|
+
this.client.interceptors.response.use(null, function (error) {
|
|
22
|
+
// need error.code (enum), error.status (same), error.message (axios)
|
|
23
|
+
// error.response.data (dmart), error.response.config. method, url,
|
|
24
|
+
const err: ClientError = {
|
|
25
|
+
code: error.code,
|
|
26
|
+
status: error.status,
|
|
27
|
+
message: error.message,
|
|
28
|
+
request: {
|
|
29
|
+
url: error.response?.config?.url,
|
|
30
|
+
method: error.response?.config?.method,
|
|
31
|
+
},
|
|
32
|
+
response: error.response?.data,
|
|
33
|
+
};
|
|
34
|
+
return Promise.reject(err);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async login(shortname: string, password: string) {
|
|
39
|
+
return login(shortname, password, this.client);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async get_profile() {
|
|
43
|
+
return get_profile(this.client);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async logout() {
|
|
47
|
+
return logout(this.client);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async query(q: QueryRequest, scope: string = "managed") {
|
|
51
|
+
return query(this.client, q, scope);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async retrieve_entry(
|
|
55
|
+
resource_type: ResourceType,
|
|
56
|
+
space_name: string,
|
|
57
|
+
subpath: string,
|
|
58
|
+
shortname: string,
|
|
59
|
+
retrieve_json_payload: boolean = false,
|
|
60
|
+
retrieve_attachments: boolean = false,
|
|
61
|
+
validate_schema: boolean = true,
|
|
62
|
+
scope: string = "managed"
|
|
63
|
+
) {
|
|
64
|
+
return retrieve_entry(
|
|
65
|
+
this.client,
|
|
66
|
+
resource_type,
|
|
67
|
+
space_name,
|
|
68
|
+
subpath,
|
|
69
|
+
shortname,
|
|
70
|
+
retrieve_json_payload,
|
|
71
|
+
retrieve_attachments,
|
|
72
|
+
validate_schema,
|
|
73
|
+
scope
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async upload_with_payload(
|
|
78
|
+
space_name: string,
|
|
79
|
+
subpath: string,
|
|
80
|
+
shortname: string,
|
|
81
|
+
resource_type: ResourceType,
|
|
82
|
+
payload_file: File,
|
|
83
|
+
content_type?: ContentType,
|
|
84
|
+
schema_shortname?: string,
|
|
85
|
+
scope: string = "managed"
|
|
86
|
+
) {
|
|
87
|
+
return upload_with_payload(
|
|
88
|
+
this.client,
|
|
89
|
+
space_name,
|
|
90
|
+
subpath,
|
|
91
|
+
shortname,
|
|
92
|
+
resource_type,
|
|
93
|
+
payload_file,
|
|
94
|
+
content_type,
|
|
95
|
+
schema_shortname,
|
|
96
|
+
scope
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async request(action: ActionRequest) {
|
|
101
|
+
return request(this.client, action);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async submit(
|
|
105
|
+
spaceName: string,
|
|
106
|
+
schemaShortname: string,
|
|
107
|
+
subpath: string,
|
|
108
|
+
record: any
|
|
109
|
+
) {
|
|
110
|
+
return submit(this.client, spaceName, schemaShortname, subpath, record);
|
|
111
|
+
}
|
|
112
|
+
}
|
package/config.ts
ADDED
package/dmart.service.ts
CHANGED
|
@@ -27,9 +27,12 @@ axios.interceptors.response.use(null, function (error) {
|
|
|
27
27
|
code: error.code,
|
|
28
28
|
status: error.status,
|
|
29
29
|
message: error.message,
|
|
30
|
-
request: {
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
request: {
|
|
31
|
+
url: error.response?.config?.url,
|
|
32
|
+
method: error.response?.config?.method,
|
|
33
|
+
},
|
|
34
|
+
response: error.response?.data,
|
|
35
|
+
};
|
|
33
36
|
return Promise.reject(err);
|
|
34
37
|
});
|
|
35
38
|
export const dmartClient = axios;
|
|
@@ -38,21 +41,17 @@ export class Dmart {
|
|
|
38
41
|
// static baseURL = "http://localhost:8282";
|
|
39
42
|
|
|
40
43
|
public static async login(shortname: string, password: string) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"Bearer " + data.records[0]?.attributes.access_token;
|
|
51
|
-
}
|
|
52
|
-
return data;
|
|
53
|
-
} catch (error: any) {
|
|
54
|
-
throw error;
|
|
44
|
+
const response = await axios.post<LoginResponse>(
|
|
45
|
+
`user/login`,
|
|
46
|
+
{ shortname, password },
|
|
47
|
+
{ headers }
|
|
48
|
+
);
|
|
49
|
+
const data: LoginResponse = response.data;
|
|
50
|
+
if (data.status == Status.success && data.records.length > 0) {
|
|
51
|
+
headers["Authorization"] =
|
|
52
|
+
"Bearer " + data.records[0]?.attributes.access_token;
|
|
55
53
|
}
|
|
54
|
+
return data;
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
public static async loginBy(credentials: any, password: string) {
|
|
@@ -358,6 +357,7 @@ export class Dmart {
|
|
|
358
357
|
}
|
|
359
358
|
}
|
|
360
359
|
|
|
360
|
+
// TODO: update to enums
|
|
361
361
|
public static async get_attachment_content(
|
|
362
362
|
resource_type: string,
|
|
363
363
|
space_name: string,
|
package/package.json
CHANGED
package/services/auth.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { Config } from '../config';
|
|
3
|
+
import {
|
|
4
|
+
ApiResponse,
|
|
5
|
+
headers,
|
|
6
|
+
LoginResponse,
|
|
7
|
+
ProfileResponse,
|
|
8
|
+
Status,
|
|
9
|
+
} from "../dmart.model";
|
|
10
|
+
|
|
11
|
+
export const login = async (
|
|
12
|
+
shortname: string,
|
|
13
|
+
password: string,
|
|
14
|
+
client: AxiosInstance
|
|
15
|
+
) => {
|
|
16
|
+
// use which client? defaultAxios if what?
|
|
17
|
+
const response = await client.post<LoginResponse>(
|
|
18
|
+
`user/login`,
|
|
19
|
+
{ shortname, password },
|
|
20
|
+
{ headers: Config.headers }
|
|
21
|
+
);
|
|
22
|
+
const data: LoginResponse = response.data;
|
|
23
|
+
if (data.status == Status.success && data.records.length > 0) {
|
|
24
|
+
headers["Authorization"] =
|
|
25
|
+
"Bearer " + data.records[0]?.attributes.access_token;
|
|
26
|
+
}
|
|
27
|
+
return data;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const get_profile = async (client: AxiosInstance) => {
|
|
31
|
+
const { data } = await client.get<ProfileResponse>(`user/profile`, {
|
|
32
|
+
headers: Config.headers,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const logout = async (client: AxiosInstance) => {
|
|
39
|
+
const { data } = await client.post<ApiResponse>(
|
|
40
|
+
`user/logout`,
|
|
41
|
+
{},
|
|
42
|
+
{ headers: Config.headers }
|
|
43
|
+
);
|
|
44
|
+
return data;
|
|
45
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { Config } from '../config';
|
|
3
|
+
import {
|
|
4
|
+
ApiQueryResponse,
|
|
5
|
+
QueryRequest,
|
|
6
|
+
QueryType,
|
|
7
|
+
ResourceType,
|
|
8
|
+
ResponseEntry,
|
|
9
|
+
SortyType,
|
|
10
|
+
} from "../dmart.model";
|
|
11
|
+
|
|
12
|
+
export const query = async (
|
|
13
|
+
client: AxiosInstance,
|
|
14
|
+
query: QueryRequest,
|
|
15
|
+
scope: string = "managed"
|
|
16
|
+
): Promise<ApiQueryResponse | null> => {
|
|
17
|
+
if (query.type !== QueryType.spaces) {
|
|
18
|
+
query.sort_type = query.sort_type || SortyType.ascending;
|
|
19
|
+
query.sort_by = query.sort_by || "created_at";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// bad!
|
|
23
|
+
query.subpath = query.subpath.replace(/\/+/g, "/");
|
|
24
|
+
const { data } = await client.post<ApiQueryResponse>(
|
|
25
|
+
`${scope}/query`,
|
|
26
|
+
query,
|
|
27
|
+
{
|
|
28
|
+
headers: Config.headers,
|
|
29
|
+
timeout: 3000,
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const retrieve_entry = async (
|
|
36
|
+
client: AxiosInstance,
|
|
37
|
+
resource_type: ResourceType,
|
|
38
|
+
space_name: string,
|
|
39
|
+
subpath: string,
|
|
40
|
+
shortname: string,
|
|
41
|
+
retrieve_json_payload: boolean = false,
|
|
42
|
+
retrieve_attachments: boolean = false,
|
|
43
|
+
validate_schema: boolean = true,
|
|
44
|
+
scope: string = "managed"
|
|
45
|
+
): Promise<ResponseEntry | null> => {
|
|
46
|
+
if (!subpath || subpath == "/") subpath = "__root__";
|
|
47
|
+
const url = `${scope}/entry/${resource_type}/${space_name}/${subpath}/${shortname}?retrieve_json_payload=${retrieve_json_payload}&retrieve_attachments=${retrieve_attachments}&validate_schema=${validate_schema}`;
|
|
48
|
+
const { data } = await client.get<ResponseEntry>(
|
|
49
|
+
`${url.replace(/\/+/g, "/")}`,
|
|
50
|
+
{ headers: Config.headers }
|
|
51
|
+
);
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { Config } from '../config';
|
|
3
|
+
import { ActionRequest, ActionResponse } from "../dmart.model";
|
|
4
|
+
|
|
5
|
+
export const request = async (
|
|
6
|
+
client: AxiosInstance,
|
|
7
|
+
action: ActionRequest
|
|
8
|
+
): Promise<ActionResponse> => {
|
|
9
|
+
const res = await client.post<ActionResponse>(`managed/request`, action, {
|
|
10
|
+
headers: Config.headers,
|
|
11
|
+
});
|
|
12
|
+
return res?.data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// this is useless
|
|
16
|
+
export const submit = async (
|
|
17
|
+
client: AxiosInstance,
|
|
18
|
+
spaceName: string,
|
|
19
|
+
schemaShortname: string,
|
|
20
|
+
subpath: string,
|
|
21
|
+
record: any
|
|
22
|
+
) => {
|
|
23
|
+
const { data } = await client.post(
|
|
24
|
+
`public/submit/${spaceName}/${schemaShortname}/${subpath}`,
|
|
25
|
+
record,
|
|
26
|
+
{ headers: Config.headers }
|
|
27
|
+
);
|
|
28
|
+
return data;
|
|
29
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { ApiResponse, ContentType, ResourceType } from "../dmart.model";
|
|
3
|
+
|
|
4
|
+
export const upload_with_payload = async (
|
|
5
|
+
client: AxiosInstance,
|
|
6
|
+
space_name: string,
|
|
7
|
+
subpath: string,
|
|
8
|
+
shortname: string,
|
|
9
|
+
resource_type: ResourceType,
|
|
10
|
+
payload_file: File,
|
|
11
|
+
content_type?: ContentType,
|
|
12
|
+
schema_shortname?: string,
|
|
13
|
+
scope: string = "managed"
|
|
14
|
+
): Promise<ApiResponse> => {
|
|
15
|
+
const request_record_body: any = {
|
|
16
|
+
resource_type,
|
|
17
|
+
subpath,
|
|
18
|
+
shortname,
|
|
19
|
+
attributes: { is_active: true, payload: { body: {} } },
|
|
20
|
+
};
|
|
21
|
+
if (content_type) {
|
|
22
|
+
request_record_body.attributes.payload.content_type = content_type;
|
|
23
|
+
}
|
|
24
|
+
if (schema_shortname) {
|
|
25
|
+
request_record_body.attributes.payload.schema_shortname = schema_shortname;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const request_record = new Blob([JSON.stringify(request_record_body)], {
|
|
29
|
+
type: "application/json",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const form_data = new FormData();
|
|
33
|
+
form_data.append("space_name", space_name);
|
|
34
|
+
form_data.append("request_record", request_record);
|
|
35
|
+
form_data.append("payload_file", payload_file);
|
|
36
|
+
|
|
37
|
+
const headers = { "Content-Type": "multipart/form-data" };
|
|
38
|
+
|
|
39
|
+
const { data } = await client.post<ApiResponse>(
|
|
40
|
+
`${scope}/resource_with_payload`,
|
|
41
|
+
form_data,
|
|
42
|
+
{ headers }
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return data;
|
|
46
|
+
};
|
package/services/user.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
import { Config } from '../config';
|
|
3
|
+
import { ApiResponse } from '../dmart.model';
|
|
4
|
+
|
|
5
|
+
export const create_user = async (client: AxiosInstance) => {
|
|
6
|
+
const { data } = await client.post<ApiResponse>(
|
|
7
|
+
`user/logout`,
|
|
8
|
+
{},
|
|
9
|
+
{ headers: Config.headers }
|
|
10
|
+
);
|
|
11
|
+
return data;
|
|
12
|
+
};
|
package/test.ts
CHANGED
|
@@ -1,46 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Dmart.baseURL = 'http://localhost:8282';
|
|
5
|
-
|
|
6
|
-
const r = await Dmart.login('dmart', 'Test1234')
|
|
7
|
-
console.log({r});
|
|
8
|
-
const request:any = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
const u = await Dmart.request(request);
|
|
46
|
-
console.log({u});
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
// Dmart.baseURL = 'http://localhost:8282';
|
|
5
|
+
//
|
|
6
|
+
// const r = await Dmart.login('dmart', 'Test1234')
|
|
7
|
+
// console.log({r});
|
|
8
|
+
// const request:any = {
|
|
9
|
+
// space_name: "management",
|
|
10
|
+
// request_type: "update",
|
|
11
|
+
// records: [
|
|
12
|
+
// {
|
|
13
|
+
// resource_type: "user",
|
|
14
|
+
// shortname: "zainebfrontend",
|
|
15
|
+
// subpath: "/users",
|
|
16
|
+
// attributes: {
|
|
17
|
+
// description: {
|
|
18
|
+
// ar: "desc ar",
|
|
19
|
+
// en: "HelloWorld en",
|
|
20
|
+
// ku: "desc ku"
|
|
21
|
+
// },
|
|
22
|
+
// displayname: {
|
|
23
|
+
// ar: "name ar",
|
|
24
|
+
// en: "myName en",
|
|
25
|
+
// ku: "name ku"
|
|
26
|
+
// },
|
|
27
|
+
// is_active: true,
|
|
28
|
+
// invitation: "",
|
|
29
|
+
// payload: {
|
|
30
|
+
// body: {
|
|
31
|
+
// email: "zainebb@gmail.com",
|
|
32
|
+
// first_name: "Jo",
|
|
33
|
+
// last_name: "Do",
|
|
34
|
+
// mobile: "792211703",
|
|
35
|
+
// language: "english"
|
|
36
|
+
// },
|
|
37
|
+
// content_type: "json",
|
|
38
|
+
// // schema_shortname: "user"
|
|
39
|
+
// },
|
|
40
|
+
// tags: []
|
|
41
|
+
// }
|
|
42
|
+
// }
|
|
43
|
+
// ]
|
|
44
|
+
// };
|
|
45
|
+
// const u = await Dmart.request(request);
|
|
46
|
+
// console.log({u});
|
|
47
|
+
|
|
48
|
+
import {Dmart, dmartClient} from "./dmart.service";
|
|
49
|
+
import {ContentType, ResourceType} from "./dmart.model";
|
|
50
|
+
import * as fs from "node:fs";
|
|
51
|
+
|
|
52
|
+
dmartClient.defaults.baseURL = 'https://nova.imx.sh/dmart';
|
|
53
|
+
|
|
54
|
+
Dmart.login('dmart', 'NdFVYdcsoo1M5u08').then(r => {
|
|
55
|
+
const myPDFBuffer = fs.readFileSync('/home/splimter/projects/zain/vvv/testpdf.pdf');
|
|
56
|
+
const myPDF = new File([myPDFBuffer], "testpdf.pdf", { type: "application/pdf" });
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
Dmart.upload_with_payload(
|
|
60
|
+
"referrals", `candidates/30304cab`, "auto",
|
|
61
|
+
ResourceType.media, myPDF, ContentType.pdf,
|
|
62
|
+
null, 'public'
|
|
63
|
+
).then(r=> {
|
|
64
|
+
console.log({r})
|
|
65
|
+
}).catch(e=> {
|
|
66
|
+
console.log({e})
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
|