@in.pulse-crm/sdk 1.0.2 → 1.0.4
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/dist/auth.sdk.d.ts +14 -0
- package/dist/auth.sdk.js +43 -53
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -5
- package/dist/instance.sdk.d.ts +10 -0
- package/dist/instance.sdk.js +21 -25
- package/dist/user.sdk.d.ts +14 -0
- package/dist/user.sdk.js +33 -36
- package/package.json +3 -2
- package/src/auth.sdk.ts +3 -11
- package/src/user.sdk.ts +1 -1
- package/tsconfig.json +7 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CreateAxiosDefaults } from "axios";
|
|
2
|
+
import { DataResponse, LoginData, SessionData } from "@in.pulse-crm/types";
|
|
3
|
+
interface AuthSDKOptions {
|
|
4
|
+
axiosConfig: CreateAxiosDefaults;
|
|
5
|
+
}
|
|
6
|
+
declare class AuthSDK {
|
|
7
|
+
private readonly _api;
|
|
8
|
+
constructor(props: AuthSDKOptions);
|
|
9
|
+
login(instance: string, login: string, password: string): Promise<DataResponse<LoginData>>;
|
|
10
|
+
fetchSessionData(instance: string, token: string): Promise<DataResponse<SessionData>>;
|
|
11
|
+
isAuthenticated(instance: string, token: string): Promise<boolean>;
|
|
12
|
+
isAuthorized(instance: string, token: string, authorizedRoles: string[]): Promise<boolean>;
|
|
13
|
+
}
|
|
14
|
+
export default AuthSDK;
|
package/dist/auth.sdk.js
CHANGED
|
@@ -1,59 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
-
};
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
7
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
6
|
const axios_1 = __importDefault(require("axios"));
|
|
7
|
+
const utils_1 = require("@in.pulse-crm/utils");
|
|
9
8
|
class AuthSDK {
|
|
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
|
-
|
|
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
|
-
}
|
|
9
|
+
_api;
|
|
10
|
+
constructor(props) {
|
|
11
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
12
|
+
}
|
|
13
|
+
async login(instance, login, password) {
|
|
14
|
+
const response = await this._api.post(`${instance}/login`, { LOGIN: login, SENHA: password });
|
|
15
|
+
return response.data;
|
|
16
|
+
}
|
|
17
|
+
async fetchSessionData(instance, token) {
|
|
18
|
+
const response = await this._api
|
|
19
|
+
.get(`/${instance}/auth`, {
|
|
20
|
+
headers: {
|
|
21
|
+
authorization: token,
|
|
22
|
+
},
|
|
23
|
+
})
|
|
24
|
+
.catch((error) => {
|
|
25
|
+
const message = (0, utils_1.sanitizeErrorMessage)(error);
|
|
26
|
+
throw new Error("Failed to fetch session data! " + message);
|
|
27
|
+
});
|
|
28
|
+
return response.data;
|
|
29
|
+
}
|
|
30
|
+
async isAuthenticated(instance, token) {
|
|
31
|
+
try {
|
|
32
|
+
const { data } = await this.fetchSessionData(instance, token);
|
|
33
|
+
return !!data.userId;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async isAuthorized(instance, token, authorizedRoles) {
|
|
40
|
+
try {
|
|
41
|
+
const { data } = await this.fetchSessionData(instance, token);
|
|
42
|
+
return authorizedRoles.includes(data.role);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
58
48
|
}
|
|
59
49
|
exports.default = AuthSDK;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
-
};
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
7
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
6
|
exports.InstanceSDK = exports.UserSDK = exports.AuthSDK = void 0;
|
|
9
7
|
const auth_sdk_1 = __importDefault(require("./auth.sdk"));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CreateAxiosDefaults } from "axios";
|
|
2
|
+
interface InstanceSDKOptions {
|
|
3
|
+
axiosConfig: CreateAxiosDefaults;
|
|
4
|
+
}
|
|
5
|
+
declare class InstanceSDK {
|
|
6
|
+
private readonly _api;
|
|
7
|
+
constructor(props: InstanceSDKOptions);
|
|
8
|
+
executeQuery<T>(instance: string, query: string, parameters: Array<any>): Promise<T>;
|
|
9
|
+
}
|
|
10
|
+
export default InstanceSDK;
|
package/dist/instance.sdk.js
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
-
};
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
7
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
6
|
const axios_1 = __importDefault(require("axios"));
|
|
9
7
|
class InstanceSDK {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return response.data.result;
|
|
29
|
-
}
|
|
8
|
+
_api;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
11
|
+
}
|
|
12
|
+
async executeQuery(instance, query, parameters) {
|
|
13
|
+
const response = await this._api
|
|
14
|
+
.post(`/${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(`Failed to execute query, status: ${error.response.status}`);
|
|
21
|
+
}
|
|
22
|
+
throw new Error(error.message);
|
|
23
|
+
});
|
|
24
|
+
return response.data.result;
|
|
25
|
+
}
|
|
30
26
|
}
|
|
31
27
|
exports.default = InstanceSDK;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CreateAxiosDefaults } from "axios";
|
|
2
|
+
import type { DataResponse, PaginatedResponse, User, CreateUserDTO, UpdateUserDTO } from "@in.pulse-crm/types";
|
|
3
|
+
interface UserSDKOptions {
|
|
4
|
+
axiosConfig: CreateAxiosDefaults;
|
|
5
|
+
}
|
|
6
|
+
declare class UserSDK {
|
|
7
|
+
private readonly _api;
|
|
8
|
+
constructor(props: UserSDKOptions);
|
|
9
|
+
getUsers(instance: string): Promise<PaginatedResponse<User>>;
|
|
10
|
+
getUserById(instance: string, userId: number): Promise<DataResponse<User>>;
|
|
11
|
+
createUser(instance: string, data: CreateUserDTO): Promise<DataResponse<User>>;
|
|
12
|
+
updateUser(instance: string, userId: string, data: UpdateUserDTO): Promise<DataResponse<User>>;
|
|
13
|
+
}
|
|
14
|
+
export default UserSDK;
|
package/dist/user.sdk.js
CHANGED
|
@@ -1,42 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
-
};
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
7
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
6
|
const axios_1 = __importDefault(require("axios"));
|
|
9
7
|
class UserSDK {
|
|
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
|
-
}
|
|
8
|
+
_api;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this._api = axios_1.default.create(props.axiosConfig);
|
|
11
|
+
}
|
|
12
|
+
async getUsers(instance) {
|
|
13
|
+
const response = await this._api.get(`/${instance}/users`);
|
|
14
|
+
return response.data;
|
|
15
|
+
}
|
|
16
|
+
async getUserById(instance, userId) {
|
|
17
|
+
const response = await this._api.get(`/${instance}/users/${userId}`);
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
async createUser(instance, data) {
|
|
21
|
+
try {
|
|
22
|
+
const response = await this._api.post(`/${instance}/users`, data);
|
|
23
|
+
return response.data;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new Error("Failed to create user", { cause: error });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async updateUser(instance, userId, data) {
|
|
30
|
+
try {
|
|
31
|
+
const response = await this._api.patch(`/${instance}/users/${userId}`, data);
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
throw new Error("Failed to update user", { cause: error });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
41
38
|
}
|
|
42
39
|
exports.default = UserSDK;
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@in.pulse-crm/sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "SDKs for abstraction of api consumption of in.pulse-crm application",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"publish": "npm publish --access public",
|
|
8
7
|
"build": "tsc",
|
|
9
8
|
"prettier": "prettier --write .",
|
|
10
9
|
"prettier:check": "prettier --check ."
|
|
@@ -16,9 +15,11 @@
|
|
|
16
15
|
"author": "Renan G. Dutra <r.granatodutra@gmail.com>",
|
|
17
16
|
"license": "ISC",
|
|
18
17
|
"dependencies": {
|
|
18
|
+
"@in.pulse-crm/utils": "^1.0.0",
|
|
19
19
|
"axios": "^1.8.1"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
+
"@in.pulse-crm/types": "^1.0.3",
|
|
22
23
|
"@types/node": "^22.13.8",
|
|
23
24
|
"prettier": "^3.5.3",
|
|
24
25
|
"typescript": "^5.8.2"
|
package/src/auth.sdk.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
|
|
2
2
|
import { DataResponse, LoginData, SessionData } from "@in.pulse-crm/types";
|
|
3
|
+
import { sanitizeErrorMessage } from "@in.pulse-crm/utils";
|
|
3
4
|
|
|
4
5
|
interface AuthSDKOptions {
|
|
5
6
|
axiosConfig: CreateAxiosDefaults;
|
|
@@ -29,17 +30,8 @@ class AuthSDK {
|
|
|
29
30
|
},
|
|
30
31
|
})
|
|
31
32
|
.catch((error) => {
|
|
32
|
-
|
|
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);
|
|
33
|
+
const message = sanitizeErrorMessage(error);
|
|
34
|
+
throw new Error("Failed to fetch session data! " + message);
|
|
43
35
|
});
|
|
44
36
|
|
|
45
37
|
return response.data;
|
package/src/user.sdk.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -7,7 +7,11 @@
|
|
|
7
7
|
"strict": true /* Enable all strict type-checking options. */,
|
|
8
8
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
|
|
9
9
|
"noUnusedLocals": false,
|
|
10
|
-
"outDir": "./dist" /* Redirect output structure to the directory. */
|
|
10
|
+
"outDir": "./dist", /* Redirect output structure to the directory. */
|
|
11
|
+
"declaration": true
|
|
11
12
|
},
|
|
12
|
-
"include": [
|
|
13
|
-
|
|
13
|
+
"include": [
|
|
14
|
+
"src/index.ts",
|
|
15
|
+
"src/**/*.{ts}"
|
|
16
|
+
]
|
|
17
|
+
}
|