heimdall-api-platform 1.0.11 → 1.0.32

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.
Files changed (51) hide show
  1. package/README.md +206 -135
  2. package/dist/LICENSE +21 -0
  3. package/dist/README.md +336 -0
  4. package/dist/index.js +6 -1
  5. package/dist/package.json +96 -0
  6. package/package.json +24 -18
  7. package/dist/clients/http-client.js +0 -1
  8. package/dist/commons-cache.js +0 -1
  9. package/dist/commons-const.js +0 -1
  10. package/dist/commons-elasticsearch.js +0 -1
  11. package/dist/commons-errors.js +0 -1
  12. package/dist/commons-opensearch.js +0 -1
  13. package/dist/commons-splunk.js +0 -1
  14. package/dist/commons-util.js +0 -1
  15. package/dist/default-routes-docs.js +0 -1
  16. package/dist/default-routes-pos.js +0 -1
  17. package/dist/default-routes-pre.js +0 -1
  18. package/dist/environment.js +0 -1
  19. package/dist/factory/api-gateway.js +0 -1
  20. package/dist/factory/client-factory.js +0 -1
  21. package/dist/factory/function-factory.js +0 -1
  22. package/dist/factory/operation-flow-factory.js +0 -1
  23. package/dist/factory/server-factory.js +0 -1
  24. package/dist/factory/transformation-function-factory.js +0 -1
  25. package/dist/handle-route.js +0 -1
  26. package/dist/jwt-util.js +0 -1
  27. package/dist/license/license-service.js +0 -1
  28. package/dist/models/base-context.js +0 -1
  29. package/dist/models/elastic-index-data.js +0 -1
  30. package/dist/models/flow-context.js +0 -1
  31. package/dist/models/flow-indexed.js +0 -1
  32. package/dist/models/operation-function-indexed.js +0 -1
  33. package/dist/models/operation-function-transformation-indexed.js +0 -1
  34. package/dist/models/operation-http-indexed.js +0 -1
  35. package/dist/models/operation-mock-indexed.js +0 -1
  36. package/dist/models/route-context.js +0 -1
  37. package/dist/models/security-route.js +0 -1
  38. package/dist/models/service-context.js +0 -1
  39. package/dist/models/service-group.js +0 -1
  40. package/dist/models/service-route.js +0 -1
  41. package/dist/models/splunk-data.js +0 -1
  42. package/dist/operations/abstract-operation.js +0 -1
  43. package/dist/operations/function.js +0 -1
  44. package/dist/operations/http.js +0 -1
  45. package/dist/operations/mock.js +0 -1
  46. package/dist/operations/monitor-check.js +0 -1
  47. package/dist/orchestration-flow.js +0 -1
  48. package/dist/router.js +0 -1
  49. package/dist/security-validation.js +0 -1
  50. package/dist/services/server.js +0 -1
  51. package/dist/services/template-monitorcheck-route.js +0 -1
package/README.md CHANGED
@@ -39,7 +39,6 @@ Ideal para arquiteturas modernas, legadas ou híbridas, ele permite centralizar
39
39
  * 📄 Documentação automática com Swagger e Redocly
40
40
  * 🎭 Suporte a mocks estáticos e dinâmicos
41
41
  * ▶️ Execução de functions customizadas, integração com clientes de API e mocks internos
42
- * 🧪 Testes automatizados com Mocha + Chai
43
42
  * 🛠️ Padronização de erros e respostas de falha
44
43
  * 🔖 Geração automática de correlation id para rastreamento
45
44
  * ☁️ Totalmente compatível com AWS Lambda e Google Cloud Functions
@@ -60,97 +59,190 @@ npm install heimdall-api-platform
60
59
 
61
60
  ---
62
61
 
63
- ## 🔧 Exemplo de Uso Básico
62
+ ## 🔧 Exemplos de Configuração de Rotas
64
63
 
65
- ### execute-function
64
+ ### 1. execute-function
66
65
 
67
66
  ```js
68
67
  module.exports = {
69
- name: 'executeFunction',
70
- description: 'Execute Function',
71
- method: 'POST',
72
- route: '/execute_function',
73
- indexedResult: false,
74
- responseHeaders: {
75
- 'Content-Type': 'application/json;charset=UTF-8'
76
- },
77
- flow: [{
78
- resultVariable: '.',
79
- skipErrors: false,
80
- cacheEnable: true,
81
- cacheTtl: 10, // TTL em segundos
82
- operation: {
83
- functionName: 'return-body-result',
84
- type: 'function',
85
- }
86
- }]
68
+ name: 'executeFunction',
69
+ description: 'Execute Function',
70
+ method: 'POST',
71
+ route: '/execute_function',
72
+ indexedResult: false,
73
+ responseHeaders: {
74
+ 'Content-Type': 'application/json;charset=UTF-8'
75
+ },
76
+ flow: [
77
+ {
78
+ resultVariable: '.',
79
+ skipErrors: false,
80
+ cacheEnable: true,
81
+ cacheTtl: 10, // TTL em segundos
82
+ operation: {
83
+ functionName: 'return-body-result',
84
+ type: 'function'
85
+ }
86
+ }
87
+ ]
87
88
  };
88
89
  ```
89
90
 
90
- ### execute-mock
91
+ | **Atributo** | **Descrição** |
92
+ | ----------------- | ----------------------------------------------- |
93
+ | `name` | Identificador único da rota/configuração. |
94
+ | `description` | Descrição para documentação. |
95
+ | `method` | Método HTTP (GET, POST, etc.). |
96
+ | `route` | Caminho exposto no gateway. |
97
+ | `indexedResult` | Se deve indexar o resultado para uso posterior. |
98
+ | `responseHeaders` | Headers fixos retornados na resposta. |
99
+
100
+ **Detalhes do `flow`**
101
+
102
+ | **Campo** | **Descrição** |
103
+ | ---------------- | -------------------------------------------- |
104
+ | `resultVariable` | Variável onde armazenar o saída da operação. |
105
+ | `skipErrors` | Se `true`, ignora erros e continua o fluxo. |
106
+ | `cacheEnable` | Se `true`, ativa cache para esta operação. |
107
+ | `cacheTtl` | Tempo de vida (segundos) do cache. |
108
+ | `operation` | Configuração da operação (`function`). |
109
+
110
+ **Configuração `operation`**
111
+
112
+ | **Campo** | **Descrição** |
113
+ | -------------- | --------------------------------------- |
114
+ | `functionName` | Nome da função registrada no framework. |
115
+ | `type` | Tipo de operação (`function`). |
116
+
117
+ ---
118
+
119
+ ### 2. execute-mock
91
120
 
92
121
  ```js
93
122
  module.exports = {
94
- name: 'executeMock',
95
- description: 'Execute Mock',
96
- method: 'GET',
97
- route: '/execute_mock',
98
- indexedResult: false,
99
- responseHeaders: {
100
- 'Content-Type': 'application/json;charset=UTF-8'
101
- },
102
- flow: [{
103
- resultVariable: '.',
104
- skipErrors: false,
105
- operation: {
106
- file: 'demo-mock.json',
107
- type: 'mock',
108
- }
109
- }]
123
+ name: 'executeMock',
124
+ description: 'Execute Mock',
125
+ method: 'GET',
126
+ route: '/execute_mock',
127
+ indexedResult: false,
128
+ responseHeaders: {
129
+ 'Content-Type': 'application/json;charset=UTF-8'
130
+ },
131
+ flow: [
132
+ {
133
+ resultVariable: '.',
134
+ skipErrors: false,
135
+ operation: {
136
+ file: 'demo-mock.json',
137
+ type: 'mock'
138
+ }
139
+ }
140
+ ]
110
141
  };
111
142
  ```
112
143
 
113
- ### execute-api-rest (usando client)
144
+ | **Atributo** | **Descrição** |
145
+ | ----------------- | ------------------------------------ |
146
+ | `name` | Identificador único da rota de mock. |
147
+ | `description` | Descrição para documentação. |
148
+ | `method` | Método HTTP. |
149
+ | `route` | Caminho exposto no mock. |
150
+ | `indexedResult` | Se deve indexar o mock. |
151
+ | `responseHeaders` | Headers fixos para resposta do mock. |
152
+
153
+ **Detalhes do `flow`**
154
+
155
+ | **Campo** | **Descrição** |
156
+ | ---------------- | ----------------------------------------- |
157
+ | `resultVariable` | Variável que receberá o conteúdo do mock. |
158
+ | `skipErrors` | Se `true`, ignora erros. |
159
+ | `operation` | Configuração da operação (`mock`). |
160
+
161
+ **Configuração `operation`**
162
+
163
+ | **Campo** | **Descrição** |
164
+ | --------- | ---------------------------------- |
165
+ | `file` | Caminho para arquivo JSON de mock. |
166
+ | `type` | Tipo de operação (`mock`). |
167
+
168
+ ---
169
+
170
+ ### 3. execute-api-rest (usando client)
114
171
 
115
172
  ```js
116
173
  module.exports = {
117
- name: "addressByZipcodeService",
118
- description: "Get Address By Zipcode",
119
- method: "GET",
120
- route: "/:zipcode/by_client_api",
121
- indexedResult: false,
122
- responseHeaders: {
123
- "Content-Type": "application/json;charset=UTF-8",
124
- },
125
- flow: [
126
- {
127
- resultVariable: ".",
128
- skipErrors: false,
129
- cacheEnable: false,
130
- cacheTtl: 24 * 60 * 30, // 30 dias
131
- cacheKey: "{{params.zipcode}}",
132
- operation: {
133
- clientName: "viaCEP",
134
- mergedHeaders: false,
135
- headersTransformation: "transformation-header-get-cep",
136
- requestTransformation: "transformation-request-get-cep",
137
- responseTransformation: "transformation-response-get-cep",
138
- onError: "on-error-get-cep",
139
- onSuccess: "on-success-get-cep",
140
- headers: {
141
- "Content-Type": "application/json;charset=UTF-8",
142
- "X-HEADER-CEP": "{{params.zipcode}}",
143
- },
144
- type: "http",
145
- method: "GET",
146
- path: "/{{params.zipcode}}/json",
147
- },
174
+ name: 'addressByZipcodeService',
175
+ description: 'Get Address By Zipcode',
176
+ method: 'GET',
177
+ route: '/:zipcode/by_client_api',
178
+ indexedResult: false,
179
+ responseHeaders: {
180
+ 'Content-Type': 'application/json;charset=UTF-8'
181
+ },
182
+ flow: [
183
+ {
184
+ resultVariable: '.',
185
+ skipErrors: false,
186
+ cacheEnable: false,
187
+ cacheTtl: 24 * 60 * 60 * 30, // 30 dias em segundos
188
+ cacheKey: '{{params.zipcode}}',
189
+ operation: {
190
+ clientName: 'viaCEP',
191
+ mergedHeaders: false,
192
+ headersTransformation: 'transformation-header-get-cep',
193
+ requestTransformation: 'transformation-request-get-cep',
194
+ responseTransformation: 'transformation-response-get-cep',
195
+ onError: 'on-error-get-cep',
196
+ onSuccess: 'on-success-get-cep',
197
+ headers: {
198
+ 'Content-Type': 'application/json;charset=UTF-8',
199
+ 'X-HEADER-CEP': '{{params.zipcode}}'
148
200
  },
149
- ],
201
+ type: 'http',
202
+ method: 'GET',
203
+ path: '/{{params.zipcode}}/json'
204
+ }
205
+ }
206
+ ]
150
207
  };
151
208
  ```
152
209
 
153
- ---
210
+ | **Atributo** | **Descrição** |
211
+ | ----------------- | ----------------------------------------------- |
212
+ | `name` | Identificador único do serviço. |
213
+ | `description` | Descrição para documentação. |
214
+ | `method` | Método HTTP da chamada externa. |
215
+ | `route` | Rota no gateway para chamada externa. |
216
+ | `indexedResult` | Se deve indexar o resultado. |
217
+ | `responseHeaders` | Headers fixos da resposta. |
218
+ | `flow` | Lista de etapas de orquestração para esta rota. |
219
+
220
+ **Detalhes do `flow`**
221
+
222
+ | **Campo** | **Descrição** |
223
+ | ---------------- | ----------------------------------------------------- |
224
+ | `resultVariable` | Variável que armazena o resultado da chamada externa. |
225
+ | `skipErrors` | Se `true`, continua o fluxo mesmo com erros. |
226
+ | `cacheEnable` | Se `true`, habilita cache para esta operação. |
227
+ | `cacheTtl` | TTL do cache em segundos. |
228
+ | `cacheKey` | Chave de cache baseada em parâmetros da rota. |
229
+ | `operation` | Configuração da operação HTTP. |
230
+
231
+ **Configuração `operation`**
232
+
233
+ | **Campo** | **Descrição** |
234
+ | ------------------------ | --------------------------------------------------------- |
235
+ | `clientName` | Nome do cliente HTTP em `mappingClients`. |
236
+ | `mergedHeaders` | Se `true`, mescla todos os cabeçalhos de entrada. |
237
+ | `headersTransformation` | Função para transformar cabeçalhos. |
238
+ | `requestTransformation` | Função para transformar payload de request. |
239
+ | `responseTransformation` | Função para transformar a resposta recebida. |
240
+ | `onError` | Função acionada em caso de erro. |
241
+ | `onSuccess` | Função acionada em caso de sucesso. |
242
+ | `headers` | Headers estáticos extras para requisição. |
243
+ | `type` | Tipo da operação (`http`). |
244
+ | `method` | Método HTTP da chamada externa. |
245
+ | `path` | Path da rota externa, suportando templates de parâmetros. |
154
246
 
155
247
  ---
156
248
 
@@ -160,80 +252,58 @@ module.exports = {
160
252
 
161
253
  ```js
162
254
  module.exports = {
163
- appName: 'demo-api-gateway',
164
- serverPort: 9010,
165
- enableRedis: true,
166
- redisCache: {
167
- reader: { host: "localhost", port: 6379 },
168
- writer: { host: "localhost", port: 6379 }
169
- },
170
- enableElasticSearch: false,
255
+ appName: process.env.appName || 'demo-api-gateway',
256
+ requestTimeout: process.env.requestTimeout || 20000,
257
+ requestPerSecond: process.env.requestPerSecond || 40,
258
+ memoryCheckInterval: process.env.memoryCheckInterval || 20000,
259
+ serverPort: process.env.serverPort || 9010,
171
260
  logger: {
172
261
  Console: {
173
- level: "info"
262
+ level: process.env.loggerLevel || 'info'
174
263
  }
264
+ },
265
+ enableRedis: true,
266
+ redisCache: {
267
+ reader: { host: 'localhost', port: 6379, connectTimeout: 5000 },
268
+ writer: { host: 'localhost', port: 6379, connectTimeout: 5000 }
269
+ },
270
+ enableElasticSearch: process.env.elasticEnable,
271
+ elasticSearch: {
272
+ indexName: process.env.elasticIndexName,
273
+ client: process.env.elasticClient,
274
+ credentials: { accessKeyId: process.env.elasticAccessKeyId, secretAccessKey: process.env.elasticSecretAccessKey },
275
+ host: process.env.elasticHost,
276
+ port: process.env.elasticPort
175
277
  }
176
278
  };
177
279
  ```
178
280
 
179
- ### `mappingClients.js`
180
-
181
- ```js
182
- module.exports = {
183
- viaCEP: {
184
- description: "Consulta CEP",
185
- basePath: "ws/",
186
- host: "https://viacep.com.br",
187
- timeout: 9000,
188
- healthCheck: "ws/01001000/json"
189
- }
190
- };
191
- ```
192
-
193
- ### `mappingServices.js`
194
-
195
- ```js
196
- "use strict";
197
- const routeCEP = require("./controller/demo/routes");
198
-
199
- module.exports = [routeCEP];
200
- ```
201
-
202
- ---
203
-
204
- ## ⚙️ Transformações e Operações de Fluxo
205
-
206
- Para cenários avançados, o Heimdall permite aplicar transformações dinâmicas a cabeçalhos, payloads e respostas em tempo de execução, além de tratamento customizado de erros. Exemplo de implementação interna de uma operação HTTP:
207
-
208
- ```js
209
- this.type = options.type || Const.FLOW_OPERATION_TYPE.HTTP;
210
- this.path = options.path || "";
211
- this.method = options.method || Const.HTTP_METHOD.GET;
212
- this.body = options.body || "";
213
- this.mergedHeaders = options.mergedHeaders === undefined ? false : options.mergedHeaders;
214
- this.encapsuleError = options.encapsuleError === undefined ? true : options.encapsuleError;
215
- this.httpClient = ClientFactory.createByName(options.clientName);
216
- this.headers = options.headers || {};
217
-
218
- // Factories para transformar headers, request e response:
219
- this.headersTransformation = TransformationFunctionFactory.create(options.headersTransformation, this);
220
- this.requestTransformation = TransformationFunctionFactory.create(options.requestTransformation, this);
221
- this.responseTransformation = TransformationFunctionFactory.create(options.responseTransformation, this);
222
-
223
- // Tratamento de erros e sucesso customizado:
224
- this.ifResponseError = TransformationFunctionFactory.create(options.ifResponseError, this);
225
- this.onError = TransformationFunctionFactory.create(options.onError, this);
226
- this.onSuccess = TransformationFunctionFactory.create(options.onSuccess, this);
227
- ```
281
+ | **Atributo** | **Descrição** |
282
+ | ----------------------------- | -------------------------------------------------------- |
283
+ | `appName` | Nome da aplicação exibido nos logs e métricas. |
284
+ | `requestTimeout` | Tempo (ms) para timeout de chamadas a serviços upstream. |
285
+ | `requestPerSecond` | Limite de requisições por segundo do gateway. |
286
+ | `memoryCheckInterval` | Intervalo (ms) para verificar uso de memória. |
287
+ | `serverPort` | Porta na qual o servidor HTTP irá escutar. |
288
+ | `logger.Console.level` | Nível de log para console (`info`, `warn`, `error`). |
289
+ | `enableRedis` | Ativa cache e throttling com Redis. |
290
+ | `redisCache.reader.*` | Configurações de conexão de leitura no Redis. |
291
+ | `redisCache.writer.*` | Configurações de conexão de escrita no Redis. |
292
+ | `enableElasticSearch` | Ativa integração com OpenSearch/ElasticSearch. |
293
+ | `elasticSearch.indexName` | Nome do índice para armazenamento de logs. |
294
+ | `elasticSearch.client` | Driver utilizado para conexão com OpenSearch. |
295
+ | `elasticSearch.credentials.*` | Credenciais de acesso ao OpenSearch. |
296
+ | `elasticSearch.host` | Host/URL do serviço OpenSearch. |
297
+ | `elasticSearch.port` | Porta do serviço OpenSearch. |
228
298
 
229
299
  ---
230
300
 
231
301
  ## ▶️ Comandos Úteis
232
302
 
233
303
  ```bash
234
- npm run build # Gera o código ofuscado e empacota a lib
235
- npm run test # Executa os testes automatizados
236
- npm start # Inicia localmente o gateway
304
+ npm run build
305
+ npm run test
306
+ npm start
237
307
  ```
238
308
 
239
309
  ---
@@ -252,8 +322,9 @@ export APP_KEY=minha-chave-da-licenca
252
322
 
253
323
  ## 📜 Política de Licença
254
324
 
255
- * Uso exige validação da licença.
256
- * Licenciado sob **MIT**.
325
+ * Produção: exige licença.
326
+ * Dev/local: sem restrição.
327
+ * MIT.
257
328
 
258
329
  ---
259
330
 
package/dist/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Márcio Rosa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.