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.mjs
CHANGED
|
@@ -240,7 +240,7 @@ var RequireToken = () => MergeClassOrMethodDecorators([
|
|
|
240
240
|
import { Observable, takeUntil } from "rxjs";
|
|
241
241
|
import { createParamDecorator } from "@nestjs/common";
|
|
242
242
|
|
|
243
|
-
// src/abort-http-signal.ts
|
|
243
|
+
// src/utility/abort-http-signal.ts
|
|
244
244
|
function toRawReq(req) {
|
|
245
245
|
if (req?.raw?.socket) return req.raw;
|
|
246
246
|
return req;
|
|
@@ -318,15 +318,27 @@ import { abortable } from "nfkit";
|
|
|
318
318
|
// src/abortable-module/abort-signal.provider.ts
|
|
319
319
|
import { Inject as Inject2, Scope } from "@nestjs/common";
|
|
320
320
|
import { REQUEST } from "@nestjs/core";
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
321
|
+
|
|
322
|
+
// src/create-provider.ts
|
|
323
|
+
var createProvider = (options, factory) => {
|
|
324
|
+
return {
|
|
325
|
+
useFactory: factory,
|
|
326
|
+
...options
|
|
327
|
+
};
|
|
329
328
|
};
|
|
329
|
+
|
|
330
|
+
// src/abortable-module/abort-signal.provider.ts
|
|
331
|
+
var ABORT_SIGNAL = Symbol(
|
|
332
|
+
"ABORT_SIGNAL"
|
|
333
|
+
);
|
|
334
|
+
var AbortSignalProvider = createProvider(
|
|
335
|
+
{
|
|
336
|
+
provide: ABORT_SIGNAL,
|
|
337
|
+
scope: Scope.REQUEST,
|
|
338
|
+
inject: [REQUEST]
|
|
339
|
+
},
|
|
340
|
+
createAbortSignalFromHttp
|
|
341
|
+
);
|
|
330
342
|
var InjectAbortSignal = () => Inject2(ABORT_SIGNAL);
|
|
331
343
|
|
|
332
344
|
// src/abortable-module/abortable.token.ts
|
|
@@ -356,12 +368,13 @@ function InjectAbortable(token) {
|
|
|
356
368
|
}
|
|
357
369
|
function createAbortableProvider(token, opts) {
|
|
358
370
|
const provide = abortableToken(token);
|
|
359
|
-
return
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
371
|
+
return createProvider(
|
|
372
|
+
{
|
|
373
|
+
provide,
|
|
374
|
+
scope: Scope2.REQUEST,
|
|
375
|
+
inject: [ModuleRef, REQUEST2, ABORT_SIGNAL]
|
|
376
|
+
},
|
|
377
|
+
async (moduleRef, req, signal) => {
|
|
365
378
|
const ctxId = ContextIdFactory.getByRequest(req);
|
|
366
379
|
const svc = await moduleRef.resolve(token, ctxId, { strict: false });
|
|
367
380
|
if (svc == null) {
|
|
@@ -373,7 +386,7 @@ function createAbortableProvider(token, opts) {
|
|
|
373
386
|
}
|
|
374
387
|
return abortable(svc, signal, opts);
|
|
375
388
|
}
|
|
376
|
-
|
|
389
|
+
);
|
|
377
390
|
}
|
|
378
391
|
|
|
379
392
|
// src/abortable-module/abortable.module.ts
|
|
@@ -401,6 +414,516 @@ var AbortableModule = class {
|
|
|
401
414
|
AbortableModule = __decorateClass([
|
|
402
415
|
Module({})
|
|
403
416
|
], AbortableModule);
|
|
417
|
+
|
|
418
|
+
// src/i18n-module/i18n.service.ts
|
|
419
|
+
import {
|
|
420
|
+
ConsoleLogger,
|
|
421
|
+
HttpException as HttpException2,
|
|
422
|
+
Inject as Inject4,
|
|
423
|
+
Injectable as Injectable2
|
|
424
|
+
} from "@nestjs/common";
|
|
425
|
+
|
|
426
|
+
// src/i18n-module/i18n-token.ts
|
|
427
|
+
import { ConfigurableModuleBuilder } from "@nestjs/common";
|
|
428
|
+
var I18nResolverToken = Symbol("I18nResolverToken");
|
|
429
|
+
var { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder().build();
|
|
430
|
+
var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
|
|
431
|
+
|
|
432
|
+
// src/utility/parse-i18n.ts
|
|
433
|
+
var parseI18n = (text) => {
|
|
434
|
+
const pieces = [];
|
|
435
|
+
if (!text) return pieces;
|
|
436
|
+
let i = 0;
|
|
437
|
+
const n = text.length;
|
|
438
|
+
while (i < n) {
|
|
439
|
+
const start = text.indexOf("#{", i);
|
|
440
|
+
if (start === -1) {
|
|
441
|
+
if (i < n) pieces.push({ type: "raw", value: text.slice(i) });
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
let j = start + 2;
|
|
445
|
+
let depth = 1;
|
|
446
|
+
while (j < n && depth > 0) {
|
|
447
|
+
const ch = text.charCodeAt(j);
|
|
448
|
+
if (ch === 123) depth++;
|
|
449
|
+
else if (ch === 125) depth--;
|
|
450
|
+
j++;
|
|
451
|
+
}
|
|
452
|
+
if (depth !== 0) {
|
|
453
|
+
pieces.push({ type: "raw", value: text.slice(i) });
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
if (start > i) {
|
|
457
|
+
pieces.push({ type: "raw", value: text.slice(i, start) });
|
|
458
|
+
}
|
|
459
|
+
const rawInner = text.slice(start + 2, j - 1);
|
|
460
|
+
const key = rawInner.trim();
|
|
461
|
+
pieces.push({ type: "ph", rawInner, key });
|
|
462
|
+
i = j;
|
|
463
|
+
}
|
|
464
|
+
return pieces;
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// src/i18n-module/i18n.service.ts
|
|
468
|
+
import { ModuleRef as ModuleRef2 } from "@nestjs/core";
|
|
469
|
+
var I18nService = class {
|
|
470
|
+
constructor(resolver, options, moduleRef) {
|
|
471
|
+
this.resolver = resolver;
|
|
472
|
+
this.options = options;
|
|
473
|
+
this.moduleRef = moduleRef;
|
|
474
|
+
this.locales = new Set(this.options.locales);
|
|
475
|
+
this.defaultLocale = this.options.defaultLocale ?? this.options.locales[0];
|
|
476
|
+
this.middlewares = [];
|
|
477
|
+
this.logger = new ConsoleLogger("I18nService");
|
|
478
|
+
}
|
|
479
|
+
middleware(mw, prior = false) {
|
|
480
|
+
if (prior) {
|
|
481
|
+
this.middlewares.unshift(mw);
|
|
482
|
+
} else {
|
|
483
|
+
this.middlewares.push(mw);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
buildFallbackChain(locale) {
|
|
487
|
+
const best = this.getExactLocale(locale);
|
|
488
|
+
const parts = [];
|
|
489
|
+
const segs = best.split("-");
|
|
490
|
+
for (let i = segs.length; i > 1; i--)
|
|
491
|
+
parts.push(segs.slice(0, i).join("-"));
|
|
492
|
+
parts.push(segs[0]);
|
|
493
|
+
if (!parts.includes(this.defaultLocale)) parts.push(this.defaultLocale);
|
|
494
|
+
return Array.from(new Set(parts)).filter((p) => this.locales.has(p));
|
|
495
|
+
}
|
|
496
|
+
async applyMiddlewares(locale, text, ctx) {
|
|
497
|
+
const mws = this.middlewares;
|
|
498
|
+
const tryLocale = async (loc) => {
|
|
499
|
+
const dispatch = async (i) => {
|
|
500
|
+
if (i >= mws.length) return void 0;
|
|
501
|
+
const mw = mws[i];
|
|
502
|
+
let nextCalled = false;
|
|
503
|
+
const next = async () => {
|
|
504
|
+
nextCalled = true;
|
|
505
|
+
return dispatch(i + 1);
|
|
506
|
+
};
|
|
507
|
+
try {
|
|
508
|
+
const res = await mw(loc, text, next, ctx);
|
|
509
|
+
if (res == null && !nextCalled) {
|
|
510
|
+
return dispatch(i + 1);
|
|
511
|
+
}
|
|
512
|
+
return res;
|
|
513
|
+
} catch (e) {
|
|
514
|
+
if (e instanceof HttpException2) {
|
|
515
|
+
throw e;
|
|
516
|
+
}
|
|
517
|
+
this.logger.warn(`Middleware at index ${i} threw an error: ${e}`);
|
|
518
|
+
return dispatch(i + 1);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
return dispatch(0);
|
|
522
|
+
};
|
|
523
|
+
for (const loc of this.buildFallbackChain(locale)) {
|
|
524
|
+
const result = await tryLocale(loc);
|
|
525
|
+
if (result != null) {
|
|
526
|
+
return result;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return void 0;
|
|
530
|
+
}
|
|
531
|
+
getExactLocale(locale) {
|
|
532
|
+
const input = (locale ?? "").trim();
|
|
533
|
+
if (!input) return this.defaultLocale;
|
|
534
|
+
if (this.locales.has(input)) return input;
|
|
535
|
+
const entries = Array.from(this.locales).map((l) => ({
|
|
536
|
+
orig: l,
|
|
537
|
+
lower: l.toLowerCase()
|
|
538
|
+
}));
|
|
539
|
+
const lower = input.toLowerCase();
|
|
540
|
+
const exact = entries.find((e) => e.lower === lower);
|
|
541
|
+
if (exact) return exact.orig;
|
|
542
|
+
const parts = lower.split("-");
|
|
543
|
+
while (parts.length > 1) {
|
|
544
|
+
parts.pop();
|
|
545
|
+
const candidate = parts.join("-");
|
|
546
|
+
const hit = entries.find((e) => e.lower === candidate);
|
|
547
|
+
if (hit) return hit.orig;
|
|
548
|
+
}
|
|
549
|
+
return this.defaultLocale;
|
|
550
|
+
}
|
|
551
|
+
async getExactLocaleFromRequest(ctx) {
|
|
552
|
+
const locale = await this.resolver(ctx, this.moduleRef);
|
|
553
|
+
return this.getExactLocale(locale);
|
|
554
|
+
}
|
|
555
|
+
async translateString(locale, text, ctx) {
|
|
556
|
+
if (!text) return text;
|
|
557
|
+
locale = this.getExactLocale(locale);
|
|
558
|
+
const pieces = parseI18n(text);
|
|
559
|
+
if (!pieces.some((p) => p.type === "ph")) {
|
|
560
|
+
return pieces.map((p) => p.type === "raw" ? p.value : `#{${p.rawInner}}`).join("");
|
|
561
|
+
}
|
|
562
|
+
const promises = [];
|
|
563
|
+
for (const p of pieces) {
|
|
564
|
+
if (p.type === "ph") {
|
|
565
|
+
promises.push(this.applyMiddlewares(locale, p.key, ctx));
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const results = await Promise.all(promises);
|
|
569
|
+
let out = "";
|
|
570
|
+
let k = 0;
|
|
571
|
+
for (const p of pieces) {
|
|
572
|
+
if (p.type === "raw") {
|
|
573
|
+
out += p.value;
|
|
574
|
+
} else {
|
|
575
|
+
const r = results[k++];
|
|
576
|
+
out += r == null ? `#{${p.rawInner}}` : r;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return out;
|
|
580
|
+
}
|
|
581
|
+
async translate(locale, obj, ctx) {
|
|
582
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
583
|
+
const isBuiltInObject = (v) => {
|
|
584
|
+
if (v == null || typeof v !== "object") return false;
|
|
585
|
+
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))
|
|
586
|
+
return true;
|
|
587
|
+
const tag = Object.prototype.toString.call(v);
|
|
588
|
+
switch (tag) {
|
|
589
|
+
case "[object URL]":
|
|
590
|
+
case "[object URLSearchParams]":
|
|
591
|
+
case "[object Error]":
|
|
592
|
+
case "[object Blob]":
|
|
593
|
+
case "[object File]":
|
|
594
|
+
case "[object FormData]":
|
|
595
|
+
return true;
|
|
596
|
+
default:
|
|
597
|
+
return false;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
const translateObjectPreservingProto = async (value) => {
|
|
601
|
+
const proto = Object.getPrototypeOf(value);
|
|
602
|
+
const out = Object.create(proto);
|
|
603
|
+
const keys = Reflect.ownKeys(value);
|
|
604
|
+
await Promise.all(
|
|
605
|
+
keys.map(async (key) => {
|
|
606
|
+
const desc = Object.getOwnPropertyDescriptor(value, key);
|
|
607
|
+
if (!desc) return;
|
|
608
|
+
if ("value" in desc) {
|
|
609
|
+
const newVal = await visit(desc.value);
|
|
610
|
+
Object.defineProperty(out, key, { ...desc, value: newVal });
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
Object.defineProperty(out, key, desc);
|
|
614
|
+
let current = void 0;
|
|
615
|
+
if (typeof desc.get === "function") {
|
|
616
|
+
try {
|
|
617
|
+
current = desc.get.call(value);
|
|
618
|
+
} catch {
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (current === void 0) return;
|
|
622
|
+
try {
|
|
623
|
+
const newVal = await visit(current);
|
|
624
|
+
if (typeof desc.set === "function") {
|
|
625
|
+
try {
|
|
626
|
+
desc.set.call(out, newVal);
|
|
627
|
+
} catch {
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
} catch {
|
|
631
|
+
}
|
|
632
|
+
})
|
|
633
|
+
);
|
|
634
|
+
return out;
|
|
635
|
+
};
|
|
636
|
+
const isTranslatable = (v) => {
|
|
637
|
+
if (!v) return { ok: false };
|
|
638
|
+
const t = typeof v;
|
|
639
|
+
if (t === "number" || t === "bigint" || t === "symbol" || t === "function") {
|
|
640
|
+
return { ok: false };
|
|
641
|
+
}
|
|
642
|
+
if (t === "string") return { ok: true, kind: "string" };
|
|
643
|
+
if (t === "object") {
|
|
644
|
+
return { ok: true, kind: "object" };
|
|
645
|
+
}
|
|
646
|
+
return { ok: false };
|
|
647
|
+
};
|
|
648
|
+
const visit = async (value) => {
|
|
649
|
+
const check = isTranslatable(value);
|
|
650
|
+
if (!check.ok) {
|
|
651
|
+
return value;
|
|
652
|
+
}
|
|
653
|
+
if (check.kind === "string") {
|
|
654
|
+
return this.translateString(locale, value, ctx);
|
|
655
|
+
}
|
|
656
|
+
if (value instanceof Promise) {
|
|
657
|
+
return value.then((resolved) => visit(resolved));
|
|
658
|
+
}
|
|
659
|
+
if (typeof value === "object") {
|
|
660
|
+
if (!Array.isArray(value) && isBuiltInObject(value)) return value;
|
|
661
|
+
if (visited.has(value)) return value;
|
|
662
|
+
visited.add(value);
|
|
663
|
+
if (Array.isArray(value)) {
|
|
664
|
+
const out = await Promise.all(value.map((v) => visit(v)));
|
|
665
|
+
return out;
|
|
666
|
+
}
|
|
667
|
+
return translateObjectPreservingProto(value);
|
|
668
|
+
}
|
|
669
|
+
return value;
|
|
670
|
+
};
|
|
671
|
+
return visit(obj);
|
|
672
|
+
}
|
|
673
|
+
async translateRequest(ctx, obj) {
|
|
674
|
+
const locale = await this.resolver(ctx, this.moduleRef);
|
|
675
|
+
return this.translate(locale, obj, ctx);
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
I18nService = __decorateClass([
|
|
679
|
+
Injectable2(),
|
|
680
|
+
__decorateParam(0, Inject4(I18nResolverToken)),
|
|
681
|
+
__decorateParam(1, Inject4(I18nModuleOptionsToken)),
|
|
682
|
+
__decorateParam(2, Inject4(ModuleRef2))
|
|
683
|
+
], I18nService);
|
|
684
|
+
|
|
685
|
+
// src/i18n-module/i18n.module.ts
|
|
686
|
+
import { Global, Module as Module2 } from "@nestjs/common";
|
|
687
|
+
|
|
688
|
+
// src/i18n-module/i18n-resolver.ts
|
|
689
|
+
var coerceToString = (v) => {
|
|
690
|
+
if (v == null) return void 0;
|
|
691
|
+
if (v === false) return void 0;
|
|
692
|
+
if (Array.isArray(v)) return v.length ? coerceToString(v[0]) : void 0;
|
|
693
|
+
if (typeof v === "string") return v;
|
|
694
|
+
if (typeof v === "number") return String(v);
|
|
695
|
+
return void 0;
|
|
696
|
+
};
|
|
697
|
+
var getHeader = (req, name) => {
|
|
698
|
+
const viaMethod = typeof req.getHeader === "function" && req.getHeader(name) || typeof req.header === "function" && req.header(name) || typeof req.get === "function" && req.get(name);
|
|
699
|
+
if (viaMethod) {
|
|
700
|
+
return coerceToString(viaMethod);
|
|
701
|
+
}
|
|
702
|
+
const n = name.toLowerCase();
|
|
703
|
+
const headers = req.headers ?? {};
|
|
704
|
+
if (n in headers) return coerceToString(headers[n]);
|
|
705
|
+
const hit = Object.entries(headers).find(([k]) => k.toLowerCase() === n)?.[1];
|
|
706
|
+
return coerceToString(hit);
|
|
707
|
+
};
|
|
708
|
+
var pickPrimaryFromAcceptLanguage = (v) => {
|
|
709
|
+
if (!v) return void 0;
|
|
710
|
+
const first = v.split(",")[0]?.trim();
|
|
711
|
+
return first?.split(";")[0]?.trim() || first;
|
|
712
|
+
};
|
|
713
|
+
function getQueryValue(req, key) {
|
|
714
|
+
const q = req.query;
|
|
715
|
+
if (q && typeof q === "object" && !("raw" in q)) {
|
|
716
|
+
const v = q[key];
|
|
717
|
+
if (v != null) return coerceToString(v);
|
|
718
|
+
}
|
|
719
|
+
const rawUrl = req.originalUrl ?? req.url;
|
|
720
|
+
if (typeof rawUrl === "string" && rawUrl.includes("?")) {
|
|
721
|
+
try {
|
|
722
|
+
const search = rawUrl.startsWith("http") ? new URL(rawUrl).search : new URL(rawUrl, "http://localhost").search;
|
|
723
|
+
if (search) {
|
|
724
|
+
const params = new URLSearchParams(search);
|
|
725
|
+
const val = params.get(key);
|
|
726
|
+
if (val != null) return val;
|
|
727
|
+
}
|
|
728
|
+
} catch {
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
return void 0;
|
|
732
|
+
}
|
|
733
|
+
var createDynamicResolverFromStatic = (_options) => {
|
|
734
|
+
if (typeof _options === "function") {
|
|
735
|
+
return _options;
|
|
736
|
+
}
|
|
737
|
+
const options = _options;
|
|
738
|
+
const field = options.paramType;
|
|
739
|
+
let name = options.paramName;
|
|
740
|
+
if (field === "header") name = name.toLowerCase();
|
|
741
|
+
return (ctx) => {
|
|
742
|
+
const req = ctx.switchToHttp().getRequest();
|
|
743
|
+
if (field === "header") {
|
|
744
|
+
let raw = getHeader(req, name);
|
|
745
|
+
if (name === "accept-language") raw = pickPrimaryFromAcceptLanguage(raw);
|
|
746
|
+
return raw;
|
|
747
|
+
}
|
|
748
|
+
if (field === "query") {
|
|
749
|
+
return getQueryValue(req, name);
|
|
750
|
+
}
|
|
751
|
+
throw new Error(`Unsupported paramType: ${field}`);
|
|
752
|
+
};
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
// src/i18n-module/locale.pipe.ts
|
|
756
|
+
import {
|
|
757
|
+
createParamDecorator as createParamDecorator2,
|
|
758
|
+
Inject as Inject5,
|
|
759
|
+
Injectable as Injectable3
|
|
760
|
+
} from "@nestjs/common";
|
|
761
|
+
var LocalePipe = class {
|
|
762
|
+
constructor(i18nService) {
|
|
763
|
+
this.i18nService = i18nService;
|
|
764
|
+
}
|
|
765
|
+
async transform(ctx, metadata) {
|
|
766
|
+
const resolver = ctx.resolver;
|
|
767
|
+
if (resolver) {
|
|
768
|
+
const _resolver = createDynamicResolverFromStatic(resolver);
|
|
769
|
+
const locale = await _resolver(ctx.ctx, void 0);
|
|
770
|
+
return this.i18nService.getExactLocale(locale);
|
|
771
|
+
} else {
|
|
772
|
+
return this.i18nService.getExactLocaleFromRequest(ctx.ctx);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
LocalePipe = __decorateClass([
|
|
777
|
+
Injectable3(),
|
|
778
|
+
__decorateParam(0, Inject5(I18nService))
|
|
779
|
+
], LocalePipe);
|
|
780
|
+
var _dec = createParamDecorator2((resolver, ctx) => {
|
|
781
|
+
return { ctx, resolver };
|
|
782
|
+
});
|
|
783
|
+
var PutLocale = (resolver) => _dec(resolver, LocalePipe);
|
|
784
|
+
|
|
785
|
+
// src/i18n-module/i18n.module.ts
|
|
786
|
+
var I18nModule = class extends ConfigurableModuleClass {
|
|
787
|
+
};
|
|
788
|
+
I18nModule = __decorateClass([
|
|
789
|
+
Global(),
|
|
790
|
+
Module2({
|
|
791
|
+
providers: [
|
|
792
|
+
createProvider(
|
|
793
|
+
{
|
|
794
|
+
provide: I18nResolverToken,
|
|
795
|
+
inject: [I18nModuleOptionsToken]
|
|
796
|
+
},
|
|
797
|
+
(o) => createDynamicResolverFromStatic(o.resolver)
|
|
798
|
+
),
|
|
799
|
+
I18nService,
|
|
800
|
+
LocalePipe
|
|
801
|
+
],
|
|
802
|
+
exports: [I18nService, LocalePipe]
|
|
803
|
+
})
|
|
804
|
+
], I18nModule);
|
|
805
|
+
|
|
806
|
+
// src/i18n-module/i18n-decorator.ts
|
|
807
|
+
import { UseInterceptors } from "@nestjs/common";
|
|
808
|
+
|
|
809
|
+
// src/i18n-module/i18n.interceptor.ts
|
|
810
|
+
import {
|
|
811
|
+
HttpException as HttpException3,
|
|
812
|
+
Inject as Inject6,
|
|
813
|
+
Injectable as Injectable4
|
|
814
|
+
} from "@nestjs/common";
|
|
815
|
+
import { from, throwError } from "rxjs";
|
|
816
|
+
import { catchError, mergeMap } from "rxjs/operators";
|
|
817
|
+
var I18nInterceptor = class {
|
|
818
|
+
constructor(i18nService) {
|
|
819
|
+
this.i18nService = i18nService;
|
|
820
|
+
}
|
|
821
|
+
intercept(context, next) {
|
|
822
|
+
return next.handle().pipe(
|
|
823
|
+
// 成功路径:把响应体交给 i18n 做异步翻译
|
|
824
|
+
mergeMap((data) => this.i18nService.translateRequest(context, data)),
|
|
825
|
+
// 错误路径:若是 HttpException,把其 response 翻译后再抛
|
|
826
|
+
catchError((err) => {
|
|
827
|
+
if (err instanceof HttpException3) {
|
|
828
|
+
const status = err.getStatus();
|
|
829
|
+
const resp = err.getResponse();
|
|
830
|
+
return from(this.i18nService.translateRequest(context, resp)).pipe(
|
|
831
|
+
mergeMap(
|
|
832
|
+
(translated) => throwError(
|
|
833
|
+
() => new HttpException3(translated, status, { cause: err })
|
|
834
|
+
)
|
|
835
|
+
)
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
return throwError(() => err);
|
|
839
|
+
})
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
I18nInterceptor = __decorateClass([
|
|
844
|
+
Injectable4(),
|
|
845
|
+
__decorateParam(0, Inject6(I18nService))
|
|
846
|
+
], I18nInterceptor);
|
|
847
|
+
|
|
848
|
+
// src/i18n-module/i18n-decorator.ts
|
|
849
|
+
import {
|
|
850
|
+
ApiHeader as ApiHeader2,
|
|
851
|
+
ApiQuery
|
|
852
|
+
} from "@nestjs/swagger";
|
|
853
|
+
var createI18nDecorator = (options) => {
|
|
854
|
+
const paramType = options.resolver?.paramType;
|
|
855
|
+
const apiOptions = {
|
|
856
|
+
name: options.resolver.paramName,
|
|
857
|
+
description: "Locale for internationalization",
|
|
858
|
+
required: false,
|
|
859
|
+
default: options.defaultLocale ?? options.locales[0],
|
|
860
|
+
enum: options.locales
|
|
861
|
+
};
|
|
862
|
+
const dec = paramType === "header" ? () => ApiHeader2(apiOptions) : paramType === "query" ? () => ApiQuery({ ...apiOptions, type: "string" }) : () => () => {
|
|
863
|
+
};
|
|
864
|
+
return () => MergeClassOrMethodDecorators([
|
|
865
|
+
UseInterceptors(I18nInterceptor),
|
|
866
|
+
...paramType ? [dec()] : []
|
|
867
|
+
]);
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
// src/i18n-module/i18n-factory.ts
|
|
871
|
+
var createI18n = (options) => {
|
|
872
|
+
if (!options.resolver) {
|
|
873
|
+
options.resolver = {
|
|
874
|
+
paramType: "header",
|
|
875
|
+
paramName: "x-client-language"
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
return {
|
|
879
|
+
UseI18n: createI18nDecorator(options),
|
|
880
|
+
I18nModule: I18nModule.register(options)
|
|
881
|
+
};
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
// src/i18n-module/middelewares/lookup.ts
|
|
885
|
+
var I18nLookupMiddleware = (dict, options) => {
|
|
886
|
+
const matchType = options?.matchType ?? "exact";
|
|
887
|
+
const dictFactory = typeof dict === "function" ? dict : () => dict;
|
|
888
|
+
const pickBestByHierarchy = (input, locales) => {
|
|
889
|
+
if (!input) return void 0;
|
|
890
|
+
const entries = locales.map((l) => ({ orig: l, lower: l.toLowerCase() }));
|
|
891
|
+
const lower = input.toLowerCase();
|
|
892
|
+
const exact = entries.find((e) => e.lower === lower);
|
|
893
|
+
if (exact) return exact.orig;
|
|
894
|
+
const parts = lower.split("-");
|
|
895
|
+
while (parts.length > 1) {
|
|
896
|
+
parts.pop();
|
|
897
|
+
const candidate = parts.join("-");
|
|
898
|
+
const hit = entries.find((e) => e.lower === candidate);
|
|
899
|
+
if (hit) return hit.orig;
|
|
900
|
+
}
|
|
901
|
+
return void 0;
|
|
902
|
+
};
|
|
903
|
+
return async (locale, key, next, ctx) => {
|
|
904
|
+
const dictResolved = await dictFactory(locale, key, ctx);
|
|
905
|
+
let dictionary = dictResolved[locale];
|
|
906
|
+
if (!dictionary) {
|
|
907
|
+
if (matchType === "hierarchy") {
|
|
908
|
+
const best = pickBestByHierarchy(locale, Object.keys(dictResolved));
|
|
909
|
+
if (best) dictionary = dictResolved[best];
|
|
910
|
+
} else if (matchType === "startsWith") {
|
|
911
|
+
const keys = Object.keys(dictResolved).filter(
|
|
912
|
+
(k) => locale.startsWith(k)
|
|
913
|
+
);
|
|
914
|
+
if (keys.length) {
|
|
915
|
+
const best = keys.reduce((a, b) => b.length > a.length ? b : a);
|
|
916
|
+
dictionary = dictResolved[best];
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
if (dictionary && Object.prototype.hasOwnProperty.call(dictionary, key)) {
|
|
921
|
+
const val = dictionary[key];
|
|
922
|
+
if (val != null) return val;
|
|
923
|
+
}
|
|
924
|
+
return next();
|
|
925
|
+
};
|
|
926
|
+
};
|
|
404
927
|
export {
|
|
405
928
|
ABORT_SIGNAL,
|
|
406
929
|
AbortSignalProvider,
|
|
@@ -417,21 +940,30 @@ export {
|
|
|
417
940
|
DataQuery,
|
|
418
941
|
GenericPaginatedReturnMessageDto,
|
|
419
942
|
GenericReturnMessageDto,
|
|
943
|
+
I18nInterceptor,
|
|
944
|
+
I18nLookupMiddleware,
|
|
945
|
+
I18nModule,
|
|
946
|
+
I18nService,
|
|
420
947
|
InjectAbortSignal,
|
|
421
948
|
InjectAbortable,
|
|
422
949
|
InsertField,
|
|
950
|
+
LocalePipe,
|
|
423
951
|
MergeClassDecorators,
|
|
424
952
|
MergeClassOrMethodDecorators,
|
|
425
953
|
MergeMethodDecorators,
|
|
426
954
|
MergeParameterDecorators,
|
|
427
955
|
MergePropertyDecorators,
|
|
428
956
|
PaginatedReturnMessageDto,
|
|
957
|
+
PutLocale,
|
|
429
958
|
RequireToken,
|
|
430
959
|
ReturnMessageDto,
|
|
431
960
|
StringReturnMessageDto,
|
|
432
961
|
TokenGuard,
|
|
433
962
|
abortableToken,
|
|
434
963
|
createAbortableProvider,
|
|
964
|
+
createI18n,
|
|
965
|
+
createI18nDecorator,
|
|
966
|
+
createProvider,
|
|
435
967
|
fromAbortable,
|
|
436
968
|
getClassFromClassOrArray,
|
|
437
969
|
takeUntilAbort
|