@lark-apaas/fullstack-nestjs-core 1.0.15-alpha.2 → 1.0.15-alpha.21
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 +512 -30
- package/dist/index.d.cts +48 -4
- package/dist/index.d.ts +48 -4
- package/dist/index.js +501 -22
- package/package.json +15 -7
package/dist/index.js
CHANGED
|
@@ -4,10 +4,15 @@ 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_PIPE } from "@nestjs/core";
|
|
7
|
+
import { CommonModule, OBSERVABLE_SERVICE } from "@lark-apaas/nestjs-common";
|
|
7
8
|
import { ConfigModule, ConfigService } from "@nestjs/config";
|
|
8
|
-
import {
|
|
9
|
+
import { NestjsObservableModule as ObservableModule, Observable } from "@lark-apaas/nestjs-observable";
|
|
10
|
+
import { HttpModule } from "@nestjs/axios";
|
|
11
|
+
import { LoggerModule, AppLogger as AppLogger2, LoggerContextMiddleware } from "@lark-apaas/nestjs-logger";
|
|
9
12
|
import { DataPaasModule, SqlExecutionContextMiddleware } from "@lark-apaas/nestjs-datapaas";
|
|
10
13
|
import { AuthNPaasModule } from "@lark-apaas/nestjs-authnpaas";
|
|
14
|
+
import { AutomationModule } from "@lark-apaas/nestjs-trigger";
|
|
15
|
+
import { PLATFORM_HTTP_CLIENT as PLATFORM_HTTP_CLIENT2 } from "@lark-apaas/nestjs-common";
|
|
11
16
|
|
|
12
17
|
// src/middlewares/user-context/index.ts
|
|
13
18
|
import { Injectable } from "@nestjs/common";
|
|
@@ -50,7 +55,11 @@ var UserContextMiddleware = class {
|
|
|
50
55
|
tenantId: webUser?.tenant_id,
|
|
51
56
|
appId: webUser?.app_id ?? "",
|
|
52
57
|
loginUrl: webUser?.login_url ?? "",
|
|
53
|
-
userType: webUser?.user_type ?? ""
|
|
58
|
+
userType: webUser?.user_type ?? "",
|
|
59
|
+
env: webUser?.env ?? "runtime",
|
|
60
|
+
userName: webUser?.user_name?.zh_cn ?? "",
|
|
61
|
+
userNameEn: webUser?.user_name?.en_us ?? "",
|
|
62
|
+
userNameI18n: webUser?.user_name ?? {}
|
|
54
63
|
};
|
|
55
64
|
next();
|
|
56
65
|
}
|
|
@@ -116,7 +125,8 @@ CsrfMiddleware = _ts_decorate2([
|
|
|
116
125
|
], CsrfMiddleware);
|
|
117
126
|
|
|
118
127
|
// src/middlewares/view-context/index.ts
|
|
119
|
-
import { Injectable as Injectable3 } from "@nestjs/common";
|
|
128
|
+
import { Inject, Injectable as Injectable3, Logger } from "@nestjs/common";
|
|
129
|
+
import { PLATFORM_HTTP_CLIENT } from "@lark-apaas/nestjs-common";
|
|
120
130
|
function _ts_decorate3(decorators, target, key, desc) {
|
|
121
131
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
122
132
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -124,17 +134,57 @@ function _ts_decorate3(decorators, target, key, desc) {
|
|
|
124
134
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
125
135
|
}
|
|
126
136
|
__name(_ts_decorate3, "_ts_decorate");
|
|
127
|
-
|
|
137
|
+
function _ts_metadata(k, v) {
|
|
138
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
139
|
+
}
|
|
140
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
141
|
+
function _ts_param(paramIndex, decorator) {
|
|
142
|
+
return function(target, key) {
|
|
143
|
+
decorator(target, key, paramIndex);
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
__name(_ts_param, "_ts_param");
|
|
147
|
+
var ViewContextMiddleware = class _ViewContextMiddleware {
|
|
128
148
|
static {
|
|
129
149
|
__name(this, "ViewContextMiddleware");
|
|
130
150
|
}
|
|
131
|
-
|
|
151
|
+
client;
|
|
152
|
+
logger = new Logger(_ViewContextMiddleware.name);
|
|
153
|
+
constructor(client) {
|
|
154
|
+
this.client = client;
|
|
155
|
+
}
|
|
156
|
+
async getAppInfo(appId) {
|
|
157
|
+
if (!appId) {
|
|
158
|
+
this.logger.warn(`appId is empty, skip get app info`);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const resp = await this.client.get(`/b/${appId}/get_published_v2`);
|
|
163
|
+
if (resp.status !== 200) {
|
|
164
|
+
throw new Error(`Failed to get app info, status: ${resp.status}`);
|
|
165
|
+
}
|
|
166
|
+
const data = await resp.json();
|
|
167
|
+
if (data.status_code !== "0") {
|
|
168
|
+
throw new Error(`Failed to get app info, status_code: ${data.status_code}`);
|
|
169
|
+
}
|
|
170
|
+
return data.data.app_info ?? {};
|
|
171
|
+
} catch (err) {
|
|
172
|
+
this.logger.error(err, "Failed to get app info");
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async use(req, res, next) {
|
|
132
177
|
const { userId, tenantId, appId, loginUrl, userType } = req.userContext;
|
|
133
178
|
const csrfToken = req.csrfToken;
|
|
179
|
+
const appInfo = await this.getAppInfo(appId);
|
|
180
|
+
this.logger.debug(`appInfo: ${JSON.stringify(appInfo)}`);
|
|
134
181
|
req.__platform_data__ = {
|
|
135
182
|
csrfToken: csrfToken ?? "",
|
|
136
183
|
userId: userId ?? "",
|
|
137
184
|
appId: appId ?? "",
|
|
185
|
+
appName: appInfo?.app_name ?? "",
|
|
186
|
+
appAvatar: appInfo?.app_avatar ?? "",
|
|
187
|
+
appDescription: appInfo?.app_description ?? "",
|
|
138
188
|
loginUrl: loginUrl ?? "",
|
|
139
189
|
userType: userType ?? "",
|
|
140
190
|
tenantId
|
|
@@ -146,13 +196,21 @@ var ViewContextMiddleware = class {
|
|
|
146
196
|
tenantId: tenantId ?? "",
|
|
147
197
|
loginUrl: loginUrl ?? "",
|
|
148
198
|
userType: userType ?? "",
|
|
149
|
-
appId: appId ?? ""
|
|
199
|
+
appId: appId ?? "",
|
|
200
|
+
appName: appInfo?.app_name ?? "\u5999\u642D\u5E94\u7528",
|
|
201
|
+
appAvatar: appInfo?.app_avatar ?? "",
|
|
202
|
+
appDescription: appInfo?.app_description ?? ""
|
|
150
203
|
};
|
|
151
204
|
next();
|
|
152
205
|
}
|
|
153
206
|
};
|
|
154
207
|
ViewContextMiddleware = _ts_decorate3([
|
|
155
|
-
Injectable3()
|
|
208
|
+
Injectable3(),
|
|
209
|
+
_ts_param(0, Inject(PLATFORM_HTTP_CLIENT)),
|
|
210
|
+
_ts_metadata("design:type", Function),
|
|
211
|
+
_ts_metadata("design:paramtypes", [
|
|
212
|
+
typeof PlatformHttpClient === "undefined" ? Object : PlatformHttpClient
|
|
213
|
+
])
|
|
156
214
|
], ViewContextMiddleware);
|
|
157
215
|
|
|
158
216
|
// src/middlewares/csrf_token/index.ts
|
|
@@ -207,7 +265,7 @@ var CsrfTokenMiddleware = class _CsrfTokenMiddleware {
|
|
|
207
265
|
res.cookie(cookieKey, token, {
|
|
208
266
|
maxAge: cookieMaxAge,
|
|
209
267
|
path: cookiePath,
|
|
210
|
-
httpOnly:
|
|
268
|
+
httpOnly: false,
|
|
211
269
|
secure: true,
|
|
212
270
|
sameSite: "none",
|
|
213
271
|
partitioned: true
|
|
@@ -220,6 +278,160 @@ CsrfTokenMiddleware = _ts_decorate4([
|
|
|
220
278
|
Injectable4()
|
|
221
279
|
], CsrfTokenMiddleware);
|
|
222
280
|
|
|
281
|
+
// src/middlewares/api-fallback/index.ts
|
|
282
|
+
function createApiNotFoundResponse(req) {
|
|
283
|
+
return {
|
|
284
|
+
code: 404,
|
|
285
|
+
message: `Cannot ${req.method} ${req.path}`,
|
|
286
|
+
error: "Not Found",
|
|
287
|
+
path: req.path,
|
|
288
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
__name(createApiNotFoundResponse, "createApiNotFoundResponse");
|
|
292
|
+
function getApiPathPrefix() {
|
|
293
|
+
const globalPrefix = process.env.CLIENT_BASE_PATH ?? "";
|
|
294
|
+
if (!globalPrefix) {
|
|
295
|
+
return "/api/";
|
|
296
|
+
}
|
|
297
|
+
const normalizedPrefix = globalPrefix.replace(/\/+$/, "");
|
|
298
|
+
return `${normalizedPrefix}/api/`;
|
|
299
|
+
}
|
|
300
|
+
__name(getApiPathPrefix, "getApiPathPrefix");
|
|
301
|
+
function apiResponseInterceptor(req, res, next) {
|
|
302
|
+
const apiPrefix = getApiPathPrefix();
|
|
303
|
+
if (!req.baseUrl.startsWith(apiPrefix)) {
|
|
304
|
+
return next();
|
|
305
|
+
}
|
|
306
|
+
res.render = function() {
|
|
307
|
+
return res.status(404).json(createApiNotFoundResponse(req));
|
|
308
|
+
};
|
|
309
|
+
next();
|
|
310
|
+
}
|
|
311
|
+
__name(apiResponseInterceptor, "apiResponseInterceptor");
|
|
312
|
+
|
|
313
|
+
// src/middlewares/request-context/index.ts
|
|
314
|
+
import { Injectable as Injectable5 } from "@nestjs/common";
|
|
315
|
+
import { RequestContextService } from "@lark-apaas/nestjs-common";
|
|
316
|
+
function _ts_decorate5(decorators, target, key, desc) {
|
|
317
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
318
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
319
|
+
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;
|
|
320
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
321
|
+
}
|
|
322
|
+
__name(_ts_decorate5, "_ts_decorate");
|
|
323
|
+
function _ts_metadata2(k, v) {
|
|
324
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
325
|
+
}
|
|
326
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
327
|
+
var RequestContextMiddleware = class {
|
|
328
|
+
static {
|
|
329
|
+
__name(this, "RequestContextMiddleware");
|
|
330
|
+
}
|
|
331
|
+
requestContext;
|
|
332
|
+
constructor(requestContext) {
|
|
333
|
+
this.requestContext = requestContext;
|
|
334
|
+
}
|
|
335
|
+
use(req, _res, next) {
|
|
336
|
+
const path = req.originalUrl ?? req.url;
|
|
337
|
+
const userContext = req.userContext ?? {};
|
|
338
|
+
this.requestContext.run({
|
|
339
|
+
path,
|
|
340
|
+
method: req.method,
|
|
341
|
+
userId: userContext.userId,
|
|
342
|
+
tenantId: userContext.tenantId,
|
|
343
|
+
appId: userContext.appId
|
|
344
|
+
}, () => next());
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
RequestContextMiddleware = _ts_decorate5([
|
|
348
|
+
Injectable5(),
|
|
349
|
+
_ts_metadata2("design:type", Function),
|
|
350
|
+
_ts_metadata2("design:paramtypes", [
|
|
351
|
+
typeof RequestContextService === "undefined" ? Object : RequestContextService
|
|
352
|
+
])
|
|
353
|
+
], RequestContextMiddleware);
|
|
354
|
+
|
|
355
|
+
// src/services/http-interceptor.service.ts
|
|
356
|
+
import { Injectable as Injectable6 } from "@nestjs/common";
|
|
357
|
+
import { HttpService } from "@nestjs/axios";
|
|
358
|
+
import { AppLogger } from "@lark-apaas/nestjs-logger";
|
|
359
|
+
function _ts_decorate6(decorators, target, key, desc) {
|
|
360
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
361
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
362
|
+
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;
|
|
363
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
364
|
+
}
|
|
365
|
+
__name(_ts_decorate6, "_ts_decorate");
|
|
366
|
+
function _ts_metadata3(k, v) {
|
|
367
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
368
|
+
}
|
|
369
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
370
|
+
var HttpInterceptorService = class {
|
|
371
|
+
static {
|
|
372
|
+
__name(this, "HttpInterceptorService");
|
|
373
|
+
}
|
|
374
|
+
httpService;
|
|
375
|
+
logger;
|
|
376
|
+
constructor(httpService, logger) {
|
|
377
|
+
this.httpService = httpService;
|
|
378
|
+
this.logger = logger;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* 模块初始化时配置拦截器
|
|
382
|
+
*/
|
|
383
|
+
onModuleInit() {
|
|
384
|
+
this.setupInterceptors();
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* 配置 axios 请求/响应拦截器
|
|
388
|
+
*/
|
|
389
|
+
setupInterceptors() {
|
|
390
|
+
const axiosInstance = this.httpService.axiosRef;
|
|
391
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
392
|
+
this.logger.log("HTTP Request", {
|
|
393
|
+
method: config.method?.toUpperCase(),
|
|
394
|
+
url: config.url,
|
|
395
|
+
headers: config.headers,
|
|
396
|
+
params: config.params,
|
|
397
|
+
data: config.data
|
|
398
|
+
}, "HttpService");
|
|
399
|
+
return config;
|
|
400
|
+
}, (error) => {
|
|
401
|
+
this.logger.error("HTTP Request Error", error, "HttpService");
|
|
402
|
+
return Promise.reject(error);
|
|
403
|
+
});
|
|
404
|
+
axiosInstance.interceptors.response.use((response) => {
|
|
405
|
+
this.logger.log("HTTP Response", {
|
|
406
|
+
method: response.config.method?.toUpperCase(),
|
|
407
|
+
url: response.config.url,
|
|
408
|
+
status: response.status,
|
|
409
|
+
statusText: response.statusText,
|
|
410
|
+
data: response.data
|
|
411
|
+
}, "HttpService");
|
|
412
|
+
return response;
|
|
413
|
+
}, (error) => {
|
|
414
|
+
this.logger.error("HTTP Response Error", {
|
|
415
|
+
method: error.config?.method?.toUpperCase(),
|
|
416
|
+
url: error.config?.url,
|
|
417
|
+
status: error.response?.status,
|
|
418
|
+
statusText: error.response?.statusText,
|
|
419
|
+
data: error.response?.data,
|
|
420
|
+
message: error.message
|
|
421
|
+
}, "HttpService");
|
|
422
|
+
return Promise.reject(error);
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
HttpInterceptorService = _ts_decorate6([
|
|
427
|
+
Injectable6(),
|
|
428
|
+
_ts_metadata3("design:type", Function),
|
|
429
|
+
_ts_metadata3("design:paramtypes", [
|
|
430
|
+
typeof HttpService === "undefined" ? Object : HttpService,
|
|
431
|
+
typeof AppLogger === "undefined" ? Object : AppLogger
|
|
432
|
+
])
|
|
433
|
+
], HttpInterceptorService);
|
|
434
|
+
|
|
223
435
|
// src/modules/platform/config/app.config.ts
|
|
224
436
|
import { registerAs } from "@nestjs/config";
|
|
225
437
|
var NAMESPACE = "app";
|
|
@@ -232,16 +444,249 @@ var app_config_default = registerAs(NAMESPACE, () => {
|
|
|
232
444
|
};
|
|
233
445
|
});
|
|
234
446
|
|
|
447
|
+
// src/middlewares/debug/index.ts
|
|
448
|
+
import { Injectable as Injectable7 } from "@nestjs/common";
|
|
449
|
+
import { HttpAdapterHost } from "@nestjs/core";
|
|
450
|
+
function _ts_decorate7(decorators, target, key, desc) {
|
|
451
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
452
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
453
|
+
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;
|
|
454
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
455
|
+
}
|
|
456
|
+
__name(_ts_decorate7, "_ts_decorate");
|
|
457
|
+
function _ts_metadata4(k, v) {
|
|
458
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
459
|
+
}
|
|
460
|
+
__name(_ts_metadata4, "_ts_metadata");
|
|
461
|
+
var processStartTimestamp = Date.now();
|
|
462
|
+
var FrameworkDebugMiddleware = class {
|
|
463
|
+
static {
|
|
464
|
+
__name(this, "FrameworkDebugMiddleware");
|
|
465
|
+
}
|
|
466
|
+
httpAdapterHost;
|
|
467
|
+
constructor(httpAdapterHost) {
|
|
468
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
469
|
+
}
|
|
470
|
+
use(_req, res) {
|
|
471
|
+
res.json(this.getDebugInfo());
|
|
472
|
+
}
|
|
473
|
+
getDebugInfo() {
|
|
474
|
+
const routes = this.getRoutes();
|
|
475
|
+
const uptimeSeconds = Math.floor(process.uptime());
|
|
476
|
+
return {
|
|
477
|
+
["\u8DEF\u7531\u914D\u7F6E"]: routes,
|
|
478
|
+
["\u8FDB\u7A0B\u5B58\u6D3B\u65F6\u95F4\uFF08\u79D2\uFF09"]: uptimeSeconds,
|
|
479
|
+
["\u8FDB\u7A0B\u542F\u52A8\u65F6\u95F4\uFF08\u79D2\uFF09"]: new Date(processStartTimestamp).toISOString(),
|
|
480
|
+
["\u8FD0\u884C\u65F6\u7248\u672C"]: process.versions
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
getRoutes() {
|
|
484
|
+
const server = this.httpAdapterHost.httpAdapter;
|
|
485
|
+
const router = server.getInstance()._router;
|
|
486
|
+
if (!router) {
|
|
487
|
+
return {
|
|
488
|
+
error: "Router \u4E0D\u53EF\u7528"
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const routes = [];
|
|
492
|
+
router.stack.forEach((middleware, index) => {
|
|
493
|
+
if (middleware.route) {
|
|
494
|
+
routes.push({
|
|
495
|
+
order: String(index),
|
|
496
|
+
path: middleware.route.path,
|
|
497
|
+
method: middleware.method,
|
|
498
|
+
regexp: middleware.regexp?.toString()
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
return {
|
|
503
|
+
total: routes.length,
|
|
504
|
+
routes
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
FrameworkDebugMiddleware = _ts_decorate7([
|
|
509
|
+
Injectable7(),
|
|
510
|
+
_ts_metadata4("design:type", Function),
|
|
511
|
+
_ts_metadata4("design:paramtypes", [
|
|
512
|
+
typeof HttpAdapterHost === "undefined" ? Object : HttpAdapterHost
|
|
513
|
+
])
|
|
514
|
+
], FrameworkDebugMiddleware);
|
|
515
|
+
|
|
516
|
+
// src/services/platform-http-client.service.ts
|
|
517
|
+
import { Injectable as Injectable8, Logger as Logger2 } from "@nestjs/common";
|
|
518
|
+
import { HttpClient } from "@lark-apaas/http-client";
|
|
519
|
+
function _ts_decorate8(decorators, target, key, desc) {
|
|
520
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
521
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
522
|
+
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;
|
|
523
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
524
|
+
}
|
|
525
|
+
__name(_ts_decorate8, "_ts_decorate");
|
|
526
|
+
function _ts_metadata5(k, v) {
|
|
527
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
528
|
+
}
|
|
529
|
+
__name(_ts_metadata5, "_ts_metadata");
|
|
530
|
+
var ProtectedHttpClient = class ProtectedHttpClient2 {
|
|
531
|
+
static {
|
|
532
|
+
__name(this, "ProtectedHttpClient");
|
|
533
|
+
}
|
|
534
|
+
client;
|
|
535
|
+
constructor(client) {
|
|
536
|
+
this.client = client;
|
|
537
|
+
}
|
|
538
|
+
request(config) {
|
|
539
|
+
return this.client.request(config);
|
|
540
|
+
}
|
|
541
|
+
get(url, config) {
|
|
542
|
+
return this.client.get(url, config);
|
|
543
|
+
}
|
|
544
|
+
post(url, data, config) {
|
|
545
|
+
return this.client.post(url, data, config);
|
|
546
|
+
}
|
|
547
|
+
put(url, data, config) {
|
|
548
|
+
return this.client.put(url, data, config);
|
|
549
|
+
}
|
|
550
|
+
patch(url, data, config) {
|
|
551
|
+
return this.client.patch(url, data, config);
|
|
552
|
+
}
|
|
553
|
+
delete(url, config) {
|
|
554
|
+
return this.client.delete(url, config);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
var PlatformHttpClientService = class _PlatformHttpClientService {
|
|
558
|
+
static {
|
|
559
|
+
__name(this, "PlatformHttpClientService");
|
|
560
|
+
}
|
|
561
|
+
client;
|
|
562
|
+
protectedClient;
|
|
563
|
+
logger = new Logger2(_PlatformHttpClientService.name);
|
|
564
|
+
constructor() {
|
|
565
|
+
const baseConfig = {
|
|
566
|
+
timeout: 5e3
|
|
567
|
+
};
|
|
568
|
+
this.client = new HttpClient({
|
|
569
|
+
...baseConfig,
|
|
570
|
+
platform: {
|
|
571
|
+
enabled: true
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
this.protectedClient = new ProtectedHttpClient(this.client);
|
|
575
|
+
this.registerGlobalInterceptors();
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* 获取受保护的 HttpClient 实例(推荐)
|
|
579
|
+
*
|
|
580
|
+
* 该实例不暴露 interceptors,防止业务代码修改全局拦截器
|
|
581
|
+
*
|
|
582
|
+
* @returns 受保护的 HttpClient 实例
|
|
583
|
+
*/
|
|
584
|
+
get instance() {
|
|
585
|
+
return this.protectedClient;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* 获取原始 HttpClient 实例(危险)
|
|
589
|
+
*
|
|
590
|
+
* ⚠️ 警告:该实例允许修改拦截器,会影响所有使用全局实例的地方
|
|
591
|
+
*
|
|
592
|
+
* 仅用于特殊场景,不应该暴露给业务代码
|
|
593
|
+
* 如果需要自定义拦截器,请使用 `create()` 方法创建独立实例
|
|
594
|
+
*
|
|
595
|
+
* @internal
|
|
596
|
+
* @returns 原始 HttpClient 实例
|
|
597
|
+
*/
|
|
598
|
+
get rawInstance() {
|
|
599
|
+
return this.client;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* 创建一个新的独立 HttpClient 实例
|
|
603
|
+
*
|
|
604
|
+
* 适用于需要自定义拦截器或配置的场景
|
|
605
|
+
* 创建的实例完全独立,不会影响全局实例
|
|
606
|
+
*
|
|
607
|
+
* @param options - 配置选项(会与全局配置合并)
|
|
608
|
+
* @returns 完整的 HttpClient 实例(允许修改拦截器)
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* ```typescript
|
|
612
|
+
* @Injectable()
|
|
613
|
+
* export class FileService {
|
|
614
|
+
* private fileClient: HttpClient;
|
|
615
|
+
*
|
|
616
|
+
* constructor(private platformHttp: PlatformHttpClientService) {
|
|
617
|
+
* // 创建超时 60 秒的独立实例
|
|
618
|
+
* this.fileClient = this.platformHttp.create({
|
|
619
|
+
* config: { timeout: 60000 }
|
|
620
|
+
* });
|
|
621
|
+
*
|
|
622
|
+
* // 为这个实例注册拦截器(不影响全局实例)
|
|
623
|
+
* this.fileClient.interceptors.request.use((config) => {
|
|
624
|
+
* console.log('File upload starting...');
|
|
625
|
+
* return config;
|
|
626
|
+
* });
|
|
627
|
+
* }
|
|
628
|
+
*
|
|
629
|
+
* async uploadFile(file: Buffer) {
|
|
630
|
+
* return this.fileClient.post('/file/upload', file);
|
|
631
|
+
* }
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
create(options) {
|
|
636
|
+
const mergedConfig = {
|
|
637
|
+
timeout: 5e3,
|
|
638
|
+
...options?.config
|
|
639
|
+
};
|
|
640
|
+
const mergedPlatform = {
|
|
641
|
+
enabled: true,
|
|
642
|
+
...options?.platform
|
|
643
|
+
};
|
|
644
|
+
return new HttpClient({
|
|
645
|
+
...mergedConfig,
|
|
646
|
+
platform: mergedPlatform
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* 注册全局拦截器
|
|
651
|
+
*/
|
|
652
|
+
registerGlobalInterceptors() {
|
|
653
|
+
this.client.interceptors.request.use((config) => {
|
|
654
|
+
this.logger.log(`HTTP Request: ${config.method?.toUpperCase()} ${config.url}`);
|
|
655
|
+
return config;
|
|
656
|
+
}, (error) => {
|
|
657
|
+
this.logger.error("HTTP Request Error", error, "HttpService");
|
|
658
|
+
return Promise.reject(error);
|
|
659
|
+
});
|
|
660
|
+
this.client.interceptors.response.use((response) => {
|
|
661
|
+
this.logger.log(`HTTP Response: ${response.status} ${response.url}`);
|
|
662
|
+
return response;
|
|
663
|
+
}, (error) => {
|
|
664
|
+
const errorLog = {
|
|
665
|
+
status: error.response?.status,
|
|
666
|
+
statusText: error.response?.statusText,
|
|
667
|
+
message: error.message
|
|
668
|
+
};
|
|
669
|
+
this.logger.error(`HTTP Response Error: ${error.config?.method?.toUpperCase()} ${error.config?.url} ${JSON.stringify(errorLog)}`);
|
|
670
|
+
return Promise.reject(error);
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
PlatformHttpClientService = _ts_decorate8([
|
|
675
|
+
Injectable8(),
|
|
676
|
+
_ts_metadata5("design:type", Function),
|
|
677
|
+
_ts_metadata5("design:paramtypes", [])
|
|
678
|
+
], PlatformHttpClientService);
|
|
679
|
+
|
|
235
680
|
// src/modules/platform/module.ts
|
|
236
681
|
import { AuthZPaasModule } from "@lark-apaas/nestjs-authzpaas";
|
|
237
|
-
function
|
|
682
|
+
function _ts_decorate9(decorators, target, key, desc) {
|
|
238
683
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
239
684
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
240
685
|
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;
|
|
241
686
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
242
687
|
}
|
|
243
|
-
__name(
|
|
244
|
-
var PLATFORM_MODULE_OPTIONS = "PLATFORM_MODULE_OPTIONS";
|
|
688
|
+
__name(_ts_decorate9, "_ts_decorate");
|
|
689
|
+
var PLATFORM_MODULE_OPTIONS = /* @__PURE__ */ Symbol("PLATFORM_MODULE_OPTIONS");
|
|
245
690
|
var PlatformModule = class _PlatformModule {
|
|
246
691
|
static {
|
|
247
692
|
__name(this, "PlatformModule");
|
|
@@ -262,7 +707,13 @@ var PlatformModule = class _PlatformModule {
|
|
|
262
707
|
app_config_default
|
|
263
708
|
]
|
|
264
709
|
}),
|
|
710
|
+
CommonModule,
|
|
711
|
+
ObservableModule,
|
|
265
712
|
LoggerModule,
|
|
713
|
+
HttpModule.register({
|
|
714
|
+
timeout: 5e3,
|
|
715
|
+
maxRedirects: 5
|
|
716
|
+
}),
|
|
266
717
|
DataPaasModule.forRootAsync({
|
|
267
718
|
imports: [
|
|
268
719
|
ConfigModule,
|
|
@@ -270,17 +721,17 @@ var PlatformModule = class _PlatformModule {
|
|
|
270
721
|
],
|
|
271
722
|
inject: [
|
|
272
723
|
ConfigService,
|
|
273
|
-
|
|
724
|
+
AppLogger2
|
|
274
725
|
],
|
|
275
726
|
useFactory: /* @__PURE__ */ __name(async (...args) => {
|
|
276
727
|
const configService = args[0];
|
|
277
728
|
const appLogger = args[1];
|
|
278
729
|
const drizzleLogger = {
|
|
279
730
|
logQuery(query, params) {
|
|
280
|
-
appLogger.log?.("SQL Query"
|
|
731
|
+
appLogger.log?.("SQL Query " + JSON.stringify({
|
|
281
732
|
query,
|
|
282
733
|
params
|
|
283
|
-
}, "Database");
|
|
734
|
+
}), "Database");
|
|
284
735
|
}
|
|
285
736
|
};
|
|
286
737
|
return {
|
|
@@ -290,6 +741,7 @@ var PlatformModule = class _PlatformModule {
|
|
|
290
741
|
}, "useFactory")
|
|
291
742
|
}),
|
|
292
743
|
AuthNPaasModule.forRoot(),
|
|
744
|
+
AutomationModule.forRoot(),
|
|
293
745
|
AuthZPaasModule.forRoot()
|
|
294
746
|
],
|
|
295
747
|
providers: [
|
|
@@ -305,11 +757,27 @@ var PlatformModule = class _PlatformModule {
|
|
|
305
757
|
enableImplicitConversion: true
|
|
306
758
|
}
|
|
307
759
|
})
|
|
308
|
-
}
|
|
760
|
+
},
|
|
761
|
+
{
|
|
762
|
+
provide: OBSERVABLE_SERVICE,
|
|
763
|
+
useClass: Observable
|
|
764
|
+
},
|
|
765
|
+
PlatformHttpClientService,
|
|
766
|
+
{
|
|
767
|
+
provide: PLATFORM_HTTP_CLIENT2,
|
|
768
|
+
useFactory: /* @__PURE__ */ __name((svc) => svc.instance, "useFactory"),
|
|
769
|
+
inject: [
|
|
770
|
+
PlatformHttpClientService
|
|
771
|
+
]
|
|
772
|
+
},
|
|
773
|
+
HttpInterceptorService
|
|
309
774
|
],
|
|
310
775
|
exports: [
|
|
311
776
|
ConfigModule,
|
|
312
|
-
LoggerModule
|
|
777
|
+
LoggerModule,
|
|
778
|
+
CommonModule,
|
|
779
|
+
OBSERVABLE_SERVICE,
|
|
780
|
+
PLATFORM_HTTP_CLIENT2
|
|
313
781
|
]
|
|
314
782
|
};
|
|
315
783
|
}
|
|
@@ -318,7 +786,11 @@ var PlatformModule = class _PlatformModule {
|
|
|
318
786
|
*/
|
|
319
787
|
configure(consumer) {
|
|
320
788
|
const options = _PlatformModule.moduleOptions;
|
|
321
|
-
consumer.apply(
|
|
789
|
+
consumer.apply(apiResponseInterceptor).forRoutes("/api/*");
|
|
790
|
+
if (process.env.NODE_ENV === "development") {
|
|
791
|
+
consumer.apply(FrameworkDebugMiddleware).forRoutes("/api/__framework__/debug");
|
|
792
|
+
}
|
|
793
|
+
consumer.apply(UserContextMiddleware, RequestContextMiddleware, LoggerContextMiddleware, SqlExecutionContextMiddleware).forRoutes("/*");
|
|
322
794
|
consumer.apply(CsrfTokenMiddleware, ViewContextMiddleware).exclude("/api/(.*)").forRoutes("*");
|
|
323
795
|
if (options.enableCsrf !== false) {
|
|
324
796
|
const csrfRoutes = options.csrfRoutes || "/api/*";
|
|
@@ -332,22 +804,25 @@ var PlatformModule = class _PlatformModule {
|
|
|
332
804
|
}
|
|
333
805
|
}
|
|
334
806
|
};
|
|
335
|
-
PlatformModule =
|
|
807
|
+
PlatformModule = _ts_decorate9([
|
|
336
808
|
Global(),
|
|
337
809
|
Module({})
|
|
338
810
|
], PlatformModule);
|
|
339
811
|
|
|
340
812
|
// src/setup.ts
|
|
341
|
-
import { AppLogger as
|
|
813
|
+
import { AppLogger as AppLogger3 } from "@lark-apaas/nestjs-logger";
|
|
342
814
|
import cookieParser from "cookie-parser";
|
|
343
815
|
import { DevToolsV2Module } from "@lark-apaas/nestjs-openapi-devtools";
|
|
344
|
-
|
|
345
|
-
|
|
816
|
+
var defaultPerms = {
|
|
817
|
+
disableSwagger: false
|
|
818
|
+
};
|
|
819
|
+
async function configureApp(app, perms = defaultPerms) {
|
|
820
|
+
app.useLogger(app.get(AppLogger3));
|
|
346
821
|
app.flushLogs();
|
|
347
822
|
app.use(cookieParser());
|
|
348
823
|
const globalPrefix = process.env.CLIENT_BASE_PATH ?? "";
|
|
349
824
|
app.setGlobalPrefix(globalPrefix);
|
|
350
|
-
if (process.env.NODE_ENV !== "production") {
|
|
825
|
+
if (process.env.NODE_ENV !== "production" && perms.disableSwagger !== true) {
|
|
351
826
|
try {
|
|
352
827
|
await DevToolsV2Module.mount(app, {
|
|
353
828
|
basePath: process.env.CLIENT_BASE_PATH,
|
|
@@ -364,7 +839,11 @@ __name(configureApp, "configureApp");
|
|
|
364
839
|
import { DevToolsModule, DevToolsV2Module as DevToolsV2Module2 } from "@lark-apaas/nestjs-openapi-devtools";
|
|
365
840
|
export * from "@lark-apaas/nestjs-authnpaas";
|
|
366
841
|
export * from "@lark-apaas/nestjs-datapaas";
|
|
842
|
+
export * from "@lark-apaas/nestjs-observable";
|
|
843
|
+
export * from "@lark-apaas/nestjs-trigger";
|
|
844
|
+
import { CanRole } from "@lark-apaas/nestjs-authzpaas";
|
|
367
845
|
export {
|
|
846
|
+
CanRole,
|
|
368
847
|
CsrfMiddleware,
|
|
369
848
|
CsrfTokenMiddleware,
|
|
370
849
|
DevToolsModule,
|