@lark-apaas/fullstack-nestjs-core 1.1.1-alpha.37 → 1.1.1-alpha.38
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 +165 -0
- package/dist/index.cjs +358 -41
- package/dist/index.d.cts +21 -2
- package/dist/index.d.ts +21 -2
- package/dist/index.js +346 -30
- package/package.json +8 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { NestModule, DynamicModule, MiddlewareConsumer, NestMiddleware } from '@nestjs/common';
|
|
2
|
+
import { HttpClientConfig, PlatformPluginOptions } from '@lark-apaas/http-client';
|
|
2
3
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
3
4
|
export { DevToolsModule, DevToolsOptions, DevToolsV2Module, DevToolsV2Options } from '@lark-apaas/nestjs-openapi-devtools';
|
|
4
5
|
import { Request, Response, NextFunction } from 'express';
|
|
6
|
+
import { PlatformHttpClient } from '@lark-apaas/nestjs-common';
|
|
7
|
+
export { AutoTrace } from '@lark-apaas/nestjs-common';
|
|
5
8
|
export * from '@lark-apaas/nestjs-authnpaas';
|
|
6
9
|
export * from '@lark-apaas/nestjs-datapaas';
|
|
7
10
|
export * from '@lark-apaas/nestjs-observable';
|
|
@@ -26,6 +29,7 @@ declare global {
|
|
|
26
29
|
userName?: string; // 默认中文名
|
|
27
30
|
userNameEn?: string; // 冗余一份英文名
|
|
28
31
|
userNameI18n?: Record<string, string>; // 带多语的用户名
|
|
32
|
+
isSystemAccount?: boolean; // trigger 场景写入值为 true,user 访问场景不写入
|
|
29
33
|
},
|
|
30
34
|
csrfToken?: string;
|
|
31
35
|
}
|
|
@@ -35,6 +39,13 @@ declare global {
|
|
|
35
39
|
/**
|
|
36
40
|
* PlatformModule 配置选项
|
|
37
41
|
*/
|
|
42
|
+
|
|
43
|
+
interface PlatformHttpClientOptions {
|
|
44
|
+
/** HttpClient 基础配置(超时、headers 等) */
|
|
45
|
+
config?: Omit<HttpClientConfig, 'platform'>;
|
|
46
|
+
/** 平台插件配置(默认启用) */
|
|
47
|
+
platform?: PlatformPluginOptions;
|
|
48
|
+
}
|
|
38
49
|
interface PlatformModuleOptions {
|
|
39
50
|
/**
|
|
40
51
|
* 是否启用 CSRF 保护
|
|
@@ -46,6 +57,10 @@ interface PlatformModuleOptions {
|
|
|
46
57
|
* 默认: '/api/*'
|
|
47
58
|
*/
|
|
48
59
|
csrfRoutes?: string | string[];
|
|
60
|
+
/**
|
|
61
|
+
* 平台 HttpClient 配置
|
|
62
|
+
*/
|
|
63
|
+
httpClient?: PlatformHttpClientOptions;
|
|
49
64
|
}
|
|
50
65
|
|
|
51
66
|
declare class PlatformModule implements NestModule {
|
|
@@ -96,7 +111,11 @@ declare class UserContextMiddleware implements NestMiddleware {
|
|
|
96
111
|
}
|
|
97
112
|
|
|
98
113
|
declare class ViewContextMiddleware implements NestMiddleware {
|
|
99
|
-
|
|
114
|
+
private readonly client;
|
|
115
|
+
private readonly logger;
|
|
116
|
+
constructor(client: PlatformHttpClient);
|
|
117
|
+
private getAppInfo;
|
|
118
|
+
use(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
100
119
|
}
|
|
101
120
|
|
|
102
121
|
/**
|
|
@@ -110,4 +129,4 @@ interface ApiNotFoundResponse {
|
|
|
110
129
|
timestamp: string;
|
|
111
130
|
}
|
|
112
131
|
|
|
113
|
-
export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
|
|
132
|
+
export { type ApiNotFoundResponse, CsrfMiddleware, CsrfTokenMiddleware, type PlatformHttpClientOptions, PlatformModule, type PlatformModuleOptions, UserContextMiddleware, ViewContextMiddleware, configureApp };
|
package/dist/index.js
CHANGED
|
@@ -3,14 +3,16 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
3
3
|
|
|
4
4
|
// src/modules/platform/module.ts
|
|
5
5
|
import { Global, Module, ValidationPipe } from "@nestjs/common";
|
|
6
|
-
import { APP_PIPE } from "@nestjs/core";
|
|
6
|
+
import { APP_INTERCEPTOR, APP_PIPE } from "@nestjs/core";
|
|
7
7
|
import { CommonModule, OBSERVABLE_SERVICE } from "@lark-apaas/nestjs-common";
|
|
8
8
|
import { ConfigModule, ConfigService } from "@nestjs/config";
|
|
9
|
-
import { NestjsObservableModule as ObservableModule, Observable, ObservableTraceMiddleware } from "@lark-apaas/nestjs-observable";
|
|
10
|
-
import {
|
|
9
|
+
import { NestjsObservableModule as ObservableModule, Observable, ObservableTraceMiddleware, TraceInterceptor } from "@lark-apaas/nestjs-observable";
|
|
10
|
+
import { HttpModule } from "@nestjs/axios";
|
|
11
|
+
import { LoggerModule, AppLogger as AppLogger2, LoggerContextMiddleware } from "@lark-apaas/nestjs-logger";
|
|
11
12
|
import { DataPaasModule, SqlExecutionContextMiddleware } from "@lark-apaas/nestjs-datapaas";
|
|
12
13
|
import { AuthNPaasModule } from "@lark-apaas/nestjs-authnpaas";
|
|
13
14
|
import { AutomationModule } from "@lark-apaas/nestjs-trigger";
|
|
15
|
+
import { PLATFORM_HTTP_CLIENT as PLATFORM_HTTP_CLIENT2 } from "@lark-apaas/nestjs-common";
|
|
14
16
|
|
|
15
17
|
// src/middlewares/user-context/index.ts
|
|
16
18
|
import { Injectable } from "@nestjs/common";
|
|
@@ -57,7 +59,8 @@ var UserContextMiddleware = class {
|
|
|
57
59
|
env: webUser?.env ?? "runtime",
|
|
58
60
|
userName: webUser?.user_name?.zh_cn ?? "",
|
|
59
61
|
userNameEn: webUser?.user_name?.en_us ?? "",
|
|
60
|
-
userNameI18n: webUser?.user_name ?? {}
|
|
62
|
+
userNameI18n: webUser?.user_name ?? {},
|
|
63
|
+
isSystemAccount: webUser?.is_system_account ?? false
|
|
61
64
|
};
|
|
62
65
|
next();
|
|
63
66
|
}
|
|
@@ -123,7 +126,8 @@ CsrfMiddleware = _ts_decorate2([
|
|
|
123
126
|
], CsrfMiddleware);
|
|
124
127
|
|
|
125
128
|
// src/middlewares/view-context/index.ts
|
|
126
|
-
import { Injectable as Injectable3 } from "@nestjs/common";
|
|
129
|
+
import { Inject, Injectable as Injectable3, Logger } from "@nestjs/common";
|
|
130
|
+
import { PLATFORM_HTTP_CLIENT } from "@lark-apaas/nestjs-common";
|
|
127
131
|
function _ts_decorate3(decorators, target, key, desc) {
|
|
128
132
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
129
133
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -131,17 +135,57 @@ function _ts_decorate3(decorators, target, key, desc) {
|
|
|
131
135
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
132
136
|
}
|
|
133
137
|
__name(_ts_decorate3, "_ts_decorate");
|
|
134
|
-
|
|
138
|
+
function _ts_metadata(k, v) {
|
|
139
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
140
|
+
}
|
|
141
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
142
|
+
function _ts_param(paramIndex, decorator) {
|
|
143
|
+
return function(target, key) {
|
|
144
|
+
decorator(target, key, paramIndex);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
__name(_ts_param, "_ts_param");
|
|
148
|
+
var ViewContextMiddleware = class _ViewContextMiddleware {
|
|
135
149
|
static {
|
|
136
150
|
__name(this, "ViewContextMiddleware");
|
|
137
151
|
}
|
|
138
|
-
|
|
152
|
+
client;
|
|
153
|
+
logger = new Logger(_ViewContextMiddleware.name);
|
|
154
|
+
constructor(client) {
|
|
155
|
+
this.client = client;
|
|
156
|
+
}
|
|
157
|
+
async getAppInfo(appId) {
|
|
158
|
+
if (!appId) {
|
|
159
|
+
this.logger.warn(`appId is empty, skip get app info`);
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const resp = await this.client.get(`/b/${appId}/get_published_v2`);
|
|
164
|
+
if (resp.status !== 200) {
|
|
165
|
+
throw new Error(`Failed to get app info, status: ${resp.status}`);
|
|
166
|
+
}
|
|
167
|
+
const data = await resp.json();
|
|
168
|
+
if (data.status_code !== "0") {
|
|
169
|
+
throw new Error(`Failed to get app info, status_code: ${data.status_code}`);
|
|
170
|
+
}
|
|
171
|
+
return data.data.app_info ?? {};
|
|
172
|
+
} catch (err) {
|
|
173
|
+
this.logger.error(err, "Failed to get app info");
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async use(req, res, next) {
|
|
139
178
|
const { userId, tenantId, appId } = req.userContext;
|
|
140
179
|
const csrfToken = req.csrfToken;
|
|
180
|
+
const appInfo = await this.getAppInfo(appId);
|
|
181
|
+
this.logger.debug(`appInfo: ${JSON.stringify(appInfo)}`);
|
|
141
182
|
req.__platform_data__ = {
|
|
142
183
|
csrfToken: csrfToken ?? "",
|
|
143
184
|
userId: userId ?? "",
|
|
144
185
|
appId: appId ?? "",
|
|
186
|
+
appName: appInfo?.app_name ?? "",
|
|
187
|
+
appAvatar: appInfo?.app_avatar ?? "",
|
|
188
|
+
appDescription: appInfo?.app_description ?? "",
|
|
145
189
|
tenantId
|
|
146
190
|
};
|
|
147
191
|
res.locals = {
|
|
@@ -149,13 +193,21 @@ var ViewContextMiddleware = class {
|
|
|
149
193
|
csrfToken: csrfToken ?? "",
|
|
150
194
|
userId: userId ?? "",
|
|
151
195
|
tenantId: tenantId ?? "",
|
|
152
|
-
appId: appId ?? ""
|
|
196
|
+
appId: appId ?? "",
|
|
197
|
+
appName: appInfo?.app_name ?? "\u5999\u642D\u5E94\u7528",
|
|
198
|
+
appAvatar: appInfo?.app_avatar ?? "",
|
|
199
|
+
appDescription: appInfo?.app_description ?? ""
|
|
153
200
|
};
|
|
154
201
|
next();
|
|
155
202
|
}
|
|
156
203
|
};
|
|
157
204
|
ViewContextMiddleware = _ts_decorate3([
|
|
158
|
-
Injectable3()
|
|
205
|
+
Injectable3(),
|
|
206
|
+
_ts_param(0, Inject(PLATFORM_HTTP_CLIENT)),
|
|
207
|
+
_ts_metadata("design:type", Function),
|
|
208
|
+
_ts_metadata("design:paramtypes", [
|
|
209
|
+
typeof PlatformHttpClient === "undefined" ? Object : PlatformHttpClient
|
|
210
|
+
])
|
|
159
211
|
], ViewContextMiddleware);
|
|
160
212
|
|
|
161
213
|
// src/middlewares/csrf_token/index.ts
|
|
@@ -265,10 +317,10 @@ function _ts_decorate5(decorators, target, key, desc) {
|
|
|
265
317
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
266
318
|
}
|
|
267
319
|
__name(_ts_decorate5, "_ts_decorate");
|
|
268
|
-
function
|
|
320
|
+
function _ts_metadata2(k, v) {
|
|
269
321
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
270
322
|
}
|
|
271
|
-
__name(
|
|
323
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
272
324
|
var RequestContextMiddleware = class {
|
|
273
325
|
static {
|
|
274
326
|
__name(this, "RequestContextMiddleware");
|
|
@@ -291,12 +343,92 @@ var RequestContextMiddleware = class {
|
|
|
291
343
|
};
|
|
292
344
|
RequestContextMiddleware = _ts_decorate5([
|
|
293
345
|
Injectable5(),
|
|
294
|
-
|
|
295
|
-
|
|
346
|
+
_ts_metadata2("design:type", Function),
|
|
347
|
+
_ts_metadata2("design:paramtypes", [
|
|
296
348
|
typeof RequestContextService === "undefined" ? Object : RequestContextService
|
|
297
349
|
])
|
|
298
350
|
], RequestContextMiddleware);
|
|
299
351
|
|
|
352
|
+
// src/services/http-interceptor.service.ts
|
|
353
|
+
import { Injectable as Injectable6 } from "@nestjs/common";
|
|
354
|
+
import { HttpService } from "@nestjs/axios";
|
|
355
|
+
import { AppLogger } from "@lark-apaas/nestjs-logger";
|
|
356
|
+
function _ts_decorate6(decorators, target, key, desc) {
|
|
357
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
358
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
359
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
360
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
361
|
+
}
|
|
362
|
+
__name(_ts_decorate6, "_ts_decorate");
|
|
363
|
+
function _ts_metadata3(k, v) {
|
|
364
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
365
|
+
}
|
|
366
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
367
|
+
var HttpInterceptorService = class {
|
|
368
|
+
static {
|
|
369
|
+
__name(this, "HttpInterceptorService");
|
|
370
|
+
}
|
|
371
|
+
httpService;
|
|
372
|
+
logger;
|
|
373
|
+
constructor(httpService, logger) {
|
|
374
|
+
this.httpService = httpService;
|
|
375
|
+
this.logger = logger;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* 模块初始化时配置拦截器
|
|
379
|
+
*/
|
|
380
|
+
onModuleInit() {
|
|
381
|
+
this.setupInterceptors();
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* 配置 axios 请求/响应拦截器
|
|
385
|
+
*/
|
|
386
|
+
setupInterceptors() {
|
|
387
|
+
const axiosInstance = this.httpService.axiosRef;
|
|
388
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
389
|
+
this.logger.log("HTTP Request", {
|
|
390
|
+
method: config.method?.toUpperCase(),
|
|
391
|
+
url: config.url,
|
|
392
|
+
headers: config.headers,
|
|
393
|
+
params: config.params,
|
|
394
|
+
data: config.data
|
|
395
|
+
}, "HttpService");
|
|
396
|
+
return config;
|
|
397
|
+
}, (error) => {
|
|
398
|
+
this.logger.error("HTTP Request Error", error, "HttpService");
|
|
399
|
+
return Promise.reject(error);
|
|
400
|
+
});
|
|
401
|
+
axiosInstance.interceptors.response.use((response) => {
|
|
402
|
+
this.logger.log("HTTP Response", {
|
|
403
|
+
method: response.config.method?.toUpperCase(),
|
|
404
|
+
url: response.config.url,
|
|
405
|
+
status: response.status,
|
|
406
|
+
statusText: response.statusText,
|
|
407
|
+
data: response.data
|
|
408
|
+
}, "HttpService");
|
|
409
|
+
return response;
|
|
410
|
+
}, (error) => {
|
|
411
|
+
this.logger.error("HTTP Response Error", {
|
|
412
|
+
method: error.config?.method?.toUpperCase(),
|
|
413
|
+
url: error.config?.url,
|
|
414
|
+
status: error.response?.status,
|
|
415
|
+
statusText: error.response?.statusText,
|
|
416
|
+
data: error.response?.data,
|
|
417
|
+
message: error.message
|
|
418
|
+
}, "HttpService");
|
|
419
|
+
return Promise.reject(error);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
HttpInterceptorService = _ts_decorate6([
|
|
424
|
+
Injectable6(),
|
|
425
|
+
_ts_metadata3("design:type", Function),
|
|
426
|
+
_ts_metadata3("design:paramtypes", [
|
|
427
|
+
typeof HttpService === "undefined" ? Object : HttpService,
|
|
428
|
+
typeof AppLogger === "undefined" ? Object : AppLogger
|
|
429
|
+
])
|
|
430
|
+
], HttpInterceptorService);
|
|
431
|
+
|
|
300
432
|
// src/modules/platform/config/app.config.ts
|
|
301
433
|
import { registerAs } from "@nestjs/config";
|
|
302
434
|
var NAMESPACE = "app";
|
|
@@ -310,19 +442,19 @@ var app_config_default = registerAs(NAMESPACE, () => {
|
|
|
310
442
|
});
|
|
311
443
|
|
|
312
444
|
// src/middlewares/debug/index.ts
|
|
313
|
-
import { Injectable as
|
|
445
|
+
import { Injectable as Injectable7 } from "@nestjs/common";
|
|
314
446
|
import { HttpAdapterHost } from "@nestjs/core";
|
|
315
|
-
function
|
|
447
|
+
function _ts_decorate7(decorators, target, key, desc) {
|
|
316
448
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
317
449
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
318
450
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
319
451
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
320
452
|
}
|
|
321
|
-
__name(
|
|
322
|
-
function
|
|
453
|
+
__name(_ts_decorate7, "_ts_decorate");
|
|
454
|
+
function _ts_metadata4(k, v) {
|
|
323
455
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
324
456
|
}
|
|
325
|
-
__name(
|
|
457
|
+
__name(_ts_metadata4, "_ts_metadata");
|
|
326
458
|
var processStartTimestamp = Date.now();
|
|
327
459
|
var FrameworkDebugMiddleware = class {
|
|
328
460
|
static {
|
|
@@ -370,23 +502,187 @@ var FrameworkDebugMiddleware = class {
|
|
|
370
502
|
};
|
|
371
503
|
}
|
|
372
504
|
};
|
|
373
|
-
FrameworkDebugMiddleware =
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
505
|
+
FrameworkDebugMiddleware = _ts_decorate7([
|
|
506
|
+
Injectable7(),
|
|
507
|
+
_ts_metadata4("design:type", Function),
|
|
508
|
+
_ts_metadata4("design:paramtypes", [
|
|
377
509
|
typeof HttpAdapterHost === "undefined" ? Object : HttpAdapterHost
|
|
378
510
|
])
|
|
379
511
|
], FrameworkDebugMiddleware);
|
|
380
512
|
|
|
513
|
+
// src/services/platform-http-client.service.ts
|
|
514
|
+
import { Injectable as Injectable8, Logger as Logger2 } from "@nestjs/common";
|
|
515
|
+
import { HttpClient } from "@lark-apaas/http-client";
|
|
516
|
+
function _ts_decorate8(decorators, target, key, desc) {
|
|
517
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
518
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
519
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
520
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
521
|
+
}
|
|
522
|
+
__name(_ts_decorate8, "_ts_decorate");
|
|
523
|
+
function _ts_metadata5(k, v) {
|
|
524
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
525
|
+
}
|
|
526
|
+
__name(_ts_metadata5, "_ts_metadata");
|
|
527
|
+
var ProtectedHttpClient = class ProtectedHttpClient2 {
|
|
528
|
+
static {
|
|
529
|
+
__name(this, "ProtectedHttpClient");
|
|
530
|
+
}
|
|
531
|
+
client;
|
|
532
|
+
constructor(client) {
|
|
533
|
+
this.client = client;
|
|
534
|
+
}
|
|
535
|
+
request(config) {
|
|
536
|
+
return this.client.request(config);
|
|
537
|
+
}
|
|
538
|
+
get(url, config) {
|
|
539
|
+
return this.client.get(url, config);
|
|
540
|
+
}
|
|
541
|
+
post(url, data, config) {
|
|
542
|
+
return this.client.post(url, data, config);
|
|
543
|
+
}
|
|
544
|
+
put(url, data, config) {
|
|
545
|
+
return this.client.put(url, data, config);
|
|
546
|
+
}
|
|
547
|
+
patch(url, data, config) {
|
|
548
|
+
return this.client.patch(url, data, config);
|
|
549
|
+
}
|
|
550
|
+
delete(url, config) {
|
|
551
|
+
return this.client.delete(url, config);
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
var PlatformHttpClientService = class _PlatformHttpClientService {
|
|
555
|
+
static {
|
|
556
|
+
__name(this, "PlatformHttpClientService");
|
|
557
|
+
}
|
|
558
|
+
client;
|
|
559
|
+
protectedClient;
|
|
560
|
+
logger = new Logger2(_PlatformHttpClientService.name);
|
|
561
|
+
constructor() {
|
|
562
|
+
const baseConfig = {
|
|
563
|
+
timeout: 5e3
|
|
564
|
+
};
|
|
565
|
+
this.client = new HttpClient({
|
|
566
|
+
...baseConfig,
|
|
567
|
+
platform: {
|
|
568
|
+
enabled: true
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
this.protectedClient = new ProtectedHttpClient(this.client);
|
|
572
|
+
this.registerGlobalInterceptors();
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* 获取受保护的 HttpClient 实例(推荐)
|
|
576
|
+
*
|
|
577
|
+
* 该实例不暴露 interceptors,防止业务代码修改全局拦截器
|
|
578
|
+
*
|
|
579
|
+
* @returns 受保护的 HttpClient 实例
|
|
580
|
+
*/
|
|
581
|
+
get instance() {
|
|
582
|
+
return this.protectedClient;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* 获取原始 HttpClient 实例(危险)
|
|
586
|
+
*
|
|
587
|
+
* ⚠️ 警告:该实例允许修改拦截器,会影响所有使用全局实例的地方
|
|
588
|
+
*
|
|
589
|
+
* 仅用于特殊场景,不应该暴露给业务代码
|
|
590
|
+
* 如果需要自定义拦截器,请使用 `create()` 方法创建独立实例
|
|
591
|
+
*
|
|
592
|
+
* @internal
|
|
593
|
+
* @returns 原始 HttpClient 实例
|
|
594
|
+
*/
|
|
595
|
+
get rawInstance() {
|
|
596
|
+
return this.client;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* 创建一个新的独立 HttpClient 实例
|
|
600
|
+
*
|
|
601
|
+
* 适用于需要自定义拦截器或配置的场景
|
|
602
|
+
* 创建的实例完全独立,不会影响全局实例
|
|
603
|
+
*
|
|
604
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
605
|
+
* @returns 完整的 HttpClient 实例(允许修改拦截器)
|
|
606
|
+
*
|
|
607
|
+
* @example
|
|
608
|
+
* ```typescript
|
|
609
|
+
* @Injectable()
|
|
610
|
+
* export class FileService {
|
|
611
|
+
* private fileClient: HttpClient;
|
|
612
|
+
*
|
|
613
|
+
* constructor(private platformHttp: PlatformHttpClientService) {
|
|
614
|
+
* // 创建超时 60 秒的独立实例
|
|
615
|
+
* this.fileClient = this.platformHttp.create({
|
|
616
|
+
* config: { timeout: 60000 }
|
|
617
|
+
* });
|
|
618
|
+
*
|
|
619
|
+
* // 为这个实例注册拦截器(不影响全局实例)
|
|
620
|
+
* this.fileClient.interceptors.request.use((config) => {
|
|
621
|
+
* console.log('File upload starting...');
|
|
622
|
+
* return config;
|
|
623
|
+
* });
|
|
624
|
+
* }
|
|
625
|
+
*
|
|
626
|
+
* async uploadFile(file: Buffer) {
|
|
627
|
+
* return this.fileClient.post('/file/upload', file);
|
|
628
|
+
* }
|
|
629
|
+
* }
|
|
630
|
+
* ```
|
|
631
|
+
*/
|
|
632
|
+
create(options) {
|
|
633
|
+
const mergedConfig = {
|
|
634
|
+
timeout: 5e3,
|
|
635
|
+
...options?.config
|
|
636
|
+
};
|
|
637
|
+
const mergedPlatform = {
|
|
638
|
+
enabled: true,
|
|
639
|
+
...options?.platform
|
|
640
|
+
};
|
|
641
|
+
return new HttpClient({
|
|
642
|
+
...mergedConfig,
|
|
643
|
+
platform: mergedPlatform
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* 注册全局拦截器
|
|
648
|
+
*/
|
|
649
|
+
registerGlobalInterceptors() {
|
|
650
|
+
this.client.interceptors.request.use((config) => {
|
|
651
|
+
this.logger.log(`HTTP Request: ${config.method?.toUpperCase()} ${config.url}`);
|
|
652
|
+
return config;
|
|
653
|
+
}, (error) => {
|
|
654
|
+
this.logger.error("HTTP Request Error", error, "HttpService");
|
|
655
|
+
return Promise.reject(error);
|
|
656
|
+
});
|
|
657
|
+
this.client.interceptors.response.use((response) => {
|
|
658
|
+
this.logger.log(`HTTP Response: ${response.status} ${response.url}`);
|
|
659
|
+
return response;
|
|
660
|
+
}, (error) => {
|
|
661
|
+
const errorLog = {
|
|
662
|
+
status: error.response?.status,
|
|
663
|
+
statusText: error.response?.statusText,
|
|
664
|
+
message: error.message
|
|
665
|
+
};
|
|
666
|
+
this.logger.error(`HTTP Response Error: ${error.config?.method?.toUpperCase()} ${error.config?.url} ${JSON.stringify(errorLog)}`);
|
|
667
|
+
return Promise.reject(error);
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
PlatformHttpClientService = _ts_decorate8([
|
|
672
|
+
Injectable8(),
|
|
673
|
+
_ts_metadata5("design:type", Function),
|
|
674
|
+
_ts_metadata5("design:paramtypes", [])
|
|
675
|
+
], PlatformHttpClientService);
|
|
676
|
+
|
|
381
677
|
// src/modules/platform/module.ts
|
|
382
|
-
function
|
|
678
|
+
function _ts_decorate9(decorators, target, key, desc) {
|
|
383
679
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
384
680
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
385
681
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
386
682
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
387
683
|
}
|
|
388
|
-
__name(
|
|
389
|
-
var PLATFORM_MODULE_OPTIONS = "PLATFORM_MODULE_OPTIONS";
|
|
684
|
+
__name(_ts_decorate9, "_ts_decorate");
|
|
685
|
+
var PLATFORM_MODULE_OPTIONS = /* @__PURE__ */ Symbol("PLATFORM_MODULE_OPTIONS");
|
|
390
686
|
var PlatformModule = class _PlatformModule {
|
|
391
687
|
static {
|
|
392
688
|
__name(this, "PlatformModule");
|
|
@@ -410,6 +706,10 @@ var PlatformModule = class _PlatformModule {
|
|
|
410
706
|
CommonModule,
|
|
411
707
|
ObservableModule,
|
|
412
708
|
LoggerModule,
|
|
709
|
+
HttpModule.register({
|
|
710
|
+
timeout: 5e3,
|
|
711
|
+
maxRedirects: 5
|
|
712
|
+
}),
|
|
413
713
|
DataPaasModule.forRootAsync({
|
|
414
714
|
imports: [
|
|
415
715
|
ConfigModule,
|
|
@@ -417,7 +717,7 @@ var PlatformModule = class _PlatformModule {
|
|
|
417
717
|
],
|
|
418
718
|
inject: [
|
|
419
719
|
ConfigService,
|
|
420
|
-
|
|
720
|
+
AppLogger2
|
|
421
721
|
],
|
|
422
722
|
useFactory: /* @__PURE__ */ __name(async (...args) => {
|
|
423
723
|
const configService = args[0];
|
|
@@ -458,13 +758,27 @@ var PlatformModule = class _PlatformModule {
|
|
|
458
758
|
{
|
|
459
759
|
provide: OBSERVABLE_SERVICE,
|
|
460
760
|
useClass: Observable
|
|
761
|
+
},
|
|
762
|
+
PlatformHttpClientService,
|
|
763
|
+
{
|
|
764
|
+
provide: PLATFORM_HTTP_CLIENT2,
|
|
765
|
+
useFactory: /* @__PURE__ */ __name((svc) => svc.instance, "useFactory"),
|
|
766
|
+
inject: [
|
|
767
|
+
PlatformHttpClientService
|
|
768
|
+
]
|
|
769
|
+
},
|
|
770
|
+
HttpInterceptorService,
|
|
771
|
+
{
|
|
772
|
+
provide: APP_INTERCEPTOR,
|
|
773
|
+
useClass: TraceInterceptor
|
|
461
774
|
}
|
|
462
775
|
],
|
|
463
776
|
exports: [
|
|
464
777
|
ConfigModule,
|
|
465
778
|
LoggerModule,
|
|
466
779
|
CommonModule,
|
|
467
|
-
OBSERVABLE_SERVICE
|
|
780
|
+
OBSERVABLE_SERVICE,
|
|
781
|
+
PLATFORM_HTTP_CLIENT2
|
|
468
782
|
]
|
|
469
783
|
};
|
|
470
784
|
}
|
|
@@ -491,20 +805,20 @@ var PlatformModule = class _PlatformModule {
|
|
|
491
805
|
}
|
|
492
806
|
}
|
|
493
807
|
};
|
|
494
|
-
PlatformModule =
|
|
808
|
+
PlatformModule = _ts_decorate9([
|
|
495
809
|
Global(),
|
|
496
810
|
Module({})
|
|
497
811
|
], PlatformModule);
|
|
498
812
|
|
|
499
813
|
// src/setup.ts
|
|
500
|
-
import { AppLogger as
|
|
814
|
+
import { AppLogger as AppLogger3 } from "@lark-apaas/nestjs-logger";
|
|
501
815
|
import cookieParser from "cookie-parser";
|
|
502
816
|
import { DevToolsV2Module } from "@lark-apaas/nestjs-openapi-devtools";
|
|
503
817
|
var defaultPerms = {
|
|
504
818
|
disableSwagger: false
|
|
505
819
|
};
|
|
506
820
|
async function configureApp(app, perms = defaultPerms) {
|
|
507
|
-
app.useLogger(app.get(
|
|
821
|
+
app.useLogger(app.get(AppLogger3));
|
|
508
822
|
app.flushLogs();
|
|
509
823
|
app.use(cookieParser());
|
|
510
824
|
const globalPrefix = process.env.CLIENT_BASE_PATH ?? "";
|
|
@@ -528,7 +842,9 @@ export * from "@lark-apaas/nestjs-authnpaas";
|
|
|
528
842
|
export * from "@lark-apaas/nestjs-datapaas";
|
|
529
843
|
export * from "@lark-apaas/nestjs-observable";
|
|
530
844
|
export * from "@lark-apaas/nestjs-trigger";
|
|
845
|
+
import { AutoTrace } from "@lark-apaas/nestjs-common";
|
|
531
846
|
export {
|
|
847
|
+
AutoTrace,
|
|
532
848
|
CsrfMiddleware,
|
|
533
849
|
CsrfTokenMiddleware,
|
|
534
850
|
DevToolsModule,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/fullstack-nestjs-core",
|
|
3
|
-
"version": "1.1.1-alpha.
|
|
3
|
+
"version": "1.1.1-alpha.38",
|
|
4
4
|
"description": "FullStack Nestjs Core",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -39,13 +39,16 @@
|
|
|
39
39
|
"prepublishOnly": "npm run build"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
+
"@lark-apaas/http-client": "0.1.2",
|
|
42
43
|
"@lark-apaas/nestjs-authnpaas": "^1.0.2",
|
|
43
|
-
"@lark-apaas/nestjs-common": "0.0
|
|
44
|
+
"@lark-apaas/nestjs-common": "0.1.0-alpha.1",
|
|
44
45
|
"@lark-apaas/nestjs-datapaas": "^1.0.6-alpha.5",
|
|
45
|
-
"@lark-apaas/nestjs-logger": "1.0.2-alpha.
|
|
46
|
-
"@lark-apaas/nestjs-observable": "0.0.1-alpha.
|
|
46
|
+
"@lark-apaas/nestjs-logger": "1.0.2-alpha.30",
|
|
47
|
+
"@lark-apaas/nestjs-observable": "0.0.1-alpha.34",
|
|
47
48
|
"@lark-apaas/nestjs-openapi-devtools": "^1.0.9",
|
|
48
|
-
"@lark-apaas/nestjs-trigger": "0.0.1
|
|
49
|
+
"@lark-apaas/nestjs-trigger": "^0.0.1",
|
|
50
|
+
"@nestjs/axios": "^4.0.1",
|
|
51
|
+
"axios": "^1.13.2",
|
|
49
52
|
"cookie-parser": "^1.4.7"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|