nesties 1.1.14 → 1.1.16

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.mjs CHANGED
@@ -24,6 +24,22 @@ function getClassFromClassOrArray(o) {
24
24
  return o instanceof Array ? o[0] : o;
25
25
  }
26
26
 
27
+ // src/utility/get-api-property.ts
28
+ import { DECORATORS } from "@nestjs/swagger/dist/constants";
29
+ var getApiProperty = (cls, key) => {
30
+ let proto = cls.prototype;
31
+ while (proto && proto !== Object.prototype) {
32
+ const meta = Reflect.getMetadata(
33
+ DECORATORS.API_MODEL_PROPERTIES,
34
+ proto,
35
+ key
36
+ );
37
+ if (meta) return meta;
38
+ proto = Object.getPrototypeOf(proto);
39
+ }
40
+ return {};
41
+ };
42
+
27
43
  // src/insert-field.ts
28
44
  function InsertField(cl, map, newName) {
29
45
  const extendedCl = class extends cl {
@@ -209,18 +225,35 @@ var DataBody = createDataPipeDec(Body);
209
225
 
210
226
  // src/token.guard.ts
211
227
  import {
212
- Inject,
213
- Injectable,
228
+ Inject as Inject2,
229
+ Injectable as Injectable2,
214
230
  UseGuards
215
231
  } from "@nestjs/common";
216
232
  import { ConfigService } from "@nestjs/config";
217
233
  import { ApiHeader as ApiHeader2 } from "@nestjs/swagger";
218
234
 
219
235
  // src/resolver.ts
236
+ import {
237
+ createParamDecorator,
238
+ Inject,
239
+ Injectable,
240
+ Scope
241
+ } from "@nestjs/common";
242
+ import { ModuleRef, REQUEST } from "@nestjs/core";
220
243
  import {
221
244
  ApiHeader,
222
245
  ApiQuery
223
246
  } from "@nestjs/swagger";
247
+
248
+ // src/create-provider.ts
249
+ var createProvider = (options, factory) => {
250
+ return {
251
+ useFactory: factory,
252
+ ...options
253
+ };
254
+ };
255
+
256
+ // src/resolver.ts
224
257
  var coerceToString = (v) => {
225
258
  if (v == null) return void 0;
226
259
  if (v === false) return void 0;
@@ -265,45 +298,161 @@ function getQueryValue(req, key) {
265
298
  }
266
299
  return void 0;
267
300
  }
268
- var createResolver = (_options) => {
269
- if (typeof _options === "function") {
270
- return _options;
271
- }
272
- const options = _options;
273
- const field = options.paramType;
274
- let name = options.paramName;
275
- if (field === "header") name = name.toLowerCase();
276
- return (ctx) => {
277
- const req = ctx.switchToHttp().getRequest();
278
- if (field === "header") {
279
- let raw = getHeader(req, name);
280
- if (name === "accept-language") raw = pickPrimaryFromAcceptLanguage(raw);
281
- return raw;
282
- }
283
- if (field === "query") {
284
- return getQueryValue(req, name);
301
+ var ParamResolverPipe = class {
302
+ constructor(moduleRef) {
303
+ this.moduleRef = moduleRef;
304
+ }
305
+ async transform(params, metadata) {
306
+ return params.resolver.resolve(params.req, this.moduleRef);
307
+ }
308
+ };
309
+ ParamResolverPipe = __decorateClass([
310
+ Injectable(),
311
+ __decorateParam(0, Inject(ModuleRef))
312
+ ], ParamResolverPipe);
313
+ var usedParamResolverTokens = /* @__PURE__ */ new Set();
314
+ var ParamResolverBase = class {
315
+ toResolverFunction() {
316
+ return async (req, ref) => this.resolve(req, ref);
317
+ }
318
+ toParamDecorator() {
319
+ const dec = createParamDecorator((_, ctx) => {
320
+ const req = ctx.switchToHttp().getRequest();
321
+ return { req, resolver: this };
322
+ });
323
+ return (...pipes) => dec(ParamResolverPipe, ...pipes);
324
+ }
325
+ // for override
326
+ toString() {
327
+ return "ParamResolverBase";
328
+ }
329
+ toRequestScopedProvider() {
330
+ const token = `PARAM_RESOLVER_${this.toString()}`;
331
+ let useToken = token;
332
+ if (usedParamResolverTokens.has(token)) {
333
+ let suffix = 0;
334
+ const tryToken = `${token}__${suffix}`;
335
+ while (usedParamResolverTokens.has(tryToken)) {
336
+ suffix++;
337
+ }
338
+ useToken = tryToken;
285
339
  }
286
- throw new Error(`Unsupported paramType: ${field}`);
287
- };
340
+ const provider = createProvider(
341
+ {
342
+ provide: useToken,
343
+ inject: [REQUEST, ModuleRef],
344
+ scope: Scope.REQUEST
345
+ },
346
+ this.toResolverFunction()
347
+ );
348
+ return {
349
+ token: useToken,
350
+ provider,
351
+ inject: () => Inject(useToken)
352
+ };
353
+ }
288
354
  };
289
- var ApiFromResolver = (_options, extras = {}) => {
290
- if (typeof _options === "function") {
291
- return () => {
355
+ var ParamResolver = class extends ParamResolverBase {
356
+ constructor(input) {
357
+ super();
358
+ if (typeof input === "function") {
359
+ this.dynamic = input;
360
+ } else {
361
+ this.info = { ...input };
362
+ if (this.info.paramType === "header") {
363
+ this.info.paramName = this.info.paramName.toLowerCase();
364
+ }
365
+ }
366
+ }
367
+ resolve(req, ref) {
368
+ if (this.info) {
369
+ if (this.info.paramType === "header") {
370
+ const name = this.info.paramName;
371
+ let raw = getHeader(req, name);
372
+ if (name === "accept-language")
373
+ raw = pickPrimaryFromAcceptLanguage(raw);
374
+ return raw;
375
+ }
376
+ if (this.info.paramType === "query") {
377
+ return getQueryValue(req, this.info.paramName);
378
+ }
379
+ throw new Error(`Unsupported paramType: ${this.info.paramType}`);
380
+ } else if (this.dynamic) {
381
+ return this.dynamic(req, ref);
382
+ }
383
+ }
384
+ toString() {
385
+ const suffix = this.info ? `${this.info.paramType.toUpperCase()}_${this.info.paramName}` : `DYNAMIC`;
386
+ return `ParamResolver_${suffix}`;
387
+ }
388
+ toApiPropertyDecorator(extras = {}) {
389
+ return (extras2 = {}) => {
390
+ if (this.info) {
391
+ const paramType = this.info.paramType;
392
+ const apiOptions = {
393
+ name: this.info.paramName,
394
+ ...extras,
395
+ ...extras2
396
+ };
397
+ return paramType === "header" ? ApiHeader(apiOptions) : paramType === "query" ? ApiQuery({ type: "string", ...apiOptions }) : () => {
398
+ };
399
+ }
400
+ return () => {
401
+ };
292
402
  };
293
403
  }
294
- const options = _options;
295
- const paramType = options?.paramType;
296
- const apiOptions = {
297
- name: options.paramName,
298
- ...extras
299
- };
300
- return paramType === "header" ? ApiHeader(apiOptions) : paramType === "query" ? ApiQuery({ type: "string", ...apiOptions }) : () => {
301
- };
404
+ };
405
+ var CombinedParamResolver = class extends ParamResolverBase {
406
+ constructor(resolvers) {
407
+ super();
408
+ this.resolvers = resolvers;
409
+ }
410
+ async resolve(req, ref) {
411
+ const result = {};
412
+ await Promise.all(
413
+ Object.entries(this.resolvers).map(async ([key, resolver]) => {
414
+ result[key] = await resolver.resolve(
415
+ req,
416
+ ref
417
+ );
418
+ })
419
+ );
420
+ return result;
421
+ }
422
+ toString() {
423
+ const suffix = Object.entries(this.resolvers).map(([key, resolver]) => `${key.toString()}_${resolver.toString()}`).join("__");
424
+ return `CombinedParamResolver_${suffix}`;
425
+ }
426
+ toApiPropertyDecorator(extras = {}) {
427
+ const decs = Object.values(this.resolvers).map(
428
+ (resolver) => resolver.toApiPropertyDecorator(extras)
429
+ );
430
+ return (extras2) => MergeClassOrMethodDecorators(
431
+ decs.map(
432
+ (dec) => dec({
433
+ ...extras,
434
+ ...extras2
435
+ })
436
+ )
437
+ );
438
+ }
439
+ };
440
+ var getParamResolver = (input) => {
441
+ if (input instanceof ParamResolverBase) {
442
+ return input;
443
+ }
444
+ return new ParamResolver(input);
445
+ };
446
+ var createResolver = (_options) => {
447
+ return getParamResolver(_options).toResolverFunction();
448
+ };
449
+ var ApiFromResolver = (_options, extras = {}) => {
450
+ return getParamResolver(_options).toApiPropertyDecorator(extras)();
302
451
  };
303
452
 
304
453
  // src/token.guard.ts
305
454
  import { MetadataSetter, Reflector } from "typed-reflector";
306
- import { ModuleRef } from "@nestjs/core";
455
+ import { ModuleRef as ModuleRef2 } from "@nestjs/core";
307
456
  var reflector = new Reflector();
308
457
  var Metadata = new MetadataSetter();
309
458
  var defaultHeaderName = "x-server-token";
@@ -329,9 +478,10 @@ var TokenGuard = class {
329
478
  config.resolver || { paramType: "header", paramName: defaultHeaderName }
330
479
  );
331
480
  const tokenSource = config.tokenSource || defaultConfigName;
481
+ const req = context.switchToHttp().getRequest();
332
482
  const [tokenFromClient, tokenFromConfig] = await Promise.all([
333
- resolver(context, this.moduleRef),
334
- typeof tokenSource === "function" ? tokenSource(context, this.moduleRef) : this.config.get(tokenSource)
483
+ resolver(req, this.moduleRef),
484
+ typeof tokenSource === "function" ? tokenSource(req, this.moduleRef) : this.config.get(tokenSource)
335
485
  ]);
336
486
  if (tokenFromConfig && tokenFromConfig !== tokenFromClient) {
337
487
  throw new BlankReturnMessageDto(
@@ -343,9 +493,9 @@ var TokenGuard = class {
343
493
  }
344
494
  };
345
495
  TokenGuard = __decorateClass([
346
- Injectable(),
347
- __decorateParam(0, Inject(ConfigService)),
348
- __decorateParam(1, Inject(ModuleRef))
496
+ Injectable2(),
497
+ __decorateParam(0, Inject2(ConfigService)),
498
+ __decorateParam(1, Inject2(ModuleRef2))
349
499
  ], TokenGuard);
350
500
  var RequireToken = (options = {}) => {
351
501
  const swaggerDec = options.resolver ? ApiFromResolver(options.resolver, {
@@ -369,7 +519,7 @@ var RequireToken = (options = {}) => {
369
519
 
370
520
  // src/abort-utils.ts
371
521
  import { Observable, takeUntil } from "rxjs";
372
- import { createParamDecorator } from "@nestjs/common";
522
+ import { createParamDecorator as createParamDecorator2 } from "@nestjs/common";
373
523
 
374
524
  // src/utility/abort-http-signal.ts
375
525
  function toRawReq(req) {
@@ -435,7 +585,7 @@ var takeUntilAbort = (signal) => {
435
585
  );
436
586
  };
437
587
  };
438
- var As = createParamDecorator(
588
+ var As = createParamDecorator2(
439
589
  (_data, ctx) => {
440
590
  const req = ctx.switchToHttp().getRequest();
441
591
  return createAbortSignalFromHttp(req);
@@ -443,37 +593,27 @@ var As = createParamDecorator(
443
593
  );
444
594
 
445
595
  // src/abortable-module/abortable.token.ts
446
- import { Inject as Inject3, Scope as Scope2 } from "@nestjs/common";
596
+ import { Inject as Inject4, Scope as Scope3 } from "@nestjs/common";
447
597
  import { abortable } from "nfkit";
448
598
 
449
599
  // src/abortable-module/abort-signal.provider.ts
450
- import { Inject as Inject2, Scope } from "@nestjs/common";
451
- import { REQUEST } from "@nestjs/core";
452
-
453
- // src/create-provider.ts
454
- var createProvider = (options, factory) => {
455
- return {
456
- useFactory: factory,
457
- ...options
458
- };
459
- };
460
-
461
- // src/abortable-module/abort-signal.provider.ts
600
+ import { Inject as Inject3, Scope as Scope2 } from "@nestjs/common";
601
+ import { REQUEST as REQUEST2 } from "@nestjs/core";
462
602
  var ABORT_SIGNAL = Symbol(
463
603
  "ABORT_SIGNAL"
464
604
  );
465
605
  var AbortSignalProvider = createProvider(
466
606
  {
467
607
  provide: ABORT_SIGNAL,
468
- scope: Scope.REQUEST,
469
- inject: [REQUEST]
608
+ scope: Scope2.REQUEST,
609
+ inject: [REQUEST2]
470
610
  },
471
611
  createAbortSignalFromHttp
472
612
  );
473
- var InjectAbortSignal = () => Inject2(ABORT_SIGNAL);
613
+ var InjectAbortSignal = () => Inject3(ABORT_SIGNAL);
474
614
 
475
615
  // src/abortable-module/abortable.token.ts
476
- import { ContextIdFactory, ModuleRef as ModuleRef2, REQUEST as REQUEST2 } from "@nestjs/core";
616
+ import { ContextIdFactory, ModuleRef as ModuleRef3, REQUEST as REQUEST3 } from "@nestjs/core";
477
617
  var tokenMemo = /* @__PURE__ */ new Map();
478
618
  var abortableToken = (token) => {
479
619
  if (tokenMemo.has(token)) return tokenMemo.get(token);
@@ -494,7 +634,7 @@ function InjectAbortable(token) {
494
634
  );
495
635
  }
496
636
  }
497
- Inject3(abortableToken(actualToken))(target, propertyKey, parameterIndex);
637
+ Inject4(abortableToken(actualToken))(target, propertyKey, parameterIndex);
498
638
  };
499
639
  }
500
640
  function createAbortableProvider(token, opts) {
@@ -502,8 +642,8 @@ function createAbortableProvider(token, opts) {
502
642
  return createProvider(
503
643
  {
504
644
  provide,
505
- scope: Scope2.REQUEST,
506
- inject: [ModuleRef2, REQUEST2, ABORT_SIGNAL]
645
+ scope: Scope3.REQUEST,
646
+ inject: [ModuleRef3, REQUEST3, ABORT_SIGNAL]
507
647
  },
508
648
  async (moduleRef, req, signal) => {
509
649
  const ctxId = ContextIdFactory.getByRequest(req);
@@ -550,8 +690,8 @@ AbortableModule = __decorateClass([
550
690
  import {
551
691
  ConsoleLogger,
552
692
  HttpException as HttpException2,
553
- Inject as Inject4,
554
- Injectable as Injectable2
693
+ Inject as Inject5,
694
+ Injectable as Injectable3
555
695
  } from "@nestjs/common";
556
696
 
557
697
  // src/i18n-module/i18n-token.ts
@@ -560,7 +700,7 @@ var { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBu
560
700
  var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
561
701
 
562
702
  // src/i18n-module/i18n.service.ts
563
- import { ModuleRef as ModuleRef3 } from "@nestjs/core";
703
+ import { ModuleRef as ModuleRef4 } from "@nestjs/core";
564
704
  import { I18n } from "nfkit";
565
705
  var I18nService = class extends I18n {
566
706
  constructor(i18nServiceOptions, moduleRef) {
@@ -595,18 +735,20 @@ var I18nService = class extends I18n {
595
735
  return this;
596
736
  }
597
737
  async getExactLocaleFromRequest(ctx) {
598
- const locale = await this.resolver(ctx, this.moduleRef);
738
+ const req = ctx.switchToHttp().getRequest();
739
+ const locale = await this.resolver(req, this.moduleRef);
599
740
  return this.getExactLocale(locale);
600
741
  }
601
742
  async translateRequest(ctx, obj) {
602
- const locale = await this.resolver(ctx, this.moduleRef);
743
+ const req = ctx.switchToHttp().getRequest();
744
+ const locale = await this.resolver(req, this.moduleRef);
603
745
  return this.translate(locale, obj, ctx);
604
746
  }
605
747
  };
606
748
  I18nService = __decorateClass([
607
- Injectable2(),
608
- __decorateParam(0, Inject4(I18nModuleOptionsToken)),
609
- __decorateParam(1, Inject4(ModuleRef3))
749
+ Injectable3(),
750
+ __decorateParam(0, Inject5(I18nModuleOptionsToken)),
751
+ __decorateParam(1, Inject5(ModuleRef4))
610
752
  ], I18nService);
611
753
 
612
754
  // src/i18n-module/i18n.module.ts
@@ -614,11 +756,11 @@ import { Global, Module as Module2 } from "@nestjs/common";
614
756
 
615
757
  // src/i18n-module/locale.pipe.ts
616
758
  import {
617
- createParamDecorator as createParamDecorator2,
618
- Inject as Inject5,
619
- Injectable as Injectable3
759
+ createParamDecorator as createParamDecorator3,
760
+ Inject as Inject6,
761
+ Injectable as Injectable4
620
762
  } from "@nestjs/common";
621
- import { ModuleRef as ModuleRef4 } from "@nestjs/core";
763
+ import { ModuleRef as ModuleRef5 } from "@nestjs/core";
622
764
  var LocalePipe = class {
623
765
  constructor(i18nService, moduleRef) {
624
766
  this.i18nService = i18nService;
@@ -628,7 +770,8 @@ var LocalePipe = class {
628
770
  const resolver = ctx.resolver;
629
771
  if (resolver) {
630
772
  const _resolver = createResolver(resolver);
631
- const locale = await _resolver(ctx.ctx, this.moduleRef);
773
+ const req = ctx.ctx.switchToHttp().getRequest();
774
+ const locale = await _resolver(req, this.moduleRef);
632
775
  return this.i18nService.getExactLocale(locale);
633
776
  } else {
634
777
  return this.i18nService.getExactLocaleFromRequest(ctx.ctx);
@@ -636,13 +779,15 @@ var LocalePipe = class {
636
779
  }
637
780
  };
638
781
  LocalePipe = __decorateClass([
639
- Injectable3(),
640
- __decorateParam(0, Inject5(I18nService)),
641
- __decorateParam(1, Inject5(ModuleRef4))
782
+ Injectable4(),
783
+ __decorateParam(0, Inject6(I18nService)),
784
+ __decorateParam(1, Inject6(ModuleRef5))
642
785
  ], LocalePipe);
643
- var _dec = createParamDecorator2((resolver, ctx) => {
644
- return { ctx, resolver };
645
- });
786
+ var _dec = createParamDecorator3(
787
+ (resolver, ctx) => {
788
+ return { ctx, resolver };
789
+ }
790
+ );
646
791
  var PutLocale = (resolver) => _dec(resolver, LocalePipe);
647
792
 
648
793
  // src/i18n-module/i18n.module.ts
@@ -662,8 +807,8 @@ import { UseInterceptors } from "@nestjs/common";
662
807
  // src/i18n-module/i18n.interceptor.ts
663
808
  import {
664
809
  HttpException as HttpException3,
665
- Inject as Inject6,
666
- Injectable as Injectable4
810
+ Inject as Inject7,
811
+ Injectable as Injectable5
667
812
  } from "@nestjs/common";
668
813
  import { from, throwError } from "rxjs";
669
814
  import { catchError, mergeMap } from "rxjs/operators";
@@ -694,8 +839,8 @@ var I18nInterceptor = class {
694
839
  }
695
840
  };
696
841
  I18nInterceptor = __decorateClass([
697
- Injectable4(),
698
- __decorateParam(0, Inject6(I18nService))
842
+ Injectable5(),
843
+ __decorateParam(0, Inject7(I18nService))
699
844
  ], I18nInterceptor);
700
845
 
701
846
  // src/i18n-module/i18n-decorator.ts
@@ -740,6 +885,7 @@ export {
740
885
  As,
741
886
  BlankPaginatedReturnMessageDto,
742
887
  BlankReturnMessageDto,
888
+ CombinedParamResolver,
743
889
  DataBody,
744
890
  DataPipe,
745
891
  DataQuery,
@@ -759,6 +905,9 @@ export {
759
905
  MergeParameterDecorators,
760
906
  MergePropertyDecorators,
761
907
  PaginatedReturnMessageDto,
908
+ ParamResolver,
909
+ ParamResolverBase,
910
+ ParamResolverPipe,
762
911
  PutLocale,
763
912
  RenameClass,
764
913
  RequireToken,
@@ -772,7 +921,9 @@ export {
772
921
  createProvider,
773
922
  createResolver,
774
923
  fromAbortable,
924
+ getApiProperty,
775
925
  getClassFromClassOrArray,
926
+ getParamResolver,
776
927
  takeUntilAbort
777
928
  };
778
929
  //# sourceMappingURL=index.mjs.map