nesties 1.1.10 → 1.1.13

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 CHANGED
@@ -59,6 +59,7 @@ __export(index_exports, {
59
59
  MergePropertyDecorators: () => MergePropertyDecorators,
60
60
  PaginatedReturnMessageDto: () => PaginatedReturnMessageDto,
61
61
  PutLocale: () => PutLocale,
62
+ RenameClass: () => RenameClass,
62
63
  RequireToken: () => RequireToken,
63
64
  ReturnMessageDto: () => ReturnMessageDto,
64
65
  StringReturnMessageDto: () => StringReturnMessageDto,
@@ -77,9 +78,19 @@ module.exports = __toCommonJS(index_exports);
77
78
 
78
79
  // src/insert-field.ts
79
80
  var import_swagger = require("@nestjs/swagger");
81
+
82
+ // src/utility/rename-class.ts
83
+ function RenameClass(cls, name) {
84
+ Object.defineProperty(cls, "name", { value: name });
85
+ return cls;
86
+ }
87
+
88
+ // src/utility/class-types.ts
80
89
  function getClassFromClassOrArray(o) {
81
90
  return o instanceof Array ? o[0] : o;
82
91
  }
92
+
93
+ // src/insert-field.ts
83
94
  function InsertField(cl, map, newName) {
84
95
  const extendedCl = class extends cl {
85
96
  };
@@ -89,10 +100,7 @@ function InsertField(cl, map, newName) {
89
100
  ...map[key].options || {}
90
101
  })(extendedCl.prototype, key);
91
102
  }
92
- Object.defineProperty(extendedCl, "name", {
93
- value: newName || cl.name
94
- });
95
- return extendedCl;
103
+ return RenameClass(extendedCl, newName || cl.name);
96
104
  }
97
105
 
98
106
  // src/merge.ts
@@ -598,251 +606,48 @@ var import_common9 = require("@nestjs/common");
598
606
 
599
607
  // src/i18n-module/i18n-token.ts
600
608
  var import_common8 = require("@nestjs/common");
601
- var I18nResolverToken = Symbol("I18nResolverToken");
602
609
  var { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new import_common8.ConfigurableModuleBuilder().build();
603
610
  var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
604
611
 
605
- // src/utility/parse-i18n.ts
606
- var parseI18n = (text) => {
607
- const pieces = [];
608
- if (!text) return pieces;
609
- let i = 0;
610
- const n = text.length;
611
- while (i < n) {
612
- const start = text.indexOf("#{", i);
613
- if (start === -1) {
614
- if (i < n) pieces.push({ type: "raw", value: text.slice(i) });
615
- break;
616
- }
617
- let j = start + 2;
618
- let depth = 1;
619
- while (j < n && depth > 0) {
620
- const ch = text.charCodeAt(j);
621
- if (ch === 123) depth++;
622
- else if (ch === 125) depth--;
623
- j++;
624
- }
625
- if (depth !== 0) {
626
- pieces.push({ type: "raw", value: text.slice(i) });
627
- break;
628
- }
629
- if (start > i) {
630
- pieces.push({ type: "raw", value: text.slice(i, start) });
631
- }
632
- const rawInner = text.slice(start + 2, j - 1);
633
- const key = rawInner.trim();
634
- pieces.push({ type: "ph", rawInner, key });
635
- i = j;
636
- }
637
- return pieces;
638
- };
639
-
640
612
  // src/i18n-module/i18n.service.ts
641
613
  var import_core4 = require("@nestjs/core");
642
- var I18nService = class {
643
- constructor(options, moduleRef) {
644
- this.options = options;
614
+ var import_nfkit2 = require("nfkit");
615
+ var I18nService = class extends import_nfkit2.I18n {
616
+ constructor(i18nServiceOptions, moduleRef) {
617
+ super(i18nServiceOptions);
618
+ this.i18nServiceOptions = i18nServiceOptions;
645
619
  this.moduleRef = moduleRef;
646
- this.resolver = createResolver(this.options.resolver);
647
- this.locales = new Set(this.options.locales);
648
- this.defaultLocale = this.options.defaultLocale ?? this.options.locales[0];
649
- this.middlewares = [];
620
+ this._shadowMiddlewareMap = /* @__PURE__ */ new Map();
650
621
  this.logger = new import_common9.ConsoleLogger("I18nService");
622
+ this.resolver = createResolver(this.i18nServiceOptions.resolver);
651
623
  }
652
624
  middleware(mw, prior = false) {
653
- if (prior) {
654
- this.middlewares.unshift(mw);
655
- } else {
656
- this.middlewares.push(mw);
657
- }
658
- }
659
- buildFallbackChain(locale) {
660
- const best = this.getExactLocale(locale);
661
- const parts = [];
662
- const segs = best.split("-");
663
- for (let i = segs.length; i > 1; i--)
664
- parts.push(segs.slice(0, i).join("-"));
665
- parts.push(segs[0]);
666
- if (!parts.includes(this.defaultLocale)) parts.push(this.defaultLocale);
667
- return Array.from(new Set(parts)).filter((p) => this.locales.has(p));
668
- }
669
- async applyMiddlewares(locale, text, ctx) {
670
- const mws = this.middlewares;
671
- const tryLocale = async (loc) => {
672
- const dispatch = async (i) => {
673
- if (i >= mws.length) return void 0;
674
- const mw = mws[i];
675
- let nextCalled = false;
676
- const next = async () => {
677
- nextCalled = true;
678
- return dispatch(i + 1);
679
- };
680
- try {
681
- const res = await mw(loc, text, next, ctx);
682
- if (res == null && !nextCalled) {
683
- return dispatch(i + 1);
684
- }
685
- return res;
686
- } catch (e) {
687
- if (e instanceof import_common9.HttpException) {
688
- throw e;
689
- }
690
- this.logger.warn(`Middleware at index ${i} threw an error: ${e}`);
691
- return dispatch(i + 1);
625
+ const wrappedMw = async (locale, text, next, ctx) => {
626
+ try {
627
+ return await mw(locale, text, next, ctx);
628
+ } catch (e) {
629
+ if (e instanceof import_common9.HttpException) {
630
+ throw e;
692
631
  }
693
- };
694
- return dispatch(0);
695
- };
696
- for (const loc of this.buildFallbackChain(locale)) {
697
- const result = await tryLocale(loc);
698
- if (result != null) {
699
- return result;
632
+ this.logger.error(`Error in i18n middleware: ${e.message}`);
633
+ return next();
700
634
  }
635
+ };
636
+ this._shadowMiddlewareMap.set(mw, wrappedMw);
637
+ return super.middleware(wrappedMw, prior);
638
+ }
639
+ removeMiddleware(mw) {
640
+ const wrappedMw = this._shadowMiddlewareMap.get(mw);
641
+ if (wrappedMw) {
642
+ this._shadowMiddlewareMap.delete(mw);
643
+ return super.removeMiddleware(wrappedMw);
701
644
  }
702
- return void 0;
703
- }
704
- getExactLocale(locale) {
705
- const input = (locale ?? "").trim();
706
- if (!input) return this.defaultLocale;
707
- if (this.locales.has(input)) return input;
708
- const entries = Array.from(this.locales).map((l) => ({
709
- orig: l,
710
- lower: l.toLowerCase()
711
- }));
712
- const lower = input.toLowerCase();
713
- const exact = entries.find((e) => e.lower === lower);
714
- if (exact) return exact.orig;
715
- const parts = lower.split("-");
716
- while (parts.length > 1) {
717
- parts.pop();
718
- const candidate = parts.join("-");
719
- const hit = entries.find((e) => e.lower === candidate);
720
- if (hit) return hit.orig;
721
- }
722
- return this.defaultLocale;
645
+ return this;
723
646
  }
724
647
  async getExactLocaleFromRequest(ctx) {
725
648
  const locale = await this.resolver(ctx, this.moduleRef);
726
649
  return this.getExactLocale(locale);
727
650
  }
728
- async translateString(locale, text, ctx) {
729
- if (!text) return text;
730
- locale = this.getExactLocale(locale);
731
- const pieces = parseI18n(text);
732
- if (!pieces.some((p) => p.type === "ph")) {
733
- return pieces.map((p) => p.type === "raw" ? p.value : `#{${p.rawInner}}`).join("");
734
- }
735
- const promises = [];
736
- for (const p of pieces) {
737
- if (p.type === "ph") {
738
- promises.push(this.applyMiddlewares(locale, p.key, ctx));
739
- }
740
- }
741
- const results = await Promise.all(promises);
742
- let out = "";
743
- let k = 0;
744
- for (const p of pieces) {
745
- if (p.type === "raw") {
746
- out += p.value;
747
- } else {
748
- const r = results[k++];
749
- out += r == null ? `#{${p.rawInner}}` : r;
750
- }
751
- }
752
- return out;
753
- }
754
- async translate(locale, obj, ctx) {
755
- const visited = /* @__PURE__ */ new WeakSet();
756
- const isBuiltInObject = (v) => {
757
- if (v == null || typeof v !== "object") return false;
758
- 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))
759
- return true;
760
- const tag = Object.prototype.toString.call(v);
761
- switch (tag) {
762
- case "[object URL]":
763
- case "[object URLSearchParams]":
764
- case "[object Error]":
765
- case "[object Blob]":
766
- case "[object File]":
767
- case "[object FormData]":
768
- return true;
769
- default:
770
- return false;
771
- }
772
- };
773
- const translateObjectPreservingProto = async (value) => {
774
- const proto = Object.getPrototypeOf(value);
775
- const out = Object.create(proto);
776
- const keys = Reflect.ownKeys(value);
777
- await Promise.all(
778
- keys.map(async (key) => {
779
- const desc = Object.getOwnPropertyDescriptor(value, key);
780
- if (!desc) return;
781
- if ("value" in desc) {
782
- const newVal = await visit(desc.value);
783
- Object.defineProperty(out, key, { ...desc, value: newVal });
784
- return;
785
- }
786
- Object.defineProperty(out, key, desc);
787
- let current = void 0;
788
- if (typeof desc.get === "function") {
789
- try {
790
- current = desc.get.call(value);
791
- } catch {
792
- }
793
- }
794
- if (current === void 0) return;
795
- try {
796
- const newVal = await visit(current);
797
- if (typeof desc.set === "function") {
798
- try {
799
- desc.set.call(out, newVal);
800
- } catch {
801
- }
802
- }
803
- } catch {
804
- }
805
- })
806
- );
807
- return out;
808
- };
809
- const isTranslatable = (v) => {
810
- if (!v) return { ok: false };
811
- const t = typeof v;
812
- if (t === "number" || t === "bigint" || t === "symbol" || t === "function") {
813
- return { ok: false };
814
- }
815
- if (t === "string") return { ok: true, kind: "string" };
816
- if (t === "object") {
817
- return { ok: true, kind: "object" };
818
- }
819
- return { ok: false };
820
- };
821
- const visit = async (value) => {
822
- const check = isTranslatable(value);
823
- if (!check.ok) {
824
- return value;
825
- }
826
- if (check.kind === "string") {
827
- return this.translateString(locale, value, ctx);
828
- }
829
- if (value instanceof Promise) {
830
- return value.then((resolved) => visit(resolved));
831
- }
832
- if (typeof value === "object") {
833
- if (!Array.isArray(value) && isBuiltInObject(value)) return value;
834
- if (visited.has(value)) return value;
835
- visited.add(value);
836
- if (Array.isArray(value)) {
837
- const out = await Promise.all(value.map((v) => visit(v)));
838
- return out;
839
- }
840
- return translateObjectPreservingProto(value);
841
- }
842
- return value;
843
- };
844
- return visit(obj);
845
- }
846
651
  async translateRequest(ctx, obj) {
847
652
  const locale = await this.resolver(ctx, this.moduleRef);
848
653
  return this.translate(locale, obj, ctx);
@@ -963,50 +768,8 @@ var createI18n = (options) => {
963
768
  };
964
769
 
965
770
  // src/i18n-module/middlewares/lookup.ts
966
- var I18nLookupMiddleware = (dict, options) => {
967
- const matchType = options?.matchType ?? "exact";
968
- const dictFactory = typeof dict === "function" ? dict : () => dict;
969
- const pickBestByHierarchy = (input, locales) => {
970
- if (!input) return void 0;
971
- const entries = locales.map((l) => ({ orig: l, lower: l.toLowerCase() }));
972
- const lower = input.toLowerCase();
973
- const exact = entries.find((e) => e.lower === lower);
974
- if (exact) return exact.orig;
975
- const parts = lower.split("-");
976
- while (parts.length > 1) {
977
- parts.pop();
978
- const candidate = parts.join("-");
979
- const hit = entries.find((e) => e.lower === candidate);
980
- if (hit) return hit.orig;
981
- }
982
- return void 0;
983
- };
984
- return async (locale, key, next, ctx) => {
985
- const dictResolved = await dictFactory(locale, key, ctx);
986
- let dictionary = dictResolved[locale];
987
- if (!dictionary) {
988
- if (matchType === "hierarchy") {
989
- const best = pickBestByHierarchy(locale, Object.keys(dictResolved));
990
- if (best) dictionary = dictResolved[best];
991
- } else if (matchType === "startsWith") {
992
- const keys = Object.keys(dictResolved).filter(
993
- (k) => locale.startsWith(k)
994
- );
995
- if (keys.length) {
996
- const best = keys.reduce((a, b) => b.length > a.length ? b : a);
997
- dictionary = dictResolved[best];
998
- }
999
- }
1000
- }
1001
- if (dictionary && Object.prototype.hasOwnProperty.call(dictionary, key)) {
1002
- const val = dictionary[key];
1003
- if (val != null) {
1004
- return val;
1005
- }
1006
- }
1007
- return next();
1008
- };
1009
- };
771
+ var import_nfkit3 = require("nfkit");
772
+ var I18nLookupMiddleware = (0, import_nfkit3.createI18nLookupMiddleware)();
1010
773
  // Annotate the CommonJS export names for ESM import in node:
1011
774
  0 && (module.exports = {
1012
775
  ABORT_SIGNAL,
@@ -1040,6 +803,7 @@ var I18nLookupMiddleware = (dict, options) => {
1040
803
  MergePropertyDecorators,
1041
804
  PaginatedReturnMessageDto,
1042
805
  PutLocale,
806
+ RenameClass,
1043
807
  RequireToken,
1044
808
  ReturnMessageDto,
1045
809
  StringReturnMessageDto,