nesties 1.1.15 → 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
@@ -225,18 +225,35 @@ var DataBody = createDataPipeDec(Body);
225
225
 
226
226
  // src/token.guard.ts
227
227
  import {
228
- Inject,
229
- Injectable,
228
+ Inject as Inject2,
229
+ Injectable as Injectable2,
230
230
  UseGuards
231
231
  } from "@nestjs/common";
232
232
  import { ConfigService } from "@nestjs/config";
233
233
  import { ApiHeader as ApiHeader2 } from "@nestjs/swagger";
234
234
 
235
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";
236
243
  import {
237
244
  ApiHeader,
238
245
  ApiQuery
239
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
240
257
  var coerceToString = (v) => {
241
258
  if (v == null) return void 0;
242
259
  if (v === false) return void 0;
@@ -281,45 +298,161 @@ function getQueryValue(req, key) {
281
298
  }
282
299
  return void 0;
283
300
  }
284
- var createResolver = (_options) => {
285
- if (typeof _options === "function") {
286
- return _options;
287
- }
288
- const options = _options;
289
- const field = options.paramType;
290
- let name = options.paramName;
291
- if (field === "header") name = name.toLowerCase();
292
- return (ctx) => {
293
- const req = ctx.switchToHttp().getRequest();
294
- if (field === "header") {
295
- let raw = getHeader(req, name);
296
- if (name === "accept-language") raw = pickPrimaryFromAcceptLanguage(raw);
297
- return raw;
298
- }
299
- if (field === "query") {
300
- 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;
301
339
  }
302
- throw new Error(`Unsupported paramType: ${field}`);
303
- };
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
+ }
304
354
  };
305
- var ApiFromResolver = (_options, extras = {}) => {
306
- if (typeof _options === "function") {
307
- 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
+ };
308
402
  };
309
403
  }
310
- const options = _options;
311
- const paramType = options?.paramType;
312
- const apiOptions = {
313
- name: options.paramName,
314
- ...extras
315
- };
316
- return paramType === "header" ? ApiHeader(apiOptions) : paramType === "query" ? ApiQuery({ type: "string", ...apiOptions }) : () => {
317
- };
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)();
318
451
  };
319
452
 
320
453
  // src/token.guard.ts
321
454
  import { MetadataSetter, Reflector } from "typed-reflector";
322
- import { ModuleRef } from "@nestjs/core";
455
+ import { ModuleRef as ModuleRef2 } from "@nestjs/core";
323
456
  var reflector = new Reflector();
324
457
  var Metadata = new MetadataSetter();
325
458
  var defaultHeaderName = "x-server-token";
@@ -345,9 +478,10 @@ var TokenGuard = class {
345
478
  config.resolver || { paramType: "header", paramName: defaultHeaderName }
346
479
  );
347
480
  const tokenSource = config.tokenSource || defaultConfigName;
481
+ const req = context.switchToHttp().getRequest();
348
482
  const [tokenFromClient, tokenFromConfig] = await Promise.all([
349
- resolver(context, this.moduleRef),
350
- 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)
351
485
  ]);
352
486
  if (tokenFromConfig && tokenFromConfig !== tokenFromClient) {
353
487
  throw new BlankReturnMessageDto(
@@ -359,9 +493,9 @@ var TokenGuard = class {
359
493
  }
360
494
  };
361
495
  TokenGuard = __decorateClass([
362
- Injectable(),
363
- __decorateParam(0, Inject(ConfigService)),
364
- __decorateParam(1, Inject(ModuleRef))
496
+ Injectable2(),
497
+ __decorateParam(0, Inject2(ConfigService)),
498
+ __decorateParam(1, Inject2(ModuleRef2))
365
499
  ], TokenGuard);
366
500
  var RequireToken = (options = {}) => {
367
501
  const swaggerDec = options.resolver ? ApiFromResolver(options.resolver, {
@@ -385,7 +519,7 @@ var RequireToken = (options = {}) => {
385
519
 
386
520
  // src/abort-utils.ts
387
521
  import { Observable, takeUntil } from "rxjs";
388
- import { createParamDecorator } from "@nestjs/common";
522
+ import { createParamDecorator as createParamDecorator2 } from "@nestjs/common";
389
523
 
390
524
  // src/utility/abort-http-signal.ts
391
525
  function toRawReq(req) {
@@ -451,7 +585,7 @@ var takeUntilAbort = (signal) => {
451
585
  );
452
586
  };
453
587
  };
454
- var As = createParamDecorator(
588
+ var As = createParamDecorator2(
455
589
  (_data, ctx) => {
456
590
  const req = ctx.switchToHttp().getRequest();
457
591
  return createAbortSignalFromHttp(req);
@@ -459,37 +593,27 @@ var As = createParamDecorator(
459
593
  );
460
594
 
461
595
  // src/abortable-module/abortable.token.ts
462
- import { Inject as Inject3, Scope as Scope2 } from "@nestjs/common";
596
+ import { Inject as Inject4, Scope as Scope3 } from "@nestjs/common";
463
597
  import { abortable } from "nfkit";
464
598
 
465
599
  // src/abortable-module/abort-signal.provider.ts
466
- import { Inject as Inject2, Scope } from "@nestjs/common";
467
- import { REQUEST } from "@nestjs/core";
468
-
469
- // src/create-provider.ts
470
- var createProvider = (options, factory) => {
471
- return {
472
- useFactory: factory,
473
- ...options
474
- };
475
- };
476
-
477
- // 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";
478
602
  var ABORT_SIGNAL = Symbol(
479
603
  "ABORT_SIGNAL"
480
604
  );
481
605
  var AbortSignalProvider = createProvider(
482
606
  {
483
607
  provide: ABORT_SIGNAL,
484
- scope: Scope.REQUEST,
485
- inject: [REQUEST]
608
+ scope: Scope2.REQUEST,
609
+ inject: [REQUEST2]
486
610
  },
487
611
  createAbortSignalFromHttp
488
612
  );
489
- var InjectAbortSignal = () => Inject2(ABORT_SIGNAL);
613
+ var InjectAbortSignal = () => Inject3(ABORT_SIGNAL);
490
614
 
491
615
  // src/abortable-module/abortable.token.ts
492
- import { ContextIdFactory, ModuleRef as ModuleRef2, REQUEST as REQUEST2 } from "@nestjs/core";
616
+ import { ContextIdFactory, ModuleRef as ModuleRef3, REQUEST as REQUEST3 } from "@nestjs/core";
493
617
  var tokenMemo = /* @__PURE__ */ new Map();
494
618
  var abortableToken = (token) => {
495
619
  if (tokenMemo.has(token)) return tokenMemo.get(token);
@@ -510,7 +634,7 @@ function InjectAbortable(token) {
510
634
  );
511
635
  }
512
636
  }
513
- Inject3(abortableToken(actualToken))(target, propertyKey, parameterIndex);
637
+ Inject4(abortableToken(actualToken))(target, propertyKey, parameterIndex);
514
638
  };
515
639
  }
516
640
  function createAbortableProvider(token, opts) {
@@ -518,8 +642,8 @@ function createAbortableProvider(token, opts) {
518
642
  return createProvider(
519
643
  {
520
644
  provide,
521
- scope: Scope2.REQUEST,
522
- inject: [ModuleRef2, REQUEST2, ABORT_SIGNAL]
645
+ scope: Scope3.REQUEST,
646
+ inject: [ModuleRef3, REQUEST3, ABORT_SIGNAL]
523
647
  },
524
648
  async (moduleRef, req, signal) => {
525
649
  const ctxId = ContextIdFactory.getByRequest(req);
@@ -566,8 +690,8 @@ AbortableModule = __decorateClass([
566
690
  import {
567
691
  ConsoleLogger,
568
692
  HttpException as HttpException2,
569
- Inject as Inject4,
570
- Injectable as Injectable2
693
+ Inject as Inject5,
694
+ Injectable as Injectable3
571
695
  } from "@nestjs/common";
572
696
 
573
697
  // src/i18n-module/i18n-token.ts
@@ -576,7 +700,7 @@ var { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBu
576
700
  var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
577
701
 
578
702
  // src/i18n-module/i18n.service.ts
579
- import { ModuleRef as ModuleRef3 } from "@nestjs/core";
703
+ import { ModuleRef as ModuleRef4 } from "@nestjs/core";
580
704
  import { I18n } from "nfkit";
581
705
  var I18nService = class extends I18n {
582
706
  constructor(i18nServiceOptions, moduleRef) {
@@ -611,18 +735,20 @@ var I18nService = class extends I18n {
611
735
  return this;
612
736
  }
613
737
  async getExactLocaleFromRequest(ctx) {
614
- const locale = await this.resolver(ctx, this.moduleRef);
738
+ const req = ctx.switchToHttp().getRequest();
739
+ const locale = await this.resolver(req, this.moduleRef);
615
740
  return this.getExactLocale(locale);
616
741
  }
617
742
  async translateRequest(ctx, obj) {
618
- const locale = await this.resolver(ctx, this.moduleRef);
743
+ const req = ctx.switchToHttp().getRequest();
744
+ const locale = await this.resolver(req, this.moduleRef);
619
745
  return this.translate(locale, obj, ctx);
620
746
  }
621
747
  };
622
748
  I18nService = __decorateClass([
623
- Injectable2(),
624
- __decorateParam(0, Inject4(I18nModuleOptionsToken)),
625
- __decorateParam(1, Inject4(ModuleRef3))
749
+ Injectable3(),
750
+ __decorateParam(0, Inject5(I18nModuleOptionsToken)),
751
+ __decorateParam(1, Inject5(ModuleRef4))
626
752
  ], I18nService);
627
753
 
628
754
  // src/i18n-module/i18n.module.ts
@@ -630,11 +756,11 @@ import { Global, Module as Module2 } from "@nestjs/common";
630
756
 
631
757
  // src/i18n-module/locale.pipe.ts
632
758
  import {
633
- createParamDecorator as createParamDecorator2,
634
- Inject as Inject5,
635
- Injectable as Injectable3
759
+ createParamDecorator as createParamDecorator3,
760
+ Inject as Inject6,
761
+ Injectable as Injectable4
636
762
  } from "@nestjs/common";
637
- import { ModuleRef as ModuleRef4 } from "@nestjs/core";
763
+ import { ModuleRef as ModuleRef5 } from "@nestjs/core";
638
764
  var LocalePipe = class {
639
765
  constructor(i18nService, moduleRef) {
640
766
  this.i18nService = i18nService;
@@ -644,7 +770,8 @@ var LocalePipe = class {
644
770
  const resolver = ctx.resolver;
645
771
  if (resolver) {
646
772
  const _resolver = createResolver(resolver);
647
- const locale = await _resolver(ctx.ctx, this.moduleRef);
773
+ const req = ctx.ctx.switchToHttp().getRequest();
774
+ const locale = await _resolver(req, this.moduleRef);
648
775
  return this.i18nService.getExactLocale(locale);
649
776
  } else {
650
777
  return this.i18nService.getExactLocaleFromRequest(ctx.ctx);
@@ -652,13 +779,15 @@ var LocalePipe = class {
652
779
  }
653
780
  };
654
781
  LocalePipe = __decorateClass([
655
- Injectable3(),
656
- __decorateParam(0, Inject5(I18nService)),
657
- __decorateParam(1, Inject5(ModuleRef4))
782
+ Injectable4(),
783
+ __decorateParam(0, Inject6(I18nService)),
784
+ __decorateParam(1, Inject6(ModuleRef5))
658
785
  ], LocalePipe);
659
- var _dec = createParamDecorator2((resolver, ctx) => {
660
- return { ctx, resolver };
661
- });
786
+ var _dec = createParamDecorator3(
787
+ (resolver, ctx) => {
788
+ return { ctx, resolver };
789
+ }
790
+ );
662
791
  var PutLocale = (resolver) => _dec(resolver, LocalePipe);
663
792
 
664
793
  // src/i18n-module/i18n.module.ts
@@ -678,8 +807,8 @@ import { UseInterceptors } from "@nestjs/common";
678
807
  // src/i18n-module/i18n.interceptor.ts
679
808
  import {
680
809
  HttpException as HttpException3,
681
- Inject as Inject6,
682
- Injectable as Injectable4
810
+ Inject as Inject7,
811
+ Injectable as Injectable5
683
812
  } from "@nestjs/common";
684
813
  import { from, throwError } from "rxjs";
685
814
  import { catchError, mergeMap } from "rxjs/operators";
@@ -710,8 +839,8 @@ var I18nInterceptor = class {
710
839
  }
711
840
  };
712
841
  I18nInterceptor = __decorateClass([
713
- Injectable4(),
714
- __decorateParam(0, Inject6(I18nService))
842
+ Injectable5(),
843
+ __decorateParam(0, Inject7(I18nService))
715
844
  ], I18nInterceptor);
716
845
 
717
846
  // src/i18n-module/i18n-decorator.ts
@@ -756,6 +885,7 @@ export {
756
885
  As,
757
886
  BlankPaginatedReturnMessageDto,
758
887
  BlankReturnMessageDto,
888
+ CombinedParamResolver,
759
889
  DataBody,
760
890
  DataPipe,
761
891
  DataQuery,
@@ -775,6 +905,9 @@ export {
775
905
  MergeParameterDecorators,
776
906
  MergePropertyDecorators,
777
907
  PaginatedReturnMessageDto,
908
+ ParamResolver,
909
+ ParamResolverBase,
910
+ ParamResolverPipe,
778
911
  PutLocale,
779
912
  RenameClass,
780
913
  RequireToken,
@@ -790,6 +923,7 @@ export {
790
923
  fromAbortable,
791
924
  getApiProperty,
792
925
  getClassFromClassOrArray,
926
+ getParamResolver,
793
927
  takeUntilAbort
794
928
  };
795
929
  //# sourceMappingURL=index.mjs.map