@viardex/viardex-libs 1.0.10 → 1.0.12
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/README.md +14 -14
- package/dist/cache/{cache.service.d.ts → index.d.mts} +14 -4
- package/dist/cache/index.d.ts +40 -3
- package/dist/cache/index.js +227 -17
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/index.mjs +193 -0
- package/dist/cache/index.mjs.map +1 -0
- package/dist/common/index.d.mts +215 -0
- package/dist/common/index.d.ts +215 -10
- package/dist/common/index.js +979 -26
- package/dist/common/index.js.map +1 -0
- package/dist/common/index.mjs +874 -0
- package/dist/common/index.mjs.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.ts +11 -9
- package/dist/index.js +1661 -25
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1527 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger/index.d.mts +15 -0
- package/dist/logger/index.d.ts +14 -2
- package/dist/logger/index.js +207 -19
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/index.mjs +183 -0
- package/dist/logger/index.mjs.map +1 -0
- package/dist/nats/{constants/nats.constants.d.ts → index.d.mts} +61 -18
- package/dist/nats/index.d.ts +200 -5
- package/dist/nats/index.js +270 -20
- package/dist/nats/index.js.map +1 -0
- package/dist/nats/index.mjs +226 -0
- package/dist/nats/index.mjs.map +1 -0
- package/dist/rpc/index.d.mts +22 -0
- package/dist/rpc/index.d.ts +7 -3
- package/dist/rpc/index.js +91 -63
- package/dist/rpc/index.js.map +1 -0
- package/dist/rpc/index.mjs +71 -0
- package/dist/rpc/index.mjs.map +1 -0
- package/package.json +31 -62
- package/dist/argon/argon.module.d.ts +0 -2
- package/dist/argon/argon.module.js +0 -21
- package/dist/argon/argon.service.d.ts +0 -6
- package/dist/argon/argon.service.js +0 -74
- package/dist/argon/index.d.ts +0 -2
- package/dist/argon/index.js +0 -18
- package/dist/auth/auth.constants.d.ts +0 -4
- package/dist/auth/auth.constants.js +0 -7
- package/dist/auth/auth.interfaces.d.ts +0 -19
- package/dist/auth/auth.interfaces.js +0 -2
- package/dist/auth/auth.module.d.ts +0 -2
- package/dist/auth/auth.module.js +0 -31
- package/dist/auth/auth.service.d.ts +0 -13
- package/dist/auth/auth.service.js +0 -104
- package/dist/auth/decorators/auth-token-types.decorator.d.ts +0 -2
- package/dist/auth/decorators/auth-token-types.decorator.js +0 -7
- package/dist/auth/decorators/current-auth.decorator.d.ts +0 -1
- package/dist/auth/decorators/current-auth.decorator.js +0 -10
- package/dist/auth/decorators/index.d.ts +0 -5
- package/dist/auth/decorators/index.js +0 -21
- package/dist/auth/decorators/permissions.decorator.d.ts +0 -1
- package/dist/auth/decorators/permissions.decorator.js +0 -7
- package/dist/auth/decorators/public.decorator.d.ts +0 -1
- package/dist/auth/decorators/public.decorator.js +0 -7
- package/dist/auth/decorators/roles.decorator.d.ts +0 -1
- package/dist/auth/decorators/roles.decorator.js +0 -7
- package/dist/auth/guards/auth.guard.d.ts +0 -10
- package/dist/auth/guards/auth.guard.js +0 -65
- package/dist/auth/guards/index.d.ts +0 -3
- package/dist/auth/guards/index.js +0 -19
- package/dist/auth/guards/permissions.guard.d.ts +0 -7
- package/dist/auth/guards/permissions.guard.js +0 -52
- package/dist/auth/guards/roles.guard.d.ts +0 -7
- package/dist/auth/guards/roles.guard.js +0 -53
- package/dist/auth/index.d.ts +0 -6
- package/dist/auth/index.js +0 -22
- package/dist/cache/cache.constants.d.ts +0 -1
- package/dist/cache/cache.constants.js +0 -4
- package/dist/cache/cache.interface.d.ts +0 -2
- package/dist/cache/cache.interface.js +0 -2
- package/dist/cache/cache.module.d.ts +0 -5
- package/dist/cache/cache.module.js +0 -31
- package/dist/cache/cache.service.js +0 -125
- package/dist/common/cards/card.utils.d.ts +0 -9
- package/dist/common/cards/card.utils.js +0 -48
- package/dist/common/cards/index.d.ts +0 -1
- package/dist/common/cards/index.js +0 -17
- package/dist/common/config/index.d.ts +0 -1
- package/dist/common/config/index.js +0 -17
- package/dist/common/config/nats.config.d.ts +0 -8
- package/dist/common/config/nats.config.js +0 -33
- package/dist/common/constants/index.d.ts +0 -2
- package/dist/common/constants/index.js +0 -18
- package/dist/common/constants/otp.constants.d.ts +0 -15
- package/dist/common/constants/otp.constants.js +0 -18
- package/dist/common/constants/queues.constants.d.ts +0 -9
- package/dist/common/constants/queues.constants.js +0 -12
- package/dist/common/crypto/crypto.utils.d.ts +0 -7
- package/dist/common/crypto/crypto.utils.js +0 -42
- package/dist/common/crypto/index.d.ts +0 -1
- package/dist/common/crypto/index.js +0 -17
- package/dist/common/dto/index.d.ts +0 -1
- package/dist/common/dto/index.js +0 -17
- package/dist/common/dto/pagination-query.dto.d.ts +0 -7
- package/dist/common/dto/pagination-query.dto.js +0 -51
- package/dist/common/enums/index.d.ts +0 -1
- package/dist/common/enums/index.js +0 -17
- package/dist/common/enums/otp.enum.d.ts +0 -16
- package/dist/common/enums/otp.enum.js +0 -20
- package/dist/common/interfaces/index.d.ts +0 -1
- package/dist/common/interfaces/index.js +0 -17
- package/dist/common/interfaces/response.interface.d.ts +0 -27
- package/dist/common/interfaces/response.interface.js +0 -2
- package/dist/common/pipes/file-validation.pipe.d.ts +0 -22
- package/dist/common/pipes/file-validation.pipe.js +0 -78
- package/dist/common/pipes/index.d.ts +0 -2
- package/dist/common/pipes/index.js +0 -18
- package/dist/common/pipes/pagination-query-cleaner.pipe.d.ts +0 -4
- package/dist/common/pipes/pagination-query-cleaner.pipe.js +0 -24
- package/dist/common/response/http-exception.filter.d.ts +0 -6
- package/dist/common/response/http-exception.filter.js +0 -61
- package/dist/common/response/index.d.ts +0 -3
- package/dist/common/response/index.js +0 -19
- package/dist/common/response/response.interceptor.d.ts +0 -6
- package/dist/common/response/response.interceptor.js +0 -57
- package/dist/common/response/response.utils.d.ts +0 -6
- package/dist/common/response/response.utils.js +0 -34
- package/dist/common/utils/country.utils.d.ts +0 -1
- package/dist/common/utils/country.utils.js +0 -38
- package/dist/common/utils/date.utils.d.ts +0 -6
- package/dist/common/utils/date.utils.js +0 -48
- package/dist/common/utils/decimal.utils.d.ts +0 -3
- package/dist/common/utils/decimal.utils.js +0 -22
- package/dist/common/utils/format.utils.d.ts +0 -13
- package/dist/common/utils/format.utils.js +0 -127
- package/dist/common/utils/index.d.ts +0 -8
- package/dist/common/utils/index.js +0 -24
- package/dist/common/utils/random.utils.d.ts +0 -10
- package/dist/common/utils/random.utils.js +0 -27
- package/dist/common/utils/reference.utils.d.ts +0 -1
- package/dist/common/utils/reference.utils.js +0 -17
- package/dist/common/utils/string.utils.d.ts +0 -10
- package/dist/common/utils/string.utils.js +0 -34
- package/dist/common/utils/validation.utils.d.ts +0 -8
- package/dist/common/utils/validation.utils.js +0 -44
- package/dist/health/health.controller.d.ts +0 -19
- package/dist/health/health.controller.js +0 -103
- package/dist/health/health.module.d.ts +0 -2
- package/dist/health/health.module.js +0 -21
- package/dist/health/index.d.ts +0 -2
- package/dist/health/index.js +0 -18
- package/dist/logger/logger.module.d.ts +0 -2
- package/dist/logger/logger.module.js +0 -72
- package/dist/logger/logging.interceptor.d.ts +0 -8
- package/dist/logger/logging.interceptor.js +0 -66
- package/dist/nats/constants/nats.constants.js +0 -101
- package/dist/nats/decorators/nats-event-pattern.decorator.d.ts +0 -2
- package/dist/nats/decorators/nats-event-pattern.decorator.js +0 -11
- package/dist/nats/dto/nats-message.dto.d.ts +0 -5
- package/dist/nats/dto/nats-message.dto.js +0 -33
- package/dist/nats/interceptors/nats-logging.interceptor.d.ts +0 -6
- package/dist/nats/interceptors/nats-logging.interceptor.js +0 -33
- package/dist/nats/interfaces/nats-options.interface.d.ts +0 -23
- package/dist/nats/interfaces/nats-options.interface.js +0 -2
- package/dist/storage/index.d.ts +0 -5
- package/dist/storage/index.js +0 -21
- package/dist/storage/r2.service.d.ts +0 -20
- package/dist/storage/r2.service.js +0 -213
- package/dist/storage/s3.service.d.ts +0 -20
- package/dist/storage/s3.service.js +0 -209
- package/dist/storage/storage.interface.d.ts +0 -29
- package/dist/storage/storage.interface.js +0 -2
- package/dist/storage/storage.module.d.ts +0 -2
- package/dist/storage/storage.module.js +0 -24
- package/dist/storage/storage.service.d.ts +0 -22
- package/dist/storage/storage.service.js +0 -63
package/README.md
CHANGED
|
@@ -192,20 +192,20 @@ They are useful for asset formatting and validation, but custody, compliance, si
|
|
|
192
192
|
|
|
193
193
|
The Go shared package in `viardex-go` follows the same auth concepts, but uses middleware and request context instead of Nest metadata and guards.
|
|
194
194
|
|
|
195
|
-
| Nest (`viardex-libs`) | Go (`viardex-go`)
|
|
196
|
-
|
|
|
197
|
-
| `AuthModule`
|
|
198
|
-
| `AuthService`
|
|
199
|
-
| `AuthPrincipal`
|
|
200
|
-
| `AuthTokenType`
|
|
201
|
-
| `AuthGuard`
|
|
202
|
-
| `RolesGuard`
|
|
203
|
-
| `PermissionsGuard`
|
|
204
|
-
| `AuthTokenTypes(...)` | `middlewares.RequireTokenTypes(...)`
|
|
205
|
-
| `Public()`
|
|
206
|
-
| `Roles(...)`
|
|
207
|
-
| `Permissions(...)`
|
|
208
|
-
| `CurrentAuth()`
|
|
195
|
+
| Nest (`viardex-libs`) | Go (`viardex-go`) |
|
|
196
|
+
| --------------------- | ----------------------------------------------------------- |
|
|
197
|
+
| `AuthModule` | `auth` package setup in service bootstrap |
|
|
198
|
+
| `AuthService` | `auth.NewService(...)` / `auth.Service` |
|
|
199
|
+
| `AuthPrincipal` | `auth.Principal` |
|
|
200
|
+
| `AuthTokenType` | `auth.TokenType` |
|
|
201
|
+
| `AuthGuard` | `middlewares.Auth(...)` |
|
|
202
|
+
| `RolesGuard` | `middlewares.RequireRoles(...)` |
|
|
203
|
+
| `PermissionsGuard` | `middlewares.RequirePermissions(...)` |
|
|
204
|
+
| `AuthTokenTypes(...)` | `middlewares.RequireTokenTypes(...)` |
|
|
205
|
+
| `Public()` | keep route/group outside auth middleware |
|
|
206
|
+
| `Roles(...)` | wrap route/group with `middlewares.RequireRoles(...)` |
|
|
207
|
+
| `Permissions(...)` | wrap route/group with `middlewares.RequirePermissions(...)` |
|
|
208
|
+
| `CurrentAuth()` | `auth.PrincipalFromContext(r.Context())` |
|
|
209
209
|
|
|
210
210
|
### Example Comparison
|
|
211
211
|
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import Redis from 'ioredis';
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { DynamicModule, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import Redis, { RedisOptions } from 'ioredis';
|
|
3
|
+
|
|
4
|
+
declare const CACHE_MODULE_OPTIONS: unique symbol;
|
|
5
|
+
|
|
6
|
+
type CacheOptions = RedisOptions;
|
|
7
|
+
|
|
8
|
+
declare class CacheModule {
|
|
9
|
+
static register(options?: CacheOptions): DynamicModule;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare class CacheService implements OnModuleInit, OnModuleDestroy {
|
|
5
13
|
private readonly options;
|
|
6
14
|
private redisClient;
|
|
7
15
|
constructor(options: CacheOptions);
|
|
@@ -28,3 +36,5 @@ export declare class CacheService implements OnModuleInit, OnModuleDestroy {
|
|
|
28
36
|
hdel(key: string, field: string): Promise<void>;
|
|
29
37
|
expire(key: string, ttlSeconds: number): Promise<void>;
|
|
30
38
|
}
|
|
39
|
+
|
|
40
|
+
export { CACHE_MODULE_OPTIONS, CacheModule, CacheService };
|
package/dist/cache/index.d.ts
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { DynamicModule, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import Redis, { RedisOptions } from 'ioredis';
|
|
3
|
+
|
|
4
|
+
declare const CACHE_MODULE_OPTIONS: unique symbol;
|
|
5
|
+
|
|
6
|
+
type CacheOptions = RedisOptions;
|
|
7
|
+
|
|
8
|
+
declare class CacheModule {
|
|
9
|
+
static register(options?: CacheOptions): DynamicModule;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare class CacheService implements OnModuleInit, OnModuleDestroy {
|
|
13
|
+
private readonly options;
|
|
14
|
+
private redisClient;
|
|
15
|
+
constructor(options: CacheOptions);
|
|
16
|
+
onModuleInit(): Promise<void>;
|
|
17
|
+
onModuleDestroy(): Promise<void>;
|
|
18
|
+
getClient(): Redis;
|
|
19
|
+
ping(): Promise<string>;
|
|
20
|
+
set(key: string, value: string, ttlSeconds?: number): Promise<void>;
|
|
21
|
+
setIfNotExists(key: string, value: string, ttlSeconds?: number): Promise<boolean>;
|
|
22
|
+
get(key: string): Promise<string | null>;
|
|
23
|
+
setJson<T>(key: string, value: T, ttlSeconds?: number): Promise<void>;
|
|
24
|
+
getJson<T>(key: string): Promise<T | null>;
|
|
25
|
+
del(key: string): Promise<void>;
|
|
26
|
+
exists(key: string): Promise<boolean>;
|
|
27
|
+
ttl(key: string): Promise<number>;
|
|
28
|
+
keys(pattern: string): Promise<string[]>;
|
|
29
|
+
scan(pattern: string, count?: number): Promise<string[]>;
|
|
30
|
+
flushAll(): Promise<void>;
|
|
31
|
+
incr(key: string): Promise<number>;
|
|
32
|
+
decr(key: string): Promise<number>;
|
|
33
|
+
hset(key: string, field: string, value: string): Promise<void>;
|
|
34
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
35
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
36
|
+
hdel(key: string, field: string): Promise<void>;
|
|
37
|
+
expire(key: string, ttlSeconds: number): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { CACHE_MODULE_OPTIONS, CacheModule, CacheService };
|
package/dist/cache/index.js
CHANGED
|
@@ -1,19 +1,229 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/cache/index.ts
|
|
31
|
+
var cache_exports = {};
|
|
32
|
+
__export(cache_exports, {
|
|
33
|
+
CACHE_MODULE_OPTIONS: () => CACHE_MODULE_OPTIONS,
|
|
34
|
+
CacheModule: () => CacheModule,
|
|
35
|
+
CacheService: () => CacheService
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(cache_exports);
|
|
38
|
+
|
|
39
|
+
// src/cache/cache.constants.ts
|
|
40
|
+
var CACHE_MODULE_OPTIONS = /* @__PURE__ */ Symbol("CACHE_MODULE_OPTIONS");
|
|
41
|
+
|
|
42
|
+
// src/cache/cache.module.ts
|
|
43
|
+
var import_common2 = require("@nestjs/common");
|
|
44
|
+
var import_config = require("@nestjs/config");
|
|
45
|
+
|
|
46
|
+
// src/cache/cache.service.ts
|
|
47
|
+
var import_common = require("@nestjs/common");
|
|
48
|
+
var import_ioredis = __toESM(require("ioredis"));
|
|
49
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
50
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
51
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
52
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
53
|
+
} else {
|
|
54
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
55
|
+
if (d = decorators[i]) {
|
|
56
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
61
|
+
}
|
|
62
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
63
|
+
function _ts_metadata(metadataKey, metadataValue) {
|
|
64
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
|
|
65
|
+
return Reflect.metadata(metadataKey, metadataValue);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
69
|
+
function _ts_param(paramIndex, decorator) {
|
|
70
|
+
return function(target, key) {
|
|
71
|
+
decorator(target, key, paramIndex);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
__name(_ts_param, "_ts_param");
|
|
75
|
+
var CacheService = class {
|
|
76
|
+
static {
|
|
77
|
+
__name(this, "CacheService");
|
|
78
|
+
}
|
|
79
|
+
options;
|
|
80
|
+
redisClient;
|
|
81
|
+
constructor(options) {
|
|
82
|
+
this.options = options;
|
|
83
|
+
}
|
|
84
|
+
async onModuleInit() {
|
|
85
|
+
const options = this.options || {};
|
|
86
|
+
this.redisClient = new import_ioredis.default({
|
|
87
|
+
...options,
|
|
88
|
+
host: options.host || process.env.REDIS_HOST || "localhost",
|
|
89
|
+
port: options.port || +(process.env?.REDIS_PORT || 6379),
|
|
90
|
+
username: options.username || process.env?.REDIS_USERNAME,
|
|
91
|
+
password: options.password || process.env?.REDIS_PASSWORD,
|
|
92
|
+
keyPrefix: options.keyPrefix || "viardex:"
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async onModuleDestroy() {
|
|
96
|
+
await this.redisClient.quit();
|
|
97
|
+
}
|
|
98
|
+
getClient() {
|
|
99
|
+
return this.redisClient;
|
|
100
|
+
}
|
|
101
|
+
async ping() {
|
|
102
|
+
return this.redisClient.ping();
|
|
103
|
+
}
|
|
104
|
+
async set(key, value, ttlSeconds) {
|
|
105
|
+
if (ttlSeconds) {
|
|
106
|
+
await this.redisClient.setex(key, ttlSeconds, value);
|
|
107
|
+
} else {
|
|
108
|
+
await this.redisClient.set(key, value);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async setIfNotExists(key, value, ttlSeconds) {
|
|
112
|
+
const result = ttlSeconds ? await this.redisClient.set(key, value, "EX", ttlSeconds, "NX") : await this.redisClient.set(key, value, "NX");
|
|
113
|
+
return result === "OK";
|
|
114
|
+
}
|
|
115
|
+
async get(key) {
|
|
116
|
+
return this.redisClient.get(key);
|
|
117
|
+
}
|
|
118
|
+
async setJson(key, value, ttlSeconds) {
|
|
119
|
+
await this.set(key, JSON.stringify(value), ttlSeconds);
|
|
120
|
+
}
|
|
121
|
+
async getJson(key) {
|
|
122
|
+
const value = await this.get(key);
|
|
123
|
+
return value ? JSON.parse(value) : null;
|
|
124
|
+
}
|
|
125
|
+
async del(key) {
|
|
126
|
+
await this.redisClient.del(key);
|
|
127
|
+
}
|
|
128
|
+
async exists(key) {
|
|
129
|
+
const result = await this.redisClient.exists(key);
|
|
130
|
+
return result === 1;
|
|
131
|
+
}
|
|
132
|
+
async ttl(key) {
|
|
133
|
+
return this.redisClient.ttl(key);
|
|
134
|
+
}
|
|
135
|
+
async keys(pattern) {
|
|
136
|
+
return this.redisClient.keys(pattern);
|
|
137
|
+
}
|
|
138
|
+
async scan(pattern, count = 100) {
|
|
139
|
+
const keys = [];
|
|
140
|
+
let cursor = "0";
|
|
141
|
+
do {
|
|
142
|
+
const [nextCursor, batch] = await this.redisClient.scan(cursor, "MATCH", pattern, "COUNT", count);
|
|
143
|
+
cursor = nextCursor;
|
|
144
|
+
keys.push(...batch);
|
|
145
|
+
} while (cursor !== "0");
|
|
146
|
+
return keys;
|
|
147
|
+
}
|
|
148
|
+
async flushAll() {
|
|
149
|
+
await this.redisClient.flushall();
|
|
150
|
+
}
|
|
151
|
+
async incr(key) {
|
|
152
|
+
return this.redisClient.incr(key);
|
|
153
|
+
}
|
|
154
|
+
async decr(key) {
|
|
155
|
+
return this.redisClient.decr(key);
|
|
156
|
+
}
|
|
157
|
+
async hset(key, field, value) {
|
|
158
|
+
await this.redisClient.hset(key, field, value);
|
|
159
|
+
}
|
|
160
|
+
async hget(key, field) {
|
|
161
|
+
return this.redisClient.hget(key, field);
|
|
162
|
+
}
|
|
163
|
+
async hgetall(key) {
|
|
164
|
+
return this.redisClient.hgetall(key);
|
|
165
|
+
}
|
|
166
|
+
async hdel(key, field) {
|
|
167
|
+
await this.redisClient.hdel(key, field);
|
|
168
|
+
}
|
|
169
|
+
async expire(key, ttlSeconds) {
|
|
170
|
+
await this.redisClient.expire(key, ttlSeconds);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
CacheService = _ts_decorate([
|
|
174
|
+
(0, import_common.Injectable)(),
|
|
175
|
+
_ts_param(0, (0, import_common.Inject)(CACHE_MODULE_OPTIONS)),
|
|
176
|
+
_ts_metadata("design:type", Function),
|
|
177
|
+
_ts_metadata("design:paramtypes", [
|
|
178
|
+
typeof CacheOptions === "undefined" ? Object : CacheOptions
|
|
179
|
+
])
|
|
180
|
+
], CacheService);
|
|
181
|
+
|
|
182
|
+
// src/cache/cache.module.ts
|
|
183
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
184
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
185
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
186
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
187
|
+
} else {
|
|
188
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
189
|
+
if (d = decorators[i]) {
|
|
190
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
191
|
+
}
|
|
7
192
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
193
|
+
}
|
|
194
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
195
|
+
}
|
|
196
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
197
|
+
var CacheModule = class _CacheModule {
|
|
198
|
+
static {
|
|
199
|
+
__name(this, "CacheModule");
|
|
200
|
+
}
|
|
201
|
+
static register(options = {}) {
|
|
202
|
+
return {
|
|
203
|
+
module: _CacheModule,
|
|
204
|
+
imports: [
|
|
205
|
+
import_config.ConfigModule
|
|
206
|
+
],
|
|
207
|
+
providers: [
|
|
208
|
+
CacheService,
|
|
209
|
+
{
|
|
210
|
+
provide: CACHE_MODULE_OPTIONS,
|
|
211
|
+
useValue: options
|
|
212
|
+
}
|
|
213
|
+
],
|
|
214
|
+
exports: [
|
|
215
|
+
CacheService
|
|
216
|
+
]
|
|
217
|
+
};
|
|
218
|
+
}
|
|
15
219
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
220
|
+
CacheModule = _ts_decorate2([
|
|
221
|
+
(0, import_common2.Module)({})
|
|
222
|
+
], CacheModule);
|
|
223
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
224
|
+
0 && (module.exports = {
|
|
225
|
+
CACHE_MODULE_OPTIONS,
|
|
226
|
+
CacheModule,
|
|
227
|
+
CacheService
|
|
228
|
+
});
|
|
229
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/cache/index.ts","../../src/cache/cache.constants.ts","../../src/cache/cache.module.ts","../../src/cache/cache.service.ts"],"sourcesContent":["export * from './cache.constants';\nexport * from './cache.module';\nexport * from './cache.service';\n","export const CACHE_MODULE_OPTIONS = Symbol('CACHE_MODULE_OPTIONS');\n","import { DynamicModule, Module } from '@nestjs/common';\nimport { ConfigModule } from '@nestjs/config';\nimport { CacheService } from './cache.service';\nimport { CacheOptions } from './cache.interface';\nimport { CACHE_MODULE_OPTIONS } from './cache.constants';\n\n@Module({})\nexport class CacheModule {\n static register(options: CacheOptions = {}): DynamicModule {\n return {\n module: CacheModule,\n imports: [ConfigModule],\n providers: [\n CacheService,\n { provide: CACHE_MODULE_OPTIONS, useValue: options },\n ],\n exports: [CacheService],\n };\n }\n}\n","import {\n Inject,\n Injectable,\n OnModuleDestroy,\n OnModuleInit,\n} from '@nestjs/common';\nimport Redis from 'ioredis';\nimport type { CacheOptions } from './cache.interface';\nimport { CACHE_MODULE_OPTIONS } from './cache.constants';\n\n@Injectable()\nexport class CacheService implements OnModuleInit, OnModuleDestroy {\n private redisClient!: Redis;\n\n constructor(\n @Inject(CACHE_MODULE_OPTIONS) private readonly options: CacheOptions\n ) {}\n\n async onModuleInit() {\n const options = this.options || {};\n\n this.redisClient = new Redis({\n ...options,\n host: options.host || process.env.REDIS_HOST || 'localhost',\n port: options.port || +(process.env?.REDIS_PORT || 6379),\n username: options.username || process.env?.REDIS_USERNAME,\n password: options.password || process.env?.REDIS_PASSWORD,\n keyPrefix: options.keyPrefix || 'viardex:',\n });\n }\n\n async onModuleDestroy() {\n await this.redisClient.quit();\n }\n\n getClient(): Redis {\n return this.redisClient;\n }\n\n async ping(): Promise<string> {\n return this.redisClient.ping();\n }\n\n async set(key: string, value: string, ttlSeconds?: number): Promise<void> {\n if (ttlSeconds) {\n await this.redisClient.setex(key, ttlSeconds, value);\n } else {\n await this.redisClient.set(key, value);\n }\n }\n\n async setIfNotExists(\n key: string,\n value: string,\n ttlSeconds?: number\n ): Promise<boolean> {\n const result = ttlSeconds\n ? await this.redisClient.set(key, value, 'EX', ttlSeconds, 'NX')\n : await this.redisClient.set(key, value, 'NX');\n\n return result === 'OK';\n }\n\n async get(key: string): Promise<string | null> {\n return this.redisClient.get(key);\n }\n\n async setJson<T>(key: string, value: T, ttlSeconds?: number): Promise<void> {\n await this.set(key, JSON.stringify(value), ttlSeconds);\n }\n\n async getJson<T>(key: string): Promise<T | null> {\n const value = await this.get(key);\n return value ? (JSON.parse(value) as T) : null;\n }\n\n async del(key: string): Promise<void> {\n await this.redisClient.del(key);\n }\n\n async exists(key: string): Promise<boolean> {\n const result = await this.redisClient.exists(key);\n return result === 1;\n }\n\n async ttl(key: string): Promise<number> {\n return this.redisClient.ttl(key);\n }\n\n async keys(pattern: string): Promise<string[]> {\n return this.redisClient.keys(pattern);\n }\n\n async scan(pattern: string, count = 100): Promise<string[]> {\n const keys: string[] = [];\n let cursor = '0';\n\n do {\n const [nextCursor, batch] = await this.redisClient.scan(\n cursor,\n 'MATCH',\n pattern,\n 'COUNT',\n count\n );\n cursor = nextCursor;\n keys.push(...batch);\n } while (cursor !== '0');\n\n return keys;\n }\n\n async flushAll(): Promise<void> {\n await this.redisClient.flushall();\n }\n\n async incr(key: string): Promise<number> {\n return this.redisClient.incr(key);\n }\n\n async decr(key: string): Promise<number> {\n return this.redisClient.decr(key);\n }\n\n async hset(key: string, field: string, value: string): Promise<void> {\n await this.redisClient.hset(key, field, value);\n }\n\n async hget(key: string, field: string): Promise<string | null> {\n return this.redisClient.hget(key, field);\n }\n\n async hgetall(key: string): Promise<Record<string, string>> {\n return this.redisClient.hgetall(key);\n }\n\n async hdel(key: string, field: string): Promise<void> {\n await this.redisClient.hdel(key, field);\n }\n\n async expire(key: string, ttlSeconds: number): Promise<void> {\n await this.redisClient.expire(key, ttlSeconds);\n }\n}\n"],"mappings":"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","names":["CACHE_MODULE_OPTIONS","Symbol","import_common","CacheService","redisClient","options","onModuleInit","Redis","host","process","env","REDIS_HOST","port","REDIS_PORT","username","REDIS_USERNAME","password","REDIS_PASSWORD","keyPrefix","onModuleDestroy","quit","getClient","ping","set","key","value","ttlSeconds","setex","setIfNotExists","result","get","setJson","JSON","stringify","getJson","parse","del","exists","ttl","keys","pattern","scan","count","cursor","nextCursor","batch","push","flushAll","flushall","incr","decr","hset","field","hget","hgetall","hdel","expire","CacheModule","register","options","module","imports","ConfigModule","providers","CacheService","provide","CACHE_MODULE_OPTIONS","useValue","exports"]}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/cache/cache.constants.ts
|
|
5
|
+
var CACHE_MODULE_OPTIONS = /* @__PURE__ */ Symbol("CACHE_MODULE_OPTIONS");
|
|
6
|
+
|
|
7
|
+
// src/cache/cache.module.ts
|
|
8
|
+
import { Module } from "@nestjs/common";
|
|
9
|
+
import { ConfigModule } from "@nestjs/config";
|
|
10
|
+
|
|
11
|
+
// src/cache/cache.service.ts
|
|
12
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import Redis from "ioredis";
|
|
14
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
15
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
17
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
18
|
+
} else {
|
|
19
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
20
|
+
if (d = decorators[i]) {
|
|
21
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
+
}
|
|
27
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
28
|
+
function _ts_metadata(metadataKey, metadataValue) {
|
|
29
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
|
|
30
|
+
return Reflect.metadata(metadataKey, metadataValue);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
34
|
+
function _ts_param(paramIndex, decorator) {
|
|
35
|
+
return function(target, key) {
|
|
36
|
+
decorator(target, key, paramIndex);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
__name(_ts_param, "_ts_param");
|
|
40
|
+
var CacheService = class {
|
|
41
|
+
static {
|
|
42
|
+
__name(this, "CacheService");
|
|
43
|
+
}
|
|
44
|
+
options;
|
|
45
|
+
redisClient;
|
|
46
|
+
constructor(options) {
|
|
47
|
+
this.options = options;
|
|
48
|
+
}
|
|
49
|
+
async onModuleInit() {
|
|
50
|
+
const options = this.options || {};
|
|
51
|
+
this.redisClient = new Redis({
|
|
52
|
+
...options,
|
|
53
|
+
host: options.host || process.env.REDIS_HOST || "localhost",
|
|
54
|
+
port: options.port || +(process.env?.REDIS_PORT || 6379),
|
|
55
|
+
username: options.username || process.env?.REDIS_USERNAME,
|
|
56
|
+
password: options.password || process.env?.REDIS_PASSWORD,
|
|
57
|
+
keyPrefix: options.keyPrefix || "viardex:"
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async onModuleDestroy() {
|
|
61
|
+
await this.redisClient.quit();
|
|
62
|
+
}
|
|
63
|
+
getClient() {
|
|
64
|
+
return this.redisClient;
|
|
65
|
+
}
|
|
66
|
+
async ping() {
|
|
67
|
+
return this.redisClient.ping();
|
|
68
|
+
}
|
|
69
|
+
async set(key, value, ttlSeconds) {
|
|
70
|
+
if (ttlSeconds) {
|
|
71
|
+
await this.redisClient.setex(key, ttlSeconds, value);
|
|
72
|
+
} else {
|
|
73
|
+
await this.redisClient.set(key, value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async setIfNotExists(key, value, ttlSeconds) {
|
|
77
|
+
const result = ttlSeconds ? await this.redisClient.set(key, value, "EX", ttlSeconds, "NX") : await this.redisClient.set(key, value, "NX");
|
|
78
|
+
return result === "OK";
|
|
79
|
+
}
|
|
80
|
+
async get(key) {
|
|
81
|
+
return this.redisClient.get(key);
|
|
82
|
+
}
|
|
83
|
+
async setJson(key, value, ttlSeconds) {
|
|
84
|
+
await this.set(key, JSON.stringify(value), ttlSeconds);
|
|
85
|
+
}
|
|
86
|
+
async getJson(key) {
|
|
87
|
+
const value = await this.get(key);
|
|
88
|
+
return value ? JSON.parse(value) : null;
|
|
89
|
+
}
|
|
90
|
+
async del(key) {
|
|
91
|
+
await this.redisClient.del(key);
|
|
92
|
+
}
|
|
93
|
+
async exists(key) {
|
|
94
|
+
const result = await this.redisClient.exists(key);
|
|
95
|
+
return result === 1;
|
|
96
|
+
}
|
|
97
|
+
async ttl(key) {
|
|
98
|
+
return this.redisClient.ttl(key);
|
|
99
|
+
}
|
|
100
|
+
async keys(pattern) {
|
|
101
|
+
return this.redisClient.keys(pattern);
|
|
102
|
+
}
|
|
103
|
+
async scan(pattern, count = 100) {
|
|
104
|
+
const keys = [];
|
|
105
|
+
let cursor = "0";
|
|
106
|
+
do {
|
|
107
|
+
const [nextCursor, batch] = await this.redisClient.scan(cursor, "MATCH", pattern, "COUNT", count);
|
|
108
|
+
cursor = nextCursor;
|
|
109
|
+
keys.push(...batch);
|
|
110
|
+
} while (cursor !== "0");
|
|
111
|
+
return keys;
|
|
112
|
+
}
|
|
113
|
+
async flushAll() {
|
|
114
|
+
await this.redisClient.flushall();
|
|
115
|
+
}
|
|
116
|
+
async incr(key) {
|
|
117
|
+
return this.redisClient.incr(key);
|
|
118
|
+
}
|
|
119
|
+
async decr(key) {
|
|
120
|
+
return this.redisClient.decr(key);
|
|
121
|
+
}
|
|
122
|
+
async hset(key, field, value) {
|
|
123
|
+
await this.redisClient.hset(key, field, value);
|
|
124
|
+
}
|
|
125
|
+
async hget(key, field) {
|
|
126
|
+
return this.redisClient.hget(key, field);
|
|
127
|
+
}
|
|
128
|
+
async hgetall(key) {
|
|
129
|
+
return this.redisClient.hgetall(key);
|
|
130
|
+
}
|
|
131
|
+
async hdel(key, field) {
|
|
132
|
+
await this.redisClient.hdel(key, field);
|
|
133
|
+
}
|
|
134
|
+
async expire(key, ttlSeconds) {
|
|
135
|
+
await this.redisClient.expire(key, ttlSeconds);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
CacheService = _ts_decorate([
|
|
139
|
+
Injectable(),
|
|
140
|
+
_ts_param(0, Inject(CACHE_MODULE_OPTIONS)),
|
|
141
|
+
_ts_metadata("design:type", Function),
|
|
142
|
+
_ts_metadata("design:paramtypes", [
|
|
143
|
+
typeof CacheOptions === "undefined" ? Object : CacheOptions
|
|
144
|
+
])
|
|
145
|
+
], CacheService);
|
|
146
|
+
|
|
147
|
+
// src/cache/cache.module.ts
|
|
148
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
149
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
150
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
151
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
152
|
+
} else {
|
|
153
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
154
|
+
if (d = decorators[i]) {
|
|
155
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
160
|
+
}
|
|
161
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
162
|
+
var CacheModule = class _CacheModule {
|
|
163
|
+
static {
|
|
164
|
+
__name(this, "CacheModule");
|
|
165
|
+
}
|
|
166
|
+
static register(options = {}) {
|
|
167
|
+
return {
|
|
168
|
+
module: _CacheModule,
|
|
169
|
+
imports: [
|
|
170
|
+
ConfigModule
|
|
171
|
+
],
|
|
172
|
+
providers: [
|
|
173
|
+
CacheService,
|
|
174
|
+
{
|
|
175
|
+
provide: CACHE_MODULE_OPTIONS,
|
|
176
|
+
useValue: options
|
|
177
|
+
}
|
|
178
|
+
],
|
|
179
|
+
exports: [
|
|
180
|
+
CacheService
|
|
181
|
+
]
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
CacheModule = _ts_decorate2([
|
|
186
|
+
Module({})
|
|
187
|
+
], CacheModule);
|
|
188
|
+
export {
|
|
189
|
+
CACHE_MODULE_OPTIONS,
|
|
190
|
+
CacheModule,
|
|
191
|
+
CacheService
|
|
192
|
+
};
|
|
193
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/cache/cache.constants.ts","../../src/cache/cache.module.ts","../../src/cache/cache.service.ts"],"sourcesContent":["export const CACHE_MODULE_OPTIONS = Symbol('CACHE_MODULE_OPTIONS');\n","import { DynamicModule, Module } from '@nestjs/common';\nimport { ConfigModule } from '@nestjs/config';\nimport { CacheService } from './cache.service';\nimport { CacheOptions } from './cache.interface';\nimport { CACHE_MODULE_OPTIONS } from './cache.constants';\n\n@Module({})\nexport class CacheModule {\n static register(options: CacheOptions = {}): DynamicModule {\n return {\n module: CacheModule,\n imports: [ConfigModule],\n providers: [\n CacheService,\n { provide: CACHE_MODULE_OPTIONS, useValue: options },\n ],\n exports: [CacheService],\n };\n }\n}\n","import {\n Inject,\n Injectable,\n OnModuleDestroy,\n OnModuleInit,\n} from '@nestjs/common';\nimport Redis from 'ioredis';\nimport type { CacheOptions } from './cache.interface';\nimport { CACHE_MODULE_OPTIONS } from './cache.constants';\n\n@Injectable()\nexport class CacheService implements OnModuleInit, OnModuleDestroy {\n private redisClient!: Redis;\n\n constructor(\n @Inject(CACHE_MODULE_OPTIONS) private readonly options: CacheOptions\n ) {}\n\n async onModuleInit() {\n const options = this.options || {};\n\n this.redisClient = new Redis({\n ...options,\n host: options.host || process.env.REDIS_HOST || 'localhost',\n port: options.port || +(process.env?.REDIS_PORT || 6379),\n username: options.username || process.env?.REDIS_USERNAME,\n password: options.password || process.env?.REDIS_PASSWORD,\n keyPrefix: options.keyPrefix || 'viardex:',\n });\n }\n\n async onModuleDestroy() {\n await this.redisClient.quit();\n }\n\n getClient(): Redis {\n return this.redisClient;\n }\n\n async ping(): Promise<string> {\n return this.redisClient.ping();\n }\n\n async set(key: string, value: string, ttlSeconds?: number): Promise<void> {\n if (ttlSeconds) {\n await this.redisClient.setex(key, ttlSeconds, value);\n } else {\n await this.redisClient.set(key, value);\n }\n }\n\n async setIfNotExists(\n key: string,\n value: string,\n ttlSeconds?: number\n ): Promise<boolean> {\n const result = ttlSeconds\n ? await this.redisClient.set(key, value, 'EX', ttlSeconds, 'NX')\n : await this.redisClient.set(key, value, 'NX');\n\n return result === 'OK';\n }\n\n async get(key: string): Promise<string | null> {\n return this.redisClient.get(key);\n }\n\n async setJson<T>(key: string, value: T, ttlSeconds?: number): Promise<void> {\n await this.set(key, JSON.stringify(value), ttlSeconds);\n }\n\n async getJson<T>(key: string): Promise<T | null> {\n const value = await this.get(key);\n return value ? (JSON.parse(value) as T) : null;\n }\n\n async del(key: string): Promise<void> {\n await this.redisClient.del(key);\n }\n\n async exists(key: string): Promise<boolean> {\n const result = await this.redisClient.exists(key);\n return result === 1;\n }\n\n async ttl(key: string): Promise<number> {\n return this.redisClient.ttl(key);\n }\n\n async keys(pattern: string): Promise<string[]> {\n return this.redisClient.keys(pattern);\n }\n\n async scan(pattern: string, count = 100): Promise<string[]> {\n const keys: string[] = [];\n let cursor = '0';\n\n do {\n const [nextCursor, batch] = await this.redisClient.scan(\n cursor,\n 'MATCH',\n pattern,\n 'COUNT',\n count\n );\n cursor = nextCursor;\n keys.push(...batch);\n } while (cursor !== '0');\n\n return keys;\n }\n\n async flushAll(): Promise<void> {\n await this.redisClient.flushall();\n }\n\n async incr(key: string): Promise<number> {\n return this.redisClient.incr(key);\n }\n\n async decr(key: string): Promise<number> {\n return this.redisClient.decr(key);\n }\n\n async hset(key: string, field: string, value: string): Promise<void> {\n await this.redisClient.hset(key, field, value);\n }\n\n async hget(key: string, field: string): Promise<string | null> {\n return this.redisClient.hget(key, field);\n }\n\n async hgetall(key: string): Promise<Record<string, string>> {\n return this.redisClient.hgetall(key);\n }\n\n async hdel(key: string, field: string): Promise<void> {\n await this.redisClient.hdel(key, field);\n }\n\n async expire(key: string, ttlSeconds: number): Promise<void> {\n await this.redisClient.expire(key, ttlSeconds);\n }\n}\n"],"mappings":"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","names":["CACHE_MODULE_OPTIONS","Symbol","Module","ConfigModule","Inject","Injectable","Redis","CacheService","redisClient","options","onModuleInit","Redis","host","process","env","REDIS_HOST","port","REDIS_PORT","username","REDIS_USERNAME","password","REDIS_PASSWORD","keyPrefix","onModuleDestroy","quit","getClient","ping","set","key","value","ttlSeconds","setex","setIfNotExists","result","get","setJson","JSON","stringify","getJson","parse","del","exists","ttl","keys","pattern","scan","count","cursor","nextCursor","batch","push","flushAll","flushall","incr","decr","hset","field","hget","hgetall","hdel","expire","CacheModule","register","options","module","imports","ConfigModule","providers","CacheService","provide","CACHE_MODULE_OPTIONS","useValue","exports"]}
|