@xfe-repo/bff-utils 1.0.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.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@xfe-repo/bff-utils`
2
+
3
+ bff 工具库
@@ -0,0 +1,30 @@
1
+ import { ConfigService } from '@nestjs/config';
2
+
3
+ declare function timeIt(): (target: object | Function, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
4
+
5
+ declare class EnvConfigService implements IEnvConfigService {
6
+ private readonly configService;
7
+ constructor(configService: ConfigService);
8
+ getEnv: <T = any>(ENV_NAME: string, defaultValue: T) => T;
9
+ get redisConfig(): {
10
+ host: string;
11
+ port: number;
12
+ password: string;
13
+ db: number;
14
+ retryAttempts: number;
15
+ retryDelay: number;
16
+ };
17
+ }
18
+ interface IEnvConfigService {
19
+ redisConfig: {
20
+ host: string;
21
+ port: number;
22
+ password: string;
23
+ db: number;
24
+ retryAttempts?: number;
25
+ retryDelay?: number;
26
+ wildcards?: boolean;
27
+ };
28
+ }
29
+
30
+ export { EnvConfigService, type IEnvConfigService, timeIt };
@@ -0,0 +1,30 @@
1
+ import { ConfigService } from '@nestjs/config';
2
+
3
+ declare function timeIt(): (target: object | Function, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
4
+
5
+ declare class EnvConfigService implements IEnvConfigService {
6
+ private readonly configService;
7
+ constructor(configService: ConfigService);
8
+ getEnv: <T = any>(ENV_NAME: string, defaultValue: T) => T;
9
+ get redisConfig(): {
10
+ host: string;
11
+ port: number;
12
+ password: string;
13
+ db: number;
14
+ retryAttempts: number;
15
+ retryDelay: number;
16
+ };
17
+ }
18
+ interface IEnvConfigService {
19
+ redisConfig: {
20
+ host: string;
21
+ port: number;
22
+ password: string;
23
+ db: number;
24
+ retryAttempts?: number;
25
+ retryDelay?: number;
26
+ wildcards?: boolean;
27
+ };
28
+ }
29
+
30
+ export { EnvConfigService, type IEnvConfigService, timeIt };
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+
23
+ // src/index.ts
24
+ var src_exports = {};
25
+ __export(src_exports, {
26
+ EnvConfigService: () => EnvConfigService,
27
+ timeIt: () => timeIt
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+
31
+ // src/decorators/timeIt.decorator.ts
32
+ var import_common = require("@nestjs/common");
33
+ function timeIt() {
34
+ const logger = new import_common.Logger(timeIt.name);
35
+ return (target, propertyKey, descriptor) => {
36
+ const originMethod = descriptor.value;
37
+ const label = `${target.constructor.name}.${propertyKey}`;
38
+ descriptor.value = function(...args) {
39
+ const start = performance.now();
40
+ const result = originMethod.apply(this, args);
41
+ const calcDurationTime = /* @__PURE__ */ __name(() => {
42
+ const end = performance.now();
43
+ const duration = end - start;
44
+ if (duration > 50) {
45
+ logger.warn(`[${label}] performance: ${duration.toFixed(3)} ms`);
46
+ } else {
47
+ logger.log(`[${label}] performance: ${duration.toFixed(3)} ms`);
48
+ }
49
+ }, "calcDurationTime");
50
+ if (result instanceof Promise) {
51
+ return result.finally(calcDurationTime);
52
+ } else {
53
+ calcDurationTime();
54
+ return result;
55
+ }
56
+ };
57
+ return descriptor;
58
+ };
59
+ }
60
+ __name(timeIt, "timeIt");
61
+
62
+ // src/services/env.config.service.ts
63
+ var import_common2 = require("@nestjs/common");
64
+ var import_config = require("@nestjs/config");
65
+ function _ts_decorate(decorators, target, key, desc) {
66
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
67
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
68
+ 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;
69
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
70
+ }
71
+ __name(_ts_decorate, "_ts_decorate");
72
+ function _ts_metadata(k, v) {
73
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
74
+ }
75
+ __name(_ts_metadata, "_ts_metadata");
76
+ var _EnvConfigService = class _EnvConfigService {
77
+ constructor(configService) {
78
+ __publicField(this, "configService");
79
+ __publicField(this, "getEnv", /* @__PURE__ */ __name((ENV_NAME, defaultValue) => {
80
+ return this.configService.get(ENV_NAME, defaultValue);
81
+ }, "getEnv"));
82
+ this.configService = configService;
83
+ }
84
+ /** Redis 配置参数, 可通过子类继承的方式覆盖默认选项 */
85
+ get redisConfig() {
86
+ return {
87
+ host: this.getEnv("REDIS_HOST", ""),
88
+ port: this.getEnv("REDIS_PORT", 0),
89
+ password: this.getEnv("REDIS_PWD", ""),
90
+ db: this.getEnv("REDIS_DB", 0),
91
+ retryAttempts: 3,
92
+ retryDelay: 1e3
93
+ };
94
+ }
95
+ };
96
+ __name(_EnvConfigService, "EnvConfigService");
97
+ var EnvConfigService = _EnvConfigService;
98
+ EnvConfigService = _ts_decorate([
99
+ (0, import_common2.Injectable)(),
100
+ _ts_metadata("design:type", Function),
101
+ _ts_metadata("design:paramtypes", [
102
+ typeof import_config.ConfigService === "undefined" ? Object : import_config.ConfigService
103
+ ])
104
+ ], EnvConfigService);
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ EnvConfigService,
108
+ timeIt
109
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,83 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+
6
+ // src/decorators/timeIt.decorator.ts
7
+ import { Logger } from "@nestjs/common";
8
+ function timeIt() {
9
+ const logger = new Logger(timeIt.name);
10
+ return (target, propertyKey, descriptor) => {
11
+ const originMethod = descriptor.value;
12
+ const label = `${target.constructor.name}.${propertyKey}`;
13
+ descriptor.value = function(...args) {
14
+ const start = performance.now();
15
+ const result = originMethod.apply(this, args);
16
+ const calcDurationTime = /* @__PURE__ */ __name(() => {
17
+ const end = performance.now();
18
+ const duration = end - start;
19
+ if (duration > 50) {
20
+ logger.warn(`[${label}] performance: ${duration.toFixed(3)} ms`);
21
+ } else {
22
+ logger.log(`[${label}] performance: ${duration.toFixed(3)} ms`);
23
+ }
24
+ }, "calcDurationTime");
25
+ if (result instanceof Promise) {
26
+ return result.finally(calcDurationTime);
27
+ } else {
28
+ calcDurationTime();
29
+ return result;
30
+ }
31
+ };
32
+ return descriptor;
33
+ };
34
+ }
35
+ __name(timeIt, "timeIt");
36
+
37
+ // src/services/env.config.service.ts
38
+ import { Injectable } from "@nestjs/common";
39
+ import { ConfigService } from "@nestjs/config";
40
+ function _ts_decorate(decorators, target, key, desc) {
41
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
42
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
43
+ 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;
44
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
45
+ }
46
+ __name(_ts_decorate, "_ts_decorate");
47
+ function _ts_metadata(k, v) {
48
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
49
+ }
50
+ __name(_ts_metadata, "_ts_metadata");
51
+ var _EnvConfigService = class _EnvConfigService {
52
+ constructor(configService) {
53
+ __publicField(this, "configService");
54
+ __publicField(this, "getEnv", /* @__PURE__ */ __name((ENV_NAME, defaultValue) => {
55
+ return this.configService.get(ENV_NAME, defaultValue);
56
+ }, "getEnv"));
57
+ this.configService = configService;
58
+ }
59
+ /** Redis 配置参数, 可通过子类继承的方式覆盖默认选项 */
60
+ get redisConfig() {
61
+ return {
62
+ host: this.getEnv("REDIS_HOST", ""),
63
+ port: this.getEnv("REDIS_PORT", 0),
64
+ password: this.getEnv("REDIS_PWD", ""),
65
+ db: this.getEnv("REDIS_DB", 0),
66
+ retryAttempts: 3,
67
+ retryDelay: 1e3
68
+ };
69
+ }
70
+ };
71
+ __name(_EnvConfigService, "EnvConfigService");
72
+ var EnvConfigService = _EnvConfigService;
73
+ EnvConfigService = _ts_decorate([
74
+ Injectable(),
75
+ _ts_metadata("design:type", Function),
76
+ _ts_metadata("design:paramtypes", [
77
+ typeof ConfigService === "undefined" ? Object : ConfigService
78
+ ])
79
+ ], EnvConfigService);
80
+ export {
81
+ EnvConfigService,
82
+ timeIt
83
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@xfe-repo/bff-utils",
3
+ "version": "1.0.0",
4
+ "sideEffects": false,
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": {
10
+ "types": "./dist/index.d.mts",
11
+ "default": "./dist/index.mjs"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {},
23
+ "devDependencies": {
24
+ "@types/node": "^25.0.10",
25
+ "@xfe-repo/eslint-config": "0.0.5",
26
+ "@xfe-repo/typescript-config": "0.0.6"
27
+ },
28
+ "peerDependencies": {
29
+ "@nestjs/common": "^11.1.12",
30
+ "@nestjs/config": "^4.0.2"
31
+ },
32
+ "publishConfig": {
33
+ "registry": "https://registry.npmjs.org/"
34
+ },
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "dev": "tsup --watch",
38
+ "lint": "eslint \"src/**/*.ts*\"",
39
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
40
+ }
41
+ }