nesties 1.1.4 → 1.1.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/dist/index.cjs +545 -20
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +548 -16
- package/dist/index.mjs.map +4 -4
- package/dist/src/abortable-module/abort-signal.provider.d.ts +3 -3
- package/dist/src/abortable-module/abortable.module.d.ts +2 -2
- package/dist/src/abortable-module/abortable.token.d.ts +5 -5
- package/dist/src/create-provider.d.ts +13 -0
- package/dist/src/i18n-module/i18n-decorator.d.ts +2 -0
- package/dist/src/i18n-module/i18n-factory.d.ts +7 -0
- package/dist/src/i18n-module/i18n-middleware.type.d.ts +3 -0
- package/dist/src/i18n-module/i18n-module.options.d.ts +14 -0
- package/dist/src/i18n-module/i18n-resolver.d.ts +2 -0
- package/dist/src/i18n-module/i18n-token.d.ts +5 -0
- package/dist/src/i18n-module/i18n.interceptor.d.ts +7 -0
- package/dist/src/i18n-module/i18n.module.d.ts +3 -0
- package/dist/src/i18n-module/i18n.service.d.ts +22 -0
- package/dist/src/i18n-module/index.d.ts +9 -0
- package/dist/src/i18n-module/locale.pipe.d.ts +14 -0
- package/dist/src/i18n-module/middelewares/index.d.ts +1 -0
- package/dist/src/i18n-module/middelewares/lookup.d.ts +9 -0
- package/dist/src/utility/awaitable.d.ts +1 -0
- package/dist/src/utility/parse-i18n.d.ts +14 -0
- package/index.ts +2 -0
- package/package.json +1 -1
- /package/dist/src/{abort-http-signal.d.ts → utility/abort-http-signal.d.ts} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -6,11 +6,11 @@ var __export = (target, all) => {
|
|
|
6
6
|
for (var name in all)
|
|
7
7
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
8
|
};
|
|
9
|
-
var __copyProps = (to,
|
|
10
|
-
if (
|
|
11
|
-
for (let key of __getOwnPropNames(
|
|
9
|
+
var __copyProps = (to, from2, except, desc) => {
|
|
10
|
+
if (from2 && typeof from2 === "object" || typeof from2 === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from2))
|
|
12
12
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () =>
|
|
13
|
+
__defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable });
|
|
14
14
|
}
|
|
15
15
|
return to;
|
|
16
16
|
};
|
|
@@ -43,21 +43,30 @@ __export(index_exports, {
|
|
|
43
43
|
DataQuery: () => DataQuery,
|
|
44
44
|
GenericPaginatedReturnMessageDto: () => GenericPaginatedReturnMessageDto,
|
|
45
45
|
GenericReturnMessageDto: () => GenericReturnMessageDto,
|
|
46
|
+
I18nInterceptor: () => I18nInterceptor,
|
|
47
|
+
I18nLookupMiddleware: () => I18nLookupMiddleware,
|
|
48
|
+
I18nModule: () => I18nModule,
|
|
49
|
+
I18nService: () => I18nService,
|
|
46
50
|
InjectAbortSignal: () => InjectAbortSignal,
|
|
47
51
|
InjectAbortable: () => InjectAbortable,
|
|
48
52
|
InsertField: () => InsertField,
|
|
53
|
+
LocalePipe: () => LocalePipe,
|
|
49
54
|
MergeClassDecorators: () => MergeClassDecorators,
|
|
50
55
|
MergeClassOrMethodDecorators: () => MergeClassOrMethodDecorators,
|
|
51
56
|
MergeMethodDecorators: () => MergeMethodDecorators,
|
|
52
57
|
MergeParameterDecorators: () => MergeParameterDecorators,
|
|
53
58
|
MergePropertyDecorators: () => MergePropertyDecorators,
|
|
54
59
|
PaginatedReturnMessageDto: () => PaginatedReturnMessageDto,
|
|
60
|
+
PutLocale: () => PutLocale,
|
|
55
61
|
RequireToken: () => RequireToken,
|
|
56
62
|
ReturnMessageDto: () => ReturnMessageDto,
|
|
57
63
|
StringReturnMessageDto: () => StringReturnMessageDto,
|
|
58
64
|
TokenGuard: () => TokenGuard,
|
|
59
65
|
abortableToken: () => abortableToken,
|
|
60
66
|
createAbortableProvider: () => createAbortableProvider,
|
|
67
|
+
createI18n: () => createI18n,
|
|
68
|
+
createI18nDecorator: () => createI18nDecorator,
|
|
69
|
+
createProvider: () => createProvider,
|
|
61
70
|
fromAbortable: () => fromAbortable,
|
|
62
71
|
getClassFromClassOrArray: () => getClassFromClassOrArray,
|
|
63
72
|
takeUntilAbort: () => takeUntilAbort
|
|
@@ -286,7 +295,7 @@ var RequireToken = () => MergeClassOrMethodDecorators([
|
|
|
286
295
|
var import_rxjs = require("rxjs");
|
|
287
296
|
var import_common4 = require("@nestjs/common");
|
|
288
297
|
|
|
289
|
-
// src/abort-http-signal.ts
|
|
298
|
+
// src/utility/abort-http-signal.ts
|
|
290
299
|
function toRawReq(req) {
|
|
291
300
|
if (req?.raw?.socket) return req.raw;
|
|
292
301
|
return req;
|
|
@@ -364,15 +373,27 @@ var import_nfkit = require("nfkit");
|
|
|
364
373
|
// src/abortable-module/abort-signal.provider.ts
|
|
365
374
|
var import_common5 = require("@nestjs/common");
|
|
366
375
|
var import_core = require("@nestjs/core");
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
}
|
|
376
|
+
|
|
377
|
+
// src/create-provider.ts
|
|
378
|
+
var createProvider = (options, factory) => {
|
|
379
|
+
return {
|
|
380
|
+
useFactory: factory,
|
|
381
|
+
...options
|
|
382
|
+
};
|
|
375
383
|
};
|
|
384
|
+
|
|
385
|
+
// src/abortable-module/abort-signal.provider.ts
|
|
386
|
+
var ABORT_SIGNAL = Symbol(
|
|
387
|
+
"ABORT_SIGNAL"
|
|
388
|
+
);
|
|
389
|
+
var AbortSignalProvider = createProvider(
|
|
390
|
+
{
|
|
391
|
+
provide: ABORT_SIGNAL,
|
|
392
|
+
scope: import_common5.Scope.REQUEST,
|
|
393
|
+
inject: [import_core.REQUEST]
|
|
394
|
+
},
|
|
395
|
+
createAbortSignalFromHttp
|
|
396
|
+
);
|
|
376
397
|
var InjectAbortSignal = () => (0, import_common5.Inject)(ABORT_SIGNAL);
|
|
377
398
|
|
|
378
399
|
// src/abortable-module/abortable.token.ts
|
|
@@ -402,12 +423,13 @@ function InjectAbortable(token) {
|
|
|
402
423
|
}
|
|
403
424
|
function createAbortableProvider(token, opts) {
|
|
404
425
|
const provide = abortableToken(token);
|
|
405
|
-
return
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
426
|
+
return createProvider(
|
|
427
|
+
{
|
|
428
|
+
provide,
|
|
429
|
+
scope: import_common6.Scope.REQUEST,
|
|
430
|
+
inject: [import_core2.ModuleRef, import_core2.REQUEST, ABORT_SIGNAL]
|
|
431
|
+
},
|
|
432
|
+
async (moduleRef, req, signal) => {
|
|
411
433
|
const ctxId = import_core2.ContextIdFactory.getByRequest(req);
|
|
412
434
|
const svc = await moduleRef.resolve(token, ctxId, { strict: false });
|
|
413
435
|
if (svc == null) {
|
|
@@ -419,7 +441,7 @@ function createAbortableProvider(token, opts) {
|
|
|
419
441
|
}
|
|
420
442
|
return (0, import_nfkit.abortable)(svc, signal, opts);
|
|
421
443
|
}
|
|
422
|
-
|
|
444
|
+
);
|
|
423
445
|
}
|
|
424
446
|
|
|
425
447
|
// src/abortable-module/abortable.module.ts
|
|
@@ -447,6 +469,500 @@ var AbortableModule = class {
|
|
|
447
469
|
AbortableModule = __decorateClass([
|
|
448
470
|
(0, import_common7.Module)({})
|
|
449
471
|
], AbortableModule);
|
|
472
|
+
|
|
473
|
+
// src/i18n-module/i18n.service.ts
|
|
474
|
+
var import_common9 = require("@nestjs/common");
|
|
475
|
+
|
|
476
|
+
// src/i18n-module/i18n-token.ts
|
|
477
|
+
var import_common8 = require("@nestjs/common");
|
|
478
|
+
var I18nResolverToken = Symbol("I18nResolverToken");
|
|
479
|
+
var { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new import_common8.ConfigurableModuleBuilder().build();
|
|
480
|
+
var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
|
|
481
|
+
|
|
482
|
+
// src/utility/parse-i18n.ts
|
|
483
|
+
var parseI18n = (text) => {
|
|
484
|
+
const pieces = [];
|
|
485
|
+
if (!text) return pieces;
|
|
486
|
+
let i = 0;
|
|
487
|
+
const n = text.length;
|
|
488
|
+
while (i < n) {
|
|
489
|
+
const start = text.indexOf("#{", i);
|
|
490
|
+
if (start === -1) {
|
|
491
|
+
if (i < n) pieces.push({ type: "raw", value: text.slice(i) });
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
let j = start + 2;
|
|
495
|
+
let depth = 1;
|
|
496
|
+
while (j < n && depth > 0) {
|
|
497
|
+
const ch = text.charCodeAt(j);
|
|
498
|
+
if (ch === 123) depth++;
|
|
499
|
+
else if (ch === 125) depth--;
|
|
500
|
+
j++;
|
|
501
|
+
}
|
|
502
|
+
if (depth !== 0) {
|
|
503
|
+
pieces.push({ type: "raw", value: text.slice(i) });
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
if (start > i) {
|
|
507
|
+
pieces.push({ type: "raw", value: text.slice(i, start) });
|
|
508
|
+
}
|
|
509
|
+
const rawInner = text.slice(start + 2, j - 1);
|
|
510
|
+
const key = rawInner.trim();
|
|
511
|
+
pieces.push({ type: "ph", rawInner, key });
|
|
512
|
+
i = j;
|
|
513
|
+
}
|
|
514
|
+
return pieces;
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// src/i18n-module/i18n.service.ts
|
|
518
|
+
var import_core3 = require("@nestjs/core");
|
|
519
|
+
var I18nService = class {
|
|
520
|
+
constructor(resolver, options, moduleRef) {
|
|
521
|
+
this.resolver = resolver;
|
|
522
|
+
this.options = options;
|
|
523
|
+
this.moduleRef = moduleRef;
|
|
524
|
+
this.locales = new Set(this.options.locales);
|
|
525
|
+
this.defaultLocale = this.options.defaultLocale ?? this.options.locales[0];
|
|
526
|
+
this.middlewares = [];
|
|
527
|
+
this.logger = new import_common9.ConsoleLogger("I18nService");
|
|
528
|
+
}
|
|
529
|
+
middleware(mw, prior = false) {
|
|
530
|
+
if (prior) {
|
|
531
|
+
this.middlewares.unshift(mw);
|
|
532
|
+
} else {
|
|
533
|
+
this.middlewares.push(mw);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
buildFallbackChain(locale) {
|
|
537
|
+
const best = this.getExactLocale(locale);
|
|
538
|
+
const parts = [];
|
|
539
|
+
const segs = best.split("-");
|
|
540
|
+
for (let i = segs.length; i > 1; i--)
|
|
541
|
+
parts.push(segs.slice(0, i).join("-"));
|
|
542
|
+
parts.push(segs[0]);
|
|
543
|
+
if (!parts.includes(this.defaultLocale)) parts.push(this.defaultLocale);
|
|
544
|
+
return Array.from(new Set(parts)).filter((p) => this.locales.has(p));
|
|
545
|
+
}
|
|
546
|
+
async applyMiddlewares(locale, text, ctx) {
|
|
547
|
+
const mws = this.middlewares;
|
|
548
|
+
const tryLocale = async (loc) => {
|
|
549
|
+
const dispatch = async (i) => {
|
|
550
|
+
if (i >= mws.length) return void 0;
|
|
551
|
+
const mw = mws[i];
|
|
552
|
+
let nextCalled = false;
|
|
553
|
+
const next = async () => {
|
|
554
|
+
nextCalled = true;
|
|
555
|
+
return dispatch(i + 1);
|
|
556
|
+
};
|
|
557
|
+
try {
|
|
558
|
+
const res = await mw(loc, text, next, ctx);
|
|
559
|
+
if (res == null && !nextCalled) {
|
|
560
|
+
return dispatch(i + 1);
|
|
561
|
+
}
|
|
562
|
+
return res;
|
|
563
|
+
} catch (e) {
|
|
564
|
+
if (e instanceof import_common9.HttpException) {
|
|
565
|
+
throw e;
|
|
566
|
+
}
|
|
567
|
+
this.logger.warn(`Middleware at index ${i} threw an error: ${e}`);
|
|
568
|
+
return dispatch(i + 1);
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
return dispatch(0);
|
|
572
|
+
};
|
|
573
|
+
for (const loc of this.buildFallbackChain(locale)) {
|
|
574
|
+
const result = await tryLocale(loc);
|
|
575
|
+
if (result != null) {
|
|
576
|
+
return result;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return void 0;
|
|
580
|
+
}
|
|
581
|
+
getExactLocale(locale) {
|
|
582
|
+
const input = (locale ?? "").trim();
|
|
583
|
+
if (!input) return this.defaultLocale;
|
|
584
|
+
if (this.locales.has(input)) return input;
|
|
585
|
+
const entries = Array.from(this.locales).map((l) => ({
|
|
586
|
+
orig: l,
|
|
587
|
+
lower: l.toLowerCase()
|
|
588
|
+
}));
|
|
589
|
+
const lower = input.toLowerCase();
|
|
590
|
+
const exact = entries.find((e) => e.lower === lower);
|
|
591
|
+
if (exact) return exact.orig;
|
|
592
|
+
const parts = lower.split("-");
|
|
593
|
+
while (parts.length > 1) {
|
|
594
|
+
parts.pop();
|
|
595
|
+
const candidate = parts.join("-");
|
|
596
|
+
const hit = entries.find((e) => e.lower === candidate);
|
|
597
|
+
if (hit) return hit.orig;
|
|
598
|
+
}
|
|
599
|
+
return this.defaultLocale;
|
|
600
|
+
}
|
|
601
|
+
async getExactLocaleFromRequest(ctx) {
|
|
602
|
+
const locale = await this.resolver(ctx, this.moduleRef);
|
|
603
|
+
return this.getExactLocale(locale);
|
|
604
|
+
}
|
|
605
|
+
async translateString(locale, text, ctx) {
|
|
606
|
+
if (!text) return text;
|
|
607
|
+
locale = this.getExactLocale(locale);
|
|
608
|
+
const pieces = parseI18n(text);
|
|
609
|
+
if (!pieces.some((p) => p.type === "ph")) {
|
|
610
|
+
return pieces.map((p) => p.type === "raw" ? p.value : `#{${p.rawInner}}`).join("");
|
|
611
|
+
}
|
|
612
|
+
const promises = [];
|
|
613
|
+
for (const p of pieces) {
|
|
614
|
+
if (p.type === "ph") {
|
|
615
|
+
promises.push(this.applyMiddlewares(locale, p.key, ctx));
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
const results = await Promise.all(promises);
|
|
619
|
+
let out = "";
|
|
620
|
+
let k = 0;
|
|
621
|
+
for (const p of pieces) {
|
|
622
|
+
if (p.type === "raw") {
|
|
623
|
+
out += p.value;
|
|
624
|
+
} else {
|
|
625
|
+
const r = results[k++];
|
|
626
|
+
out += r == null ? `#{${p.rawInner}}` : r;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return out;
|
|
630
|
+
}
|
|
631
|
+
async translate(locale, obj, ctx) {
|
|
632
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
633
|
+
const isBuiltInObject = (v) => {
|
|
634
|
+
if (v == null || typeof v !== "object") return false;
|
|
635
|
+
if (v instanceof Date || v instanceof RegExp || v instanceof Map || v instanceof Set || v instanceof WeakMap || v instanceof WeakSet || v instanceof ArrayBuffer || v instanceof DataView || ArrayBuffer.isView(v))
|
|
636
|
+
return true;
|
|
637
|
+
const tag = Object.prototype.toString.call(v);
|
|
638
|
+
switch (tag) {
|
|
639
|
+
case "[object URL]":
|
|
640
|
+
case "[object URLSearchParams]":
|
|
641
|
+
case "[object Error]":
|
|
642
|
+
case "[object Blob]":
|
|
643
|
+
case "[object File]":
|
|
644
|
+
case "[object FormData]":
|
|
645
|
+
return true;
|
|
646
|
+
default:
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
const translateObjectPreservingProto = async (value) => {
|
|
651
|
+
const proto = Object.getPrototypeOf(value);
|
|
652
|
+
const out = Object.create(proto);
|
|
653
|
+
const keys = Reflect.ownKeys(value);
|
|
654
|
+
await Promise.all(
|
|
655
|
+
keys.map(async (key) => {
|
|
656
|
+
const desc = Object.getOwnPropertyDescriptor(value, key);
|
|
657
|
+
if (!desc) return;
|
|
658
|
+
if ("value" in desc) {
|
|
659
|
+
const newVal = await visit(desc.value);
|
|
660
|
+
Object.defineProperty(out, key, { ...desc, value: newVal });
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
Object.defineProperty(out, key, desc);
|
|
664
|
+
let current = void 0;
|
|
665
|
+
if (typeof desc.get === "function") {
|
|
666
|
+
try {
|
|
667
|
+
current = desc.get.call(value);
|
|
668
|
+
} catch {
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (current === void 0) return;
|
|
672
|
+
try {
|
|
673
|
+
const newVal = await visit(current);
|
|
674
|
+
if (typeof desc.set === "function") {
|
|
675
|
+
try {
|
|
676
|
+
desc.set.call(out, newVal);
|
|
677
|
+
} catch {
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
} catch {
|
|
681
|
+
}
|
|
682
|
+
})
|
|
683
|
+
);
|
|
684
|
+
return out;
|
|
685
|
+
};
|
|
686
|
+
const isTranslatable = (v) => {
|
|
687
|
+
if (!v) return { ok: false };
|
|
688
|
+
const t = typeof v;
|
|
689
|
+
if (t === "number" || t === "bigint" || t === "symbol" || t === "function") {
|
|
690
|
+
return { ok: false };
|
|
691
|
+
}
|
|
692
|
+
if (t === "string") return { ok: true, kind: "string" };
|
|
693
|
+
if (t === "object") {
|
|
694
|
+
return { ok: true, kind: "object" };
|
|
695
|
+
}
|
|
696
|
+
return { ok: false };
|
|
697
|
+
};
|
|
698
|
+
const visit = async (value) => {
|
|
699
|
+
const check = isTranslatable(value);
|
|
700
|
+
if (!check.ok) {
|
|
701
|
+
return value;
|
|
702
|
+
}
|
|
703
|
+
if (check.kind === "string") {
|
|
704
|
+
return this.translateString(locale, value, ctx);
|
|
705
|
+
}
|
|
706
|
+
if (value instanceof Promise) {
|
|
707
|
+
return value.then((resolved) => visit(resolved));
|
|
708
|
+
}
|
|
709
|
+
if (typeof value === "object") {
|
|
710
|
+
if (!Array.isArray(value) && isBuiltInObject(value)) return value;
|
|
711
|
+
if (visited.has(value)) return value;
|
|
712
|
+
visited.add(value);
|
|
713
|
+
if (Array.isArray(value)) {
|
|
714
|
+
const out = await Promise.all(value.map((v) => visit(v)));
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
return translateObjectPreservingProto(value);
|
|
718
|
+
}
|
|
719
|
+
return value;
|
|
720
|
+
};
|
|
721
|
+
return visit(obj);
|
|
722
|
+
}
|
|
723
|
+
async translateRequest(ctx, obj) {
|
|
724
|
+
const locale = await this.resolver(ctx, this.moduleRef);
|
|
725
|
+
return this.translate(locale, obj, ctx);
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
I18nService = __decorateClass([
|
|
729
|
+
(0, import_common9.Injectable)(),
|
|
730
|
+
__decorateParam(0, (0, import_common9.Inject)(I18nResolverToken)),
|
|
731
|
+
__decorateParam(1, (0, import_common9.Inject)(I18nModuleOptionsToken)),
|
|
732
|
+
__decorateParam(2, (0, import_common9.Inject)(import_core3.ModuleRef))
|
|
733
|
+
], I18nService);
|
|
734
|
+
|
|
735
|
+
// src/i18n-module/i18n.module.ts
|
|
736
|
+
var import_common11 = require("@nestjs/common");
|
|
737
|
+
|
|
738
|
+
// src/i18n-module/i18n-resolver.ts
|
|
739
|
+
var coerceToString = (v) => {
|
|
740
|
+
if (v == null) return void 0;
|
|
741
|
+
if (v === false) return void 0;
|
|
742
|
+
if (Array.isArray(v)) return v.length ? coerceToString(v[0]) : void 0;
|
|
743
|
+
if (typeof v === "string") return v;
|
|
744
|
+
if (typeof v === "number") return String(v);
|
|
745
|
+
return void 0;
|
|
746
|
+
};
|
|
747
|
+
var getHeader = (req, name) => {
|
|
748
|
+
const viaMethod = typeof req.getHeader === "function" && req.getHeader(name) || typeof req.header === "function" && req.header(name) || typeof req.get === "function" && req.get(name);
|
|
749
|
+
if (viaMethod) {
|
|
750
|
+
return coerceToString(viaMethod);
|
|
751
|
+
}
|
|
752
|
+
const n = name.toLowerCase();
|
|
753
|
+
const headers = req.headers ?? {};
|
|
754
|
+
if (n in headers) return coerceToString(headers[n]);
|
|
755
|
+
const hit = Object.entries(headers).find(([k]) => k.toLowerCase() === n)?.[1];
|
|
756
|
+
return coerceToString(hit);
|
|
757
|
+
};
|
|
758
|
+
var pickPrimaryFromAcceptLanguage = (v) => {
|
|
759
|
+
if (!v) return void 0;
|
|
760
|
+
const first = v.split(",")[0]?.trim();
|
|
761
|
+
return first?.split(";")[0]?.trim() || first;
|
|
762
|
+
};
|
|
763
|
+
function getQueryValue(req, key) {
|
|
764
|
+
const q = req.query;
|
|
765
|
+
if (q && typeof q === "object" && !("raw" in q)) {
|
|
766
|
+
const v = q[key];
|
|
767
|
+
if (v != null) return coerceToString(v);
|
|
768
|
+
}
|
|
769
|
+
const rawUrl = req.originalUrl ?? req.url;
|
|
770
|
+
if (typeof rawUrl === "string" && rawUrl.includes("?")) {
|
|
771
|
+
try {
|
|
772
|
+
const search = rawUrl.startsWith("http") ? new URL(rawUrl).search : new URL(rawUrl, "http://localhost").search;
|
|
773
|
+
if (search) {
|
|
774
|
+
const params = new URLSearchParams(search);
|
|
775
|
+
const val = params.get(key);
|
|
776
|
+
if (val != null) return val;
|
|
777
|
+
}
|
|
778
|
+
} catch {
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return void 0;
|
|
782
|
+
}
|
|
783
|
+
var createDynamicResolverFromStatic = (_options) => {
|
|
784
|
+
if (typeof _options === "function") {
|
|
785
|
+
return _options;
|
|
786
|
+
}
|
|
787
|
+
const options = _options;
|
|
788
|
+
const field = options.paramType;
|
|
789
|
+
let name = options.paramName;
|
|
790
|
+
if (field === "header") name = name.toLowerCase();
|
|
791
|
+
return (ctx) => {
|
|
792
|
+
const req = ctx.switchToHttp().getRequest();
|
|
793
|
+
if (field === "header") {
|
|
794
|
+
let raw = getHeader(req, name);
|
|
795
|
+
if (name === "accept-language") raw = pickPrimaryFromAcceptLanguage(raw);
|
|
796
|
+
return raw;
|
|
797
|
+
}
|
|
798
|
+
if (field === "query") {
|
|
799
|
+
return getQueryValue(req, name);
|
|
800
|
+
}
|
|
801
|
+
throw new Error(`Unsupported paramType: ${field}`);
|
|
802
|
+
};
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
// src/i18n-module/locale.pipe.ts
|
|
806
|
+
var import_common10 = require("@nestjs/common");
|
|
807
|
+
var LocalePipe = class {
|
|
808
|
+
constructor(i18nService) {
|
|
809
|
+
this.i18nService = i18nService;
|
|
810
|
+
}
|
|
811
|
+
async transform(ctx, metadata) {
|
|
812
|
+
const resolver = ctx.resolver;
|
|
813
|
+
if (resolver) {
|
|
814
|
+
const _resolver = createDynamicResolverFromStatic(resolver);
|
|
815
|
+
const locale = await _resolver(ctx.ctx, void 0);
|
|
816
|
+
return this.i18nService.getExactLocale(locale);
|
|
817
|
+
} else {
|
|
818
|
+
return this.i18nService.getExactLocaleFromRequest(ctx.ctx);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
LocalePipe = __decorateClass([
|
|
823
|
+
(0, import_common10.Injectable)(),
|
|
824
|
+
__decorateParam(0, (0, import_common10.Inject)(I18nService))
|
|
825
|
+
], LocalePipe);
|
|
826
|
+
var _dec = (0, import_common10.createParamDecorator)((resolver, ctx) => {
|
|
827
|
+
return { ctx, resolver };
|
|
828
|
+
});
|
|
829
|
+
var PutLocale = (resolver) => _dec(resolver, LocalePipe);
|
|
830
|
+
|
|
831
|
+
// src/i18n-module/i18n.module.ts
|
|
832
|
+
var I18nModule = class extends ConfigurableModuleClass {
|
|
833
|
+
};
|
|
834
|
+
I18nModule = __decorateClass([
|
|
835
|
+
(0, import_common11.Global)(),
|
|
836
|
+
(0, import_common11.Module)({
|
|
837
|
+
providers: [
|
|
838
|
+
createProvider(
|
|
839
|
+
{
|
|
840
|
+
provide: I18nResolverToken,
|
|
841
|
+
inject: [I18nModuleOptionsToken]
|
|
842
|
+
},
|
|
843
|
+
(o) => createDynamicResolverFromStatic(o.resolver)
|
|
844
|
+
),
|
|
845
|
+
I18nService,
|
|
846
|
+
LocalePipe
|
|
847
|
+
],
|
|
848
|
+
exports: [I18nService, LocalePipe]
|
|
849
|
+
})
|
|
850
|
+
], I18nModule);
|
|
851
|
+
|
|
852
|
+
// src/i18n-module/i18n-decorator.ts
|
|
853
|
+
var import_common13 = require("@nestjs/common");
|
|
854
|
+
|
|
855
|
+
// src/i18n-module/i18n.interceptor.ts
|
|
856
|
+
var import_common12 = require("@nestjs/common");
|
|
857
|
+
var import_rxjs2 = require("rxjs");
|
|
858
|
+
var import_operators = require("rxjs/operators");
|
|
859
|
+
var I18nInterceptor = class {
|
|
860
|
+
constructor(i18nService) {
|
|
861
|
+
this.i18nService = i18nService;
|
|
862
|
+
}
|
|
863
|
+
intercept(context, next) {
|
|
864
|
+
return next.handle().pipe(
|
|
865
|
+
// 成功路径:把响应体交给 i18n 做异步翻译
|
|
866
|
+
(0, import_operators.mergeMap)((data) => this.i18nService.translateRequest(context, data)),
|
|
867
|
+
// 错误路径:若是 HttpException,把其 response 翻译后再抛
|
|
868
|
+
(0, import_operators.catchError)((err) => {
|
|
869
|
+
if (err instanceof import_common12.HttpException) {
|
|
870
|
+
const status = err.getStatus();
|
|
871
|
+
const resp = err.getResponse();
|
|
872
|
+
return (0, import_rxjs2.from)(this.i18nService.translateRequest(context, resp)).pipe(
|
|
873
|
+
(0, import_operators.mergeMap)(
|
|
874
|
+
(translated) => (0, import_rxjs2.throwError)(
|
|
875
|
+
() => new import_common12.HttpException(translated, status, { cause: err })
|
|
876
|
+
)
|
|
877
|
+
)
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
return (0, import_rxjs2.throwError)(() => err);
|
|
881
|
+
})
|
|
882
|
+
);
|
|
883
|
+
}
|
|
884
|
+
};
|
|
885
|
+
I18nInterceptor = __decorateClass([
|
|
886
|
+
(0, import_common12.Injectable)(),
|
|
887
|
+
__decorateParam(0, (0, import_common12.Inject)(I18nService))
|
|
888
|
+
], I18nInterceptor);
|
|
889
|
+
|
|
890
|
+
// src/i18n-module/i18n-decorator.ts
|
|
891
|
+
var import_swagger5 = require("@nestjs/swagger");
|
|
892
|
+
var createI18nDecorator = (options) => {
|
|
893
|
+
const paramType = options.resolver?.paramType;
|
|
894
|
+
const apiOptions = {
|
|
895
|
+
name: options.resolver.paramName,
|
|
896
|
+
description: "Locale for internationalization",
|
|
897
|
+
required: false,
|
|
898
|
+
default: options.defaultLocale ?? options.locales[0],
|
|
899
|
+
enum: options.locales
|
|
900
|
+
};
|
|
901
|
+
const dec = paramType === "header" ? () => (0, import_swagger5.ApiHeader)(apiOptions) : paramType === "query" ? () => (0, import_swagger5.ApiQuery)({ ...apiOptions, type: "string" }) : () => () => {
|
|
902
|
+
};
|
|
903
|
+
return () => MergeClassOrMethodDecorators([
|
|
904
|
+
(0, import_common13.UseInterceptors)(I18nInterceptor),
|
|
905
|
+
...paramType ? [dec()] : []
|
|
906
|
+
]);
|
|
907
|
+
};
|
|
908
|
+
|
|
909
|
+
// src/i18n-module/i18n-factory.ts
|
|
910
|
+
var createI18n = (options) => {
|
|
911
|
+
if (!options.resolver) {
|
|
912
|
+
options.resolver = {
|
|
913
|
+
paramType: "header",
|
|
914
|
+
paramName: "x-client-language"
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
return {
|
|
918
|
+
UseI18n: createI18nDecorator(options),
|
|
919
|
+
I18nModule: I18nModule.register(options)
|
|
920
|
+
};
|
|
921
|
+
};
|
|
922
|
+
|
|
923
|
+
// src/i18n-module/middelewares/lookup.ts
|
|
924
|
+
var I18nLookupMiddleware = (dict, options) => {
|
|
925
|
+
const matchType = options?.matchType ?? "exact";
|
|
926
|
+
const dictFactory = typeof dict === "function" ? dict : () => dict;
|
|
927
|
+
const pickBestByHierarchy = (input, locales) => {
|
|
928
|
+
if (!input) return void 0;
|
|
929
|
+
const entries = locales.map((l) => ({ orig: l, lower: l.toLowerCase() }));
|
|
930
|
+
const lower = input.toLowerCase();
|
|
931
|
+
const exact = entries.find((e) => e.lower === lower);
|
|
932
|
+
if (exact) return exact.orig;
|
|
933
|
+
const parts = lower.split("-");
|
|
934
|
+
while (parts.length > 1) {
|
|
935
|
+
parts.pop();
|
|
936
|
+
const candidate = parts.join("-");
|
|
937
|
+
const hit = entries.find((e) => e.lower === candidate);
|
|
938
|
+
if (hit) return hit.orig;
|
|
939
|
+
}
|
|
940
|
+
return void 0;
|
|
941
|
+
};
|
|
942
|
+
return async (locale, key, next, ctx) => {
|
|
943
|
+
const dictResolved = await dictFactory(locale, key, ctx);
|
|
944
|
+
let dictionary = dictResolved[locale];
|
|
945
|
+
if (!dictionary) {
|
|
946
|
+
if (matchType === "hierarchy") {
|
|
947
|
+
const best = pickBestByHierarchy(locale, Object.keys(dictResolved));
|
|
948
|
+
if (best) dictionary = dictResolved[best];
|
|
949
|
+
} else if (matchType === "startsWith") {
|
|
950
|
+
const keys = Object.keys(dictResolved).filter(
|
|
951
|
+
(k) => locale.startsWith(k)
|
|
952
|
+
);
|
|
953
|
+
if (keys.length) {
|
|
954
|
+
const best = keys.reduce((a, b) => b.length > a.length ? b : a);
|
|
955
|
+
dictionary = dictResolved[best];
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
if (dictionary && Object.prototype.hasOwnProperty.call(dictionary, key)) {
|
|
960
|
+
const val = dictionary[key];
|
|
961
|
+
if (val != null) return val;
|
|
962
|
+
}
|
|
963
|
+
return next();
|
|
964
|
+
};
|
|
965
|
+
};
|
|
450
966
|
// Annotate the CommonJS export names for ESM import in node:
|
|
451
967
|
0 && (module.exports = {
|
|
452
968
|
ABORT_SIGNAL,
|
|
@@ -464,21 +980,30 @@ AbortableModule = __decorateClass([
|
|
|
464
980
|
DataQuery,
|
|
465
981
|
GenericPaginatedReturnMessageDto,
|
|
466
982
|
GenericReturnMessageDto,
|
|
983
|
+
I18nInterceptor,
|
|
984
|
+
I18nLookupMiddleware,
|
|
985
|
+
I18nModule,
|
|
986
|
+
I18nService,
|
|
467
987
|
InjectAbortSignal,
|
|
468
988
|
InjectAbortable,
|
|
469
989
|
InsertField,
|
|
990
|
+
LocalePipe,
|
|
470
991
|
MergeClassDecorators,
|
|
471
992
|
MergeClassOrMethodDecorators,
|
|
472
993
|
MergeMethodDecorators,
|
|
473
994
|
MergeParameterDecorators,
|
|
474
995
|
MergePropertyDecorators,
|
|
475
996
|
PaginatedReturnMessageDto,
|
|
997
|
+
PutLocale,
|
|
476
998
|
RequireToken,
|
|
477
999
|
ReturnMessageDto,
|
|
478
1000
|
StringReturnMessageDto,
|
|
479
1001
|
TokenGuard,
|
|
480
1002
|
abortableToken,
|
|
481
1003
|
createAbortableProvider,
|
|
1004
|
+
createI18n,
|
|
1005
|
+
createI18nDecorator,
|
|
1006
|
+
createProvider,
|
|
482
1007
|
fromAbortable,
|
|
483
1008
|
getClassFromClassOrArray,
|
|
484
1009
|
takeUntilAbort
|