ixc-orm 1.1.2 → 1.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.
@@ -0,0 +1 @@
1
+ {"root":["../src/ixcclient.ts","../src/index.ts","../src/request.ts","../src/types.ts"],"version":"5.6.2"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ixc-orm",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "MIT",
@@ -28,13 +28,13 @@
28
28
  "url": "https://github.com/SousaFelipe/ixc-orm/issues"
29
29
  },
30
30
  "dependencies": {
31
- "axios": "^1.7.5",
31
+ "axios": "^1.7.7",
32
32
  "dotenv": "^16.4.5"
33
33
  },
34
34
  "devDependencies": {
35
- "@types/node": "^22.5.0",
36
- "nodemon": "^3.1.4",
35
+ "@types/node": "^22.5.5",
36
+ "nodemon": "^3.1.5",
37
37
  "ts-node": "^10.9.2",
38
- "typescript": "^5.5.4"
38
+ "typescript": "^5.6.2"
39
39
  }
40
40
  }
package/src/IXCClient.ts DELETED
@@ -1,184 +0,0 @@
1
- import { AxiosError } from 'axios';
2
- import { createAxiosInstance, createRequestPayload } from './request';
3
- import { IXCOperator, IXCOptions, IXCQuery, IXCResponse, IXCSortOrder } from './types';
4
-
5
-
6
-
7
- export default abstract class IXCClient {
8
-
9
-
10
- protected table: string;
11
- protected params: IXCQuery[];
12
- protected options: IXCOptions;
13
-
14
-
15
- /**
16
- *
17
- * @param table O nome da tabela correspondente ao banco de dados do seu servidor IXC
18
- * @see {@link https://wikiapiprovedor.ixcsoft.com.br/index.php}
19
- */
20
- constructor(table: string) {
21
-
22
- this.table = table;
23
- this.params = [];
24
-
25
- this.options = {
26
- page: 1,
27
- rowsPerPage: 20,
28
- sortName: 'id',
29
- sortOrder: 'asc'
30
- };
31
- }
32
-
33
-
34
- /**
35
- *
36
- * @param whereClauses Um array de strings, no formato [coluna, operador, valor]
37
- * Obs: se você passar um array no formato [coluna, valor] o operador será considerado como '='
38
- * Operadores válidos: =, !=, >, <, >=, <=, LIKE
39
- * @returns A própria instância
40
- */
41
- where(whereClauses: string[]) : IXCClient {
42
-
43
- if (whereClauses.length > 3) {
44
- throw new Error(
45
- `> O array de cláusulas não pode conter mais de 3 elementos.`
46
- );
47
- }
48
-
49
- const [
50
- alwaysColumn,
51
- operatorOrValue,
52
- valueOrUndefined
53
- ] = whereClauses;
54
-
55
- const availableOperators = Object.keys(IXCOperator);
56
-
57
- if (whereClauses.length > 2 && !availableOperators.includes(operatorOrValue)) {
58
- throw new Error(
59
- `> O operador ${ operatorOrValue }, não faz parte dos operadores válidos: ${ availableOperators }.`
60
- );
61
- }
62
-
63
- this.params.push({
64
- TB: alwaysColumn,
65
- OP: valueOrUndefined ? operatorOrValue : '=',
66
- P: valueOrUndefined ? valueOrUndefined : operatorOrValue
67
- });
68
-
69
- return this;
70
- }
71
-
72
-
73
- /**
74
- *
75
- * @param column A coluna que será usada para ordenar a busca
76
- * @param order A ordem da busca ('asc'ou 'desc')
77
- * @returns A própria instância
78
- */
79
- orderBy(column: string, order: keyof typeof IXCSortOrder) : IXCClient {
80
- this.options.sortName = column;
81
- this.options.sortOrder = order;
82
- return this;
83
- }
84
-
85
-
86
- /**
87
- *
88
- * @param pg O número da página que será solicitada ao IXC
89
- * @param rows A quantidade de linhas (registros) por página
90
- * @returns Promise<null | IXCResponse | AxiosError>
91
- */
92
- async get(pg?: number, rows?: number) : Promise<null | IXCResponse | AxiosError> {
93
-
94
- const { page, rowsPerPage, ...rest } = this.options;
95
-
96
- const opts = {
97
- page: pg ?? page,
98
- rowsPerPage: rows ?? rowsPerPage,
99
- ...rest
100
- };
101
-
102
- const axios = createAxiosInstance('GET');
103
- const payload = createRequestPayload(this.table, this.params, opts);
104
-
105
- try {
106
- const response = await axios.get<IXCResponse>(this.table, payload);
107
- return response.data;
108
- }
109
- catch (error: any) {
110
- console.error(error);
111
-
112
- if (error instanceof AxiosError) {
113
- return error;
114
- }
115
-
116
- return null;
117
- }
118
- finally {
119
- this.params = [];
120
- this.options = {
121
- page: 1,
122
- rowsPerPage: 20,
123
- sortName: 'id',
124
- sortOrder: 'asc'
125
- };
126
- }
127
- }
128
-
129
-
130
- /**
131
- *
132
- * @param body Um objeto no formado "chave: valor" contendo as informações do novo registro
133
- * a ser inserido no banco de dados do seu servidor IXC
134
- * @returns Promise<null | IXCResponse | AxiosError>
135
- */
136
- async post(body?: { [key: string]: any }) : Promise<null | IXCResponse | AxiosError> {
137
- const axios = createAxiosInstance('POST');
138
-
139
- try {
140
- const response = await axios.post<IXCResponse>(this.table, { data: body });
141
- return response.data;
142
- }
143
- catch (error: any) {
144
- console.error(error);
145
-
146
- if (error instanceof AxiosError) {
147
- return error;
148
- }
149
-
150
- return null;
151
- }
152
- finally {
153
- this.params = [];
154
- }
155
- }
156
-
157
-
158
- /**
159
- *
160
- * @param id O id do registro que será alterado
161
- * @param body Um objeto no formado "chave : valor" contendo as colunas que serão alteradas
162
- * @returns Promise<null | IXCResponse | AxiosError>
163
- */
164
- async put(id: number, body?: { [key: string]: any }) : Promise<null | IXCResponse | AxiosError> {
165
- const axios = createAxiosInstance('PUT');
166
-
167
- try {
168
- const response = await axios.put<IXCResponse>(`${ this.table }/${ id }`, { data: body });
169
- return response.data;
170
- }
171
- catch (error: any) {
172
- console.error(error);
173
-
174
- if (error instanceof AxiosError) {
175
- return error;
176
- }
177
-
178
- return null;
179
- }
180
- finally {
181
- this.params = [];
182
- }
183
- }
184
- }
package/src/index.ts DELETED
@@ -1,37 +0,0 @@
1
- import path from 'path';
2
- import dotenv from 'dotenv';
3
-
4
- import IXCClient from './IXCClient';
5
-
6
- import {
7
- IXCOperator,
8
- IXCOptions,
9
- IXCQuery,
10
- IXCRequest,
11
- IXCRequestMethods,
12
- IXCResponse,
13
- IXCSortOrder
14
- } from './types';
15
-
16
-
17
-
18
- const env = dotenv.config({
19
- path: path.join(__dirname, `../.env`)
20
- });
21
-
22
- if (env.error) {
23
- console.error(env.error);
24
- process.exit(-1);
25
- }
26
-
27
-
28
- export {
29
- IXCClient,
30
- IXCOperator,
31
- IXCOptions,
32
- IXCQuery,
33
- IXCRequest,
34
- IXCRequestMethods,
35
- IXCResponse,
36
- IXCSortOrder
37
- };
package/src/request.ts DELETED
@@ -1,77 +0,0 @@
1
- import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
2
- import { IXCOptions, IXCQuery, IXCRequest, IXCRequestMethods } from './types';
3
-
4
-
5
- /**
6
- *
7
- * @param method GET | POST | PUT
8
- * @returns A instância de um objeto do tipo AxiosInstance, pré-configurado com os cabeçalhos necessários
9
- */
10
- export function createAxiosInstance(method: keyof typeof IXCRequestMethods = 'GET') : AxiosInstance {
11
-
12
- const host = process.env.IXC_HOST;
13
- const token = process.env.IXC_TOKEN;
14
-
15
- return axios.create({
16
- method: method,
17
- baseURL: host ?? 'http://127.0.0.1/webservice/v1',
18
- headers: {
19
- 'ixcsoft': (method === 'GET') ? 'listar' : '',
20
- 'Content-Type': 'application/json',
21
- 'Authorization': `Basic ${ Buffer.from(token ?? '').toString('base64') }`
22
- }
23
- });
24
- }
25
-
26
-
27
- /**
28
- *
29
- * @param table Nome da tabela do IXC onde será feita a busca, atualização, inserção ou remoção
30
- * @param params Parâmetros da busca (desconciderado quando a ação é a de inserir novos registros)
31
- * @param options Parâmetros de formatação dos dados da responsta (página, ítens por página e ordenação)
32
- * @returns
33
- */
34
- export function createRequestPayload(
35
- table: string,
36
- params: IXCQuery | IXCQuery[],
37
- options?: IXCOptions
38
- ) : AxiosRequestConfig<IXCRequest> {
39
-
40
- const page = options?.page ?? 1;
41
- const rowsPerPage = options?.rowsPerPage ?? 20;
42
- const sortName = options?.sortName ?? 'id';
43
- const sortOrder = options?.sortOrder ?? 'asc';
44
-
45
- if (Array.isArray(params)) {
46
- let grid_param: object[] = [];
47
-
48
- params.forEach(p => {
49
- grid_param.push({
50
- TB: `${table}.${p.TB}`,
51
- OP: p.OP || '=',
52
- P: p.P
53
- });
54
- });
55
-
56
- return { data: {
57
- qtype: table,
58
- query: '',
59
- oper: '',
60
- page: page,
61
- rp: rowsPerPage,
62
- sortname: `${table}.${sortName}`,
63
- sortorder: sortOrder,
64
- grid_param: JSON.stringify(grid_param)
65
- }};
66
- }
67
-
68
- return { data: {
69
- qtype: `${table}.${params.TB}`,
70
- query: params.P,
71
- oper: params.OP || '=',
72
- page: page,
73
- rp: rowsPerPage,
74
- sortname: `${table}.${sortName}`,
75
- sortorder: sortOrder
76
- }};
77
- }
package/src/types.ts DELETED
@@ -1,61 +0,0 @@
1
-
2
-
3
-
4
- export const IXCOperator = {
5
- '=': '=',
6
- '>': '>',
7
- '<': '<',
8
- '>=': '>=',
9
- '<=': '<=',
10
- '!=': '!=',
11
- 'LIKE': 'L'
12
- }
13
-
14
- export const IXCSortOrder = {
15
- asc: 'asc',
16
- desc: 'desc'
17
- }
18
-
19
- export const IXCRequestMethods = {
20
- GET: 'GET',
21
- POST: 'POST',
22
- PUT: 'PUT',
23
- DELETE: 'DELETE'
24
- }
25
-
26
-
27
- export interface IXCOptions {
28
- page: number,
29
- rowsPerPage?: number,
30
- sortName?: string,
31
- sortOrder?: keyof typeof IXCSortOrder
32
- }
33
-
34
-
35
- export interface IXCQuery {
36
- TB: string,
37
- OP?: string,
38
- P: string
39
- }
40
-
41
-
42
- export interface IXCRequest {
43
- qtype: string,
44
- query: string,
45
- oper: string,
46
- page: number,
47
- rp: number,
48
- sortname: string,
49
- sortorder: string,
50
- grid_param?: string
51
- }
52
-
53
-
54
- export interface IXCResponse {
55
- error?: boolean | object,
56
- message?: string | null,
57
- id?: string | number,
58
- page: number | string,
59
- total: number,
60
- registros: Array<{ [key: string]: any }>
61
- }