create-pardx-scaffold 0.1.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 +45 -0
- package/cli.js +104 -0
- package/package.json +15 -0
- package/template/.cursorrules +479 -0
- package/template/.prettierignore +29 -0
- package/template/.prettierrc +11 -0
- package/template/.vscode/extensions.json +8 -0
- package/template/.vscode/settings.json +122 -0
- package/template/CLAUDE.md +762 -0
- package/template/README.md +125 -0
- package/template/apps/api/.env.example +11 -0
- package/template/apps/api/.eslintrc.js +222 -0
- package/template/apps/api/config.local.yaml +397 -0
- package/template/apps/api/libs/domain/auth/package.json +11 -0
- package/template/apps/api/libs/domain/auth/src/README.md +189 -0
- package/template/apps/api/libs/domain/auth/src/auth-validation.service.ts +37 -0
- package/template/apps/api/libs/domain/auth/src/auth.guard.ts +173 -0
- package/template/apps/api/libs/domain/auth/src/auth.module.ts +23 -0
- package/template/apps/api/libs/domain/auth/src/auth.service.ts +198 -0
- package/template/apps/api/libs/domain/auth/src/auth.ts +66 -0
- package/template/apps/api/libs/domain/auth/src/decorators/presets.decorator.ts +50 -0
- package/template/apps/api/libs/domain/auth/src/decorators/rbac.decorator.ts +67 -0
- package/template/apps/api/libs/domain/auth/src/decorators/resource-owner.decorator.ts +67 -0
- package/template/apps/api/libs/domain/auth/src/dto/auth.dto.ts +10 -0
- package/template/apps/api/libs/domain/auth/src/guards/streaming-asr-session.guard.ts +179 -0
- package/template/apps/api/libs/domain/auth/src/index.ts +12 -0
- package/template/apps/api/libs/domain/auth/src/types/auth.interface.ts +52 -0
- package/template/apps/api/libs/domain/auth/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/domain/db/package.json +11 -0
- package/template/apps/api/libs/domain/db/src/index.ts +14 -0
- package/template/apps/api/libs/domain/db/src/modules/country-code/country-code.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/country-code/country-code.service.ts +140 -0
- package/template/apps/api/libs/domain/db/src/modules/country-code/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/discord-auth/discord-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/discord-auth/discord-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/discord-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/email-auth/email-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/email-auth/email-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/email-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/file-source/file-source.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/file-source/file-source.service.ts +109 -0
- package/template/apps/api/libs/domain/db/src/modules/file-source/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/google-auth/google-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/google-auth/google-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/google-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/message/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/message/message.module.ts +10 -0
- package/template/apps/api/libs/domain/db/src/modules/message/message.service.ts +314 -0
- package/template/apps/api/libs/domain/db/src/modules/mobile-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/mobile-auth/mobile-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/mobile-auth/mobile-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/risk-detection-record/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/risk-detection-record/risk-detection-record.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/risk-detection-record/risk-detection-record.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/system-task-queue/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/system-task-queue/system-task-queue.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/system-task-queue/system-task-queue.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/user-info/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/user-info/user-info.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/user-info/user-info.service.ts +139 -0
- package/template/apps/api/libs/domain/db/src/modules/wechat-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/wechat-auth/wechat-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/wechat-auth/wechat-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/clients/internal/ai/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/ai/risk-detection.client.ts +301 -0
- package/template/apps/api/libs/infra/clients/internal/ai/risk-detection.module.ts +22 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/crypt-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/crypt.client.ts +37 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/crypt.module.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/email/dto/email.dto.ts +75 -0
- package/template/apps/api/libs/infra/clients/internal/email/index.ts +11 -0
- package/template/apps/api/libs/infra/clients/internal/email/sendcloud.client.ts +400 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/README.md +255 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/dto/file-cdn.dto.ts +96 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/file-cdn-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/file-cdn.client.ts +620 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/file-cdn.module.ts +19 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/index.ts +40 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/config/file.config.ts +14 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/dto/file.dto.ts +127 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-gcs.client.ts +154 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-qiniu.client.ts +729 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-s3.client.ts +1097 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-storage.interface.ts +114 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-tos.client.ts +767 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-us3.client.ts +176 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/index.ts +16 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/dto/ocr.dto.ts +61 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/index.ts +3 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/ocr.client.ts +123 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/ocr.module.ts +15 -0
- package/template/apps/api/libs/infra/clients/internal/openai/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/openai/openai.client.ts +135 -0
- package/template/apps/api/libs/infra/clients/internal/openai/openai.module.ts +17 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/README.md +508 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/index.ts +44 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/openspeech.client.ts +441 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/openspeech.factory.ts +450 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/openspeech.module.ts +56 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/aliyun.provider.ts +308 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/base.provider.ts +114 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/index.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/volcengine-streaming.provider.ts +1689 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/volcengine.provider.ts +387 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/types.ts +467 -0
- package/template/apps/api/libs/infra/clients/internal/sms/dto/sms.dto.ts +97 -0
- package/template/apps/api/libs/infra/clients/internal/sms/index.ts +15 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-aliyun.client.ts +52 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-http.client.ts +111 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-tencent.client.ts +54 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-volcengine.client.ts +165 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-zxjc.client.ts +47 -0
- package/template/apps/api/libs/infra/clients/internal/sse/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/sse/sse-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/sse/sse.client.ts +360 -0
- package/template/apps/api/libs/infra/clients/internal/sse/sse.module.ts +17 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/third-party-sse.client.ts +51 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/third-party-sse.module.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/third-party-sse.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/verify/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/verify/verify.client.ts +42 -0
- package/template/apps/api/libs/infra/clients/internal/verify/verify.module.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/dto/tts.dto.ts +64 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/index.ts +3 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/volcengine-tts.client.ts +846 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/volcengine-tts.module.ts +21 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/wechat-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/wechat.client.ts +4 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/wechat.module.ts +8 -0
- package/template/apps/api/libs/infra/clients/plugin/decorators/inject-client.decorator.ts +60 -0
- package/template/apps/api/libs/infra/clients/plugin/index.ts +21 -0
- package/template/apps/api/libs/infra/clients/plugin/interceptors/http-logging.interceptor.ts +130 -0
- package/template/apps/api/libs/infra/clients/plugin/interfaces/client.interface.ts +86 -0
- package/template/apps/api/libs/infra/clients/plugin/utils/retry.util.ts +157 -0
- package/template/apps/api/libs/infra/common/adapters/base.adapter.ts +94 -0
- package/template/apps/api/libs/infra/common/adapters/index.ts +38 -0
- package/template/apps/api/libs/infra/common/config/README.md +254 -0
- package/template/apps/api/libs/infra/common/config/agentx.config.ts +91 -0
- package/template/apps/api/libs/infra/common/config/configuration.ts +289 -0
- package/template/apps/api/libs/infra/common/config/constant/config.constants.ts +92 -0
- package/template/apps/api/libs/infra/common/config/dto/config.dto.ts +282 -0
- package/template/apps/api/libs/infra/common/config/package.json +11 -0
- package/template/apps/api/libs/infra/common/config/validation/env.validation.ts +161 -0
- package/template/apps/api/libs/infra/common/config/validation/index.ts +188 -0
- package/template/apps/api/libs/infra/common/config/validation/keys.validation.ts +564 -0
- package/template/apps/api/libs/infra/common/config/validation/yaml.validation.ts +582 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.controller.ts +135 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.interceptor.ts +36 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.module.ts +27 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.service.ts +252 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/index.ts +13 -0
- package/template/apps/api/libs/infra/common/decorators/cache/cache.decorator.ts +437 -0
- package/template/apps/api/libs/infra/common/decorators/cache/cache.interceptor.ts +268 -0
- package/template/apps/api/libs/infra/common/decorators/cache/cache.module.ts +24 -0
- package/template/apps/api/libs/infra/common/decorators/cache/index.ts +33 -0
- package/template/apps/api/libs/infra/common/decorators/event/event.decorator.ts +229 -0
- package/template/apps/api/libs/infra/common/decorators/event/event.interceptor.ts +155 -0
- package/template/apps/api/libs/infra/common/decorators/event/event.module.ts +47 -0
- package/template/apps/api/libs/infra/common/decorators/event/handlers/cache-event.handler.ts +159 -0
- package/template/apps/api/libs/infra/common/decorators/event/index.ts +29 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.decorator.ts +221 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.interceptor.ts +150 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.module.ts +27 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.service.spec.ts +330 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.service.ts +423 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/index.ts +28 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/dto/rate-limit.dto.ts +201 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/index.ts +54 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.decorator.ts +216 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.exception.ts +74 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.module.ts +37 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.service.ts +430 -0
- package/template/apps/api/libs/infra/common/decorators/response.decorator.ts +67 -0
- package/template/apps/api/libs/infra/common/decorators/skip-version-check.decorator.ts +27 -0
- package/template/apps/api/libs/infra/common/decorators/transaction/index.ts +12 -0
- package/template/apps/api/libs/infra/common/decorators/transaction/transactional.decorator.ts +677 -0
- package/template/apps/api/libs/infra/common/decorators/ts-rest-controller.decorator.ts +63 -0
- package/template/apps/api/libs/infra/common/decorators/validation.decorator.ts +120 -0
- package/template/apps/api/libs/infra/common/decorators/version/index.ts +24 -0
- package/template/apps/api/libs/infra/common/decorators/version/version.decorator.ts +168 -0
- package/template/apps/api/libs/infra/common/decorators/version/version.interceptor.ts +97 -0
- package/template/apps/api/libs/infra/common/decorators/version/version.module.ts +21 -0
- package/template/apps/api/libs/infra/common/enums/action.enum.ts +7 -0
- package/template/apps/api/libs/infra/common/enums/error-codes.ts +71 -0
- package/template/apps/api/libs/infra/common/enums/role.enum.ts +4 -0
- package/template/apps/api/libs/infra/common/filter/exception/api.exception.ts +168 -0
- package/template/apps/api/libs/infra/common/filter/exception/exception.ts +47 -0
- package/template/apps/api/libs/infra/common/filter/exception/http.exception.ts +126 -0
- package/template/apps/api/libs/infra/common/guards/index.ts +1 -0
- package/template/apps/api/libs/infra/common/guards/version.guard.ts +312 -0
- package/template/apps/api/libs/infra/common/interceptor/mask/index.ts +1 -0
- package/template/apps/api/libs/infra/common/interceptor/mask/mask.interceptor.ts +242 -0
- package/template/apps/api/libs/infra/common/interceptor/rate-limit/no-rate-limit.interceptor.ts +14 -0
- package/template/apps/api/libs/infra/common/interceptor/rate-limit/rate-limit.interceptor.ts +230 -0
- package/template/apps/api/libs/infra/common/interceptor/transform/transform.interceptor.spec.ts +7 -0
- package/template/apps/api/libs/infra/common/interceptor/transform/transform.interceptor.ts +75 -0
- package/template/apps/api/libs/infra/common/interceptor/version/index.ts +1 -0
- package/template/apps/api/libs/infra/common/interceptor/version/version-header.interceptor.ts +62 -0
- package/template/apps/api/libs/infra/common/middleware/request.middleware.ts +109 -0
- package/template/apps/api/libs/infra/common/package.json +11 -0
- package/template/apps/api/libs/infra/common/pipes/transform-root.pipe.ts +12 -0
- package/template/apps/api/libs/infra/common/ts-rest/index.ts +26 -0
- package/template/apps/api/libs/infra/common/ts-rest/response.helper.ts +233 -0
- package/template/apps/api/libs/infra/i18n/en/errors.json +77 -0
- package/template/apps/api/libs/infra/i18n/en/events.json +1 -0
- package/template/apps/api/libs/infra/i18n/package.json +11 -0
- package/template/apps/api/libs/infra/i18n/zh-CN/errors.json +77 -0
- package/template/apps/api/libs/infra/i18n/zh-CN/events.json +1 -0
- package/template/apps/api/libs/infra/jwt/dto/jwt.dto.ts +1 -0
- package/template/apps/api/libs/infra/jwt/jwt.module.ts +26 -0
- package/template/apps/api/libs/infra/jwt/package.json +11 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/package.json +11 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/src/db-metrics.module.ts +141 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/src/db-metrics.service.ts +456 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/src/index.ts +2 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/prisma/middleware/soft-delete.middleware.ts +179 -0
- package/template/apps/api/libs/infra/prisma/package.json +11 -0
- package/template/apps/api/libs/infra/prisma/prisma/index.ts +3 -0
- package/template/apps/api/libs/infra/prisma/prisma/prisma.module.ts +12 -0
- package/template/apps/api/libs/infra/prisma/prisma/prisma.service.ts +18 -0
- package/template/apps/api/libs/infra/prisma/prisma/types.ts +6 -0
- package/template/apps/api/libs/infra/prisma/prisma-read/prisma-read.module.ts +11 -0
- package/template/apps/api/libs/infra/prisma/prisma-read/prisma-read.service.ts +280 -0
- package/template/apps/api/libs/infra/prisma/prisma-write/prisma-write.module.ts +11 -0
- package/template/apps/api/libs/infra/prisma/prisma-write/prisma-write.service.ts +278 -0
- package/template/apps/api/libs/infra/prisma/prometheus/index.ts +1 -0
- package/template/apps/api/libs/infra/prisma/prometheus/prometheus.module.ts +231 -0
- package/template/apps/api/libs/infra/rabbitmq/package.json +11 -0
- package/template/apps/api/libs/infra/rabbitmq/src/dto/rabbitmq.dto.ts +13 -0
- package/template/apps/api/libs/infra/rabbitmq/src/index.ts +5 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq-events.module.ts +132 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq-events.service.ts +199 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq.module.ts +101 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq.service.ts +543 -0
- package/template/apps/api/libs/infra/rabbitmq/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/redis/dto/redis.dto.ts +3 -0
- package/template/apps/api/libs/infra/redis/package.json +11 -0
- package/template/apps/api/libs/infra/redis/src/index.ts +2 -0
- package/template/apps/api/libs/infra/redis/src/redis.module.ts +63 -0
- package/template/apps/api/libs/infra/redis/src/redis.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/redis/src/redis.service.ts +730 -0
- package/template/apps/api/libs/infra/redis/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/shared-db/index.ts +14 -0
- package/template/apps/api/libs/infra/shared-db/transaction-context.ts +51 -0
- package/template/apps/api/libs/infra/shared-db/transaction.module.ts +15 -0
- package/template/apps/api/libs/infra/shared-db/transaction.perf.spec.ts +226 -0
- package/template/apps/api/libs/infra/shared-db/transactional-service.base.ts +102 -0
- package/template/apps/api/libs/infra/shared-db/unit-of-work.service.ts +142 -0
- package/template/apps/api/libs/infra/shared-services/email/dto/email.dto.ts +87 -0
- package/template/apps/api/libs/infra/shared-services/email/email.module.ts +27 -0
- package/template/apps/api/libs/infra/shared-services/email/email.service.ts +258 -0
- package/template/apps/api/libs/infra/shared-services/email/index.ts +5 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/README.md +376 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/bucket-resolver.ts +306 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/file-storage.factory.ts +347 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/file-storage.module.ts +62 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/file-storage.service.ts +849 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/index.ts +57 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/types.ts +210 -0
- package/template/apps/api/libs/infra/shared-services/ip-info/index.ts +2 -0
- package/template/apps/api/libs/infra/shared-services/ip-info/ip-info.module.ts +18 -0
- package/template/apps/api/libs/infra/shared-services/ip-info/ip-info.service.ts +118 -0
- package/template/apps/api/libs/infra/shared-services/sms/index.ts +11 -0
- package/template/apps/api/libs/infra/shared-services/sms/sms.factory.ts +367 -0
- package/template/apps/api/libs/infra/shared-services/sms/sms.module.ts +27 -0
- package/template/apps/api/libs/infra/shared-services/sms/sms.service.ts +315 -0
- package/template/apps/api/libs/infra/shared-services/sms/types.ts +297 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/index.ts +50 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/streaming-asr.module.ts +47 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/streaming-asr.service.ts +1336 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/types.ts +208 -0
- package/template/apps/api/libs/infra/shared-services/system-health/index.ts +3 -0
- package/template/apps/api/libs/infra/shared-services/system-health/system-health.controller.ts +61 -0
- package/template/apps/api/libs/infra/shared-services/system-health/system-health.module.ts +16 -0
- package/template/apps/api/libs/infra/shared-services/system-health/system-health.service.ts +69 -0
- package/template/apps/api/libs/infra/shared-services/uploader/index.ts +2 -0
- package/template/apps/api/libs/infra/shared-services/uploader/uploader.module.ts +11 -0
- package/template/apps/api/libs/infra/shared-services/uploader/uploader.service.ts +265 -0
- package/template/apps/api/libs/infra/utils/array-buffer.util.ts +8 -0
- package/template/apps/api/libs/infra/utils/array.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/bcrypt.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/bigint.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/bytes.convert.util.ts +13 -0
- package/template/apps/api/libs/infra/utils/crypto.util.ts +206 -0
- package/template/apps/api/libs/infra/utils/download.ts +21 -0
- package/template/apps/api/libs/infra/utils/enviroment.util.ts +130 -0
- package/template/apps/api/libs/infra/utils/ffmpeg.util.ts +29 -0
- package/template/apps/api/libs/infra/utils/file.util.ts +448 -0
- package/template/apps/api/libs/infra/utils/folder.util.ts +11 -0
- package/template/apps/api/libs/infra/utils/frame.util.ts +24 -0
- package/template/apps/api/libs/infra/utils/http-client.ts +133 -0
- package/template/apps/api/libs/infra/utils/ip.util.ts +22 -0
- package/template/apps/api/libs/infra/utils/json.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/load-env.util.ts +53 -0
- package/template/apps/api/libs/infra/utils/logger.util.ts +121 -0
- package/template/apps/api/libs/infra/utils/object.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/package.json +11 -0
- package/template/apps/api/libs/infra/utils/prisma-error.util.ts +397 -0
- package/template/apps/api/libs/infra/utils/response.ts +23 -0
- package/template/apps/api/libs/infra/utils/serialize.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/string.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/timer.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/urlencode.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/validate.util.ts +3 -0
- package/template/apps/api/nest-cli.json +25 -0
- package/template/apps/api/package.json +174 -0
- package/template/apps/api/prisma/schema.prisma +352 -0
- package/template/apps/api/prisma/seed.ts +30 -0
- package/template/apps/api/scripts/generate-db-crud.js +344 -0
- package/template/apps/api/scripts/insert-country-codes.ts +325 -0
- package/template/apps/api/scripts/link-prisma.js +44 -0
- package/template/apps/api/scripts/validate-api-versions.ts +273 -0
- package/template/apps/api/src/app.module.ts +208 -0
- package/template/apps/api/src/main.ts +298 -0
- package/template/apps/api/src/modules/health/health.controller.ts +13 -0
- package/template/apps/api/src/modules/health/health.module.ts +7 -0
- package/template/apps/api/tsconfig.build.json +4 -0
- package/template/apps/api/tsconfig.json +123 -0
- package/template/apps/web/.env.example +5 -0
- package/template/apps/web/app/globals.css +27 -0
- package/template/apps/web/app/layout.tsx +19 -0
- package/template/apps/web/app/page.tsx +42 -0
- package/template/apps/web/hooks/useAspectRatioSize.ts +187 -0
- package/template/apps/web/hooks/useDebouncedValue.ts +25 -0
- package/template/apps/web/hooks/useErrorHandler.ts +113 -0
- package/template/apps/web/hooks/useHotkeys.ts +251 -0
- package/template/apps/web/hooks/useI18nToast.ts +240 -0
- package/template/apps/web/hooks/useI18nValidation.ts +262 -0
- package/template/apps/web/hooks/useNotificationSSE.ts +270 -0
- package/template/apps/web/hooks/useOperationFeedback.ts +108 -0
- package/template/apps/web/hooks/usePerformanceMonitor.ts +105 -0
- package/template/apps/web/hooks/usePermissions.ts +17 -0
- package/template/apps/web/hooks/useTask.ts +489 -0
- package/template/apps/web/hooks/useVersionCheck.ts +329 -0
- package/template/apps/web/i18n/config.ts +50 -0
- package/template/apps/web/i18n/index.ts +30 -0
- package/template/apps/web/i18n/navigation.ts +26 -0
- package/template/apps/web/i18n/request.ts +50 -0
- package/template/apps/web/i18n/routing.ts +21 -0
- package/template/apps/web/i18n/types.ts +57 -0
- package/template/apps/web/lib/actions/auth.ts +81 -0
- package/template/apps/web/lib/actions/chat.ts +129 -0
- package/template/apps/web/lib/actions/common.ts +13 -0
- package/template/apps/web/lib/actions/task.ts +20 -0
- package/template/apps/web/lib/agent/chat-client.ts +42 -0
- package/template/apps/web/lib/agent/prompts.ts +43 -0
- package/template/apps/web/lib/analytics/components/PageTracker.tsx +137 -0
- package/template/apps/web/lib/analytics/hooks/usePageTracking.ts +137 -0
- package/template/apps/web/lib/analytics/index.ts +180 -0
- package/template/apps/web/lib/api/agents.ts +7 -0
- package/template/apps/web/lib/api/agno-chat.ts +263 -0
- package/template/apps/web/lib/api/auth-server.ts +244 -0
- package/template/apps/web/lib/api/avatar-upload.ts +96 -0
- package/template/apps/web/lib/api/cache-config.ts +236 -0
- package/template/apps/web/lib/api/client.ts +649 -0
- package/template/apps/web/lib/api/contracts/client.ts +336 -0
- package/template/apps/web/lib/api/contracts/hooks/index.ts +25 -0
- package/template/apps/web/lib/api/contracts/hooks/notification.ts +180 -0
- package/template/apps/web/lib/api/contracts/hooks/setting.ts +33 -0
- package/template/apps/web/lib/api/contracts/index.ts +18 -0
- package/template/apps/web/lib/api/contracts/server-client.ts +145 -0
- package/template/apps/web/lib/api/hooks/use-python-task.ts +154 -0
- package/template/apps/web/lib/api/queries/analytics.ts +51 -0
- package/template/apps/web/lib/api/queries/message.ts +75 -0
- package/template/apps/web/lib/api.ts +179 -0
- package/template/apps/web/lib/aspect-ratio.ts +10 -0
- package/template/apps/web/lib/audio-buffer-queue.ts +273 -0
- package/template/apps/web/lib/config.ts +163 -0
- package/template/apps/web/lib/data/industry.json +369 -0
- package/template/apps/web/lib/data/region.json +501 -0
- package/template/apps/web/lib/errors/error-handler.ts +194 -0
- package/template/apps/web/lib/errors/index.ts +16 -0
- package/template/apps/web/lib/errors/streaming-asr-errors.ts +434 -0
- package/template/apps/web/lib/form/index.ts +23 -0
- package/template/apps/web/lib/form/use-form.ts +143 -0
- package/template/apps/web/lib/icons-usage.md +99 -0
- package/template/apps/web/lib/icons.tsx +395 -0
- package/template/apps/web/lib/performance/monitor.ts +225 -0
- package/template/apps/web/lib/requests.ts +177 -0
- package/template/apps/web/lib/storage/index.ts +158 -0
- package/template/apps/web/lib/upload/api.ts +260 -0
- package/template/apps/web/lib/upload/batch-uploader.ts +286 -0
- package/template/apps/web/lib/upload/errors.ts +44 -0
- package/template/apps/web/lib/upload/folder-utils.ts +295 -0
- package/template/apps/web/lib/upload/uploader.ts +439 -0
- package/template/apps/web/lib/utils/reconnect.ts +223 -0
- package/template/apps/web/lib/utils/transcript-export.ts +321 -0
- package/template/apps/web/lib/version-mismatch.ts +147 -0
- package/template/apps/web/lib/version.ts +60 -0
- package/template/apps/web/next-env.d.ts +6 -0
- package/template/apps/web/next.config.ts +97 -0
- package/template/apps/web/package.json +89 -0
- package/template/apps/web/providers/app-provider.tsx +45 -0
- package/template/apps/web/providers/index.tsx +45 -0
- package/template/apps/web/providers/query-provider.tsx +181 -0
- package/template/apps/web/providers/theme-provider.tsx +26 -0
- package/template/apps/web/tsconfig.json +30 -0
- package/template/package.json +91 -0
- package/template/packages/config/eslint/base.js +32 -0
- package/template/packages/config/eslint/next.js +134 -0
- package/template/packages/config/eslint/react-internal.js +41 -0
- package/template/packages/config/eslint.config.mjs +26 -0
- package/template/packages/config/eslint.nestjs.config.mjs +62 -0
- package/template/packages/config/index.ts +2 -0
- package/template/packages/config/package.json +44 -0
- package/template/packages/config/postcss.config.mjs +8 -0
- package/template/packages/config/prettier.config.mjs +14 -0
- package/template/packages/config/tsconfig.json +19 -0
- package/template/packages/config/typescript/base.json +20 -0
- package/template/packages/config/typescript/nextjs.json +12 -0
- package/template/packages/config/typescript/react-library.json +8 -0
- package/template/packages/constants/README.md +111 -0
- package/template/packages/constants/package.json +25 -0
- package/template/packages/constants/src/index.ts +243 -0
- package/template/packages/constants/tsconfig.build.json +13 -0
- package/template/packages/constants/tsconfig.json +12 -0
- package/template/packages/contracts/ERROR-MIGRATION.md +179 -0
- package/template/packages/contracts/README.md +203 -0
- package/template/packages/contracts/jest.config.js +11 -0
- package/template/packages/contracts/package.json +60 -0
- package/template/packages/contracts/src/api/analytics.contract.ts +45 -0
- package/template/packages/contracts/src/api/download.contract.ts +66 -0
- package/template/packages/contracts/src/api/index.ts +12 -0
- package/template/packages/contracts/src/api/message.contract.ts +70 -0
- package/template/packages/contracts/src/api/risk-words.contract.ts +44 -0
- package/template/packages/contracts/src/api/setting.contract.ts +127 -0
- package/template/packages/contracts/src/api/sign.contract.ts +269 -0
- package/template/packages/contracts/src/api/sms.contract.ts +95 -0
- package/template/packages/contracts/src/api/system.contract.ts +52 -0
- package/template/packages/contracts/src/api/task.contract.ts +58 -0
- package/template/packages/contracts/src/api/uploader.contract.ts +93 -0
- package/template/packages/contracts/src/api/user.contract.ts +60 -0
- package/template/packages/contracts/src/api/webhook.contract.ts +73 -0
- package/template/packages/contracts/src/base.ts +319 -0
- package/template/packages/contracts/src/errors/codes.ts +55 -0
- package/template/packages/contracts/src/errors/domains/common.errors.ts +212 -0
- package/template/packages/contracts/src/errors/domains/index.ts +7 -0
- package/template/packages/contracts/src/errors/domains/user.errors.ts +51 -0
- package/template/packages/contracts/src/errors/error-response.ts +145 -0
- package/template/packages/contracts/src/errors/index.ts +16 -0
- package/template/packages/contracts/src/errors/messages.ts +240 -0
- package/template/packages/contracts/src/index.ts +16 -0
- package/template/packages/contracts/src/schemas/analytics.schema.ts +81 -0
- package/template/packages/contracts/src/schemas/download.schema.ts +59 -0
- package/template/packages/contracts/src/schemas/index.ts +18 -0
- package/template/packages/contracts/src/schemas/message.schema.ts +83 -0
- package/template/packages/contracts/src/schemas/risk-words.schema.ts +25 -0
- package/template/packages/contracts/src/schemas/setting.schema.ts +84 -0
- package/template/packages/contracts/src/schemas/sign.schema.ts +171 -0
- package/template/packages/contracts/src/schemas/sms.schema.ts +53 -0
- package/template/packages/contracts/src/schemas/sse.schema.ts +30 -0
- package/template/packages/contracts/src/schemas/system.schema.ts +26 -0
- package/template/packages/contracts/src/schemas/tag.schema.ts +65 -0
- package/template/packages/contracts/src/schemas/task.schema.ts +47 -0
- package/template/packages/contracts/src/schemas/uploader.schema.ts +121 -0
- package/template/packages/contracts/src/schemas/user.schema.ts +75 -0
- package/template/packages/contracts/src/schemas/webhook.schema.ts +72 -0
- package/template/packages/contracts/tsconfig.build.json +20 -0
- package/template/packages/contracts/tsconfig.json +12 -0
- package/template/packages/types/README.md +143 -0
- package/template/packages/types/ai.ts +30 -0
- package/template/packages/types/auth.ts +99 -0
- package/template/packages/types/common.ts +13 -0
- package/template/packages/types/creative.ts +68 -0
- package/template/packages/types/image-factory.ts +122 -0
- package/template/packages/types/index.ts +8 -0
- package/template/packages/types/package.json +21 -0
- package/template/packages/types/task.ts +27 -0
- package/template/packages/types/tsconfig.json +11 -0
- package/template/packages/ui/README.md +30 -0
- package/template/packages/ui/components.json +22 -0
- package/template/packages/ui/eslint.config.js +4 -0
- package/template/packages/ui/package.json +58 -0
- package/template/packages/ui/postcss.config.mjs +6 -0
- package/template/packages/ui/src/components/accordion.tsx +66 -0
- package/template/packages/ui/src/components/alert.tsx +61 -0
- package/template/packages/ui/src/components/avatar.tsx +57 -0
- package/template/packages/ui/src/components/badge.tsx +38 -0
- package/template/packages/ui/src/components/button.tsx +60 -0
- package/template/packages/ui/src/components/calendar.tsx +71 -0
- package/template/packages/ui/src/components/card.tsx +92 -0
- package/template/packages/ui/src/components/carousel.tsx +241 -0
- package/template/packages/ui/src/components/checkbox.tsx +32 -0
- package/template/packages/ui/src/components/command.tsx +184 -0
- package/template/packages/ui/src/components/dialog.tsx +134 -0
- package/template/packages/ui/src/components/dropdown-menu.tsx +257 -0
- package/template/packages/ui/src/components/empty.tsx +104 -0
- package/template/packages/ui/src/components/field.tsx +248 -0
- package/template/packages/ui/src/components/form.tsx +172 -0
- package/template/packages/ui/src/components/input-group.tsx +170 -0
- package/template/packages/ui/src/components/input.tsx +21 -0
- package/template/packages/ui/src/components/item.tsx +193 -0
- package/template/packages/ui/src/components/label.tsx +24 -0
- package/template/packages/ui/src/components/password-strength.tsx +248 -0
- package/template/packages/ui/src/components/popover.tsx +48 -0
- package/template/packages/ui/src/components/progress.tsx +35 -0
- package/template/packages/ui/src/components/scroll-area.tsx +48 -0
- package/template/packages/ui/src/components/select.tsx +190 -0
- package/template/packages/ui/src/components/separator.tsx +28 -0
- package/template/packages/ui/src/components/sheet.tsx +139 -0
- package/template/packages/ui/src/components/sidebar.tsx +729 -0
- package/template/packages/ui/src/components/skeleton.tsx +13 -0
- package/template/packages/ui/src/components/slider.tsx +87 -0
- package/template/packages/ui/src/components/sonner.tsx +40 -0
- package/template/packages/ui/src/components/switch.tsx +31 -0
- package/template/packages/ui/src/components/tabs.tsx +66 -0
- package/template/packages/ui/src/components/textarea.tsx +18 -0
- package/template/packages/ui/src/components/tooltip.tsx +61 -0
- package/template/packages/ui/src/hooks/use-mobile.ts +21 -0
- package/template/packages/ui/src/index.ts +38 -0
- package/template/packages/ui/src/lib/utils.ts +6 -0
- package/template/packages/ui/src/styles/globals.css +134 -0
- package/template/packages/ui/tsconfig.json +11 -0
- package/template/packages/ui/tsconfig.lint.json +8 -0
- package/template/packages/utils/README.md +173 -0
- package/template/packages/utils/array.util.ts +335 -0
- package/template/packages/utils/bcrypt.util.ts +10 -0
- package/template/packages/utils/bigint.util.ts +111 -0
- package/template/packages/utils/cn.ts +6 -0
- package/template/packages/utils/encrypt.ts +104 -0
- package/template/packages/utils/fetch.ts +170 -0
- package/template/packages/utils/file.ts +275 -0
- package/template/packages/utils/headers.ts +116 -0
- package/template/packages/utils/index.ts +22 -0
- package/template/packages/utils/jest.config.js +28 -0
- package/template/packages/utils/json.util.ts +9 -0
- package/template/packages/utils/mask.util.ts +348 -0
- package/template/packages/utils/object.util.ts +149 -0
- package/template/packages/utils/package.json +112 -0
- package/template/packages/utils/serialize.util.ts +17 -0
- package/template/packages/utils/string.util.ts +159 -0
- package/template/packages/utils/timer.util.ts +210 -0
- package/template/packages/utils/tsconfig.build.json +17 -0
- package/template/packages/utils/tsconfig.json +13 -0
- package/template/packages/utils/urlencode.util.ts +18 -0
- package/template/packages/utils/validate.util.ts +25 -0
- package/template/packages/validators/README.md +149 -0
- package/template/packages/validators/jest.config.js +20 -0
- package/template/packages/validators/package.json +32 -0
- package/template/packages/validators/src/index.ts +178 -0
- package/template/packages/validators/tsconfig.build.json +19 -0
- package/template/packages/validators/tsconfig.json +12 -0
- package/template/pnpm-lock.yaml +21574 -0
- package/template/pnpm-workspace.yaml +4 -0
- package/template/scripts/generate-i18n-errors.ts +371 -0
- package/template/scripts/generate-prisma-enums.js +170 -0
- package/template/scripts/generate-prisma-enums.ts +172 -0
- package/template/scripts/init-project.js +232 -0
- package/template/turbo.json +55 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature Flag Service
|
|
3
|
+
*
|
|
4
|
+
* 功能开关服务,支持多种后端实现:
|
|
5
|
+
* - 内存: 开发测试用
|
|
6
|
+
* - Redis: 生产环境分布式
|
|
7
|
+
* - Unleash: 企业级功能管理
|
|
8
|
+
*
|
|
9
|
+
* 配置项 (config.local.yaml):
|
|
10
|
+
* ```yaml
|
|
11
|
+
* featureFlags:
|
|
12
|
+
* provider: 'redis' # 'memory' | 'redis' | 'unleash'
|
|
13
|
+
* unleash:
|
|
14
|
+
* url: 'http://localhost:4242/api'
|
|
15
|
+
* appName: 'pardx-api'
|
|
16
|
+
* instanceId: 'instance-1'
|
|
17
|
+
* defaultFlags:
|
|
18
|
+
* new-feature: true
|
|
19
|
+
* beta-dashboard: false
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Injectable, Inject, OnModuleInit, Optional } from '@nestjs/common';
|
|
24
|
+
import { ConfigService } from '@nestjs/config';
|
|
25
|
+
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
|
|
26
|
+
import { Logger } from 'winston';
|
|
27
|
+
import { RedisService } from '@app/redis';
|
|
28
|
+
import * as crypto from 'crypto';
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
FeatureFlagContext,
|
|
32
|
+
FeatureFlagOptions,
|
|
33
|
+
FeatureFlagStrategy,
|
|
34
|
+
} from './feature-flag.decorator';
|
|
35
|
+
import enviroment from '@/utils/enviroment.util';
|
|
36
|
+
|
|
37
|
+
// ============================================================================
|
|
38
|
+
// Types
|
|
39
|
+
// ============================================================================
|
|
40
|
+
|
|
41
|
+
export type FeatureFlagProvider = 'memory' | 'redis' | 'unleash';
|
|
42
|
+
|
|
43
|
+
export interface FeatureFlagRedisConfig {
|
|
44
|
+
keyPrefix?: string;
|
|
45
|
+
defaultTTL?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface FeatureFlagConfig {
|
|
49
|
+
provider: FeatureFlagProvider;
|
|
50
|
+
redis?: FeatureFlagRedisConfig;
|
|
51
|
+
unleash?: {
|
|
52
|
+
url: string;
|
|
53
|
+
appName: string;
|
|
54
|
+
instanceId?: string;
|
|
55
|
+
refreshInterval?: number;
|
|
56
|
+
metricsInterval?: number;
|
|
57
|
+
};
|
|
58
|
+
defaultFlags?: Record<string, boolean>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ============================================================================
|
|
62
|
+
// Service
|
|
63
|
+
// ============================================================================
|
|
64
|
+
|
|
65
|
+
@Injectable()
|
|
66
|
+
export class FeatureFlagService implements OnModuleInit {
|
|
67
|
+
private provider: FeatureFlagProvider = 'memory';
|
|
68
|
+
private memoryFlags: Map<string, boolean> = new Map();
|
|
69
|
+
private unleashClient: any = null;
|
|
70
|
+
private redisKeyPrefix = 'pardx:feature:';
|
|
71
|
+
private redisDefaultTTL = 0; // 0 = never expire
|
|
72
|
+
|
|
73
|
+
constructor(
|
|
74
|
+
private readonly configService: ConfigService,
|
|
75
|
+
@Optional() private readonly redis: RedisService,
|
|
76
|
+
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
|
|
77
|
+
) {}
|
|
78
|
+
|
|
79
|
+
async onModuleInit() {
|
|
80
|
+
const config = this.configService.get<FeatureFlagConfig>('featureFlags');
|
|
81
|
+
|
|
82
|
+
if (config) {
|
|
83
|
+
this.provider = config.provider || 'memory';
|
|
84
|
+
|
|
85
|
+
// 如果配置为 Redis 但 Redis 不可用,降级到内存
|
|
86
|
+
if (this.provider === 'redis' && !this.redis) {
|
|
87
|
+
this.logger.warn(
|
|
88
|
+
'Redis configured but not available, falling back to memory provider',
|
|
89
|
+
);
|
|
90
|
+
this.provider = 'memory';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 加载 Redis 配置
|
|
94
|
+
if (config.redis) {
|
|
95
|
+
this.redisKeyPrefix = config.redis.keyPrefix || 'pardx:feature:';
|
|
96
|
+
this.redisDefaultTTL = config.redis.defaultTTL || 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 加载默认开关
|
|
100
|
+
if (config.defaultFlags) {
|
|
101
|
+
for (const [key, value] of Object.entries(config.defaultFlags)) {
|
|
102
|
+
await this.setFlag(key, value);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 初始化 Unleash (如果配置)
|
|
107
|
+
if (this.provider === 'unleash' && config.unleash) {
|
|
108
|
+
await this.initUnleash(config.unleash);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (enviroment.isProduction()) {
|
|
113
|
+
this.logger.info('Feature flag service module initialized', {
|
|
114
|
+
provider: this.provider,
|
|
115
|
+
redisKeyPrefix: this.redisKeyPrefix,
|
|
116
|
+
redisDefaultTTL: this.redisDefaultTTL,
|
|
117
|
+
redisAvailable: !!this.redis,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 初始化 Unleash 客户端
|
|
124
|
+
*/
|
|
125
|
+
private async initUnleash(
|
|
126
|
+
config: FeatureFlagConfig['unleash'],
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
try {
|
|
129
|
+
// 动态导入 unleash-client
|
|
130
|
+
const { initialize } = await import('unleash-client');
|
|
131
|
+
|
|
132
|
+
this.unleashClient = initialize({
|
|
133
|
+
url: config.url,
|
|
134
|
+
appName: config.appName,
|
|
135
|
+
instanceId: config.instanceId || 'default',
|
|
136
|
+
refreshInterval: config.refreshInterval || 15000,
|
|
137
|
+
metricsInterval: config.metricsInterval || 60000,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
this.unleashClient.on('ready', () => {
|
|
141
|
+
this.logger.info('Unleash client ready');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
this.unleashClient.on('error', (err: Error) => {
|
|
145
|
+
this.logger.error('Unleash error', { error: err.message });
|
|
146
|
+
});
|
|
147
|
+
} catch (error) {
|
|
148
|
+
this.logger.warn('Unleash not available, falling back to Redis/memory', {
|
|
149
|
+
error: error.message,
|
|
150
|
+
});
|
|
151
|
+
this.provider = this.redis ? 'redis' : 'memory';
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 检查功能是否启用
|
|
157
|
+
*/
|
|
158
|
+
async isEnabled(
|
|
159
|
+
flagName: string,
|
|
160
|
+
context?: FeatureFlagContext,
|
|
161
|
+
): Promise<boolean> {
|
|
162
|
+
try {
|
|
163
|
+
switch (this.provider) {
|
|
164
|
+
case 'unleash':
|
|
165
|
+
return this.checkUnleash(flagName, context);
|
|
166
|
+
case 'redis':
|
|
167
|
+
return this.checkRedis(flagName);
|
|
168
|
+
case 'memory':
|
|
169
|
+
default:
|
|
170
|
+
return this.memoryFlags.get(flagName) ?? false;
|
|
171
|
+
}
|
|
172
|
+
} catch (error) {
|
|
173
|
+
this.logger.error('Feature flag check error', {
|
|
174
|
+
flagName,
|
|
175
|
+
error: error.message,
|
|
176
|
+
});
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 评估功能开关 (支持策略)
|
|
183
|
+
*/
|
|
184
|
+
async evaluate(
|
|
185
|
+
options: FeatureFlagOptions,
|
|
186
|
+
context?: FeatureFlagContext,
|
|
187
|
+
): Promise<boolean> {
|
|
188
|
+
const { flagName, strategy } = options;
|
|
189
|
+
|
|
190
|
+
// 先检查基本开关
|
|
191
|
+
const isEnabled = await this.isEnabled(flagName, context);
|
|
192
|
+
if (!isEnabled) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// 应用策略
|
|
197
|
+
switch (strategy) {
|
|
198
|
+
case 'userId':
|
|
199
|
+
return this.evaluateUserId(options, context);
|
|
200
|
+
case 'percentage':
|
|
201
|
+
return this.evaluatePercentage(options, context);
|
|
202
|
+
case 'environment':
|
|
203
|
+
return this.evaluateEnvironment(options, context);
|
|
204
|
+
case 'custom':
|
|
205
|
+
return this.evaluateCustom(options, context);
|
|
206
|
+
case 'boolean':
|
|
207
|
+
default:
|
|
208
|
+
return isEnabled;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* 设置功能开关
|
|
214
|
+
* @param flagName 功能开关名称
|
|
215
|
+
* @param enabled 是否启用
|
|
216
|
+
* @param ttl 过期时间 (秒), 0=使用默认TTL, -1=永不过期
|
|
217
|
+
*/
|
|
218
|
+
async setFlag(
|
|
219
|
+
flagName: string,
|
|
220
|
+
enabled: boolean,
|
|
221
|
+
ttl?: number,
|
|
222
|
+
): Promise<void> {
|
|
223
|
+
switch (this.provider) {
|
|
224
|
+
case 'redis':
|
|
225
|
+
// 如果 Redis 不可用,降级到内存存储
|
|
226
|
+
if (!this.redis) {
|
|
227
|
+
this.memoryFlags.set(flagName, enabled);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const key = `${this.redisKeyPrefix}${flagName}`;
|
|
232
|
+
const value = enabled ? '1' : '0';
|
|
233
|
+
const expireSeconds = ttl ?? this.redisDefaultTTL;
|
|
234
|
+
|
|
235
|
+
// 使用 RedisService 的 set 方法,支持过期时间
|
|
236
|
+
if (expireSeconds > 0) {
|
|
237
|
+
await this.redis.set(key, value, { EX: expireSeconds });
|
|
238
|
+
} else {
|
|
239
|
+
await this.redis.set(key, value);
|
|
240
|
+
}
|
|
241
|
+
break;
|
|
242
|
+
case 'memory':
|
|
243
|
+
default:
|
|
244
|
+
this.memoryFlags.set(flagName, enabled);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
this.logger.debug('Feature flag updated', { flagName, enabled, ttl });
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 获取所有功能开关
|
|
252
|
+
*/
|
|
253
|
+
async getAllFlags(): Promise<Record<string, boolean>> {
|
|
254
|
+
switch (this.provider) {
|
|
255
|
+
case 'redis':
|
|
256
|
+
return this.getAllRedisFlags();
|
|
257
|
+
case 'memory':
|
|
258
|
+
default:
|
|
259
|
+
return Object.fromEntries(this.memoryFlags);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 删除功能开关
|
|
265
|
+
*/
|
|
266
|
+
async deleteFlag(flagName: string): Promise<void> {
|
|
267
|
+
switch (this.provider) {
|
|
268
|
+
case 'redis':
|
|
269
|
+
// 如果 Redis 不可用,从内存删除
|
|
270
|
+
if (!this.redis) {
|
|
271
|
+
this.memoryFlags.delete(flagName);
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
await this.redis.del(`${this.redisKeyPrefix}${flagName}`);
|
|
275
|
+
break;
|
|
276
|
+
case 'memory':
|
|
277
|
+
default:
|
|
278
|
+
this.memoryFlags.delete(flagName);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
this.logger.debug('Feature flag deleted', { flagName });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 获取功能开关的 TTL (秒)
|
|
286
|
+
* @returns TTL 秒数, -1=永不过期, -2=不存在
|
|
287
|
+
*/
|
|
288
|
+
async getFlagTTL(flagName: string): Promise<number> {
|
|
289
|
+
if (this.provider !== 'redis' || !this.redis) {
|
|
290
|
+
return -1;
|
|
291
|
+
}
|
|
292
|
+
const ttl = await this.redis.ttl(`${this.redisKeyPrefix}${flagName}`);
|
|
293
|
+
return ttl ?? -2;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 设置临时功能开关 (自动过期)
|
|
298
|
+
* @param flagName 功能开关名称
|
|
299
|
+
* @param enabled 是否启用
|
|
300
|
+
* @param ttlSeconds 过期时间 (秒)
|
|
301
|
+
*/
|
|
302
|
+
async setTemporaryFlag(
|
|
303
|
+
flagName: string,
|
|
304
|
+
enabled: boolean,
|
|
305
|
+
ttlSeconds: number,
|
|
306
|
+
): Promise<void> {
|
|
307
|
+
await this.setFlag(flagName, enabled, ttlSeconds);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ========================================================================
|
|
311
|
+
// Private Methods
|
|
312
|
+
// ========================================================================
|
|
313
|
+
|
|
314
|
+
private checkUnleash(
|
|
315
|
+
flagName: string,
|
|
316
|
+
context?: FeatureFlagContext,
|
|
317
|
+
): boolean {
|
|
318
|
+
if (!this.unleashClient) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const unleashContext = {
|
|
323
|
+
userId: context?.userId,
|
|
324
|
+
sessionId: context?.request?.sessionId,
|
|
325
|
+
remoteAddress: context?.request?.ip,
|
|
326
|
+
properties: context?.properties,
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
return this.unleashClient.isEnabled(flagName, unleashContext);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private async checkRedis(flagName: string): Promise<boolean> {
|
|
333
|
+
if (!this.redis) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const value = await this.redis.get(`${this.redisKeyPrefix}${flagName}`);
|
|
338
|
+
// RedisService.get 会返回解析后的值,对于字符串 '1' 会返回字符串 '1'
|
|
339
|
+
return value === '1' || value === 1;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private async getAllRedisFlags(): Promise<Record<string, boolean>> {
|
|
343
|
+
if (!this.redis || !this.redis.redis) {
|
|
344
|
+
return {};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const flags: Record<string, boolean> = {};
|
|
348
|
+
const pattern = `${this.redisKeyPrefix}*`;
|
|
349
|
+
let cursor = '0';
|
|
350
|
+
|
|
351
|
+
do {
|
|
352
|
+
// 使用 redis.redis.scan 因为 RedisService 没有封装 scan 方法
|
|
353
|
+
const result = await this.redis.redis.scan(
|
|
354
|
+
cursor,
|
|
355
|
+
'MATCH',
|
|
356
|
+
pattern,
|
|
357
|
+
'COUNT',
|
|
358
|
+
100,
|
|
359
|
+
);
|
|
360
|
+
cursor = result[0];
|
|
361
|
+
|
|
362
|
+
for (const key of result[1]) {
|
|
363
|
+
const value = await this.redis.get(key);
|
|
364
|
+
const flagName = key.replace(this.redisKeyPrefix, '');
|
|
365
|
+
// RedisService.get 会返回解析后的值
|
|
366
|
+
flags[flagName] = value === '1' || value === 1;
|
|
367
|
+
}
|
|
368
|
+
} while (cursor !== '0');
|
|
369
|
+
|
|
370
|
+
return flags;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private evaluateUserId(
|
|
374
|
+
options: FeatureFlagOptions,
|
|
375
|
+
context?: FeatureFlagContext,
|
|
376
|
+
): boolean {
|
|
377
|
+
// 如果没有用户 ID,返回 false
|
|
378
|
+
if (!context?.userId) {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
// 可以在这里添加用户白名单逻辑
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private evaluatePercentage(
|
|
386
|
+
options: FeatureFlagOptions,
|
|
387
|
+
context?: FeatureFlagContext,
|
|
388
|
+
): boolean {
|
|
389
|
+
const { percentage = 0, flagName } = options;
|
|
390
|
+
const identifier = context?.userId || context?.request?.ip || 'anonymous';
|
|
391
|
+
|
|
392
|
+
// 使用 hash 确保同一用户始终得到相同结果
|
|
393
|
+
const hash = crypto
|
|
394
|
+
.createHash('md5')
|
|
395
|
+
.update(`${flagName}:${identifier}`)
|
|
396
|
+
.digest('hex');
|
|
397
|
+
const hashNumber = parseInt(hash.substring(0, 8), 16);
|
|
398
|
+
const normalizedValue = (hashNumber % 100) + 1;
|
|
399
|
+
|
|
400
|
+
return normalizedValue <= percentage;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private evaluateEnvironment(
|
|
404
|
+
options: FeatureFlagOptions,
|
|
405
|
+
context?: FeatureFlagContext,
|
|
406
|
+
): boolean {
|
|
407
|
+
const { environments = [] } = options;
|
|
408
|
+
const currentEnv = context?.environment || process.env.NODE_ENV || 'dev';
|
|
409
|
+
|
|
410
|
+
return environments.includes(currentEnv);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private async evaluateCustom(
|
|
414
|
+
options: FeatureFlagOptions,
|
|
415
|
+
context?: FeatureFlagContext,
|
|
416
|
+
): Promise<boolean> {
|
|
417
|
+
if (!options.customEvaluator) {
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return options.customEvaluator(context);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature Flag Module Exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
// Decorators
|
|
7
|
+
FeatureEnabled,
|
|
8
|
+
FeatureFlags,
|
|
9
|
+
IfFeatureEnabled,
|
|
10
|
+
BetaFeature,
|
|
11
|
+
GradualRollout,
|
|
12
|
+
// Constants
|
|
13
|
+
FEATURE_FLAG_METADATA_KEY,
|
|
14
|
+
FEATURE_FLAGS_METADATA_KEY,
|
|
15
|
+
// Types
|
|
16
|
+
FeatureFlagStrategy,
|
|
17
|
+
FeatureFlagOptions,
|
|
18
|
+
FeatureFlagContext,
|
|
19
|
+
} from './feature-flag.decorator';
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
FeatureFlagService,
|
|
23
|
+
FeatureFlagProvider,
|
|
24
|
+
FeatureFlagConfig,
|
|
25
|
+
} from './feature-flag.service';
|
|
26
|
+
|
|
27
|
+
export { FeatureFlagInterceptor } from './feature-flag.interceptor';
|
|
28
|
+
export { FeatureFlagModule } from './feature-flag.module';
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limit DTO
|
|
3
|
+
*
|
|
4
|
+
* 限流模块类型定义
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { FastifyRequest } from 'fastify';
|
|
8
|
+
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Types
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 限流维度
|
|
15
|
+
* - ip: 基于 IP 地址
|
|
16
|
+
* - userId: 基于用户 ID
|
|
17
|
+
* - tenantId: 基于租户/团队 ID
|
|
18
|
+
* - apiKey: 基于 API Key
|
|
19
|
+
* - composite: 复合维度 (自定义)
|
|
20
|
+
*/
|
|
21
|
+
export type RateLimitDimension =
|
|
22
|
+
| 'ip'
|
|
23
|
+
| 'userId'
|
|
24
|
+
| 'tenantId'
|
|
25
|
+
| 'apiKey'
|
|
26
|
+
| 'composite';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 限流上下文
|
|
30
|
+
* 包含请求相关的所有维度信息
|
|
31
|
+
*/
|
|
32
|
+
export interface RateLimitContext {
|
|
33
|
+
/** 客户端 IP 地址 */
|
|
34
|
+
ip: string;
|
|
35
|
+
/** 用户 ID (已认证用户) */
|
|
36
|
+
userId?: string;
|
|
37
|
+
/** 租户/团队 ID */
|
|
38
|
+
tenantId?: string;
|
|
39
|
+
/** API Key (用于外部 API 调用) */
|
|
40
|
+
apiKey?: string;
|
|
41
|
+
/** 请求路径 */
|
|
42
|
+
path: string;
|
|
43
|
+
/** HTTP 方法 */
|
|
44
|
+
method: string;
|
|
45
|
+
/** 追踪 ID */
|
|
46
|
+
traceId: string;
|
|
47
|
+
/** 原始请求对象 */
|
|
48
|
+
request: FastifyRequest;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 限流选项
|
|
53
|
+
* 用于 @RateLimit 装饰器配置
|
|
54
|
+
*/
|
|
55
|
+
export interface RateLimitOptions {
|
|
56
|
+
/** 时间窗口内允许的最大请求数 */
|
|
57
|
+
limit: number;
|
|
58
|
+
/** 时间窗口 (秒) */
|
|
59
|
+
window: number;
|
|
60
|
+
/** 限流维度,默认自动选择最佳维度 */
|
|
61
|
+
dimension?: RateLimitDimension;
|
|
62
|
+
/** 自定义 key 生成器 */
|
|
63
|
+
keyGenerator?: (context: RateLimitContext) => string;
|
|
64
|
+
/** 自定义错误消息 */
|
|
65
|
+
message?: string;
|
|
66
|
+
/** 关联的 Feature Flag,用于动态开关 */
|
|
67
|
+
featureFlag?: string;
|
|
68
|
+
/** 跳过限流的条件判断函数 */
|
|
69
|
+
skip?: (context: RateLimitContext) => boolean | Promise<boolean>;
|
|
70
|
+
/** 是否在限流时抛出异常 (默认 true) */
|
|
71
|
+
throwOnLimit?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 限流检查结果
|
|
76
|
+
*/
|
|
77
|
+
export interface RateLimitResult {
|
|
78
|
+
/** 请求是否被允许 */
|
|
79
|
+
allowed: boolean;
|
|
80
|
+
/** 时间窗口内的请求限制数 */
|
|
81
|
+
limit: number;
|
|
82
|
+
/** 剩余请求数 */
|
|
83
|
+
remaining: number;
|
|
84
|
+
/** 限流重置时间 (Unix 时间戳,秒) */
|
|
85
|
+
resetTime: number;
|
|
86
|
+
/** 需要等待的秒数 */
|
|
87
|
+
retryAfter: number;
|
|
88
|
+
/** 使用的限流维度 */
|
|
89
|
+
dimension: RateLimitDimension;
|
|
90
|
+
/** 维度标识符 */
|
|
91
|
+
identifier: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 限流信息 (用于异常响应)
|
|
96
|
+
*/
|
|
97
|
+
export interface RateLimitInfo {
|
|
98
|
+
/** 时间窗口内的请求限制数 */
|
|
99
|
+
limit: number;
|
|
100
|
+
/** 剩余请求数 */
|
|
101
|
+
remaining: number;
|
|
102
|
+
/** 限流重置时间 (Unix 时间戳,秒) */
|
|
103
|
+
resetTime: number;
|
|
104
|
+
/** 需要等待的秒数 */
|
|
105
|
+
retryAfter: number;
|
|
106
|
+
/** 使用的限流维度 */
|
|
107
|
+
dimension: RateLimitDimension;
|
|
108
|
+
/** 维度标识符 */
|
|
109
|
+
identifier: string;
|
|
110
|
+
/** 触发限流的端点 */
|
|
111
|
+
endpoint?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ============================================================================
|
|
115
|
+
// Configuration
|
|
116
|
+
// ============================================================================
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 维度限流配置
|
|
120
|
+
*/
|
|
121
|
+
export interface RateLimitDimensionConfig {
|
|
122
|
+
/** 请求限制数 */
|
|
123
|
+
limit: number;
|
|
124
|
+
/** 时间窗口 (秒) */
|
|
125
|
+
window: number;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 白名单配置
|
|
130
|
+
*/
|
|
131
|
+
export interface RateLimitWhitelist {
|
|
132
|
+
/** IP 白名单 */
|
|
133
|
+
ips: string[];
|
|
134
|
+
/** 用户 ID 白名单 */
|
|
135
|
+
userIds: string[];
|
|
136
|
+
/** API Key 白名单 */
|
|
137
|
+
apiKeys: string[];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Redis 配置
|
|
142
|
+
*/
|
|
143
|
+
export interface RateLimitRedisConfig {
|
|
144
|
+
/** Redis key 前缀 */
|
|
145
|
+
keyPrefix: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 限流配置 (从 YAML 加载)
|
|
150
|
+
*/
|
|
151
|
+
export interface RateLimitConfig {
|
|
152
|
+
/** 是否启用限流 */
|
|
153
|
+
enabled: boolean;
|
|
154
|
+
/** 全局 Feature Flag */
|
|
155
|
+
featureFlag?: string;
|
|
156
|
+
/** 默认限流配置 */
|
|
157
|
+
default: RateLimitDimensionConfig;
|
|
158
|
+
/** 各维度的默认配置 */
|
|
159
|
+
dimensions: {
|
|
160
|
+
ip: RateLimitDimensionConfig;
|
|
161
|
+
userId: RateLimitDimensionConfig;
|
|
162
|
+
tenantId: RateLimitDimensionConfig;
|
|
163
|
+
apiKey: RateLimitDimensionConfig;
|
|
164
|
+
};
|
|
165
|
+
/** Redis 配置 */
|
|
166
|
+
redis: RateLimitRedisConfig;
|
|
167
|
+
/** 白名单 */
|
|
168
|
+
whitelist: RateLimitWhitelist;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ============================================================================
|
|
172
|
+
// Metrics
|
|
173
|
+
// ============================================================================
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 限流事件 (用于日志和监控)
|
|
177
|
+
*/
|
|
178
|
+
export interface RateLimitEvent {
|
|
179
|
+
/** 事件类型 */
|
|
180
|
+
type: 'allowed' | 'blocked';
|
|
181
|
+
/** 追踪 ID */
|
|
182
|
+
traceId: string;
|
|
183
|
+
/** 限流维度 */
|
|
184
|
+
dimension: RateLimitDimension;
|
|
185
|
+
/** 维度标识符 */
|
|
186
|
+
identifier: string;
|
|
187
|
+
/** 请求路径 */
|
|
188
|
+
path: string;
|
|
189
|
+
/** HTTP 方法 */
|
|
190
|
+
method: string;
|
|
191
|
+
/** 当前计数 */
|
|
192
|
+
currentCount: number;
|
|
193
|
+
/** 限制数 */
|
|
194
|
+
limit: number;
|
|
195
|
+
/** 用户 ID (如有) */
|
|
196
|
+
userId?: string;
|
|
197
|
+
/** IP 地址 */
|
|
198
|
+
ip: string;
|
|
199
|
+
/** 时间戳 */
|
|
200
|
+
timestamp: Date;
|
|
201
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limit Module Exports
|
|
3
|
+
*
|
|
4
|
+
* 统一导出限流模块的所有组件
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import {
|
|
9
|
+
* RateLimit,
|
|
10
|
+
* RateLimitPresets,
|
|
11
|
+
* SkipRateLimit,
|
|
12
|
+
* RateLimitService,
|
|
13
|
+
* RateLimitException,
|
|
14
|
+
* RateLimitModule,
|
|
15
|
+
* } from '@/decorators/rate-limit';
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Decorators
|
|
20
|
+
export {
|
|
21
|
+
RateLimit,
|
|
22
|
+
SkipRateLimit,
|
|
23
|
+
RateLimitPresets,
|
|
24
|
+
RateLimitHigh,
|
|
25
|
+
RateLimitStandard,
|
|
26
|
+
RateLimitLow,
|
|
27
|
+
RateLimitSensitive,
|
|
28
|
+
RateLimitStrict,
|
|
29
|
+
RATE_LIMIT_KEY,
|
|
30
|
+
SKIP_RATE_LIMIT_KEY,
|
|
31
|
+
} from './rate-limit.decorator';
|
|
32
|
+
|
|
33
|
+
// Service
|
|
34
|
+
export { RateLimitService } from './rate-limit.service';
|
|
35
|
+
|
|
36
|
+
// Exception
|
|
37
|
+
export { RateLimitException } from './rate-limit.exception';
|
|
38
|
+
|
|
39
|
+
// Module
|
|
40
|
+
export { RateLimitModule } from './rate-limit.module';
|
|
41
|
+
|
|
42
|
+
// Types
|
|
43
|
+
export type {
|
|
44
|
+
RateLimitOptions,
|
|
45
|
+
RateLimitContext,
|
|
46
|
+
RateLimitResult,
|
|
47
|
+
RateLimitInfo,
|
|
48
|
+
RateLimitDimension,
|
|
49
|
+
RateLimitConfig,
|
|
50
|
+
RateLimitDimensionConfig,
|
|
51
|
+
RateLimitWhitelist,
|
|
52
|
+
RateLimitRedisConfig,
|
|
53
|
+
RateLimitEvent,
|
|
54
|
+
} from './dto/rate-limit.dto';
|