@nest-omni/core 4.1.3-0 → 4.1.3-10
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/audit/audit.module.d.ts +10 -0
- package/audit/audit.module.js +15 -0
- package/audit/controllers/audit.controller.d.ts +24 -0
- package/audit/controllers/audit.controller.js +24 -0
- package/audit/decorators/audit-controller.decorator.d.ts +8 -0
- package/audit/decorators/audit-controller.decorator.js +9 -0
- package/audit/decorators/audit-operation.decorator.d.ts +45 -0
- package/audit/decorators/audit-operation.decorator.js +49 -0
- package/audit/decorators/entity-audit.decorator.d.ts +8 -0
- package/audit/decorators/entity-audit.decorator.js +9 -0
- package/audit/dto/audit-log-query.dto.d.ts +3 -0
- package/audit/dto/audit-log-query.dto.js +3 -0
- package/audit/dto/begin-transaction.dto.d.ts +3 -0
- package/audit/dto/begin-transaction.dto.js +3 -0
- package/audit/dto/compare-entities.dto.d.ts +3 -0
- package/audit/dto/compare-entities.dto.js +3 -0
- package/audit/dto/pre-check-restore.dto.d.ts +3 -0
- package/audit/dto/pre-check-restore.dto.js +3 -0
- package/audit/dto/restore-entity.dto.d.ts +3 -0
- package/audit/dto/restore-entity.dto.js +3 -0
- package/audit/entities/entity-audit-log.entity.d.ts +3 -0
- package/audit/entities/entity-audit-log.entity.js +3 -0
- package/audit/entities/entity-transaction.entity.d.ts +3 -0
- package/audit/entities/entity-transaction.entity.js +3 -0
- package/audit/entities/manual-operation-log.entity.d.ts +4 -0
- package/audit/entities/manual-operation-log.entity.js +4 -0
- package/audit/entities/operation-template.entity.d.ts +4 -0
- package/audit/entities/operation-template.entity.js +4 -0
- package/audit/enums/audit.enums.d.ts +17 -2
- package/audit/enums/audit.enums.js +15 -0
- package/audit/index.js +10 -0
- package/audit/interceptors/audit.interceptor.d.ts +15 -0
- package/audit/interceptors/audit.interceptor.js +23 -1
- package/audit/interfaces/audit.interfaces.d.ts +42 -0
- package/audit/services/audit-context.service.d.ts +15 -0
- package/audit/services/audit-context.service.js +15 -0
- package/audit/services/audit-strategy.service.d.ts +6 -0
- package/audit/services/audit-strategy.service.js +13 -0
- package/audit/services/entity-audit.service.d.ts +57 -0
- package/audit/services/entity-audit.service.js +91 -0
- package/audit/services/manual-audit-log.service.d.ts +124 -0
- package/audit/services/manual-audit-log.service.js +138 -0
- package/audit/services/multi-database.service.d.ts +12 -0
- package/audit/services/multi-database.service.js +12 -0
- package/audit/services/operation-description.service.d.ts +59 -0
- package/audit/services/operation-description.service.js +76 -2
- package/audit/services/transaction-audit.service.d.ts +30 -0
- package/audit/services/transaction-audit.service.js +47 -0
- package/audit/subscribers/entity-audit.subscriber.d.ts +15 -0
- package/audit/subscribers/entity-audit.subscriber.js +29 -1
- package/cache/cache-metrics.service.d.ts +67 -0
- package/cache/cache-metrics.service.js +68 -4
- package/cache/cache-serialization.service.d.ts +31 -0
- package/cache/cache-serialization.service.js +25 -0
- package/cache/cache.constants.d.ts +9 -0
- package/cache/cache.constants.js +9 -0
- package/cache/cache.health.d.ts +26 -0
- package/cache/cache.health.js +30 -0
- package/cache/cache.module.d.ts +86 -0
- package/cache/cache.module.js +71 -0
- package/cache/cache.service.d.ts +140 -0
- package/cache/cache.service.js +157 -0
- package/cache/cache.warmup.service.d.ts +39 -0
- package/cache/cache.warmup.service.js +32 -0
- package/cache/decorators/cache-evict.decorator.d.ts +47 -0
- package/cache/decorators/cache-evict.decorator.js +56 -0
- package/cache/decorators/cache-put.decorator.d.ts +34 -0
- package/cache/decorators/cache-put.decorator.js +39 -0
- package/cache/decorators/cacheable.decorator.d.ts +40 -0
- package/cache/decorators/cacheable.decorator.js +55 -0
- package/cache/dependencies/callback.dependency.d.ts +33 -0
- package/cache/dependencies/callback.dependency.js +39 -1
- package/cache/dependencies/chain.dependency.d.ts +28 -0
- package/cache/dependencies/chain.dependency.js +34 -0
- package/cache/dependencies/db.dependency.d.ts +45 -0
- package/cache/dependencies/db.dependency.js +48 -1
- package/cache/dependencies/file.dependency.d.ts +32 -0
- package/cache/dependencies/file.dependency.js +34 -0
- package/cache/dependencies/tag.dependency.d.ts +36 -0
- package/cache/dependencies/tag.dependency.js +36 -0
- package/cache/dependencies/time.dependency.d.ts +43 -0
- package/cache/dependencies/time.dependency.js +43 -0
- package/cache/examples/basic-usage.d.ts +15 -0
- package/cache/examples/basic-usage.js +62 -8
- package/cache/index.js +9 -0
- package/cache/interfaces/cache-dependency.interface.d.ts +53 -0
- package/cache/interfaces/cache-options.interface.d.ts +81 -0
- package/cache/interfaces/cache-options.interface.js +6 -0
- package/cache/interfaces/cache-provider.interface.d.ts +78 -0
- package/cache/providers/base-cache.provider.d.ts +14 -0
- package/cache/providers/base-cache.provider.js +16 -0
- package/cache/providers/cls-cache.provider.d.ts +20 -0
- package/cache/providers/cls-cache.provider.js +28 -0
- package/cache/providers/memory-cache.provider.d.ts +23 -0
- package/cache/providers/memory-cache.provider.js +26 -0
- package/cache/providers/redis-cache.provider.d.ts +26 -0
- package/cache/providers/redis-cache.provider.js +29 -0
- package/cache/utils/dependency-manager.util.d.ts +52 -0
- package/cache/utils/dependency-manager.util.js +59 -0
- package/cache/utils/key-generator.util.d.ts +42 -0
- package/cache/utils/key-generator.util.js +53 -1
- package/common/abstract.entity.d.ts +14 -0
- package/common/abstract.entity.js +14 -0
- package/common/boilerplate.polyfill.d.ts +142 -4
- package/common/boilerplate.polyfill.js +24 -131
- package/common/dto/dto-container.d.ts +16 -0
- package/common/dto/dto-container.js +20 -0
- package/common/dto/dto-decorators.d.ts +18 -0
- package/common/dto/dto-decorators.js +14 -0
- package/common/dto/dto-extensions.d.ts +11 -0
- package/common/dto/dto-extensions.js +9 -0
- package/common/dto/dto-service-accessor.d.ts +17 -0
- package/common/dto/dto-service-accessor.js +18 -0
- package/common/dto/dto-transformer.d.ts +12 -0
- package/common/dto/dto-transformer.js +9 -0
- package/common/dto/index.js +2 -0
- package/common/examples/paginate-and-map.example.d.ts +6 -0
- package/common/examples/paginate-and-map.example.js +26 -0
- package/common/utils.d.ts +15 -0
- package/common/utils.js +15 -0
- package/constants/language-code.js +1 -0
- package/decorators/field.decorators.js +8 -1
- package/decorators/property.decorators.js +1 -0
- package/decorators/public-route.decorator.js +1 -0
- package/decorators/transform.decorators.d.ts +27 -0
- package/decorators/transform.decorators.js +29 -0
- package/decorators/translate.decorator.js +1 -0
- package/decorators/user.decorator.js +1 -0
- package/decorators/validator.decorators.d.ts +8 -18
- package/decorators/validator.decorators.js +22 -190
- package/filters/constraint-errors.js +1 -0
- package/helpers/common.helper.d.ts +13 -0
- package/helpers/common.helper.js +13 -0
- package/http-client/config/http-client.config.d.ts +15 -0
- package/http-client/config/http-client.config.js +25 -9
- package/http-client/decorators/http-client.decorators.d.ts +63 -0
- package/http-client/decorators/http-client.decorators.js +71 -3
- package/http-client/entities/http-log.entity.d.ts +229 -0
- package/http-client/entities/http-log.entity.js +6 -1
- package/http-client/errors/http-client.errors.d.ts +57 -0
- package/http-client/errors/http-client.errors.js +58 -0
- package/http-client/examples/advanced-usage.example.d.ts +41 -0
- package/http-client/examples/advanced-usage.example.js +68 -24
- package/http-client/examples/auth-with-waiting-lock.example.d.ts +31 -0
- package/http-client/examples/auth-with-waiting-lock.example.js +52 -5
- package/http-client/examples/basic-usage.example.d.ts +60 -0
- package/http-client/examples/basic-usage.example.js +60 -0
- package/http-client/examples/multi-api-configuration.example.d.ts +60 -0
- package/http-client/examples/multi-api-configuration.example.js +76 -5
- package/http-client/http-client.module.d.ts +13 -0
- package/http-client/http-client.module.js +19 -0
- package/http-client/index.js +8 -0
- package/http-client/interfaces/api-client-config.interface.d.ts +125 -0
- package/http-client/interfaces/api-client-config.interface.js +3 -0
- package/http-client/interfaces/http-client-config.interface.d.ts +60 -0
- package/http-client/services/api-client-registry.service.d.ts +57 -0
- package/http-client/services/api-client-registry.service.js +84 -1
- package/http-client/services/cache.service.d.ts +52 -0
- package/http-client/services/cache.service.js +72 -3
- package/http-client/services/circuit-breaker.service.d.ts +46 -0
- package/http-client/services/circuit-breaker.service.js +52 -0
- package/http-client/services/http-client.service.d.ts +67 -0
- package/http-client/services/http-client.service.js +105 -4
- package/http-client/services/http-log-query.service.d.ts +83 -0
- package/http-client/services/http-log-query.service.js +122 -1
- package/http-client/services/http-replay.service.d.ts +101 -0
- package/http-client/services/http-replay.service.js +86 -0
- package/http-client/services/log-cleanup.service.d.ts +63 -0
- package/http-client/services/log-cleanup.service.js +54 -2
- package/http-client/services/logging.service.d.ts +40 -0
- package/http-client/services/logging.service.js +53 -0
- package/http-client/utils/call-stack-extractor.util.d.ts +37 -0
- package/http-client/utils/call-stack-extractor.util.js +48 -0
- package/http-client/utils/context-extractor.util.d.ts +49 -0
- package/http-client/utils/context-extractor.util.js +52 -0
- package/http-client/utils/curl-generator.util.d.ts +21 -0
- package/http-client/utils/curl-generator.util.js +44 -3
- package/http-client/utils/request-id.util.d.ts +18 -0
- package/http-client/utils/request-id.util.js +20 -0
- package/http-client/utils/retry-recorder.util.d.ts +42 -0
- package/http-client/utils/retry-recorder.util.js +44 -0
- package/i18n/en_US/validation.json +2 -1
- package/i18n/zh_CN/validation.json +2 -1
- package/index.js +8 -0
- package/interceptors/translation-interceptor.service.js +5 -0
- package/package.json +1 -1
- package/providers/context.provider.js +2 -0
- package/providers/generator.provider.d.ts +4 -0
- package/providers/generator.provider.js +4 -0
- package/redis-lock/comprehensive-lock-cleanup.service.d.ts +94 -0
- package/redis-lock/comprehensive-lock-cleanup.service.js +253 -0
- package/redis-lock/examples/lock-strategy.examples.d.ts +89 -0
- package/redis-lock/examples/lock-strategy.examples.js +130 -15
- package/redis-lock/index.d.ts +2 -0
- package/redis-lock/index.js +8 -1
- package/redis-lock/lock-heartbeat.service.d.ts +78 -0
- package/redis-lock/lock-heartbeat.service.js +222 -0
- package/redis-lock/redis-lock.decorator.d.ts +101 -0
- package/redis-lock/redis-lock.decorator.js +120 -0
- package/redis-lock/redis-lock.module.d.ts +66 -0
- package/redis-lock/redis-lock.module.js +175 -70
- package/redis-lock/redis-lock.service.d.ts +260 -0
- package/redis-lock/redis-lock.service.js +244 -4
- package/setup/bootstrap.setup.js +20 -0
- package/setup/mode.setup.d.ts +44 -0
- package/setup/mode.setup.js +44 -0
- package/setup/schedule.decorator.d.ts +227 -0
- package/setup/schedule.decorator.js +219 -6
- package/setup/worker.decorator.d.ts +86 -0
- package/setup/worker.decorator.js +88 -0
- package/shared/serviceRegistryModule.js +9 -1
- package/shared/services/api-config.service.d.ts +3 -0
- package/shared/services/api-config.service.js +20 -9
- package/validator-json/decorators.d.ts +17 -0
- package/validator-json/decorators.js +17 -2
- package/validator-json/default.d.ts +6 -0
- package/validator-json/default.js +30 -2
- package/validator-json/defaultConverters.js +1 -0
- package/validator-json/options.d.ts +23 -0
- package/validators/common-validators.d.ts +143 -0
- package/validators/common-validators.js +249 -0
- package/validators/custom-validate.examples.d.ts +96 -0
- package/validators/custom-validate.examples.js +400 -0
- package/validators/custom-validate.validator.d.ts +134 -0
- package/validators/custom-validate.validator.js +214 -0
- package/validators/index.d.ts +2 -0
- package/validators/index.js +2 -0
- package/validators/is-exists.validator.d.ts +18 -4
- package/validators/is-exists.validator.js +67 -6
- package/validators/is-unique.validator.d.ts +32 -5
- package/validators/is-unique.validator.js +99 -17
- package/validators/skip-empty.validator.d.ts +5 -0
- package/validators/skip-empty.validator.js +5 -0
- package/vault/interfaces/vault-options.interface.d.ts +9 -0
- package/vault/vault-config.loader.d.ts +30 -0
- package/vault/vault-config.loader.js +48 -1
- package/vault/vault-config.service.d.ts +53 -0
- package/vault/vault-config.service.js +57 -0
- package/vault/vault.module.d.ts +4 -0
- package/vault/vault.module.js +4 -0
- package/decorators/examples/validation-decorators.example.d.ts +0 -69
- package/decorators/examples/validation-decorators.example.js +0 -331
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
import { CacheOptions } from '../../cache/interfaces/cache-options.interface';
|
|
3
3
|
import { LogCleanupConfig } from '../services/log-cleanup.service';
|
|
4
|
+
/**
|
|
5
|
+
* HTTP客户端配置接口
|
|
6
|
+
* 基于Spring Boot的@ConfigurationProperties设计理念
|
|
7
|
+
*/
|
|
4
8
|
export interface HttpClientConfig {
|
|
9
|
+
/** 基础配置 */
|
|
5
10
|
baseURL?: string;
|
|
6
11
|
timeout?: number;
|
|
12
|
+
/** 重试配置 */
|
|
7
13
|
retry?: RetryConfig;
|
|
14
|
+
/** 熔断器配置 */
|
|
8
15
|
circuitBreaker?: CircuitBreakerConfig;
|
|
16
|
+
/** 缓存配置 */
|
|
9
17
|
cache?: HttpCacheConfig;
|
|
18
|
+
/** 代理配置 */
|
|
10
19
|
proxy?: ProxyConfig;
|
|
20
|
+
/** 日志配置 */
|
|
11
21
|
logging?: LoggingConfig;
|
|
22
|
+
/** 日志清理配置 */
|
|
12
23
|
logCleanup?: LogCleanupConfig;
|
|
24
|
+
/** 拦截器配置 */
|
|
13
25
|
interceptors?: InterceptorConfig;
|
|
26
|
+
/** 连接池配置 */
|
|
14
27
|
connectionPool?: ConnectionPoolConfig;
|
|
15
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* 重试配置
|
|
31
|
+
* 基于axios-retry库的配置
|
|
32
|
+
*/
|
|
16
33
|
export interface RetryConfig {
|
|
17
34
|
enabled: boolean;
|
|
18
35
|
retries?: number;
|
|
@@ -21,6 +38,10 @@ export interface RetryConfig {
|
|
|
21
38
|
shouldResetTimeout?: boolean;
|
|
22
39
|
onRetry?: (retryCount: number, error: any, requestConfig: AxiosRequestConfig) => void;
|
|
23
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* 熔断器配置
|
|
43
|
+
* 类似Spring Cloud CircuitBreaker
|
|
44
|
+
*/
|
|
24
45
|
export interface CircuitBreakerConfig {
|
|
25
46
|
enabled: boolean;
|
|
26
47
|
failureThreshold: number;
|
|
@@ -29,14 +50,26 @@ export interface CircuitBreakerConfig {
|
|
|
29
50
|
minimumThroughputThreshold: number;
|
|
30
51
|
countHalfOpenCalls: boolean;
|
|
31
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* HTTP缓存配置
|
|
55
|
+
* 集成现有的三层缓存架构
|
|
56
|
+
*/
|
|
32
57
|
export interface HttpCacheConfig {
|
|
33
58
|
enabled: boolean;
|
|
59
|
+
/** 缓存选项,使用core中的CacheOptions */
|
|
34
60
|
options?: Omit<CacheOptions, 'ttl' | 'namespace'>;
|
|
61
|
+
/** 默认TTL(毫秒) */
|
|
35
62
|
defaultTtl?: number;
|
|
63
|
+
/** 可缓存的方法 */
|
|
36
64
|
cacheableMethods: string[];
|
|
65
|
+
/** 可缓存的状态码 */
|
|
37
66
|
cacheableStatusCodes: number[];
|
|
67
|
+
/** 自定义缓存键生成器 */
|
|
38
68
|
keyGenerator?: (config: AxiosRequestConfig) => string;
|
|
39
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* 代理配置
|
|
72
|
+
*/
|
|
40
73
|
export interface ProxyConfig {
|
|
41
74
|
enabled: boolean;
|
|
42
75
|
host?: string;
|
|
@@ -47,6 +80,10 @@ export interface ProxyConfig {
|
|
|
47
80
|
password: string;
|
|
48
81
|
};
|
|
49
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* 日志配置
|
|
85
|
+
* 类似Spring Boot的logging配置
|
|
86
|
+
*/
|
|
50
87
|
export interface LoggingConfig {
|
|
51
88
|
enabled: boolean;
|
|
52
89
|
logRequests: boolean;
|
|
@@ -63,11 +100,17 @@ export interface LoggingConfig {
|
|
|
63
100
|
tableName: string;
|
|
64
101
|
};
|
|
65
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* 拦截器配置
|
|
105
|
+
*/
|
|
66
106
|
export interface InterceptorConfig {
|
|
67
107
|
requestInterceptors: string[];
|
|
68
108
|
responseInterceptors: string[];
|
|
69
109
|
errorInterceptors: string[];
|
|
70
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* 连接池配置
|
|
113
|
+
*/
|
|
71
114
|
export interface ConnectionPoolConfig {
|
|
72
115
|
enabled: boolean;
|
|
73
116
|
maxSockets: number;
|
|
@@ -75,6 +118,10 @@ export interface ConnectionPoolConfig {
|
|
|
75
118
|
timeoutMs: number;
|
|
76
119
|
keepAlive: boolean;
|
|
77
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* HTTP请求上下文
|
|
123
|
+
* 类似Spring的RequestContextHolder
|
|
124
|
+
*/
|
|
78
125
|
export interface HttpContext {
|
|
79
126
|
requestId: string;
|
|
80
127
|
startTime: number;
|
|
@@ -85,6 +132,10 @@ export interface HttpContext {
|
|
|
85
132
|
correlationId?: string;
|
|
86
133
|
metadata: Record<string, any>;
|
|
87
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* HTTP请求日志实体
|
|
137
|
+
* 用于TypeORM数据库记录
|
|
138
|
+
*/
|
|
88
139
|
export interface HttpLogEntity {
|
|
89
140
|
id: string;
|
|
90
141
|
requestId: string;
|
|
@@ -101,12 +152,21 @@ export interface HttpLogEntity {
|
|
|
101
152
|
timestamp: Date;
|
|
102
153
|
metadata: Record<string, any>;
|
|
103
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* 拦截器接口
|
|
157
|
+
*/
|
|
104
158
|
export interface HttpInterceptor {
|
|
105
159
|
name: string;
|
|
106
160
|
order: number;
|
|
107
161
|
intercept(request: AxiosRequestConfig, next: () => Promise<AxiosResponse>): Promise<AxiosResponse>;
|
|
108
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* HTTP方法类型
|
|
165
|
+
*/
|
|
109
166
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
167
|
+
/**
|
|
168
|
+
* HTTP请求统计信息
|
|
169
|
+
*/
|
|
110
170
|
export interface HttpStats {
|
|
111
171
|
totalRequests: number;
|
|
112
172
|
successfulRequests: number;
|
|
@@ -4,6 +4,9 @@ import { HttpCircuitBreakerService } from './circuit-breaker.service';
|
|
|
4
4
|
import { HttpLoggingService } from './logging.service';
|
|
5
5
|
import { HttpCacheService } from './cache.service';
|
|
6
6
|
export { ApiClientInstanceConfig, AuthType, OAuth2Config, ResponseTransformerConfig, };
|
|
7
|
+
/**
|
|
8
|
+
* API客户端注册服务
|
|
9
|
+
*/
|
|
7
10
|
export declare class ApiClientRegistryService {
|
|
8
11
|
private readonly httpClientService;
|
|
9
12
|
private readonly circuitBreakerService;
|
|
@@ -14,18 +17,57 @@ export declare class ApiClientRegistryService {
|
|
|
14
17
|
private readonly clientConfigs;
|
|
15
18
|
private readonly globalDefaults;
|
|
16
19
|
constructor(httpClientService: HttpClientService, circuitBreakerService: HttpCircuitBreakerService, loggingService: HttpLoggingService, cacheService: HttpCacheService, globalConfig?: ApiClientRegistryConfig);
|
|
20
|
+
/**
|
|
21
|
+
* 创建API客户端实例
|
|
22
|
+
*/
|
|
17
23
|
createClient<T = any>(config: ApiClientInstanceConfig): T;
|
|
24
|
+
/**
|
|
25
|
+
* 获取已注册的客户端
|
|
26
|
+
*/
|
|
18
27
|
getClient<T = any>(name: string): T;
|
|
28
|
+
/**
|
|
29
|
+
* 列出所有已注册的客户端
|
|
30
|
+
*/
|
|
19
31
|
listClients(): string[];
|
|
32
|
+
/**
|
|
33
|
+
* 更新客户端配置
|
|
34
|
+
*/
|
|
20
35
|
updateClient(name: string, config: Partial<ApiClientConfig>): void;
|
|
36
|
+
/**
|
|
37
|
+
* 移除客户端
|
|
38
|
+
*/
|
|
21
39
|
removeClient(name: string): void;
|
|
40
|
+
/**
|
|
41
|
+
* 获取客户端配置
|
|
42
|
+
*/
|
|
22
43
|
getClientConfig(name: string): ApiClientConfig | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* 获取所有客户端配置
|
|
46
|
+
*/
|
|
23
47
|
getAllClientConfigs(): Record<string, ApiClientConfig>;
|
|
48
|
+
/**
|
|
49
|
+
* 批量创建客户端
|
|
50
|
+
*/
|
|
24
51
|
createClients(configs: ApiClientInstanceConfig[]): void;
|
|
52
|
+
/**
|
|
53
|
+
* 获取全局配置
|
|
54
|
+
*/
|
|
25
55
|
getGlobalConfig(): ApiClientRegistryConfig;
|
|
56
|
+
/**
|
|
57
|
+
* 更新全局配置
|
|
58
|
+
*/
|
|
26
59
|
updateGlobalConfig(config: Partial<ApiClientRegistryConfig>): void;
|
|
60
|
+
/**
|
|
61
|
+
* 获取统计信息
|
|
62
|
+
*/
|
|
27
63
|
getAllStats(): Record<string, any>;
|
|
64
|
+
/**
|
|
65
|
+
* 重置所有统计信息
|
|
66
|
+
*/
|
|
28
67
|
resetAllStats(): void;
|
|
68
|
+
/**
|
|
69
|
+
* 健康检查
|
|
70
|
+
*/
|
|
29
71
|
healthCheck(): Promise<{
|
|
30
72
|
status: 'healthy' | 'unhealthy';
|
|
31
73
|
clients: Record<string, {
|
|
@@ -33,9 +75,24 @@ export declare class ApiClientRegistryService {
|
|
|
33
75
|
error?: string;
|
|
34
76
|
}>;
|
|
35
77
|
}>;
|
|
78
|
+
/**
|
|
79
|
+
* 创建Axios实例
|
|
80
|
+
*/
|
|
36
81
|
private createAxiosInstance;
|
|
82
|
+
/**
|
|
83
|
+
* 应用鉴权配置
|
|
84
|
+
*/
|
|
37
85
|
private applyAuthentication;
|
|
86
|
+
/**
|
|
87
|
+
* 应用响应转换器
|
|
88
|
+
*/
|
|
38
89
|
private applyResponseTransformer;
|
|
90
|
+
/**
|
|
91
|
+
* 根据路径提取数据
|
|
92
|
+
*/
|
|
39
93
|
private extractDataByPath;
|
|
94
|
+
/**
|
|
95
|
+
* 合并环境特定配置
|
|
96
|
+
*/
|
|
40
97
|
private mergeEnvironmentConfig;
|
|
41
98
|
}
|
|
@@ -31,6 +31,9 @@ const logging_service_1 = require("./logging.service");
|
|
|
31
31
|
const cache_service_1 = require("./cache.service");
|
|
32
32
|
const context_extractor_util_1 = require("../utils/context-extractor.util");
|
|
33
33
|
const utils_1 = require("../../common/utils");
|
|
34
|
+
/**
|
|
35
|
+
* API客户端注册服务
|
|
36
|
+
*/
|
|
34
37
|
let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegistryService {
|
|
35
38
|
constructor(httpClientService, circuitBreakerService, loggingService, cacheService, globalConfig = {}) {
|
|
36
39
|
this.httpClientService = httpClientService;
|
|
@@ -60,6 +63,9 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
60
63
|
maxRecords: 1000000,
|
|
61
64
|
}, enableGlobalRetry: true, enableCircuitBreaker: true }, globalConfig);
|
|
62
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* 创建API客户端实例
|
|
68
|
+
*/
|
|
63
69
|
createClient(config) {
|
|
64
70
|
const clientName = config.name;
|
|
65
71
|
const existingClient = this.clients.get(clientName);
|
|
@@ -67,14 +73,21 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
67
73
|
this.logger.warn(`Client '${clientName}' already exists. Use override: true to replace it.`);
|
|
68
74
|
return existingClient;
|
|
69
75
|
}
|
|
76
|
+
// 合并环境特定配置
|
|
70
77
|
const finalConfig = this.mergeEnvironmentConfig(config);
|
|
78
|
+
// 创建Axios实例
|
|
71
79
|
const axiosInstance = this.createAxiosInstance(finalConfig);
|
|
80
|
+
// 创建API客户端
|
|
72
81
|
const client = new ApiClientInstance(clientName, finalConfig, axiosInstance, this.httpClientService, this.loggingService, this.cacheService);
|
|
82
|
+
// 存储客户端和配置
|
|
73
83
|
this.clients.set(clientName, client);
|
|
74
84
|
this.clientConfigs.set(clientName, finalConfig);
|
|
75
85
|
this.logger.log(`Created API client: ${clientName}`);
|
|
76
86
|
return client;
|
|
77
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* 获取已注册的客户端
|
|
90
|
+
*/
|
|
78
91
|
getClient(name) {
|
|
79
92
|
const client = this.clients.get(name);
|
|
80
93
|
if (!client) {
|
|
@@ -82,9 +95,15 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
82
95
|
}
|
|
83
96
|
return client;
|
|
84
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* 列出所有已注册的客户端
|
|
100
|
+
*/
|
|
85
101
|
listClients() {
|
|
86
102
|
return Array.from(this.clients.keys());
|
|
87
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* 更新客户端配置
|
|
106
|
+
*/
|
|
88
107
|
updateClient(name, config) {
|
|
89
108
|
const existingConfig = this.clientConfigs.get(name);
|
|
90
109
|
if (!existingConfig) {
|
|
@@ -92,9 +111,13 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
92
111
|
}
|
|
93
112
|
const updatedConfig = Object.assign(Object.assign({}, existingConfig), config);
|
|
94
113
|
this.clientConfigs.set(name, updatedConfig);
|
|
114
|
+
// 重新创建客户端实例
|
|
95
115
|
this.createClient(Object.assign(Object.assign({}, updatedConfig), { name, override: true }));
|
|
96
116
|
this.logger.log(`Updated API client: ${name}`);
|
|
97
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* 移除客户端
|
|
120
|
+
*/
|
|
98
121
|
removeClient(name) {
|
|
99
122
|
const removed = this.clients.delete(name);
|
|
100
123
|
this.clientConfigs.delete(name);
|
|
@@ -102,9 +125,15 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
102
125
|
this.logger.log(`Removed API client: ${name}`);
|
|
103
126
|
}
|
|
104
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* 获取客户端配置
|
|
130
|
+
*/
|
|
105
131
|
getClientConfig(name) {
|
|
106
132
|
return this.clientConfigs.get(name);
|
|
107
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* 获取所有客户端配置
|
|
136
|
+
*/
|
|
108
137
|
getAllClientConfigs() {
|
|
109
138
|
const configs = {};
|
|
110
139
|
this.clientConfigs.forEach((config, name) => {
|
|
@@ -112,6 +141,9 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
112
141
|
});
|
|
113
142
|
return configs;
|
|
114
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* 批量创建客户端
|
|
146
|
+
*/
|
|
115
147
|
createClients(configs) {
|
|
116
148
|
configs.forEach((config) => {
|
|
117
149
|
try {
|
|
@@ -122,13 +154,22 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
122
154
|
}
|
|
123
155
|
});
|
|
124
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* 获取全局配置
|
|
159
|
+
*/
|
|
125
160
|
getGlobalConfig() {
|
|
126
161
|
return this.globalDefaults;
|
|
127
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* 更新全局配置
|
|
165
|
+
*/
|
|
128
166
|
updateGlobalConfig(config) {
|
|
129
167
|
Object.assign(this.globalDefaults, config);
|
|
130
168
|
this.logger.log('Global configuration updated');
|
|
131
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* 获取统计信息
|
|
172
|
+
*/
|
|
132
173
|
getAllStats() {
|
|
133
174
|
const stats = {};
|
|
134
175
|
this.clients.forEach((client, name) => {
|
|
@@ -136,17 +177,24 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
136
177
|
});
|
|
137
178
|
return stats;
|
|
138
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* 重置所有统计信息
|
|
182
|
+
*/
|
|
139
183
|
resetAllStats() {
|
|
140
184
|
this.clients.forEach((client) => {
|
|
141
185
|
client.resetStats();
|
|
142
186
|
});
|
|
143
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* 健康检查
|
|
190
|
+
*/
|
|
144
191
|
healthCheck() {
|
|
145
192
|
return __awaiter(this, void 0, void 0, function* () {
|
|
146
193
|
const results = {};
|
|
147
194
|
let overallStatus = 'healthy';
|
|
148
195
|
for (const [name, client] of this.clients.entries()) {
|
|
149
196
|
try {
|
|
197
|
+
// 简单的健康检查:尝试获取客户端统计信息
|
|
150
198
|
client.getStats();
|
|
151
199
|
results[name] = { status: 'healthy' };
|
|
152
200
|
}
|
|
@@ -161,6 +209,9 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
161
209
|
};
|
|
162
210
|
});
|
|
163
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* 创建Axios实例
|
|
214
|
+
*/
|
|
164
215
|
createAxiosInstance(config) {
|
|
165
216
|
var _a, _b, _c, _d, _e, _f;
|
|
166
217
|
const axiosConfig = {
|
|
@@ -170,9 +221,12 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
170
221
|
params: config.defaultParams,
|
|
171
222
|
};
|
|
172
223
|
const instance = axios_1.default.create(axiosConfig);
|
|
173
|
-
const clientName = config.name;
|
|
224
|
+
const clientName = config.name; // Capture client name for use in closures
|
|
225
|
+
// 配置请求拦截器
|
|
174
226
|
instance.interceptors.request.use((config) => {
|
|
227
|
+
// 添加鉴权信息 - for now, skip async authentication to avoid type issues
|
|
175
228
|
const authenticatedConfig = config;
|
|
229
|
+
// Add simple auth headers for basic cases
|
|
176
230
|
if (config.auth) {
|
|
177
231
|
switch (config.auth.type) {
|
|
178
232
|
case api_client_config_interface_1.AuthType.BEARER_TOKEN:
|
|
@@ -194,6 +248,7 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
194
248
|
break;
|
|
195
249
|
}
|
|
196
250
|
}
|
|
251
|
+
// 添加请求ID和上下文信息
|
|
197
252
|
const context = context_extractor_util_1.ContextExtractor.getHttpContext();
|
|
198
253
|
const requestId = context.requestId || (0, utils_1.generateRequestId)();
|
|
199
254
|
authenticatedConfig.headers = authenticatedConfig.headers || {};
|
|
@@ -201,21 +256,26 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
201
256
|
if (context.userId) {
|
|
202
257
|
authenticatedConfig.headers['X-User-ID'] = context.userId;
|
|
203
258
|
}
|
|
259
|
+
// 添加API标识
|
|
204
260
|
authenticatedConfig.headers['X-API-Client'] = clientName;
|
|
205
261
|
return authenticatedConfig;
|
|
206
262
|
}, (error) => {
|
|
207
263
|
this.logger.error(`[${clientName}] Request interceptor error`, error);
|
|
208
264
|
return Promise.reject(error);
|
|
209
265
|
});
|
|
266
|
+
// 配置响应拦截器
|
|
210
267
|
instance.interceptors.response.use((response) => {
|
|
268
|
+
// 应用响应转换器
|
|
211
269
|
return this.applyResponseTransformer(response, config.responseTransformer);
|
|
212
270
|
}, (error) => {
|
|
213
271
|
var _a;
|
|
272
|
+
// 应用错误转换器
|
|
214
273
|
if ((_a = config.responseTransformer) === null || _a === void 0 ? void 0 : _a.error) {
|
|
215
274
|
return config.responseTransformer.error(error);
|
|
216
275
|
}
|
|
217
276
|
return Promise.reject(error);
|
|
218
277
|
});
|
|
278
|
+
// 配置axios-retry
|
|
219
279
|
if (((_b = config.retry) === null || _b === void 0 ? void 0 : _b.enabled) !== false &&
|
|
220
280
|
this.globalDefaults.enableGlobalRetry) {
|
|
221
281
|
(0, axios_retry_1.default)(instance, {
|
|
@@ -239,6 +299,9 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
239
299
|
}
|
|
240
300
|
return instance;
|
|
241
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* 应用鉴权配置
|
|
304
|
+
*/
|
|
242
305
|
applyAuthentication(config, auth) {
|
|
243
306
|
return __awaiter(this, void 0, void 0, function* () {
|
|
244
307
|
if (!auth) {
|
|
@@ -274,6 +337,8 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
274
337
|
break;
|
|
275
338
|
case api_client_config_interface_1.AuthType.OAUTH2:
|
|
276
339
|
const oauthConfig = auth.config;
|
|
340
|
+
// OAuth2实现需要异步获取token
|
|
341
|
+
// 这里简化处理,实际项目中可能需要更复杂的逻辑
|
|
277
342
|
authenticatedConfig.headers = Object.assign(Object.assign({}, authenticatedConfig.headers), { Authorization: `Bearer ${oauthConfig.clientSecret}` });
|
|
278
343
|
break;
|
|
279
344
|
case api_client_config_interface_1.AuthType.CUSTOM:
|
|
@@ -283,16 +348,22 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
283
348
|
return authenticatedConfig;
|
|
284
349
|
});
|
|
285
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* 应用响应转换器
|
|
353
|
+
*/
|
|
286
354
|
applyResponseTransformer(response, transformer) {
|
|
287
355
|
if (!transformer) {
|
|
288
356
|
return response;
|
|
289
357
|
}
|
|
358
|
+
// 应用成功响应转换器
|
|
290
359
|
if (transformer.success) {
|
|
291
360
|
response.data = transformer.success(response);
|
|
292
361
|
}
|
|
362
|
+
// 应用数据路径提取
|
|
293
363
|
if (transformer.dataPath) {
|
|
294
364
|
response.data = this.extractDataByPath(response.data, transformer.dataPath);
|
|
295
365
|
}
|
|
366
|
+
// 应用响应验证
|
|
296
367
|
if (transformer.validateResponse && transformer.validator) {
|
|
297
368
|
if (!transformer.validator(response.data)) {
|
|
298
369
|
throw new Error(`Response validation failed for ${transformer.dataPath}`);
|
|
@@ -300,6 +371,9 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
300
371
|
}
|
|
301
372
|
return response;
|
|
302
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* 根据路径提取数据
|
|
376
|
+
*/
|
|
303
377
|
extractDataByPath(data, path) {
|
|
304
378
|
const keys = path.split('.');
|
|
305
379
|
let current = data;
|
|
@@ -311,6 +385,9 @@ let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegis
|
|
|
311
385
|
}
|
|
312
386
|
return current;
|
|
313
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* 合并环境特定配置
|
|
390
|
+
*/
|
|
314
391
|
mergeEnvironmentConfig(config) {
|
|
315
392
|
var _a;
|
|
316
393
|
const env = process.env.NODE_ENV || 'development';
|
|
@@ -326,6 +403,9 @@ exports.ApiClientRegistryService = ApiClientRegistryService = ApiClientRegistryS
|
|
|
326
403
|
logging_service_1.HttpLoggingService,
|
|
327
404
|
cache_service_1.HttpCacheService, Object])
|
|
328
405
|
], ApiClientRegistryService);
|
|
406
|
+
/**
|
|
407
|
+
* API客户端实例实现
|
|
408
|
+
*/
|
|
329
409
|
class ApiClientInstance {
|
|
330
410
|
constructor(name, config, axiosInstance, httpClientService, loggingService, cacheService) {
|
|
331
411
|
this.name = name;
|
|
@@ -373,11 +453,13 @@ class ApiClientInstance {
|
|
|
373
453
|
this.stats.totalRequests++;
|
|
374
454
|
try {
|
|
375
455
|
const response = yield this.axiosInstance.request(config);
|
|
456
|
+
// 更新统计信息
|
|
376
457
|
const responseTime = Date.now() - startTime;
|
|
377
458
|
this.updateStats(true, responseTime);
|
|
378
459
|
return response.data;
|
|
379
460
|
}
|
|
380
461
|
catch (error) {
|
|
462
|
+
// 更新统计信息
|
|
381
463
|
const responseTime = Date.now() - startTime;
|
|
382
464
|
this.updateStats(false, responseTime);
|
|
383
465
|
throw error;
|
|
@@ -404,6 +486,7 @@ class ApiClientInstance {
|
|
|
404
486
|
this.stats.totalResponseTime += responseTime;
|
|
405
487
|
this.stats.averageResponseTime =
|
|
406
488
|
this.stats.totalResponseTime / this.stats.totalRequests;
|
|
489
|
+
// 响应时间警告
|
|
407
490
|
if (this.config.responseTimeWarningThreshold &&
|
|
408
491
|
responseTime > this.config.responseTimeWarningThreshold) {
|
|
409
492
|
console.warn(`[API Client ${this.name}] Slow response detected: ${responseTime}ms for ${this.axiosInstance.defaults.baseURL}`);
|
|
@@ -1,24 +1,76 @@
|
|
|
1
1
|
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
import { HttpCacheConfig } from '../interfaces/http-client-config.interface';
|
|
3
3
|
import { CacheService } from '../../cache/cache.service';
|
|
4
|
+
/**
|
|
5
|
+
* HTTP缓存服务
|
|
6
|
+
* 集成现有的三层缓存架构
|
|
7
|
+
*/
|
|
4
8
|
export declare class HttpCacheService {
|
|
5
9
|
private readonly cacheService;
|
|
6
10
|
private readonly logger;
|
|
7
11
|
constructor(cacheService: CacheService);
|
|
12
|
+
/**
|
|
13
|
+
* 获取缓存的响应
|
|
14
|
+
*/
|
|
8
15
|
get(config: AxiosRequestConfig, cacheOptions: HttpCacheConfig): Promise<AxiosResponse | null>;
|
|
16
|
+
/**
|
|
17
|
+
* 缓存响应
|
|
18
|
+
*/
|
|
9
19
|
set(config: AxiosRequestConfig, response: AxiosResponse, cacheOptions: HttpCacheConfig): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* 清除缓存
|
|
22
|
+
*/
|
|
10
23
|
clear(pattern?: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* 获取缓存统计信息
|
|
26
|
+
*/
|
|
11
27
|
getStats(): Promise<any>;
|
|
28
|
+
/**
|
|
29
|
+
* 清理过期缓存
|
|
30
|
+
*/
|
|
12
31
|
cleanup(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* 预热缓存
|
|
34
|
+
*/
|
|
13
35
|
warmup(urls: string[], headers?: Record<string, string>): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* 获取缓存命中率详情
|
|
38
|
+
*/
|
|
14
39
|
getDetailedStats(): Promise<any>;
|
|
40
|
+
/**
|
|
41
|
+
* 生成缓存键
|
|
42
|
+
*/
|
|
15
43
|
private generateCacheKey;
|
|
44
|
+
/**
|
|
45
|
+
* 构建完整的缓存键
|
|
46
|
+
*/
|
|
16
47
|
private buildCacheKey;
|
|
48
|
+
/**
|
|
49
|
+
* 映射缓存层
|
|
50
|
+
*/
|
|
17
51
|
private mapLayers;
|
|
52
|
+
/**
|
|
53
|
+
* 选择用于缓存键的头信息
|
|
54
|
+
*/
|
|
18
55
|
private selectHeadersForCacheKey;
|
|
56
|
+
/**
|
|
57
|
+
* 判断响应是否可缓存
|
|
58
|
+
*/
|
|
19
59
|
private isResponseCacheable;
|
|
60
|
+
/**
|
|
61
|
+
* 哈希对象
|
|
62
|
+
*/
|
|
20
63
|
private hashObject;
|
|
64
|
+
/**
|
|
65
|
+
* 计算综合命中率
|
|
66
|
+
*/
|
|
21
67
|
private calculateHitRate;
|
|
68
|
+
/**
|
|
69
|
+
* 获取最近的缓存活动
|
|
70
|
+
*/
|
|
22
71
|
private getRecentCacheActivity;
|
|
72
|
+
/**
|
|
73
|
+
* 生成缓存优化建议
|
|
74
|
+
*/
|
|
23
75
|
private generateRecommendations;
|
|
24
76
|
}
|