@lark-apaas/fullstack-nestjs-core 1.1.22-beta.5 → 1.1.23-alpha.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/dist/index.cjs +53 -3
- package/dist/index.d.cts +142 -2
- package/dist/index.d.ts +142 -2
- package/dist/index.js +53 -4
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -39,6 +39,7 @@ __export(index_exports, {
|
|
|
39
39
|
DevToolsModule: () => import_nestjs_openapi_devtools2.DevToolsModule,
|
|
40
40
|
DevToolsV2Module: () => import_nestjs_openapi_devtools2.DevToolsV2Module,
|
|
41
41
|
FileService: () => FileService,
|
|
42
|
+
PlatformHttpClientService: () => PlatformHttpClientService,
|
|
42
43
|
PlatformModule: () => PlatformModule,
|
|
43
44
|
UserContextMiddleware: () => UserContextMiddleware,
|
|
44
45
|
ViewContextMiddleware: () => ViewContextMiddleware,
|
|
@@ -759,10 +760,47 @@ var PlatformHttpClientService = class _PlatformHttpClientService {
|
|
|
759
760
|
});
|
|
760
761
|
}
|
|
761
762
|
/**
|
|
762
|
-
*
|
|
763
|
+
* 创建一个带全局拦截器的独立 HttpClient 实例
|
|
764
|
+
*
|
|
765
|
+
* 与 `create()` 类似,但会自动注册全局拦截器(日志、x-tt-env 透传等)
|
|
766
|
+
* 适用于需要保留平台标准行为,同时又需要添加自定义拦截器的场景
|
|
767
|
+
*
|
|
768
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
769
|
+
* @returns 完整的 HttpClient 实例(带全局拦截器,允许添加更多拦截器)
|
|
770
|
+
*
|
|
771
|
+
* @example
|
|
772
|
+
* ```typescript
|
|
773
|
+
* const client = this.platformHttp.createWithGlobalInterceptors();
|
|
774
|
+
* // 客户端已包含日志和 x-tt-env 拦截器
|
|
775
|
+
* // 可以继续添加自定义拦截器
|
|
776
|
+
* client.interceptors.request.use((config) => {
|
|
777
|
+
* config.headers['x-custom'] = 'value';
|
|
778
|
+
* return config;
|
|
779
|
+
* });
|
|
780
|
+
* ```
|
|
781
|
+
*/
|
|
782
|
+
createWithGlobalInterceptors(options) {
|
|
783
|
+
const client = this.create(options);
|
|
784
|
+
this.registerInterceptorsForClient(client);
|
|
785
|
+
return client;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* 注册全局拦截器(用于单例实例)
|
|
763
789
|
*/
|
|
764
790
|
registerGlobalInterceptors() {
|
|
765
|
-
this.client
|
|
791
|
+
this.registerInterceptorsForClient(this.client);
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* 为指定的 HttpClient 实例注册标准拦截器
|
|
795
|
+
*
|
|
796
|
+
* 包含:
|
|
797
|
+
* - 请求日志记录
|
|
798
|
+
* - x-tt-env header 透传
|
|
799
|
+
* - 响应日志记录
|
|
800
|
+
* - 错误日志记录
|
|
801
|
+
*/
|
|
802
|
+
registerInterceptorsForClient(client) {
|
|
803
|
+
client.interceptors.request.use((config) => {
|
|
766
804
|
this.logger.debug(`Server SDK HTTP Request: ${config.method?.toUpperCase()} ${config.url}`);
|
|
767
805
|
const ttEnv = this.requestContext.get("ttEnv");
|
|
768
806
|
if (ttEnv) {
|
|
@@ -776,7 +814,7 @@ var PlatformHttpClientService = class _PlatformHttpClientService {
|
|
|
776
814
|
this.logger.error("Server SDK HTTP Request Error", error, "HttpService");
|
|
777
815
|
return Promise.reject(error);
|
|
778
816
|
});
|
|
779
|
-
|
|
817
|
+
client.interceptors.response.use((response) => {
|
|
780
818
|
this.logger.debug(`Server SDK HTTP Response: ${response.status} ${response.url}`);
|
|
781
819
|
return response;
|
|
782
820
|
}, (error) => {
|
|
@@ -1290,6 +1328,15 @@ var PlatformModule = class _PlatformModule {
|
|
|
1290
1328
|
PlatformHttpClientService
|
|
1291
1329
|
]
|
|
1292
1330
|
},
|
|
1331
|
+
{
|
|
1332
|
+
provide: import_nestjs_common5.HTTP_CLIENT_FACTORY,
|
|
1333
|
+
useFactory: /* @__PURE__ */ __name((svc) => ({
|
|
1334
|
+
create: /* @__PURE__ */ __name((options2) => svc.createWithGlobalInterceptors(options2), "create")
|
|
1335
|
+
}), "useFactory"),
|
|
1336
|
+
inject: [
|
|
1337
|
+
PlatformHttpClientService
|
|
1338
|
+
]
|
|
1339
|
+
},
|
|
1293
1340
|
HttpInterceptorService,
|
|
1294
1341
|
{
|
|
1295
1342
|
provide: import_core2.APP_INTERCEPTOR,
|
|
@@ -1303,6 +1350,8 @@ var PlatformModule = class _PlatformModule {
|
|
|
1303
1350
|
import_nestjs_common5.CommonModule,
|
|
1304
1351
|
import_nestjs_common5.OBSERVABLE_SERVICE,
|
|
1305
1352
|
import_nestjs_common6.PLATFORM_HTTP_CLIENT,
|
|
1353
|
+
import_nestjs_common5.HTTP_CLIENT_FACTORY,
|
|
1354
|
+
PlatformHttpClientService,
|
|
1306
1355
|
import_nestjs_capability.CapabilityModule,
|
|
1307
1356
|
FileService
|
|
1308
1357
|
]
|
|
@@ -1386,6 +1435,7 @@ var import_nestjs_authzpaas2 = require("@lark-apaas/nestjs-authzpaas");
|
|
|
1386
1435
|
DevToolsModule,
|
|
1387
1436
|
DevToolsV2Module,
|
|
1388
1437
|
FileService,
|
|
1438
|
+
PlatformHttpClientService,
|
|
1389
1439
|
PlatformModule,
|
|
1390
1440
|
UserContextMiddleware,
|
|
1391
1441
|
ViewContextMiddleware,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NestModule, DynamicModule, MiddlewareConsumer, NestMiddleware } from '@nestjs/common';
|
|
2
|
-
import { HttpClientConfig, PlatformPluginOptions } from '@lark-apaas/http-client';
|
|
2
|
+
import { HttpClientConfig, PlatformPluginOptions, HttpClient } from '@lark-apaas/http-client';
|
|
3
3
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
4
4
|
export { DevToolsModule, DevToolsOptions, DevToolsV2Module, DevToolsV2Options } from '@lark-apaas/nestjs-openapi-devtools';
|
|
5
5
|
import { Request, Response, NextFunction } from 'express';
|
|
@@ -169,4 +169,144 @@ declare class FileService {
|
|
|
169
169
|
private _getFileMetadata;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
/**
|
|
173
|
+
* 平台 HttpClient 服务
|
|
174
|
+
*
|
|
175
|
+
* 提供两种使用方式:
|
|
176
|
+
* 1. 全局实例:通过 `instance` 或 `PLATFORM_HTTP_CLIENT` 注入,适用于 95% 的场景
|
|
177
|
+
* 2. 独立实例:通过 `create()` 创建,适用于需要自定义拦截器或配置的场景
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* 基本使用(推荐)
|
|
181
|
+
* ```typescript
|
|
182
|
+
* @Injectable()
|
|
183
|
+
* export class UserService {
|
|
184
|
+
* constructor(
|
|
185
|
+
* @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient
|
|
186
|
+
* ) {}
|
|
187
|
+
*
|
|
188
|
+
* async getUser() {
|
|
189
|
+
* return this.http.get('/user');
|
|
190
|
+
* }
|
|
191
|
+
* }
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* 需要自定义拦截器(高级场景)
|
|
196
|
+
* ```typescript
|
|
197
|
+
* @Injectable()
|
|
198
|
+
* export class FileService {
|
|
199
|
+
* private fileClient: HttpClient;
|
|
200
|
+
*
|
|
201
|
+
* constructor(private platformHttp: PlatformHttpClientService) {
|
|
202
|
+
* this.fileClient = this.platformHttp.create({
|
|
203
|
+
* config: { timeout: 60000 }
|
|
204
|
+
* });
|
|
205
|
+
*
|
|
206
|
+
* this.fileClient.interceptors.request.use((config) => {
|
|
207
|
+
* console.log('File upload starting...');
|
|
208
|
+
* return config;
|
|
209
|
+
* });
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
declare class PlatformHttpClientService {
|
|
215
|
+
private readonly requestContext;
|
|
216
|
+
private readonly client;
|
|
217
|
+
private readonly protectedClient;
|
|
218
|
+
private readonly logger;
|
|
219
|
+
constructor(requestContext: RequestContextService);
|
|
220
|
+
/**
|
|
221
|
+
* 获取受保护的 HttpClient 实例(推荐)
|
|
222
|
+
*
|
|
223
|
+
* 该实例不暴露 interceptors,防止业务代码修改全局拦截器
|
|
224
|
+
*
|
|
225
|
+
* @returns 受保护的 HttpClient 实例
|
|
226
|
+
*/
|
|
227
|
+
get instance(): PlatformHttpClient;
|
|
228
|
+
/**
|
|
229
|
+
* 获取原始 HttpClient 实例(危险)
|
|
230
|
+
*
|
|
231
|
+
* ⚠️ 警告:该实例允许修改拦截器,会影响所有使用全局实例的地方
|
|
232
|
+
*
|
|
233
|
+
* 仅用于特殊场景,不应该暴露给业务代码
|
|
234
|
+
* 如果需要自定义拦截器,请使用 `create()` 方法创建独立实例
|
|
235
|
+
*
|
|
236
|
+
* @internal
|
|
237
|
+
* @returns 原始 HttpClient 实例
|
|
238
|
+
*/
|
|
239
|
+
get rawInstance(): HttpClient;
|
|
240
|
+
/**
|
|
241
|
+
* 创建一个新的独立 HttpClient 实例
|
|
242
|
+
*
|
|
243
|
+
* 适用于需要自定义拦截器或配置的场景
|
|
244
|
+
* 创建的实例完全独立,不会影响全局实例
|
|
245
|
+
*
|
|
246
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
247
|
+
* @returns 完整的 HttpClient 实例(允许修改拦截器)
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* @Injectable()
|
|
252
|
+
* export class FileService {
|
|
253
|
+
* private fileClient: HttpClient;
|
|
254
|
+
*
|
|
255
|
+
* constructor(private platformHttp: PlatformHttpClientService) {
|
|
256
|
+
* // 创建超时 60 秒的独立实例
|
|
257
|
+
* this.fileClient = this.platformHttp.create({
|
|
258
|
+
* config: { timeout: 60000 }
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* // 为这个实例注册拦截器(不影响全局实例)
|
|
262
|
+
* this.fileClient.interceptors.request.use((config) => {
|
|
263
|
+
* console.log('File upload starting...');
|
|
264
|
+
* return config;
|
|
265
|
+
* });
|
|
266
|
+
* }
|
|
267
|
+
*
|
|
268
|
+
* async uploadFile(file: Buffer) {
|
|
269
|
+
* return this.fileClient.post('/file/upload', file);
|
|
270
|
+
* }
|
|
271
|
+
* }
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
create(options?: PlatformHttpClientOptions): HttpClient;
|
|
275
|
+
/**
|
|
276
|
+
* 创建一个带全局拦截器的独立 HttpClient 实例
|
|
277
|
+
*
|
|
278
|
+
* 与 `create()` 类似,但会自动注册全局拦截器(日志、x-tt-env 透传等)
|
|
279
|
+
* 适用于需要保留平台标准行为,同时又需要添加自定义拦截器的场景
|
|
280
|
+
*
|
|
281
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
282
|
+
* @returns 完整的 HttpClient 实例(带全局拦截器,允许添加更多拦截器)
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const client = this.platformHttp.createWithGlobalInterceptors();
|
|
287
|
+
* // 客户端已包含日志和 x-tt-env 拦截器
|
|
288
|
+
* // 可以继续添加自定义拦截器
|
|
289
|
+
* client.interceptors.request.use((config) => {
|
|
290
|
+
* config.headers['x-custom'] = 'value';
|
|
291
|
+
* return config;
|
|
292
|
+
* });
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
createWithGlobalInterceptors(options?: PlatformHttpClientOptions): HttpClient;
|
|
296
|
+
/**
|
|
297
|
+
* 注册全局拦截器(用于单例实例)
|
|
298
|
+
*/
|
|
299
|
+
private registerGlobalInterceptors;
|
|
300
|
+
/**
|
|
301
|
+
* 为指定的 HttpClient 实例注册标准拦截器
|
|
302
|
+
*
|
|
303
|
+
* 包含:
|
|
304
|
+
* - 请求日志记录
|
|
305
|
+
* - x-tt-env header 透传
|
|
306
|
+
* - 响应日志记录
|
|
307
|
+
* - 错误日志记录
|
|
308
|
+
*/
|
|
309
|
+
private registerInterceptorsForClient;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, FileService, type PlatformHttpClientOptions, PlatformHttpClientService, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NestModule, DynamicModule, MiddlewareConsumer, NestMiddleware } from '@nestjs/common';
|
|
2
|
-
import { HttpClientConfig, PlatformPluginOptions } from '@lark-apaas/http-client';
|
|
2
|
+
import { HttpClientConfig, PlatformPluginOptions, HttpClient } from '@lark-apaas/http-client';
|
|
3
3
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
4
4
|
export { DevToolsModule, DevToolsOptions, DevToolsV2Module, DevToolsV2Options } from '@lark-apaas/nestjs-openapi-devtools';
|
|
5
5
|
import { Request, Response, NextFunction } from 'express';
|
|
@@ -169,4 +169,144 @@ declare class FileService {
|
|
|
169
169
|
private _getFileMetadata;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
/**
|
|
173
|
+
* 平台 HttpClient 服务
|
|
174
|
+
*
|
|
175
|
+
* 提供两种使用方式:
|
|
176
|
+
* 1. 全局实例:通过 `instance` 或 `PLATFORM_HTTP_CLIENT` 注入,适用于 95% 的场景
|
|
177
|
+
* 2. 独立实例:通过 `create()` 创建,适用于需要自定义拦截器或配置的场景
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* 基本使用(推荐)
|
|
181
|
+
* ```typescript
|
|
182
|
+
* @Injectable()
|
|
183
|
+
* export class UserService {
|
|
184
|
+
* constructor(
|
|
185
|
+
* @Inject(PLATFORM_HTTP_CLIENT) private http: SafeHttpClient
|
|
186
|
+
* ) {}
|
|
187
|
+
*
|
|
188
|
+
* async getUser() {
|
|
189
|
+
* return this.http.get('/user');
|
|
190
|
+
* }
|
|
191
|
+
* }
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* 需要自定义拦截器(高级场景)
|
|
196
|
+
* ```typescript
|
|
197
|
+
* @Injectable()
|
|
198
|
+
* export class FileService {
|
|
199
|
+
* private fileClient: HttpClient;
|
|
200
|
+
*
|
|
201
|
+
* constructor(private platformHttp: PlatformHttpClientService) {
|
|
202
|
+
* this.fileClient = this.platformHttp.create({
|
|
203
|
+
* config: { timeout: 60000 }
|
|
204
|
+
* });
|
|
205
|
+
*
|
|
206
|
+
* this.fileClient.interceptors.request.use((config) => {
|
|
207
|
+
* console.log('File upload starting...');
|
|
208
|
+
* return config;
|
|
209
|
+
* });
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
declare class PlatformHttpClientService {
|
|
215
|
+
private readonly requestContext;
|
|
216
|
+
private readonly client;
|
|
217
|
+
private readonly protectedClient;
|
|
218
|
+
private readonly logger;
|
|
219
|
+
constructor(requestContext: RequestContextService);
|
|
220
|
+
/**
|
|
221
|
+
* 获取受保护的 HttpClient 实例(推荐)
|
|
222
|
+
*
|
|
223
|
+
* 该实例不暴露 interceptors,防止业务代码修改全局拦截器
|
|
224
|
+
*
|
|
225
|
+
* @returns 受保护的 HttpClient 实例
|
|
226
|
+
*/
|
|
227
|
+
get instance(): PlatformHttpClient;
|
|
228
|
+
/**
|
|
229
|
+
* 获取原始 HttpClient 实例(危险)
|
|
230
|
+
*
|
|
231
|
+
* ⚠️ 警告:该实例允许修改拦截器,会影响所有使用全局实例的地方
|
|
232
|
+
*
|
|
233
|
+
* 仅用于特殊场景,不应该暴露给业务代码
|
|
234
|
+
* 如果需要自定义拦截器,请使用 `create()` 方法创建独立实例
|
|
235
|
+
*
|
|
236
|
+
* @internal
|
|
237
|
+
* @returns 原始 HttpClient 实例
|
|
238
|
+
*/
|
|
239
|
+
get rawInstance(): HttpClient;
|
|
240
|
+
/**
|
|
241
|
+
* 创建一个新的独立 HttpClient 实例
|
|
242
|
+
*
|
|
243
|
+
* 适用于需要自定义拦截器或配置的场景
|
|
244
|
+
* 创建的实例完全独立,不会影响全局实例
|
|
245
|
+
*
|
|
246
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
247
|
+
* @returns 完整的 HttpClient 实例(允许修改拦截器)
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* @Injectable()
|
|
252
|
+
* export class FileService {
|
|
253
|
+
* private fileClient: HttpClient;
|
|
254
|
+
*
|
|
255
|
+
* constructor(private platformHttp: PlatformHttpClientService) {
|
|
256
|
+
* // 创建超时 60 秒的独立实例
|
|
257
|
+
* this.fileClient = this.platformHttp.create({
|
|
258
|
+
* config: { timeout: 60000 }
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* // 为这个实例注册拦截器(不影响全局实例)
|
|
262
|
+
* this.fileClient.interceptors.request.use((config) => {
|
|
263
|
+
* console.log('File upload starting...');
|
|
264
|
+
* return config;
|
|
265
|
+
* });
|
|
266
|
+
* }
|
|
267
|
+
*
|
|
268
|
+
* async uploadFile(file: Buffer) {
|
|
269
|
+
* return this.fileClient.post('/file/upload', file);
|
|
270
|
+
* }
|
|
271
|
+
* }
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
create(options?: PlatformHttpClientOptions): HttpClient;
|
|
275
|
+
/**
|
|
276
|
+
* 创建一个带全局拦截器的独立 HttpClient 实例
|
|
277
|
+
*
|
|
278
|
+
* 与 `create()` 类似,但会自动注册全局拦截器(日志、x-tt-env 透传等)
|
|
279
|
+
* 适用于需要保留平台标准行为,同时又需要添加自定义拦截器的场景
|
|
280
|
+
*
|
|
281
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
282
|
+
* @returns 完整的 HttpClient 实例(带全局拦截器,允许添加更多拦截器)
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const client = this.platformHttp.createWithGlobalInterceptors();
|
|
287
|
+
* // 客户端已包含日志和 x-tt-env 拦截器
|
|
288
|
+
* // 可以继续添加自定义拦截器
|
|
289
|
+
* client.interceptors.request.use((config) => {
|
|
290
|
+
* config.headers['x-custom'] = 'value';
|
|
291
|
+
* return config;
|
|
292
|
+
* });
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
createWithGlobalInterceptors(options?: PlatformHttpClientOptions): HttpClient;
|
|
296
|
+
/**
|
|
297
|
+
* 注册全局拦截器(用于单例实例)
|
|
298
|
+
*/
|
|
299
|
+
private registerGlobalInterceptors;
|
|
300
|
+
/**
|
|
301
|
+
* 为指定的 HttpClient 实例注册标准拦截器
|
|
302
|
+
*
|
|
303
|
+
* 包含:
|
|
304
|
+
* - 请求日志记录
|
|
305
|
+
* - x-tt-env header 透传
|
|
306
|
+
* - 响应日志记录
|
|
307
|
+
* - 错误日志记录
|
|
308
|
+
*/
|
|
309
|
+
private registerInterceptorsForClient;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, FileService, type PlatformHttpClientOptions, PlatformHttpClientService, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
4
4
|
// src/modules/platform/module.ts
|
|
5
5
|
import { Global, Module, ValidationPipe } from "@nestjs/common";
|
|
6
6
|
import { APP_INTERCEPTOR, APP_PIPE } from "@nestjs/core";
|
|
7
|
-
import { CommonModule, OBSERVABLE_SERVICE as OBSERVABLE_SERVICE2 } from "@lark-apaas/nestjs-common";
|
|
7
|
+
import { CommonModule, OBSERVABLE_SERVICE as OBSERVABLE_SERVICE2, HTTP_CLIENT_FACTORY } from "@lark-apaas/nestjs-common";
|
|
8
8
|
import { ConfigModule, ConfigService } from "@nestjs/config";
|
|
9
9
|
import { NestjsObservableModule as ObservableModule, Observable, ObservableTraceMiddleware, TraceInterceptor } from "@lark-apaas/nestjs-observable";
|
|
10
10
|
import { HttpModule } from "@nestjs/axios";
|
|
@@ -714,10 +714,47 @@ var PlatformHttpClientService = class _PlatformHttpClientService {
|
|
|
714
714
|
});
|
|
715
715
|
}
|
|
716
716
|
/**
|
|
717
|
-
*
|
|
717
|
+
* 创建一个带全局拦截器的独立 HttpClient 实例
|
|
718
|
+
*
|
|
719
|
+
* 与 `create()` 类似,但会自动注册全局拦截器(日志、x-tt-env 透传等)
|
|
720
|
+
* 适用于需要保留平台标准行为,同时又需要添加自定义拦截器的场景
|
|
721
|
+
*
|
|
722
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
723
|
+
* @returns 完整的 HttpClient 实例(带全局拦截器,允许添加更多拦截器)
|
|
724
|
+
*
|
|
725
|
+
* @example
|
|
726
|
+
* ```typescript
|
|
727
|
+
* const client = this.platformHttp.createWithGlobalInterceptors();
|
|
728
|
+
* // 客户端已包含日志和 x-tt-env 拦截器
|
|
729
|
+
* // 可以继续添加自定义拦截器
|
|
730
|
+
* client.interceptors.request.use((config) => {
|
|
731
|
+
* config.headers['x-custom'] = 'value';
|
|
732
|
+
* return config;
|
|
733
|
+
* });
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
createWithGlobalInterceptors(options) {
|
|
737
|
+
const client = this.create(options);
|
|
738
|
+
this.registerInterceptorsForClient(client);
|
|
739
|
+
return client;
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* 注册全局拦截器(用于单例实例)
|
|
718
743
|
*/
|
|
719
744
|
registerGlobalInterceptors() {
|
|
720
|
-
this.client
|
|
745
|
+
this.registerInterceptorsForClient(this.client);
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* 为指定的 HttpClient 实例注册标准拦截器
|
|
749
|
+
*
|
|
750
|
+
* 包含:
|
|
751
|
+
* - 请求日志记录
|
|
752
|
+
* - x-tt-env header 透传
|
|
753
|
+
* - 响应日志记录
|
|
754
|
+
* - 错误日志记录
|
|
755
|
+
*/
|
|
756
|
+
registerInterceptorsForClient(client) {
|
|
757
|
+
client.interceptors.request.use((config) => {
|
|
721
758
|
this.logger.debug(`Server SDK HTTP Request: ${config.method?.toUpperCase()} ${config.url}`);
|
|
722
759
|
const ttEnv = this.requestContext.get("ttEnv");
|
|
723
760
|
if (ttEnv) {
|
|
@@ -731,7 +768,7 @@ var PlatformHttpClientService = class _PlatformHttpClientService {
|
|
|
731
768
|
this.logger.error("Server SDK HTTP Request Error", error, "HttpService");
|
|
732
769
|
return Promise.reject(error);
|
|
733
770
|
});
|
|
734
|
-
|
|
771
|
+
client.interceptors.response.use((response) => {
|
|
735
772
|
this.logger.debug(`Server SDK HTTP Response: ${response.status} ${response.url}`);
|
|
736
773
|
return response;
|
|
737
774
|
}, (error) => {
|
|
@@ -1245,6 +1282,15 @@ var PlatformModule = class _PlatformModule {
|
|
|
1245
1282
|
PlatformHttpClientService
|
|
1246
1283
|
]
|
|
1247
1284
|
},
|
|
1285
|
+
{
|
|
1286
|
+
provide: HTTP_CLIENT_FACTORY,
|
|
1287
|
+
useFactory: /* @__PURE__ */ __name((svc) => ({
|
|
1288
|
+
create: /* @__PURE__ */ __name((options2) => svc.createWithGlobalInterceptors(options2), "create")
|
|
1289
|
+
}), "useFactory"),
|
|
1290
|
+
inject: [
|
|
1291
|
+
PlatformHttpClientService
|
|
1292
|
+
]
|
|
1293
|
+
},
|
|
1248
1294
|
HttpInterceptorService,
|
|
1249
1295
|
{
|
|
1250
1296
|
provide: APP_INTERCEPTOR,
|
|
@@ -1258,6 +1304,8 @@ var PlatformModule = class _PlatformModule {
|
|
|
1258
1304
|
CommonModule,
|
|
1259
1305
|
OBSERVABLE_SERVICE2,
|
|
1260
1306
|
PLATFORM_HTTP_CLIENT3,
|
|
1307
|
+
HTTP_CLIENT_FACTORY,
|
|
1308
|
+
PlatformHttpClientService,
|
|
1261
1309
|
CapabilityModule,
|
|
1262
1310
|
FileService
|
|
1263
1311
|
]
|
|
@@ -1340,6 +1388,7 @@ export {
|
|
|
1340
1388
|
DevToolsModule,
|
|
1341
1389
|
DevToolsV2Module2 as DevToolsV2Module,
|
|
1342
1390
|
FileService,
|
|
1391
|
+
PlatformHttpClientService,
|
|
1343
1392
|
PlatformModule,
|
|
1344
1393
|
UserContextMiddleware,
|
|
1345
1394
|
ViewContextMiddleware,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/fullstack-nestjs-core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.23-alpha.0",
|
|
4
4
|
"description": "FullStack Nestjs Core",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"@lark-apaas/file-service": "^0.1.1",
|
|
43
43
|
"@lark-apaas/http-client": "^0.1.2",
|
|
44
44
|
"@lark-apaas/nestjs-authnpaas": "^1.0.2",
|
|
45
|
-
"@lark-apaas/nestjs-authzpaas": "^0.1.
|
|
46
|
-
"@lark-apaas/nestjs-capability": "^0.1.
|
|
47
|
-
"@lark-apaas/nestjs-common": "^0.1.
|
|
48
|
-
"@lark-apaas/nestjs-datapaas": "^1.0.
|
|
49
|
-
"@lark-apaas/nestjs-logger": "^1.0.
|
|
50
|
-
"@lark-apaas/nestjs-observable": "^0.0.
|
|
45
|
+
"@lark-apaas/nestjs-authzpaas": "^0.1.2-alpha.0",
|
|
46
|
+
"@lark-apaas/nestjs-capability": "^0.1.5-alpha.0",
|
|
47
|
+
"@lark-apaas/nestjs-common": "^0.1.4-alpha.0",
|
|
48
|
+
"@lark-apaas/nestjs-datapaas": "^1.0.10-alpha.0",
|
|
49
|
+
"@lark-apaas/nestjs-logger": "^1.0.10-alpha.0",
|
|
50
|
+
"@lark-apaas/nestjs-observable": "^0.0.5-alpha.0",
|
|
51
51
|
"@lark-apaas/nestjs-openapi-devtools": "^1.0.9",
|
|
52
52
|
"@lark-apaas/nestjs-trigger": "^0.0.2",
|
|
53
53
|
"@nestjs/axios": "^4.0.1",
|