plac-micro-common 1.1.11 → 1.1.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.
@@ -0,0 +1 @@
1
+ export * from "./redis";
@@ -0,0 +1,17 @@
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("./redis"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./redis.constant";
2
+ export * from "./redis.module";
3
+ export * from "./redis.service";
@@ -0,0 +1,19 @@
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("./redis.constant"), exports);
18
+ __exportStar(require("./redis.module"), exports);
19
+ __exportStar(require("./redis.service"), exports);
@@ -0,0 +1,9 @@
1
+ export declare const REDIS_CLIENT = "REDIS_CLIENT";
2
+ export type RedisModuleOptions = {
3
+ url?: string;
4
+ host?: string;
5
+ port?: number;
6
+ password?: string;
7
+ db?: number;
8
+ keyPrefix?: string;
9
+ };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REDIS_CLIENT = void 0;
4
+ exports.REDIS_CLIENT = "REDIS_CLIENT";
@@ -0,0 +1,4 @@
1
+ import { DynamicModule } from "@nestjs/common";
2
+ export declare class RedisModule {
3
+ static forRootAsync(): DynamicModule;
4
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ var RedisModule_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.RedisModule = void 0;
14
+ const common_1 = require("@nestjs/common");
15
+ const config_1 = require("@nestjs/config");
16
+ const ioredis_1 = __importDefault(require("ioredis"));
17
+ const redis_constant_1 = require("./redis.constant");
18
+ const redis_service_1 = require("./redis.service");
19
+ let RedisModule = RedisModule_1 = class RedisModule {
20
+ static forRootAsync() {
21
+ return {
22
+ module: RedisModule_1,
23
+ imports: [config_1.ConfigModule],
24
+ providers: [
25
+ {
26
+ provide: redis_constant_1.REDIS_CLIENT,
27
+ inject: [config_1.ConfigService],
28
+ useFactory: (config) => {
29
+ const url = config.get("REDIS_URL");
30
+ const keyPrefix = config.get("REDIS_KEY_PREFIX") || undefined;
31
+ // Prefer REDIS_URL if present
32
+ const client = url
33
+ ? new ioredis_1.default(url, { keyPrefix })
34
+ : new ioredis_1.default({
35
+ host: config.get("REDIS_HOST"),
36
+ port: config.get("REDIS_PORT"),
37
+ password: config.get("REDIS_PASSWORD") || undefined,
38
+ db: config.get("REDIS_DB") || 0,
39
+ keyPrefix,
40
+ });
41
+ // (Optional) basic visibility
42
+ client.on("error", (err) => {
43
+ // don’t throw here; just log
44
+ // eslint-disable-next-line no-console
45
+ console.error("[redis] error:", err?.message || err);
46
+ });
47
+ return client;
48
+ },
49
+ },
50
+ redis_service_1.RedisService,
51
+ ],
52
+ exports: [redis_constant_1.REDIS_CLIENT, redis_service_1.RedisService],
53
+ };
54
+ }
55
+ };
56
+ exports.RedisModule = RedisModule;
57
+ exports.RedisModule = RedisModule = RedisModule_1 = __decorate([
58
+ (0, common_1.Global)(),
59
+ (0, common_1.Module)({})
60
+ ], RedisModule);
@@ -0,0 +1,13 @@
1
+ import { OnModuleDestroy } from "@nestjs/common";
2
+ import { Redis } from "ioredis";
3
+ export declare class RedisService implements OnModuleDestroy {
4
+ private readonly redis;
5
+ constructor(redis: Redis);
6
+ get(key: string): Promise<string | null>;
7
+ del(key: string): Promise<number>;
8
+ exists(key: string): Promise<number>;
9
+ getJson<T>(key: string): Promise<T | null>;
10
+ setJson(key: string, value: unknown, ttlSeconds?: number): Promise<"OK">;
11
+ client(): Redis;
12
+ onModuleDestroy(): Promise<void>;
13
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.RedisService = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const ioredis_1 = require("ioredis");
18
+ const redis_constant_1 = require("./redis.constant");
19
+ let RedisService = class RedisService {
20
+ constructor(redis) {
21
+ this.redis = redis;
22
+ }
23
+ // Raw helpers
24
+ get(key) {
25
+ return this.redis.get(key);
26
+ }
27
+ del(key) {
28
+ return this.redis.del(key);
29
+ }
30
+ exists(key) {
31
+ return this.redis.exists(key);
32
+ }
33
+ // JSON helpers (most common use)
34
+ async getJson(key) {
35
+ const val = await this.redis.get(key);
36
+ if (!val)
37
+ return null;
38
+ try {
39
+ return JSON.parse(val);
40
+ }
41
+ catch {
42
+ return null;
43
+ }
44
+ }
45
+ async setJson(key, value, ttlSeconds) {
46
+ const payload = JSON.stringify(value);
47
+ if (ttlSeconds && ttlSeconds > 0) {
48
+ return this.redis.set(key, payload, "EX", ttlSeconds);
49
+ }
50
+ return this.redis.set(key, payload);
51
+ }
52
+ // Optional: if you need access to ioredis commands directly
53
+ client() {
54
+ return this.redis;
55
+ }
56
+ async onModuleDestroy() {
57
+ // Graceful shutdown
58
+ try {
59
+ await this.redis.quit();
60
+ }
61
+ catch {
62
+ // fallback
63
+ this.redis.disconnect();
64
+ }
65
+ }
66
+ };
67
+ exports.RedisService = RedisService;
68
+ exports.RedisService = RedisService = __decorate([
69
+ (0, common_1.Injectable)(),
70
+ __param(0, (0, common_1.Inject)(redis_constant_1.REDIS_CLIENT)),
71
+ __metadata("design:paramtypes", [ioredis_1.Redis])
72
+ ], RedisService);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plac-micro-common",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -29,7 +29,9 @@
29
29
  "peerDependencies": {
30
30
  "@nestjs/common": "^11.1.9",
31
31
  "@nestjs/core": "^11.1.9",
32
+ "@nestjs/config": "^4.0.2",
32
33
  "express": "^5.2.1",
34
+ "ioredis": "^5.9.1",
33
35
  "jsonwebtoken": "^9.0.3",
34
36
  "typeorm": "^0.3.28"
35
37
  }