@in.pulse-crm/sdk 1.0.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/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "tabWidth": 4,
3
+ "useTabs": true
4
+ }
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./src/sdks"), exports);
18
+ __exportStar(require("./src/types"), exports);
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const axios_1 = __importDefault(require("axios"));
7
+ class AuthSDK {
8
+ _api;
9
+ constructor(props) {
10
+ this._api = axios_1.default.create(props.axiosConfig);
11
+ }
12
+ async fetchSessionData(instance, token) {
13
+ const response = await this._api
14
+ .get(`/${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(`Failed to authenticate, status: ${error.response.status}`);
25
+ }
26
+ throw new Error(error.message);
27
+ });
28
+ return response.data.data;
29
+ }
30
+ async isAuthenticated(instance, token) {
31
+ try {
32
+ await this.fetchSessionData(instance, token);
33
+ return true;
34
+ }
35
+ catch {
36
+ return false;
37
+ }
38
+ }
39
+ async isAuthorized(instance, token, authorizedRoles) {
40
+ try {
41
+ const session = await this.fetchSessionData(instance, token);
42
+ return authorizedRoles.includes(session.role);
43
+ }
44
+ catch {
45
+ return false;
46
+ }
47
+ }
48
+ }
49
+ exports.default = AuthSDK;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InstanceSDK = exports.AuthSDK = exports.UserSDK = void 0;
7
+ const user_sdk_1 = __importDefault(require("./user.sdk"));
8
+ exports.UserSDK = user_sdk_1.default;
9
+ const auth_sdk_1 = __importDefault(require("./auth.sdk"));
10
+ exports.AuthSDK = auth_sdk_1.default;
11
+ const instance_sdk_1 = __importDefault(require("./instance.sdk"));
12
+ exports.InstanceSDK = instance_sdk_1.default;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const axios_1 = __importDefault(require("axios"));
7
+ class InstanceSDK {
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.data;
25
+ }
26
+ }
27
+ exports.default = InstanceSDK;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const axios_1 = __importDefault(require("axios"));
7
+ class UserSDK {
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
+ }
38
+ }
39
+ exports.default = UserSDK;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./src/sdks";
2
+ export * from "./src/types";
3
+
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@in.pulse-crm/sdk",
3
+ "version": "1.0.0",
4
+ "description": "SDKs for abstraction of api consumption of in.pulse-crm application",
5
+ "main": "./dist/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "in.pulse-crm"
12
+ },
13
+ "author": "Renan G. Dutra <r.granatodutra@gmail.com>",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "axios": "^1.8.1"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^22.13.8",
20
+ "prettier": "^3.5.3",
21
+ "typescript": "^5.8.2"
22
+ }
23
+ }
@@ -0,0 +1,59 @@
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;
@@ -0,0 +1,5 @@
1
+ import UserSDK from "./user.sdk";
2
+ import AuthSDK from "./auth.sdk";
3
+ import InstanceSDK from "./instance.sdk";
4
+
5
+ export { UserSDK, AuthSDK, InstanceSDK };
@@ -0,0 +1,32 @@
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;
@@ -0,0 +1,62 @@
1
+ import axios, { AxiosInstance } from "axios";
2
+ import {
3
+ User,
4
+ UserSDKOptions,
5
+ CreateUserDTO,
6
+ UpdateUserDTO,
7
+ } from "../types/user.types";
8
+ import { SuccessDataResponse } from "../types/response.types";
9
+
10
+ class UserSDK {
11
+ private readonly _api: AxiosInstance;
12
+
13
+ constructor(props: UserSDKOptions) {
14
+ this._api = axios.create(props.axiosConfig);
15
+ }
16
+
17
+ public async getUsers(instance: string) {
18
+ const response = await this._api.get<Array<User>>(`/${instance}/users`);
19
+
20
+ return response.data;
21
+ }
22
+
23
+ public async getUserById(instance: string, userId: string) {
24
+ const response = await this._api.get<User>(
25
+ `/${instance}/users/${userId}`,
26
+ );
27
+
28
+ return response.data;
29
+ }
30
+
31
+ public async createUser(instance: string, data: CreateUserDTO) {
32
+ try {
33
+ const response = await this._api.post<SuccessDataResponse<User>>(
34
+ `/${instance}/users`,
35
+ data,
36
+ );
37
+
38
+ return response.data;
39
+ } catch (error) {
40
+ throw new Error("Failed to create user", { cause: error });
41
+ }
42
+ }
43
+
44
+ public async updateUser(
45
+ instance: string,
46
+ userId: string,
47
+ data: UpdateUserDTO,
48
+ ) {
49
+ try {
50
+ const response = await this._api.patch<SuccessDataResponse<User>>(
51
+ `/${instance}/users/${userId}`,
52
+ data,
53
+ );
54
+
55
+ return response.data;
56
+ } catch (error) {
57
+ throw new Error("Failed to update user", { cause: error });
58
+ }
59
+ }
60
+ }
61
+
62
+ export default UserSDK;
@@ -0,0 +1,10 @@
1
+ import { CreateAxiosDefaults } from "axios";
2
+
3
+ export interface AuthSDKOptions {
4
+ axiosConfig: CreateAxiosDefaults;
5
+ }
6
+
7
+ export interface SessionData {
8
+ userId: number;
9
+ role: string;
10
+ }
@@ -0,0 +1,4 @@
1
+ import { CreateUserDTO, User, UserSDKOptions } from "./user.types";
2
+ import { AuthSDKOptions, SessionData } from "./auth.types";
3
+
4
+ export { CreateUserDTO, User, UserSDKOptions, AuthSDKOptions, SessionData };
@@ -0,0 +1,5 @@
1
+ import { CreateAxiosDefaults } from "axios";
2
+
3
+ export interface InstanceSDKOptions {
4
+ axiosConfig: CreateAxiosDefaults;
5
+ }
@@ -0,0 +1,4 @@
1
+ export interface SuccessDataResponse<T> {
2
+ message: string;
3
+ data: T;
4
+ }
@@ -0,0 +1,62 @@
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>;
package/tsconfig.json ADDED
@@ -0,0 +1,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": [
13
+ "index.ts",
14
+ "src/**/*.{ts}"
15
+ ]
16
+ }