@buka/nestjs-kit 4.4.3 → 4.4.6
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/CHANGELOG.md +23 -0
- package/dist/index.d.mts +74 -7
- package/dist/index.d.ts +74 -7
- package/dist/index.js +48 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -16,7 +16,8 @@ import { Migrator } from "@mikro-orm/migrations";
|
|
|
16
16
|
import * as fs from "node:fs";
|
|
17
17
|
import { KeqRequest, RequestException } from "keq";
|
|
18
18
|
import { setBaseUrl } from "@keq-request/url";
|
|
19
|
-
import { createExceptionByStatusCode, validateStatusCode } from "@keq-request/exception";
|
|
19
|
+
import { clarifyFetchFailed, createExceptionByStatusCode, validateStatusCode } from "@keq-request/exception";
|
|
20
|
+
import { InjectKeqConsumer, KEQ_ROUTES, KeqConsumer, KeqModule } from "@keq-request/nestjs";
|
|
20
21
|
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "crypto";
|
|
21
22
|
import bcrypt from "bcryptjs";
|
|
22
23
|
import { Base32, Base32 as Base32$1, ErrorCategory, ErrorCategory as ErrorCategory$1, ErrorCode, ErrorCode as ErrorCode$1 } from "@buka/error-codes";
|
|
@@ -728,12 +729,16 @@ function ValidateNestedDictionary(typeFunc, validationOptions) {
|
|
|
728
729
|
//#endregion
|
|
729
730
|
//#region src/modules/core/decorators/is-browser-request.ts
|
|
730
731
|
/**
|
|
731
|
-
* 参数装饰器,通过检测请求头中是否存在 `Sec-Fetch
|
|
732
|
+
* 参数装饰器,通过检测请求头中是否存在 `Sec-Fetch-Site` 头,
|
|
732
733
|
* 判断请求是否来自浏览器。
|
|
733
734
|
*
|
|
734
735
|
* 浏览器在发起请求时会自动附加 `Sec-Fetch-Site`、`Sec-Fetch-Mode`、
|
|
735
|
-
* `Sec-Fetch-Dest` 等头([Fetch Metadata Request Headers](https://www.w3.org/TR/fetch-metadata/)
|
|
736
|
-
* 非浏览器客户端(如 curl、Postman
|
|
736
|
+
* `Sec-Fetch-Dest` 等头([Fetch Metadata Request Headers](https://www.w3.org/TR/fetch-metadata/))。
|
|
737
|
+
* 非浏览器客户端(如 curl、Postman)通常不会携带这些头。
|
|
738
|
+
*
|
|
739
|
+
* **注意:** Node.js 的 undici(globalThis.fetch)会自动添加 `Sec-Fetch-Mode: cors`,
|
|
740
|
+
* 但不会添加 `Sec-Fetch-Site`。因此使用 `Sec-Fetch-Site` 而非任意 `Sec-Fetch-*`
|
|
741
|
+
* 头来区分真正的浏览器请求和服务端 fetch 调用。
|
|
737
742
|
*
|
|
738
743
|
* 装饰的参数类型应为 `boolean`。
|
|
739
744
|
*
|
|
@@ -752,7 +757,7 @@ function ValidateNestedDictionary(typeFunc, validationOptions) {
|
|
|
752
757
|
*/
|
|
753
758
|
const IsBrowserRequest = createParamDecorator((_data, ctx) => {
|
|
754
759
|
const request = ctx.switchToHttp().getRequest();
|
|
755
|
-
return Object.keys(request.headers).some((key) => key.toLowerCase()
|
|
760
|
+
return Object.keys(request.headers).some((key) => key.toLowerCase() === "sec-fetch-site");
|
|
756
761
|
});
|
|
757
762
|
//#endregion
|
|
758
763
|
//#region src/modules/core/decorators/model/model.register.ts
|
|
@@ -2689,7 +2694,7 @@ const Column = {
|
|
|
2689
2694
|
};
|
|
2690
2695
|
//#endregion
|
|
2691
2696
|
//#region src/modules/mikro-orm/models/timestamped-entity.ts
|
|
2692
|
-
var _ref$14, _ref2$
|
|
2697
|
+
var _ref$14, _ref2$4;
|
|
2693
2698
|
var TimestampedEntity = class {
|
|
2694
2699
|
[OptionalProps];
|
|
2695
2700
|
createdAt = /* @__PURE__ */ new Date();
|
|
@@ -2704,7 +2709,7 @@ __decorate([Column.Timestamptz({
|
|
|
2704
2709
|
onUpdate: () => /* @__PURE__ */ new Date(),
|
|
2705
2710
|
defaultRaw: "CURRENT_TIMESTAMP",
|
|
2706
2711
|
comment: "更新时间"
|
|
2707
|
-
}), __decorateMetadata("design:type", typeof (_ref2$
|
|
2712
|
+
}), __decorateMetadata("design:type", typeof (_ref2$4 = typeof Date !== "undefined" && Date) === "function" ? _ref2$4 : Object)], TimestampedEntity.prototype, "updatedAt", void 0);
|
|
2708
2713
|
//#endregion
|
|
2709
2714
|
//#region src/modules/mikro-orm/models/linear-entity.ts
|
|
2710
2715
|
var LinearEntity = class extends TimestampedEntity {
|
|
@@ -2835,6 +2840,7 @@ function PrimaryKeyType(classRef) {
|
|
|
2835
2840
|
const primaryProperties = getMetadata(classRef).filter((prop) => prop.primary).map((prop) => prop.name);
|
|
2836
2841
|
if (!primaryProperties.length) throw new Error(`Cannot create PrimaryKeyType for ${classRef.name} because it has no primary properties.`);
|
|
2837
2842
|
class PrimaryKeyTypeClass {}
|
|
2843
|
+
Object.defineProperty(PrimaryKeyTypeClass, "name", { value: `${classRef.name}PrimaryKeyType` });
|
|
2838
2844
|
PrimaryKeyTypeClass[PrimaryKeyTypeClassMetadataPropertyKey] = classRef;
|
|
2839
2845
|
cloneMetadata(PrimaryKeyTypeClass, classRef, primaryProperties);
|
|
2840
2846
|
inheritValidationMetadata(classRef, PrimaryKeyTypeClass, (key) => primaryProperties.includes(key));
|
|
@@ -3160,7 +3166,7 @@ __decorate([
|
|
|
3160
3166
|
], DatabaseConfig.prototype, "poolIdleTimeoutMillis", void 0);
|
|
3161
3167
|
//#endregion
|
|
3162
3168
|
//#region src/modules/core/pipes/validation.pipe.ts
|
|
3163
|
-
var _ref$13, _ref2$
|
|
3169
|
+
var _ref$13, _ref2$3, _ref3$1, _BukaValidationPipe;
|
|
3164
3170
|
function deepMap(obj, fn) {
|
|
3165
3171
|
if (Array.isArray(obj)) return obj.map((item) => deepMap(item, fn));
|
|
3166
3172
|
else return fn(obj);
|
|
@@ -3221,7 +3227,7 @@ let BukaValidationPipe = _BukaValidationPipe = class BukaValidationPipe extends
|
|
|
3221
3227
|
};
|
|
3222
3228
|
BukaValidationPipe = _BukaValidationPipe = __decorate([Injectable(), __decorateMetadata("design:paramtypes", [
|
|
3223
3229
|
typeof (_ref$13 = typeof MikroORM !== "undefined" && MikroORM) === "function" ? _ref$13 : Object,
|
|
3224
|
-
typeof (_ref2$
|
|
3230
|
+
typeof (_ref2$3 = typeof EntityManager !== "undefined" && EntityManager) === "function" ? _ref2$3 : Object,
|
|
3225
3231
|
typeof (_ref3$1 = typeof ValidationPipeOptions !== "undefined" && ValidationPipeOptions) === "function" ? _ref3$1 : Object
|
|
3226
3232
|
])], BukaValidationPipe);
|
|
3227
3233
|
//#endregion
|
|
@@ -6454,6 +6460,7 @@ OpenBaoHttpClient = _OpenBaoHttpClient = __decorate([Injectable(), __decorateMet
|
|
|
6454
6460
|
//#endregion
|
|
6455
6461
|
//#region src/apis/open-bao-http/open-bao-http.module.ts
|
|
6456
6462
|
const KEQ_REQUEST_TOKEN = Symbol("OpenBaoHttpKeqRequest");
|
|
6463
|
+
const KEQ_CONSUMER_TOKEN = Symbol("OpenBaoHttpKeqConsumer");
|
|
6457
6464
|
const { ConfigurableModuleClass: ConfigurableModuleClass$3, MODULE_OPTIONS_TOKEN: MODULE_OPTIONS_TOKEN$3 } = new ConfigurableModuleBuilder().setExtras({ isGlobal: false }, (definition, extras) => ({
|
|
6458
6465
|
...definition,
|
|
6459
6466
|
global: extras.isGlobal
|
|
@@ -6476,12 +6483,23 @@ const CLIENT_PROVIDER = {
|
|
|
6476
6483
|
useFactory: (request) => new OpenBaoHttpClient(request),
|
|
6477
6484
|
inject: [KEQ_REQUEST_TOKEN]
|
|
6478
6485
|
};
|
|
6486
|
+
const CONSUMER_PROVIDER = {
|
|
6487
|
+
provide: KEQ_CONSUMER_TOKEN,
|
|
6488
|
+
useFactory: (request) => new KeqConsumer(request),
|
|
6489
|
+
inject: [KEQ_REQUEST_TOKEN]
|
|
6490
|
+
};
|
|
6479
6491
|
let OpenBaoHttpModule = class OpenBaoHttpModule extends ConfigurableModuleClass$3 {
|
|
6480
6492
|
static KEQ_REQUEST = KEQ_REQUEST_TOKEN;
|
|
6493
|
+
static KEQ_CONSUMER = KEQ_CONSUMER_TOKEN;
|
|
6481
6494
|
};
|
|
6482
6495
|
OpenBaoHttpModule = __decorate([Module({
|
|
6483
|
-
|
|
6484
|
-
|
|
6496
|
+
imports: [KeqModule],
|
|
6497
|
+
providers: [
|
|
6498
|
+
REQUEST_PROVIDER,
|
|
6499
|
+
CONSUMER_PROVIDER,
|
|
6500
|
+
CLIENT_PROVIDER
|
|
6501
|
+
],
|
|
6502
|
+
exports: [OpenBaoHttpClient, KEQ_CONSUMER_TOKEN]
|
|
6485
6503
|
})], OpenBaoHttpModule);
|
|
6486
6504
|
//#endregion
|
|
6487
6505
|
//#region src/modules/open-bao/set-open-bao-token.middleware.ts
|
|
@@ -6538,6 +6556,7 @@ function setOpenBaoToken(getToken) {
|
|
|
6538
6556
|
}
|
|
6539
6557
|
//#endregion
|
|
6540
6558
|
//#region src/modules/open-bao/request/format-error.ts
|
|
6559
|
+
const logger$1 = new Logger$1("ClarifyOpenbaoError");
|
|
6541
6560
|
/**
|
|
6542
6561
|
* 根据 HTTP 状态码生成中文操作建议
|
|
6543
6562
|
*/
|
|
@@ -6554,14 +6573,19 @@ function getStatusCodeGuidance(statusCode) {
|
|
|
6554
6573
|
* 必须注册在 `validateStatusCode` 之后,作为中间件链的最外层,
|
|
6555
6574
|
* 捕获 `RequestException` 并重写其错误消息,补充请求上下文和操作建议。
|
|
6556
6575
|
*
|
|
6557
|
-
* 非 RequestException
|
|
6576
|
+
* 非 RequestException 错误(网络故障、超时等)记录日志后原样透传。
|
|
6558
6577
|
*/
|
|
6559
|
-
function
|
|
6578
|
+
function clarifyOpenbaoError() {
|
|
6560
6579
|
const middleware = async (ctx, next) => {
|
|
6561
6580
|
try {
|
|
6562
6581
|
await next();
|
|
6563
6582
|
} catch (err) {
|
|
6564
|
-
if (!(err instanceof RequestException))
|
|
6583
|
+
if (!(err instanceof RequestException)) {
|
|
6584
|
+
const method = ctx.request.method.toUpperCase();
|
|
6585
|
+
const url = ctx.request.url.toString();
|
|
6586
|
+
logger$1.error(`OpenBaoModule API 请求异常: ${method} ${url}`, err instanceof Error ? err.stack : void 0);
|
|
6587
|
+
throw err;
|
|
6588
|
+
}
|
|
6565
6589
|
const method = ctx.request.method.toUpperCase();
|
|
6566
6590
|
const url = ctx.request.url.toString();
|
|
6567
6591
|
const statusCode = err.statusCode;
|
|
@@ -6578,31 +6602,34 @@ function formatError() {
|
|
|
6578
6602
|
throw err;
|
|
6579
6603
|
}
|
|
6580
6604
|
};
|
|
6581
|
-
middleware.__keqMiddlewareName__ = "
|
|
6605
|
+
middleware.__keqMiddlewareName__ = "clarifyOpenbaoError";
|
|
6582
6606
|
return middleware;
|
|
6583
6607
|
}
|
|
6584
6608
|
//#endregion
|
|
6585
6609
|
//#region src/modules/open-bao/open-bao.module.ts
|
|
6586
|
-
var _ref$10;
|
|
6610
|
+
var _ref$10, _ref2$2;
|
|
6587
6611
|
let OpenBaoModule = class OpenBaoModule extends ConfigurableModuleClass$4 {
|
|
6588
|
-
constructor(options, tokenManager) {
|
|
6612
|
+
constructor(options, tokenManager, consumer) {
|
|
6589
6613
|
super();
|
|
6590
6614
|
this.options = options;
|
|
6591
6615
|
this.tokenManager = tokenManager;
|
|
6592
|
-
|
|
6593
|
-
configureKeqMiddleware(consumer) {
|
|
6594
|
-
consumer.apply(formatError(), setBaseUrl(this.options.address + "/v1/"), setOpenBaoToken(() => this.tokenManager.getToken()), validateStatusCode()).forRoutes(OpenBaoHttpModule);
|
|
6616
|
+
consumer.apply(clarifyOpenbaoError(), setBaseUrl(this.options.address + "/v1/"), setOpenBaoToken(() => this.tokenManager.getToken()), validateStatusCode(), clarifyFetchFailed()).forRoutes(KEQ_ROUTES.ALL);
|
|
6595
6617
|
}
|
|
6596
6618
|
};
|
|
6597
6619
|
OpenBaoModule = __decorate([
|
|
6598
6620
|
Global(),
|
|
6599
6621
|
Module({
|
|
6600
|
-
imports: [OpenBaoHttpModule.register({ isGlobal: true })],
|
|
6622
|
+
imports: [KeqModule, OpenBaoHttpModule.register({ isGlobal: true })],
|
|
6601
6623
|
providers: [OpenBaoTokenManager],
|
|
6602
6624
|
exports: [OpenBaoTokenManager, OpenBaoHttpModule]
|
|
6603
6625
|
}),
|
|
6604
6626
|
__decorateParam(0, Inject(MODULE_OPTIONS_TOKEN$4)),
|
|
6605
|
-
|
|
6627
|
+
__decorateParam(2, InjectKeqConsumer(OpenBaoHttpModule)),
|
|
6628
|
+
__decorateMetadata("design:paramtypes", [
|
|
6629
|
+
Object,
|
|
6630
|
+
typeof (_ref$10 = typeof OpenBaoTokenManager !== "undefined" && OpenBaoTokenManager) === "function" ? _ref$10 : Object,
|
|
6631
|
+
typeof (_ref2$2 = typeof KeqConsumer !== "undefined" && KeqConsumer) === "function" ? _ref2$2 : Object
|
|
6632
|
+
])
|
|
6606
6633
|
], OpenBaoModule);
|
|
6607
6634
|
//#endregion
|
|
6608
6635
|
//#region src/modules/open-bao/open-bao.config.ts
|