nesties 1.1.23 → 1.1.25
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 +264 -13
- package/dist/index.cjs.map +4 -4
- package/dist/index.mjs +258 -13
- package/dist/index.mjs.map +4 -4
- package/dist/src/i18n-module/i18n-factory.d.ts +6 -0
- package/dist/src/i18n-module/i18n-param-resolver.token.d.ts +2 -0
- package/dist/src/i18n-module/i18n.module.d.ts +2 -0
- package/dist/src/i18n-module/i18n.service.d.ts +3 -2
- package/dist/src/i18n-module/locale.context.d.ts +8 -0
- package/dist/src/metadata.d.ts +11 -0
- package/dist/src/resolver/index.d.ts +3 -0
- package/dist/src/resolver/lambda.d.ts +10 -0
- package/dist/src/{resolver.d.ts → resolver/resolver.d.ts} +3 -3
- package/dist/src/utility/apply-decorator-unique.d.ts +1 -0
- package/dist/src/utility/index.d.ts +1 -0
- package/dist/src/utility/parse-bool.d.ts +1 -0
- package/package.json +1 -1
- package/dist/src/utility/parse-i18n.d.ts +0 -14
- /package/dist/src/{api-inject.d.ts → resolver/api-inject.d.ts} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -67,6 +67,18 @@ var createMutateInject = (tokenResolver) => (token) => (target, propertyKey, par
|
|
|
67
67
|
);
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
+
// src/utility/parse-bool.ts
|
|
71
|
+
var parseBool = (value) => {
|
|
72
|
+
const trueValues = ["true", "1", "yes", "on", true, 1];
|
|
73
|
+
const falseValues = ["false", "0", "no", "off", false, 0];
|
|
74
|
+
if (trueValues.indexOf(value) !== -1) return true;
|
|
75
|
+
if (falseValues.indexOf(value) !== -1) return false;
|
|
76
|
+
if (!!value) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
return void 0;
|
|
80
|
+
};
|
|
81
|
+
|
|
70
82
|
// src/insert-field.ts
|
|
71
83
|
function InsertField(cl, map, newName) {
|
|
72
84
|
const extendedCl = class extends cl {
|
|
@@ -259,7 +271,7 @@ import {
|
|
|
259
271
|
import { ConfigService } from "@nestjs/config";
|
|
260
272
|
import { ApiHeader as ApiHeader2 } from "@nestjs/swagger";
|
|
261
273
|
|
|
262
|
-
// src/resolver.ts
|
|
274
|
+
// src/resolver/resolver.ts
|
|
263
275
|
import {
|
|
264
276
|
createParamDecorator,
|
|
265
277
|
Inject as Inject2,
|
|
@@ -303,7 +315,42 @@ var uniqBy = (arr, fn) => {
|
|
|
303
315
|
return result;
|
|
304
316
|
};
|
|
305
317
|
|
|
306
|
-
// src/
|
|
318
|
+
// src/metadata.ts
|
|
319
|
+
import { MetadataSetter, Reflector } from "typed-reflector";
|
|
320
|
+
var reflector = new Reflector();
|
|
321
|
+
var Metadata = new MetadataSetter();
|
|
322
|
+
|
|
323
|
+
// src/utility/apply-decorator-unique.ts
|
|
324
|
+
var ApplyDecoratorUnique = (dec, identifier) => ((...args) => {
|
|
325
|
+
const isClassRelated = args.length === 1 || args[1] === void 0;
|
|
326
|
+
const target = args[0];
|
|
327
|
+
const cls = typeof target === "function" ? target : target.constructor;
|
|
328
|
+
if (isClassRelated) {
|
|
329
|
+
const used = reflector.getArray("usedUniqueDecoratorIdentifiers", cls);
|
|
330
|
+
if (used.includes(identifier)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
Metadata.appendUnique("usedUniqueDecoratorIdentifiers", identifier)(cls);
|
|
334
|
+
return dec(...args);
|
|
335
|
+
} else {
|
|
336
|
+
const keyName = args[1];
|
|
337
|
+
const used = reflector.getProperty(
|
|
338
|
+
"usedUniqueDecoratorIdentifiers",
|
|
339
|
+
cls,
|
|
340
|
+
keyName
|
|
341
|
+
);
|
|
342
|
+
if (used.includes(identifier)) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
Metadata.appendUnique("usedUniqueDecoratorIdentifiers", identifier)(
|
|
346
|
+
cls,
|
|
347
|
+
keyName
|
|
348
|
+
);
|
|
349
|
+
return dec(...args);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// src/resolver/resolver.ts
|
|
307
354
|
var ParamResolverCopiedFieldsFromSwagger = [
|
|
308
355
|
"required",
|
|
309
356
|
"description",
|
|
@@ -385,7 +432,12 @@ var ParamResolverBase = class {
|
|
|
385
432
|
toApiPropertyDecorator(extras = {}) {
|
|
386
433
|
const swaggerInfo = uniqBy(this.toSwaggerInfo(), (info) => info.token);
|
|
387
434
|
return (extras2 = {}) => MergeClassOrMethodDecorators(
|
|
388
|
-
swaggerInfo.map(
|
|
435
|
+
swaggerInfo.map(
|
|
436
|
+
(info) => ApplyDecoratorUnique(
|
|
437
|
+
info.swagger({ ...extras, ...extras2 }),
|
|
438
|
+
info.token
|
|
439
|
+
)
|
|
440
|
+
)
|
|
389
441
|
);
|
|
390
442
|
}
|
|
391
443
|
toResolverFunction() {
|
|
@@ -566,11 +618,136 @@ var ApiFromResolver = (_options, extras = {}) => {
|
|
|
566
618
|
return getParamResolver(_options).toApiPropertyDecorator(extras)();
|
|
567
619
|
};
|
|
568
620
|
|
|
621
|
+
// src/resolver/lambda.ts
|
|
622
|
+
var createLambdaParamResolver = (cb) => (resolver) => new TransformParamResolver(resolver, cb);
|
|
623
|
+
var ParseIntParamResolver = createLambdaParamResolver((s) => {
|
|
624
|
+
if (s == null) {
|
|
625
|
+
return s;
|
|
626
|
+
}
|
|
627
|
+
const res = parseInt(s, 10);
|
|
628
|
+
if (isNaN(res)) {
|
|
629
|
+
throw new BlankReturnMessageDto(
|
|
630
|
+
400,
|
|
631
|
+
"Invalid integer parameter"
|
|
632
|
+
).toException();
|
|
633
|
+
}
|
|
634
|
+
return res;
|
|
635
|
+
});
|
|
636
|
+
var ParseFloatParamResolver = createLambdaParamResolver(
|
|
637
|
+
(s) => {
|
|
638
|
+
if (s == null) {
|
|
639
|
+
return s;
|
|
640
|
+
}
|
|
641
|
+
const res = parseFloat(s);
|
|
642
|
+
if (isNaN(res)) {
|
|
643
|
+
throw new BlankReturnMessageDto(
|
|
644
|
+
400,
|
|
645
|
+
"Invalid float parameter"
|
|
646
|
+
).toException();
|
|
647
|
+
}
|
|
648
|
+
return res;
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
var ParseBoolParamResolver = createLambdaParamResolver(parseBool);
|
|
652
|
+
var ParseDateParamResolver = createLambdaParamResolver(
|
|
653
|
+
(s) => {
|
|
654
|
+
if (s == null) {
|
|
655
|
+
return s;
|
|
656
|
+
}
|
|
657
|
+
const res = new Date(s);
|
|
658
|
+
if (isNaN(res.getTime())) {
|
|
659
|
+
throw new BlankReturnMessageDto(
|
|
660
|
+
400,
|
|
661
|
+
"Invalid date parameter"
|
|
662
|
+
).toException();
|
|
663
|
+
}
|
|
664
|
+
return res;
|
|
665
|
+
}
|
|
666
|
+
);
|
|
667
|
+
var ParseBase64ParamResolver = createLambdaParamResolver(
|
|
668
|
+
(s) => {
|
|
669
|
+
if (s == null) {
|
|
670
|
+
return s;
|
|
671
|
+
}
|
|
672
|
+
const normalized = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
673
|
+
const padded = normalized + "=".repeat((4 - normalized.length % 4) % 4);
|
|
674
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(padded)) {
|
|
675
|
+
throw new BlankReturnMessageDto(
|
|
676
|
+
400,
|
|
677
|
+
"Invalid base64 parameter"
|
|
678
|
+
).toException();
|
|
679
|
+
}
|
|
680
|
+
const buf = Buffer.from(padded, "base64");
|
|
681
|
+
const re = buf.toString("base64").replace(/=+$/g, "");
|
|
682
|
+
const in0 = padded.replace(/=+$/g, "");
|
|
683
|
+
if (re !== in0) {
|
|
684
|
+
throw new BlankReturnMessageDto(
|
|
685
|
+
400,
|
|
686
|
+
"Invalid base64 parameter"
|
|
687
|
+
).toException();
|
|
688
|
+
}
|
|
689
|
+
return buf;
|
|
690
|
+
}
|
|
691
|
+
);
|
|
692
|
+
var DefaultValueParamResolver = (r, defaultValue) => createLambdaParamResolver(
|
|
693
|
+
(value) => value === void 0 || value === null ? defaultValue : value
|
|
694
|
+
)(r);
|
|
695
|
+
|
|
696
|
+
// src/utility/collect-info-from-injection-token.ts
|
|
697
|
+
import {
|
|
698
|
+
PARAMTYPES_METADATA,
|
|
699
|
+
SELF_DECLARED_DEPS_METADATA
|
|
700
|
+
} from "@nestjs/common/constants";
|
|
701
|
+
function asArray(v) {
|
|
702
|
+
if (v == null) return [];
|
|
703
|
+
return Array.isArray(v) ? v : [v];
|
|
704
|
+
}
|
|
705
|
+
function getCtorDepsFromClass(cls) {
|
|
706
|
+
const paramTypes = Reflect.getMetadata(PARAMTYPES_METADATA, cls) ?? [];
|
|
707
|
+
const selfDeclared = Reflect.getMetadata(SELF_DECLARED_DEPS_METADATA, cls) ?? [];
|
|
708
|
+
const overrides = new Map(
|
|
709
|
+
selfDeclared.map((d) => [d.index, d.param])
|
|
710
|
+
);
|
|
711
|
+
return paramTypes.map((t, i) => overrides.get(i) ?? t).filter((t) => t != null && t !== Object);
|
|
712
|
+
}
|
|
713
|
+
var collectInfoFromInjectionToken = (map, token) => {
|
|
714
|
+
const visited = /* @__PURE__ */ new Set();
|
|
715
|
+
const out = /* @__PURE__ */ new Set();
|
|
716
|
+
const dfs = (t) => {
|
|
717
|
+
if (t == null) return;
|
|
718
|
+
if (visited.has(t)) return;
|
|
719
|
+
visited.add(t);
|
|
720
|
+
for (const v of asArray(map.get(t))) {
|
|
721
|
+
out.add(v);
|
|
722
|
+
}
|
|
723
|
+
const deps = InjectionTokenMap.get(t);
|
|
724
|
+
if (deps?.length) {
|
|
725
|
+
for (const d of deps) dfs(d);
|
|
726
|
+
}
|
|
727
|
+
if (typeof t === "function") {
|
|
728
|
+
const ctorDeps = getCtorDepsFromClass(t);
|
|
729
|
+
for (const d of ctorDeps) dfs(d);
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
dfs(token);
|
|
733
|
+
return Array.from(out);
|
|
734
|
+
};
|
|
735
|
+
var createCollectorFromInjectionToken = (map) => (token) => collectInfoFromInjectionToken(map, token);
|
|
736
|
+
|
|
737
|
+
// src/utility/swagger-injection-collector.ts
|
|
738
|
+
var swaggerInjectionCollector = createCollectorFromInjectionToken(ResolverSwaggerMap);
|
|
739
|
+
|
|
740
|
+
// src/resolver/api-inject.ts
|
|
741
|
+
var ApiInject = createMutateInject((token, cls) => {
|
|
742
|
+
const swaggers = swaggerInjectionCollector(token);
|
|
743
|
+
for (const swagger of swaggers) {
|
|
744
|
+
ApplyDecoratorUnique(swagger.swagger(), swagger.token)(cls);
|
|
745
|
+
}
|
|
746
|
+
return token;
|
|
747
|
+
});
|
|
748
|
+
|
|
569
749
|
// src/token.guard.ts
|
|
570
|
-
import { MetadataSetter, Reflector } from "typed-reflector";
|
|
571
750
|
import { ModuleRef as ModuleRef2 } from "@nestjs/core";
|
|
572
|
-
var reflector = new Reflector();
|
|
573
|
-
var Metadata = new MetadataSetter();
|
|
574
751
|
var defaultHeaderName = "x-server-token";
|
|
575
752
|
var defaultConfigName = "SERVER_TOKEN";
|
|
576
753
|
var defaultErrorCode = 401;
|
|
@@ -804,14 +981,20 @@ var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
|
|
|
804
981
|
// src/i18n-module/i18n.service.ts
|
|
805
982
|
import { ModuleRef as ModuleRef4 } from "@nestjs/core";
|
|
806
983
|
import { I18n } from "nfkit";
|
|
984
|
+
|
|
985
|
+
// src/i18n-module/i18n-param-resolver.token.ts
|
|
986
|
+
var I18nParamResolverToken = "I18N_PARAM_RESOLVER";
|
|
987
|
+
var I18nParamResolverProviderToken = "I18N_PARAM_RESOLVER_PROVIDER";
|
|
988
|
+
|
|
989
|
+
// src/i18n-module/i18n.service.ts
|
|
807
990
|
var I18nService = class extends I18n {
|
|
808
|
-
constructor(i18nServiceOptions, moduleRef) {
|
|
991
|
+
constructor(i18nServiceOptions, moduleRef, resolver) {
|
|
809
992
|
super(i18nServiceOptions);
|
|
810
993
|
this.i18nServiceOptions = i18nServiceOptions;
|
|
811
994
|
this.moduleRef = moduleRef;
|
|
995
|
+
this.resolver = resolver;
|
|
812
996
|
this._shadowMiddlewareMap = /* @__PURE__ */ new Map();
|
|
813
997
|
this.logger = new ConsoleLogger("I18nService");
|
|
814
|
-
this.resolver = createResolver(this.i18nServiceOptions.resolver);
|
|
815
998
|
}
|
|
816
999
|
middleware(mw, prior = false) {
|
|
817
1000
|
const wrappedMw = async (locale, text, next, ctx) => {
|
|
@@ -838,19 +1021,20 @@ var I18nService = class extends I18n {
|
|
|
838
1021
|
}
|
|
839
1022
|
async getExactLocaleFromRequest(ctx) {
|
|
840
1023
|
const req = ctx.switchToHttp().getRequest();
|
|
841
|
-
const locale = await this.resolver(req, this.moduleRef);
|
|
1024
|
+
const locale = await this.resolver.resolve(req, this.moduleRef);
|
|
842
1025
|
return this.getExactLocale(locale);
|
|
843
1026
|
}
|
|
844
1027
|
async translateRequest(ctx, obj) {
|
|
845
1028
|
const req = ctx.switchToHttp().getRequest();
|
|
846
|
-
const locale = await this.resolver(req, this.moduleRef);
|
|
1029
|
+
const locale = await this.resolver.resolve(req, this.moduleRef);
|
|
847
1030
|
return this.translate(locale, obj, ctx);
|
|
848
1031
|
}
|
|
849
1032
|
};
|
|
850
1033
|
I18nService = __decorateClass([
|
|
851
1034
|
Injectable3(),
|
|
852
1035
|
__decorateParam(0, Inject6(I18nModuleOptionsToken)),
|
|
853
|
-
__decorateParam(1, Inject6(ModuleRef4))
|
|
1036
|
+
__decorateParam(1, Inject6(ModuleRef4)),
|
|
1037
|
+
__decorateParam(2, Inject6(I18nParamResolverToken))
|
|
854
1038
|
], I18nService);
|
|
855
1039
|
|
|
856
1040
|
// src/i18n-module/i18n.module.ts
|
|
@@ -893,12 +1077,26 @@ var _dec = createParamDecorator3(
|
|
|
893
1077
|
var PutLocale = (resolver) => _dec(resolver, LocalePipe);
|
|
894
1078
|
|
|
895
1079
|
// src/i18n-module/i18n.module.ts
|
|
1080
|
+
var providerFromOptions = createProvider(
|
|
1081
|
+
{
|
|
1082
|
+
provide: I18nParamResolverToken,
|
|
1083
|
+
inject: [I18nModuleOptionsToken]
|
|
1084
|
+
},
|
|
1085
|
+
(options) => getParamResolver(
|
|
1086
|
+
options.resolver || {
|
|
1087
|
+
paramType: "header",
|
|
1088
|
+
paramName: "x-client-language"
|
|
1089
|
+
}
|
|
1090
|
+
)
|
|
1091
|
+
);
|
|
896
1092
|
var I18nModule = class extends ConfigurableModuleClass {
|
|
1093
|
+
static forRoot(options) {
|
|
1094
|
+
}
|
|
897
1095
|
};
|
|
898
1096
|
I18nModule = __decorateClass([
|
|
899
1097
|
Global(),
|
|
900
1098
|
Module2({
|
|
901
|
-
providers: [I18nService, LocalePipe],
|
|
1099
|
+
providers: [I18nService, LocalePipe, providerFromOptions],
|
|
902
1100
|
exports: [I18nService, LocalePipe]
|
|
903
1101
|
})
|
|
904
1102
|
], I18nModule);
|
|
@@ -958,6 +1156,24 @@ var createI18nDecorator = (options) => {
|
|
|
958
1156
|
]);
|
|
959
1157
|
};
|
|
960
1158
|
|
|
1159
|
+
// src/i18n-module/locale.context.ts
|
|
1160
|
+
import { Inject as Inject9, Injectable as Injectable6, Scope as Scope4 } from "@nestjs/common";
|
|
1161
|
+
var LocaleContext = class {
|
|
1162
|
+
constructor(localeInput, i18nService) {
|
|
1163
|
+
this.localeInput = localeInput;
|
|
1164
|
+
this.i18nService = i18nService;
|
|
1165
|
+
this.locale = this.i18nService.getExactLocale(this.localeInput);
|
|
1166
|
+
}
|
|
1167
|
+
translate(v) {
|
|
1168
|
+
return this.i18nService.translate(this.locale, v);
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
LocaleContext = __decorateClass([
|
|
1172
|
+
Injectable6({ scope: Scope4.REQUEST }),
|
|
1173
|
+
__decorateParam(0, Inject9(I18nParamResolverProviderToken)),
|
|
1174
|
+
__decorateParam(1, Inject9(I18nService))
|
|
1175
|
+
], LocaleContext);
|
|
1176
|
+
|
|
961
1177
|
// src/i18n-module/i18n-factory.ts
|
|
962
1178
|
var createI18n = (options) => {
|
|
963
1179
|
if (!options.resolver) {
|
|
@@ -966,9 +1182,29 @@ var createI18n = (options) => {
|
|
|
966
1182
|
paramName: "x-client-language"
|
|
967
1183
|
};
|
|
968
1184
|
}
|
|
1185
|
+
const resolver = getParamResolver(options.resolver);
|
|
1186
|
+
const paramProvider = resolver.toRequestScopedProvider();
|
|
1187
|
+
const module = I18nModule.register(options);
|
|
1188
|
+
module.providers ??= [];
|
|
1189
|
+
module.exports ??= [];
|
|
1190
|
+
module.providers.push(
|
|
1191
|
+
paramProvider.provider,
|
|
1192
|
+
LocaleContext,
|
|
1193
|
+
// re-exports the resolved param value for LocaleContext injection
|
|
1194
|
+
createProvider(
|
|
1195
|
+
{
|
|
1196
|
+
provide: I18nParamResolverProviderToken,
|
|
1197
|
+
inject: [paramProvider.token]
|
|
1198
|
+
},
|
|
1199
|
+
(s) => s
|
|
1200
|
+
)
|
|
1201
|
+
);
|
|
1202
|
+
module.exports.push(paramProvider.provider, LocaleContext);
|
|
969
1203
|
return {
|
|
970
1204
|
UseI18n: createI18nDecorator(options),
|
|
971
|
-
I18nModule:
|
|
1205
|
+
I18nModule: module,
|
|
1206
|
+
I18nParamResolver: resolver,
|
|
1207
|
+
I18nParamResolverProvider: paramProvider
|
|
972
1208
|
};
|
|
973
1209
|
};
|
|
974
1210
|
|
|
@@ -983,6 +1219,7 @@ export {
|
|
|
983
1219
|
ApiError,
|
|
984
1220
|
ApiErrorTyped,
|
|
985
1221
|
ApiFromResolver,
|
|
1222
|
+
ApiInject,
|
|
986
1223
|
ApiTypeResponse,
|
|
987
1224
|
As,
|
|
988
1225
|
BlankPaginatedReturnMessageDto,
|
|
@@ -991,6 +1228,7 @@ export {
|
|
|
991
1228
|
DataBody,
|
|
992
1229
|
DataPipe,
|
|
993
1230
|
DataQuery,
|
|
1231
|
+
DefaultValueParamResolver,
|
|
994
1232
|
GenericPaginatedReturnMessageDto,
|
|
995
1233
|
GenericReturnMessageDto,
|
|
996
1234
|
I18nInterceptor,
|
|
@@ -1010,6 +1248,11 @@ export {
|
|
|
1010
1248
|
ParamResolver,
|
|
1011
1249
|
ParamResolverBase,
|
|
1012
1250
|
ParamResolverPipe,
|
|
1251
|
+
ParseBase64ParamResolver,
|
|
1252
|
+
ParseBoolParamResolver,
|
|
1253
|
+
ParseDateParamResolver,
|
|
1254
|
+
ParseFloatParamResolver,
|
|
1255
|
+
ParseIntParamResolver,
|
|
1013
1256
|
PutLocale,
|
|
1014
1257
|
RenameClass,
|
|
1015
1258
|
RequireToken,
|
|
@@ -1021,6 +1264,7 @@ export {
|
|
|
1021
1264
|
createAbortableProvider,
|
|
1022
1265
|
createI18n,
|
|
1023
1266
|
createI18nDecorator,
|
|
1267
|
+
createLambdaParamResolver,
|
|
1024
1268
|
createMutateInject,
|
|
1025
1269
|
createProvider,
|
|
1026
1270
|
createResolver,
|
|
@@ -1028,6 +1272,7 @@ export {
|
|
|
1028
1272
|
getApiProperty,
|
|
1029
1273
|
getClassFromClassOrArray,
|
|
1030
1274
|
getParamResolver,
|
|
1275
|
+
parseBool,
|
|
1031
1276
|
takeUntilAbort
|
|
1032
1277
|
};
|
|
1033
1278
|
//# sourceMappingURL=index.mjs.map
|