@seidor-cloud-produtos/orbit-backend-lib 0.0.65 → 0.0.66

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,12 @@
1
+ import { CacheClient } from './client';
2
+ export declare abstract class Cache {
3
+ protected abstract client: CacheClient;
4
+ protected DEFAULT_EXPIRATION_SECONDS: number;
5
+ start(): Promise<void>;
6
+ get(keyCache: string): Promise<any>;
7
+ set(keyCache: string, data: any, expiration?: number): Promise<void>;
8
+ del(keyCache: string): Promise<void>;
9
+ getKeys(pattern?: string): Promise<string[]>;
10
+ delBatch(patterns: string[]): Promise<void>;
11
+ isRunning(): Promise<any>;
12
+ }
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.Cache = void 0;
4
+ const env_1 = require('../../infra/environment/env');
5
+ class Cache {
6
+ constructor() {
7
+ this.DEFAULT_EXPIRATION_SECONDS = env_1.env.DEFAULT_CACHE_EXPIRATION;
8
+ }
9
+ async start() {
10
+ await this.client.start();
11
+ console.log('Cache Connected');
12
+ }
13
+ async get(keyCache) {
14
+ const dataFromCache = await this.client.get(keyCache);
15
+ return dataFromCache ? dataFromCache : null;
16
+ }
17
+ async set(keyCache, data, expiration = this.DEFAULT_EXPIRATION_SECONDS) {
18
+ return await this.client.set(keyCache, JSON.stringify(data), expiration);
19
+ }
20
+ async del(keyCache) {
21
+ return await this.client.del(keyCache);
22
+ }
23
+ async getKeys(pattern) {
24
+ return await this.client.getKeys(pattern);
25
+ }
26
+ async delBatch(patterns) {
27
+ return await this.client.delBatch(patterns);
28
+ }
29
+ async isRunning() {
30
+ return await this.client.isRunning();
31
+ }
32
+ }
33
+ exports.Cache = Cache;
@@ -0,0 +1,9 @@
1
+ export declare abstract class CacheClient {
2
+ abstract start(): Promise<void>;
3
+ abstract get(keyCache: string): Promise<any>;
4
+ abstract set(keyCache: string, data: any, expiration: number): Promise<void>;
5
+ abstract del(keyCache: string): Promise<void>;
6
+ abstract delBatch(keyCache: string[]): Promise<void>;
7
+ abstract getKeys(pattern?: string): Promise<string[]>;
8
+ isRunning(): Promise<any>;
9
+ }
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.CacheClient = void 0;
4
+ const unique_entity_id_1 = require('../../domain/entities/unique-entity-id');
5
+ class CacheClient {
6
+ async isRunning() {
7
+ const key = new unique_entity_id_1.UniqueEntityId().toValue();
8
+ await this.set(key, JSON.stringify({ ping: true }), 60);
9
+ const cachedData = await this.get(key);
10
+ return cachedData?.ping || false;
11
+ }
12
+ }
13
+ exports.CacheClient = CacheClient;
@@ -0,0 +1,13 @@
1
+ import { CacheClient } from '../../../application/cache/client';
2
+ export declare class NodeCache extends CacheClient {
3
+ private client;
4
+ start(): Promise<void>;
5
+ get(key: string): Promise<any>;
6
+ protected hasFreeMemory(): boolean;
7
+ set(key: string, data: string, expiration: number): Promise<void>;
8
+ del(keyCache: string): Promise<void>;
9
+ delBatch(keyCache: string[]): Promise<void>;
10
+ getKeys(pattern?: string | undefined): Promise<string[]>;
11
+ }
12
+ declare const _default: NodeCache;
13
+ export default _default;
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.NodeCache = void 0;
4
+ const tslib_1 = require('tslib');
5
+ const client_1 = require('../../../application/cache/client');
6
+ const node_cache_1 = tslib_1.__importDefault(require('node-cache'));
7
+ class NodeCache extends client_1.CacheClient {
8
+ async start() {
9
+ this.client = new node_cache_1.default();
10
+ }
11
+ async get(key) {
12
+ const data = await this.client.get(key);
13
+ if (!data) return null;
14
+ return JSON.parse(data);
15
+ }
16
+ hasFreeMemory() {
17
+ const maxSizeMB = 50;
18
+ const maxSizeByte = maxSizeMB * 1000000;
19
+ const stats = this.client.getStats();
20
+ const currentSize = stats.vsize + stats.ksize;
21
+ return currentSize < maxSizeByte;
22
+ }
23
+ async set(key, data, expiration) {
24
+ if (!this.hasFreeMemory()) return;
25
+ this.client.set(key, data, expiration);
26
+ }
27
+ async del(keyCache) {
28
+ this.client.del(keyCache);
29
+ }
30
+ async delBatch(keyCache) {
31
+ await Promise.all(keyCache.map(this.del));
32
+ }
33
+ async getKeys(pattern) {
34
+ const keys = this.client.keys();
35
+ if (!pattern) return keys;
36
+ return keys.filter(new RegExp(pattern).test);
37
+ }
38
+ }
39
+ exports.NodeCache = NodeCache;
40
+ exports.default = new NodeCache();
@@ -0,0 +1,14 @@
1
+ import { RedisOptions } from 'ioredis';
2
+ import { CacheClient } from '../../../application/cache/client';
3
+ export declare abstract class Redis extends CacheClient {
4
+ protected connection: RedisOptions;
5
+ private client;
6
+ constructor(connection: RedisOptions);
7
+ private static delayOperation;
8
+ start(): Promise<void>;
9
+ get(key: string): Promise<any>;
10
+ set(key: string, data: string, expiration: number): Promise<void>;
11
+ del(keyCache: string): Promise<void>;
12
+ delBatch(keyCache: string[]): Promise<void>;
13
+ getKeys(pattern?: string | undefined): Promise<string[]>;
14
+ }
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.Redis = void 0;
4
+ const tslib_1 = require('tslib');
5
+ const ioredis_1 = tslib_1.__importDefault(require('ioredis'));
6
+ const client_1 = require('../../../application/cache/client');
7
+ class Redis extends client_1.CacheClient {
8
+ constructor(connection) {
9
+ super();
10
+ this.connection = connection;
11
+ }
12
+ static async delayOperation() {
13
+ const maxDelayMs = 5000;
14
+ await new Promise(resolve => setTimeout(resolve, maxDelayMs));
15
+ return null;
16
+ }
17
+ async start() {
18
+ this.client = new ioredis_1.default(this.connection);
19
+ }
20
+ async get(key) {
21
+ const get = async () => {
22
+ const data = await this.client.get(key);
23
+ if (!data) return null;
24
+ return JSON.parse(data);
25
+ };
26
+ // If redis not connected the get method never returns
27
+ return await Promise.race([get(), Redis.delayOperation()]);
28
+ }
29
+ async set(key, data, expiration) {
30
+ const set = async () => {
31
+ await this.client.set(key, data);
32
+ await this.client.expire(key, expiration);
33
+ };
34
+ // If redis not connected the set method never returns
35
+ await Promise.race([set(), Redis.delayOperation()]);
36
+ }
37
+ async del(keyCache) {
38
+ await this.client.del(keyCache);
39
+ }
40
+ async delBatch(keyCache) {
41
+ const pipeline = this.client.pipeline();
42
+ keyCache.forEach(key => pipeline.del(key));
43
+ await pipeline.exec();
44
+ }
45
+ async getKeys(pattern) {
46
+ return await this.client.keys(pattern || '*');
47
+ }
48
+ }
49
+ exports.Redis = Redis;
@@ -0,0 +1,7 @@
1
+ import 'dotenv/config';
2
+ import { ENVIRONMENT_TYPES } from './environment';
3
+ export declare const env: {
4
+ NODE_ENV: 'test' | 'production';
5
+ CURRENT_ENVIRONMENT: ENVIRONMENT_TYPES;
6
+ DEFAULT_CACHE_EXPIRATION: number;
7
+ };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.env = void 0;
4
+ require('dotenv/config');
5
+ const zod_1 = require('zod');
6
+ const environment_1 = require('./environment');
7
+ const envSchema = zod_1.z.object({
8
+ NODE_ENV: zod_1.z.enum(['test', 'production']).default('production'),
9
+ CURRENT_ENVIRONMENT: zod_1.z
10
+ .enum([
11
+ environment_1.ENVIRONMENT_TYPES.TEST,
12
+ environment_1.ENVIRONMENT_TYPES.LOCAL,
13
+ environment_1.ENVIRONMENT_TYPES.DEVELOPMENT,
14
+ environment_1.ENVIRONMENT_TYPES.HOMOLOGATION,
15
+ environment_1.ENVIRONMENT_TYPES.PRODUCTION,
16
+ ])
17
+ .default(environment_1.ENVIRONMENT_TYPES.LOCAL),
18
+ DEFAULT_CACHE_EXPIRATION: zod_1.z.number().default(360),
19
+ });
20
+ const _env = envSchema.safeParse(process.env);
21
+ exports.env = _env.data;
@@ -0,0 +1,7 @@
1
+ export declare enum ENVIRONMENT_TYPES {
2
+ PRODUCTION = 'prod',
3
+ HOMOLOGATION = 'hmg',
4
+ DEVELOPMENT = 'dev',
5
+ LOCAL = 'local',
6
+ TEST = 'test',
7
+ }
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.ENVIRONMENT_TYPES = void 0;
4
+ var ENVIRONMENT_TYPES;
5
+ (function (ENVIRONMENT_TYPES) {
6
+ ENVIRONMENT_TYPES['PRODUCTION'] = 'prod';
7
+ ENVIRONMENT_TYPES['HOMOLOGATION'] = 'hmg';
8
+ ENVIRONMENT_TYPES['DEVELOPMENT'] = 'dev';
9
+ ENVIRONMENT_TYPES['LOCAL'] = 'local';
10
+ ENVIRONMENT_TYPES['TEST'] = 'test';
11
+ })(ENVIRONMENT_TYPES || (exports.ENVIRONMENT_TYPES = ENVIRONMENT_TYPES = {}));
@@ -58,6 +58,7 @@ describe('MappingModel', () => {
58
58
  expect(instance['getBody'].mock.calls.length).toBe(1);
59
59
  expect(instance['setHeader'].mock.calls).toStrictEqual([
60
60
  ['authorizations', ['test']],
61
+ ['accept-encoding', 'identity'],
61
62
  ]);
62
63
  expect(instance['deleteParams'].mock.calls.length).toBe(1);
63
64
  expect(instance['next'].mock.calls.length).toBe(1);
@@ -115,6 +116,7 @@ describe('MappingModel', () => {
115
116
  ['header_name_2', 'header_value_2'],
116
117
  ['accept', 'new_header_accept'],
117
118
  ['authorizations', ['test']],
119
+ ['accept-encoding', 'identity'],
118
120
  ]);
119
121
  expect(instance['setQuery'].mock.calls).toStrictEqual([
120
122
  ['query_string_name_1', 'query_string_value_1'],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "0.0.65",
3
+ "version": "0.0.66",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -79,10 +79,13 @@
79
79
  "dependencies": {
80
80
  "@fastify/cors": "8.3.0",
81
81
  "@nestjs/common": "8.4.7",
82
- "tslib": "2.6.2",
83
82
  "amqplib": "0.10.4",
84
83
  "compression": "1.7.4",
85
84
  "express": "4.19.2",
86
- "fastify": "4.18.0"
85
+ "fastify": "4.18.0",
86
+ "ioredis": "5.4.1",
87
+ "node-cache": "5.1.2",
88
+ "tslib": "2.6.2",
89
+ "zod": "3.23.8"
87
90
  }
88
91
  }