@in.pulse-crm/sdk 1.0.0 → 1.0.2
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/.prettierrc +4 -4
- package/dist/auth.sdk.js +59 -0
- package/dist/index.js +12 -16
- package/dist/instance.sdk.js +31 -0
- package/dist/src/sdks/auth.sdk.js +46 -43
- package/dist/src/sdks/index.js +5 -3
- package/dist/src/sdks/instance.sdk.js +25 -21
- package/dist/src/sdks/user.sdk.js +36 -33
- package/dist/user.sdk.js +42 -0
- package/package.json +5 -2
- package/src/auth.sdk.ts +73 -0
- package/src/{sdks/index.ts → index.ts} +2 -2
- package/src/instance.sdk.ts +38 -0
- package/src/{sdks/user.sdk.ts → user.sdk.ts} +15 -9
- package/tsconfig.json +13 -16
- package/index.ts +0 -3
- package/src/sdks/auth.sdk.ts +0 -59
- package/src/sdks/instance.sdk.ts +0 -32
- package/src/types/auth.types.ts +0 -10
- package/src/types/index.ts +0 -4
- package/src/types/instance.types.ts +0 -5
- package/src/types/response.types.ts +0 -4
- package/src/types/user.types.ts +0 -62
package/.prettierrc
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"tabWidth": 4,
|
|
3
|
+
"useTabs": true
|
|
4
|
+
}
|
package/dist/auth.sdk.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
class AuthSDK {
|
|
10
|
+
_api;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
async login(instance, login, password) {
|
|
15
|
+
const response = await this._api.post(`${instance}/login`, {
|
|
16
|
+
LOGIN: login,
|
|
17
|
+
SENHA: password,
|
|
18
|
+
});
|
|
19
|
+
return response.data;
|
|
20
|
+
}
|
|
21
|
+
async fetchSessionData(instance, token) {
|
|
22
|
+
const response = await this._api
|
|
23
|
+
.get(`/${instance}/auth`, {
|
|
24
|
+
headers: {
|
|
25
|
+
authorization: token,
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
.catch((error) => {
|
|
29
|
+
console.error(error.response?.data?.error);
|
|
30
|
+
if (error.response?.data?.message) {
|
|
31
|
+
throw new Error(error.response.data.message);
|
|
32
|
+
}
|
|
33
|
+
if (error.response?.status) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Failed to authenticate, status: ${error.response.status}`,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
throw new Error(error.message);
|
|
39
|
+
});
|
|
40
|
+
return response.data;
|
|
41
|
+
}
|
|
42
|
+
async isAuthenticated(instance, token) {
|
|
43
|
+
try {
|
|
44
|
+
const { data } = await this.fetchSessionData(instance, token);
|
|
45
|
+
return !!data.userId;
|
|
46
|
+
} catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async isAuthorized(instance, token, authorizedRoles) {
|
|
51
|
+
try {
|
|
52
|
+
const { data } = await this.fetchSessionData(instance, token);
|
|
53
|
+
return authorizedRoles.includes(data.role);
|
|
54
|
+
} catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.default = AuthSDK;
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
16
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
exports.InstanceSDK = exports.UserSDK = exports.AuthSDK = void 0;
|
|
9
|
+
const auth_sdk_1 = __importDefault(require("./auth.sdk"));
|
|
10
|
+
exports.AuthSDK = auth_sdk_1.default;
|
|
11
|
+
const user_sdk_1 = __importDefault(require("./user.sdk"));
|
|
12
|
+
exports.UserSDK = user_sdk_1.default;
|
|
13
|
+
const instance_sdk_1 = __importDefault(require("./instance.sdk"));
|
|
14
|
+
exports.InstanceSDK = instance_sdk_1.default;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
class InstanceSDK {
|
|
10
|
+
_api;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
async executeQuery(instance, query, parameters) {
|
|
15
|
+
const response = await this._api
|
|
16
|
+
.post(`/${instance}/query`, { query, parameters })
|
|
17
|
+
.catch((error) => {
|
|
18
|
+
if (error.response?.data?.message) {
|
|
19
|
+
throw new Error(error.response.data.message);
|
|
20
|
+
}
|
|
21
|
+
if (error.response?.status) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Failed to execute query, status: ${error.response.status}`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
throw new Error(error.message);
|
|
27
|
+
});
|
|
28
|
+
return response.data.result;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.default = InstanceSDK;
|
|
@@ -1,49 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
const axios_1 = __importDefault(require("axios"));
|
|
7
9
|
class AuthSDK {
|
|
8
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
10
|
+
_api;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
async fetchSessionData(instance, token) {
|
|
15
|
+
const response = await this._api
|
|
16
|
+
.get(`/${instance}/auth`, {
|
|
17
|
+
headers: {
|
|
18
|
+
Authorization: `Bearer ${token}`,
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
.catch((error) => {
|
|
22
|
+
console.error(error);
|
|
23
|
+
if (error.response?.data?.message) {
|
|
24
|
+
throw new Error(error.response.data.message);
|
|
25
|
+
}
|
|
26
|
+
if (error.response?.status) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Failed to authenticate, status: ${error.response.status}`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
throw new Error(error.message);
|
|
32
|
+
});
|
|
33
|
+
return response.data.data;
|
|
34
|
+
}
|
|
35
|
+
async isAuthenticated(instance, token) {
|
|
36
|
+
try {
|
|
37
|
+
await this.fetchSessionData(instance, token);
|
|
38
|
+
return true;
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async isAuthorized(instance, token, authorizedRoles) {
|
|
44
|
+
try {
|
|
45
|
+
const session = await this.fetchSessionData(instance, token);
|
|
46
|
+
return authorizedRoles.includes(session.role);
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
48
51
|
}
|
|
49
52
|
exports.default = AuthSDK;
|
package/dist/src/sdks/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
exports.InstanceSDK = exports.AuthSDK = exports.UserSDK = void 0;
|
|
7
9
|
const user_sdk_1 = __importDefault(require("./user.sdk"));
|
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
const axios_1 = __importDefault(require("axios"));
|
|
7
9
|
class InstanceSDK {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
_api;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
async executeQuery(instance, query, parameters) {
|
|
15
|
+
const response = await this._api
|
|
16
|
+
.post(`/${instance}/query`, { query, parameters })
|
|
17
|
+
.catch((error) => {
|
|
18
|
+
if (error.response?.data?.message) {
|
|
19
|
+
throw new Error(error.response.data.message);
|
|
20
|
+
}
|
|
21
|
+
if (error.response?.status) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Failed to execute query, status: ${error.response.status}`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
throw new Error(error.message);
|
|
27
|
+
});
|
|
28
|
+
return response.data.result;
|
|
29
|
+
}
|
|
26
30
|
}
|
|
27
31
|
exports.default = InstanceSDK;
|
|
@@ -1,39 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
const axios_1 = __importDefault(require("axios"));
|
|
7
9
|
class UserSDK {
|
|
8
|
-
|
|
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
|
-
|
|
10
|
+
_api;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
async getUsers(instance) {
|
|
15
|
+
const response = await this._api.get(`/${instance}/users`);
|
|
16
|
+
return response.data;
|
|
17
|
+
}
|
|
18
|
+
async getUserById(instance, userId) {
|
|
19
|
+
const response = await this._api.get(`/${instance}/users/${userId}`);
|
|
20
|
+
return response.data;
|
|
21
|
+
}
|
|
22
|
+
async createUser(instance, data) {
|
|
23
|
+
try {
|
|
24
|
+
const response = await this._api.post(`/${instance}/users`, data);
|
|
25
|
+
return response.data;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new Error("Failed to create user", { cause: error });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async updateUser(instance, userId, data) {
|
|
31
|
+
try {
|
|
32
|
+
const response = await this._api.patch(
|
|
33
|
+
`/${instance}/users/${userId}`,
|
|
34
|
+
data,
|
|
35
|
+
);
|
|
36
|
+
return response.data;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error("Failed to update user", { cause: error });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
38
41
|
}
|
|
39
42
|
exports.default = UserSDK;
|
package/dist/user.sdk.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
class UserSDK {
|
|
10
|
+
_api;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
async getUsers(instance) {
|
|
15
|
+
const response = await this._api.get(`/${instance}/users`);
|
|
16
|
+
return response.data;
|
|
17
|
+
}
|
|
18
|
+
async getUserById(instance, userId) {
|
|
19
|
+
const response = await this._api.get(`/${instance}/users/${userId}`);
|
|
20
|
+
return response.data;
|
|
21
|
+
}
|
|
22
|
+
async createUser(instance, data) {
|
|
23
|
+
try {
|
|
24
|
+
const response = await this._api.post(`/${instance}/users`, data);
|
|
25
|
+
return response.data;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new Error("Failed to create user", { cause: error });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async updateUser(instance, userId, data) {
|
|
31
|
+
try {
|
|
32
|
+
const response = await this._api.patch(
|
|
33
|
+
`/${instance}/users/${userId}`,
|
|
34
|
+
data,
|
|
35
|
+
);
|
|
36
|
+
return response.data;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error("Failed to update user", { cause: error });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.default = UserSDK;
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@in.pulse-crm/sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "SDKs for abstraction of api consumption of in.pulse-crm application",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"
|
|
7
|
+
"publish": "npm publish --access public",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prettier": "prettier --write .",
|
|
10
|
+
"prettier:check": "prettier --check ."
|
|
8
11
|
},
|
|
9
12
|
"repository": {
|
|
10
13
|
"type": "git",
|
package/src/auth.sdk.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
|
|
2
|
+
import { DataResponse, LoginData, SessionData } from "@in.pulse-crm/types";
|
|
3
|
+
|
|
4
|
+
interface AuthSDKOptions {
|
|
5
|
+
axiosConfig: CreateAxiosDefaults;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
class AuthSDK {
|
|
9
|
+
private readonly _api: AxiosInstance;
|
|
10
|
+
|
|
11
|
+
constructor(props: AuthSDKOptions) {
|
|
12
|
+
this._api = axios.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async login(instance: string, login: string, password: string) {
|
|
16
|
+
const response = await this._api.post<DataResponse<LoginData>>(
|
|
17
|
+
`${instance}/login`,
|
|
18
|
+
{ LOGIN: login, SENHA: password },
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
return response.data;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async fetchSessionData(instance: string, token: string) {
|
|
25
|
+
const response = await this._api
|
|
26
|
+
.get<DataResponse<SessionData>>(`/${instance}/auth`, {
|
|
27
|
+
headers: {
|
|
28
|
+
authorization: token,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
.catch((error) => {
|
|
32
|
+
console.error(error.response?.data?.error);
|
|
33
|
+
|
|
34
|
+
if (error.response?.data?.message) {
|
|
35
|
+
throw new Error(error.response.data.message);
|
|
36
|
+
}
|
|
37
|
+
if (error.response?.status) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Failed to authenticate, status: ${error.response.status}`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
throw new Error(error.message);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return response.data;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async isAuthenticated(instance: string, token: string) {
|
|
49
|
+
try {
|
|
50
|
+
const { data } = await this.fetchSessionData(instance, token);
|
|
51
|
+
|
|
52
|
+
return !!data.userId;
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public async isAuthorized(
|
|
59
|
+
instance: string,
|
|
60
|
+
token: string,
|
|
61
|
+
authorizedRoles: string[],
|
|
62
|
+
) {
|
|
63
|
+
try {
|
|
64
|
+
const { data } = await this.fetchSessionData(instance, token);
|
|
65
|
+
|
|
66
|
+
return authorizedRoles.includes(data.role);
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default AuthSDK;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
|
|
2
|
+
import { QueryResponse } from "@in.pulse-crm/types";
|
|
3
|
+
|
|
4
|
+
interface InstanceSDKOptions {
|
|
5
|
+
axiosConfig: CreateAxiosDefaults;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
class InstanceSDK {
|
|
9
|
+
private readonly _api: AxiosInstance;
|
|
10
|
+
|
|
11
|
+
constructor(props: InstanceSDKOptions) {
|
|
12
|
+
this._api = axios.create(props.axiosConfig);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async executeQuery<T>(
|
|
16
|
+
instance: string,
|
|
17
|
+
query: string,
|
|
18
|
+
parameters: Array<any>,
|
|
19
|
+
) {
|
|
20
|
+
const response = await this._api
|
|
21
|
+
.post<QueryResponse<T>>(`/${instance}/query`, { query, parameters })
|
|
22
|
+
.catch((error) => {
|
|
23
|
+
if (error.response?.data?.message) {
|
|
24
|
+
throw new Error(error.response.data.message);
|
|
25
|
+
}
|
|
26
|
+
if (error.response?.status) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Failed to execute query, status: ${error.response.status}`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
throw new Error(error.message);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return response.data.result;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default InstanceSDK;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from "axios";
|
|
1
|
+
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
|
|
2
2
|
import {
|
|
3
|
+
DataResponse,
|
|
4
|
+
PaginatedResponse,
|
|
3
5
|
User,
|
|
4
|
-
UserSDKOptions,
|
|
5
6
|
CreateUserDTO,
|
|
6
7
|
UpdateUserDTO,
|
|
7
|
-
} from "
|
|
8
|
-
|
|
8
|
+
} from "@in.pulse-crm/types";
|
|
9
|
+
|
|
10
|
+
interface UserSDKOptions {
|
|
11
|
+
axiosConfig: CreateAxiosDefaults;
|
|
12
|
+
}
|
|
9
13
|
|
|
10
14
|
class UserSDK {
|
|
11
15
|
private readonly _api: AxiosInstance;
|
|
@@ -15,13 +19,15 @@ class UserSDK {
|
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
public async getUsers(instance: string) {
|
|
18
|
-
const response = await this._api.get<
|
|
22
|
+
const response = await this._api.get<PaginatedResponse<User>>(
|
|
23
|
+
`/${instance}/users`,
|
|
24
|
+
);
|
|
19
25
|
|
|
20
26
|
return response.data;
|
|
21
27
|
}
|
|
22
28
|
|
|
23
|
-
public async getUserById(instance: string, userId:
|
|
24
|
-
const response = await this._api.get<User
|
|
29
|
+
public async getUserById(instance: string, userId: number) {
|
|
30
|
+
const response = await this._api.get<DataResponse<User>>(
|
|
25
31
|
`/${instance}/users/${userId}`,
|
|
26
32
|
);
|
|
27
33
|
|
|
@@ -30,7 +36,7 @@ class UserSDK {
|
|
|
30
36
|
|
|
31
37
|
public async createUser(instance: string, data: CreateUserDTO) {
|
|
32
38
|
try {
|
|
33
|
-
const response = await this._api.post<
|
|
39
|
+
const response = await this._api.post<DataResponse<User>>(
|
|
34
40
|
`/${instance}/users`,
|
|
35
41
|
data,
|
|
36
42
|
);
|
|
@@ -47,7 +53,7 @@ class UserSDK {
|
|
|
47
53
|
data: UpdateUserDTO,
|
|
48
54
|
) {
|
|
49
55
|
try {
|
|
50
|
-
const response = await this._api.patch<
|
|
56
|
+
const response = await this._api.patch<DataResponse<User>>(
|
|
51
57
|
`/${instance}/users/${userId}`,
|
|
52
58
|
data,
|
|
53
59
|
);
|
package/tsconfig.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"src/**/*.{ts}"
|
|
15
|
-
]
|
|
16
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
4
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
|
5
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
6
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
7
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
8
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
|
|
9
|
+
"noUnusedLocals": false,
|
|
10
|
+
"outDir": "./dist" /* Redirect output structure to the directory. */
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/index.ts", "src/**/*.{ts}"]
|
|
13
|
+
}
|
package/index.ts
DELETED
package/src/sdks/auth.sdk.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from "axios";
|
|
2
|
-
import { AuthSDKOptions, SessionData } from "../types/auth.types";
|
|
3
|
-
import { SuccessDataResponse } from "../types/response.types";
|
|
4
|
-
|
|
5
|
-
class AuthSDK {
|
|
6
|
-
private readonly _api: AxiosInstance;
|
|
7
|
-
|
|
8
|
-
constructor(props: AuthSDKOptions) {
|
|
9
|
-
this._api = axios.create(props.axiosConfig);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
private async fetchSessionData(instance: string, token: string) {
|
|
13
|
-
const response = await this._api
|
|
14
|
-
.get<SuccessDataResponse<SessionData>>(`/${instance}/auth`, {
|
|
15
|
-
headers: {
|
|
16
|
-
Authorization: `Bearer ${token}`,
|
|
17
|
-
},
|
|
18
|
-
})
|
|
19
|
-
.catch((error) => {
|
|
20
|
-
if (error.response?.data?.message) {
|
|
21
|
-
throw new Error(error.response.data.message);
|
|
22
|
-
}
|
|
23
|
-
if (error.response?.status) {
|
|
24
|
-
throw new Error(
|
|
25
|
-
`Failed to authenticate, status: ${error.response.status}`,
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
throw new Error(error.message);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
return response.data.data;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public async isAuthenticated(instance: string, token: string) {
|
|
35
|
-
try {
|
|
36
|
-
await this.fetchSessionData(instance, token);
|
|
37
|
-
|
|
38
|
-
return true;
|
|
39
|
-
} catch {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public async isAuthorized(
|
|
45
|
-
instance: string,
|
|
46
|
-
token: string,
|
|
47
|
-
authorizedRoles: string[],
|
|
48
|
-
) {
|
|
49
|
-
try {
|
|
50
|
-
const session = await this.fetchSessionData(instance, token);
|
|
51
|
-
|
|
52
|
-
return authorizedRoles.includes(session.role);
|
|
53
|
-
} catch {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export default AuthSDK;
|
package/src/sdks/instance.sdk.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from "axios";
|
|
2
|
-
import { InstanceSDKOptions } from "../types/instance.types";
|
|
3
|
-
import { SuccessDataResponse } from "../types/response.types";
|
|
4
|
-
|
|
5
|
-
class InstanceSDK {
|
|
6
|
-
private readonly _api: AxiosInstance;
|
|
7
|
-
|
|
8
|
-
constructor(props: InstanceSDKOptions) {
|
|
9
|
-
this._api = axios.create(props.axiosConfig);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public async executeQuery<T>(instance: string, query: string, parameters: Array<any>) {
|
|
13
|
-
const response = await this._api
|
|
14
|
-
.post<SuccessDataResponse<T>>(`/${instance}/query`, { query, parameters })
|
|
15
|
-
.catch((error) => {
|
|
16
|
-
if (error.response?.data?.message) {
|
|
17
|
-
throw new Error(error.response.data.message);
|
|
18
|
-
}
|
|
19
|
-
if (error.response?.status) {
|
|
20
|
-
throw new Error(
|
|
21
|
-
`Failed to execute query, status: ${error.response.status}`,
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
throw new Error(error.message);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
return response.data.data;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export default InstanceSDK;
|
package/src/types/auth.types.ts
DELETED
package/src/types/index.ts
DELETED
package/src/types/user.types.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { CreateAxiosDefaults } from "axios";
|
|
2
|
-
|
|
3
|
-
export interface User {
|
|
4
|
-
CODIGO: number;
|
|
5
|
-
ATIVO: "SIM" | "NAO" | null;
|
|
6
|
-
NOME: string;
|
|
7
|
-
LOGIN: string;
|
|
8
|
-
EMAIl: string;
|
|
9
|
-
NIVEL: "ATIVO" | "RECEP" | "AMBOS" | "ADMIN" | null;
|
|
10
|
-
HORARIO: number;
|
|
11
|
-
DATACAD: Date | null;
|
|
12
|
-
SETOR: number;
|
|
13
|
-
NOME_EXIBICAO: string | null;
|
|
14
|
-
CODIGO_ERP: string;
|
|
15
|
-
SETOR_NOME: string;
|
|
16
|
-
SENHA?: string | null;
|
|
17
|
-
EXPIRA_EM: Date | null;
|
|
18
|
-
ALTERA_SENHA: "SIM" | "NAO" | null;
|
|
19
|
-
EDITA_CONTATOS: "SIM" | "NAO" | null;
|
|
20
|
-
VISUALIZA_COMPRAS: "SIM" | "NAO" | null;
|
|
21
|
-
CADASTRO: "TOTAL" | "NULOS" | null;
|
|
22
|
-
LOGADO: number | null;
|
|
23
|
-
ULTIMO_LOGIN_INI: Date | null;
|
|
24
|
-
ULTIMO_LOGIN_FIM: Date | null;
|
|
25
|
-
CODTELEFONIA: string;
|
|
26
|
-
AGENDA_LIG: "SIM" | "NAO";
|
|
27
|
-
LIGA_REPRESENTANTE: "SIM" | "NAO";
|
|
28
|
-
BANCO: string;
|
|
29
|
-
FILTRA_DDD: "SIM" | "NAO";
|
|
30
|
-
FILTRA_ESTADO: "SIM" | "NAO";
|
|
31
|
-
ASTERISK_RAMAL: string | null;
|
|
32
|
-
ASTERISK_USERID: string | null;
|
|
33
|
-
ASTERISK_LOGIN: string | null;
|
|
34
|
-
ASTERISK_SENHA: string | null;
|
|
35
|
-
CODEC: string | null;
|
|
36
|
-
ASSINATURA_EMAIL: string | null; // VARCHAR 255
|
|
37
|
-
LIGA_REPRESENTANTE_DIAS: number | null;
|
|
38
|
-
EMAILOPERADOR: string | null; // VARCHAR(100)
|
|
39
|
-
SENHAEMAILOPERADOR: string | null; // CHAR(30)
|
|
40
|
-
EMAIL_EXIBICAO: string | null; // VARCHAR(40)
|
|
41
|
-
limite_diario_agendamento: number | null;
|
|
42
|
-
OMNI: number | null;
|
|
43
|
-
CAMINHO_DATABASE: string | null;
|
|
44
|
-
IDCAMPANHA_WEON: string | null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface UserSDKOptions {
|
|
48
|
-
axiosConfig: CreateAxiosDefaults;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface CreateUserDTO {
|
|
52
|
-
NOME: string;
|
|
53
|
-
LOGIN: string;
|
|
54
|
-
SENHA: string;
|
|
55
|
-
NIVEL: "ATIVO" | "RECEP" | "AMBOS" | "ADMIN";
|
|
56
|
-
SETOR: number;
|
|
57
|
-
EMAIL?: string;
|
|
58
|
-
NOME_EXIBICAO?: string;
|
|
59
|
-
CODIGO_ERP?: string;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type UpdateUserDTO = Partial<CreateUserDTO>;
|