nesties 1.1.15 → 1.1.17

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,41 @@ 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
257
+ var ParamResolverCopiedFieldsFromSwagger = [
258
+ "required",
259
+ "description",
260
+ "example",
261
+ "examples"
262
+ ];
240
263
  var coerceToString = (v) => {
241
264
  if (v == null) return void 0;
242
265
  if (v === false) return void 0;
@@ -281,45 +304,168 @@ function getQueryValue(req, key) {
281
304
  }
282
305
  return void 0;
283
306
  }
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);
307
+ var ParamResolverPipe = class {
308
+ constructor(moduleRef) {
309
+ this.moduleRef = moduleRef;
310
+ }
311
+ async transform(params, metadata) {
312
+ return params.resolver.resolve(params.req, this.moduleRef);
313
+ }
314
+ };
315
+ ParamResolverPipe = __decorateClass([
316
+ Injectable(),
317
+ __decorateParam(0, Inject(ModuleRef))
318
+ ], ParamResolverPipe);
319
+ var usedParamResolverTokens = /* @__PURE__ */ new Set();
320
+ var ParamResolverBase = class {
321
+ toResolverFunction() {
322
+ return async (req, ref) => this.resolve(req, ref);
323
+ }
324
+ toParamDecorator() {
325
+ const dec = createParamDecorator((_, ctx) => {
326
+ const req = ctx.switchToHttp().getRequest();
327
+ return { req, resolver: this };
328
+ });
329
+ return (...pipes) => dec(ParamResolverPipe, ...pipes);
330
+ }
331
+ // for override
332
+ toString() {
333
+ return "ParamResolverBase";
334
+ }
335
+ toRequestScopedProvider() {
336
+ const token = `PARAM_RESOLVER_${this.toString()}`;
337
+ let useToken = token;
338
+ if (usedParamResolverTokens.has(token)) {
339
+ let suffix = 0;
340
+ const tryToken = `${token}__${suffix}`;
341
+ while (usedParamResolverTokens.has(tryToken)) {
342
+ suffix++;
343
+ }
344
+ useToken = tryToken;
301
345
  }
302
- throw new Error(`Unsupported paramType: ${field}`);
303
- };
346
+ const provider = createProvider(
347
+ {
348
+ provide: useToken,
349
+ inject: [REQUEST, ModuleRef],
350
+ scope: Scope.REQUEST
351
+ },
352
+ this.toResolverFunction()
353
+ );
354
+ return {
355
+ token: useToken,
356
+ provider,
357
+ inject: () => Inject(useToken)
358
+ };
359
+ }
304
360
  };
305
- var ApiFromResolver = (_options, extras = {}) => {
306
- if (typeof _options === "function") {
307
- return () => {
361
+ var ParamResolver = class extends ParamResolverBase {
362
+ constructor(input) {
363
+ super();
364
+ if (typeof input === "function") {
365
+ this.dynamic = input;
366
+ } else {
367
+ this.info = { ...input };
368
+ if (this.info.paramType === "header") {
369
+ this.info.paramName = this.info.paramName.toLowerCase();
370
+ }
371
+ }
372
+ }
373
+ resolve(req, ref) {
374
+ if (this.info) {
375
+ if (this.info.paramType === "header") {
376
+ const name = this.info.paramName;
377
+ let raw = getHeader(req, name);
378
+ if (name === "accept-language")
379
+ raw = pickPrimaryFromAcceptLanguage(raw);
380
+ return raw;
381
+ }
382
+ if (this.info.paramType === "query") {
383
+ return getQueryValue(req, this.info.paramName);
384
+ }
385
+ throw new Error(`Unsupported paramType: ${this.info.paramType}`);
386
+ } else if (this.dynamic) {
387
+ return this.dynamic(req, ref);
388
+ }
389
+ }
390
+ toString() {
391
+ const suffix = this.info ? `${this.info.paramType.toUpperCase()}_${this.info.paramName}` : `DYNAMIC`;
392
+ return `ParamResolver_${suffix}`;
393
+ }
394
+ toApiPropertyDecorator(extras = {}) {
395
+ return (extras2 = {}) => {
396
+ if (this.info) {
397
+ const paramType = this.info.paramType;
398
+ const apiOptions = {
399
+ name: this.info.paramName,
400
+ ...ParamResolverCopiedFieldsFromSwagger.reduce((acc, field) => {
401
+ if (field in this.info) {
402
+ acc[field] = this.info[field];
403
+ }
404
+ return acc;
405
+ }, {}),
406
+ ...this.info.openapiExtras || {},
407
+ ...extras,
408
+ ...extras2
409
+ };
410
+ return paramType === "header" ? ApiHeader(apiOptions) : paramType === "query" ? ApiQuery({ type: "string", ...apiOptions }) : () => {
411
+ };
412
+ }
413
+ return () => {
414
+ };
308
415
  };
309
416
  }
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
- };
417
+ };
418
+ var CombinedParamResolver = class extends ParamResolverBase {
419
+ constructor(resolvers) {
420
+ super();
421
+ this.resolvers = resolvers;
422
+ }
423
+ async resolve(req, ref) {
424
+ const result = {};
425
+ await Promise.all(
426
+ Object.entries(this.resolvers).map(async ([key, resolver]) => {
427
+ result[key] = await resolver.resolve(
428
+ req,
429
+ ref
430
+ );
431
+ })
432
+ );
433
+ return result;
434
+ }
435
+ toString() {
436
+ const suffix = Object.entries(this.resolvers).map(([key, resolver]) => `${key.toString()}_${resolver.toString()}`).join("__");
437
+ return `CombinedParamResolver_${suffix}`;
438
+ }
439
+ toApiPropertyDecorator(extras = {}) {
440
+ const decs = Object.values(this.resolvers).map(
441
+ (resolver) => resolver.toApiPropertyDecorator(extras)
442
+ );
443
+ return (extras2) => MergeClassOrMethodDecorators(
444
+ decs.map(
445
+ (dec) => dec({
446
+ ...extras,
447
+ ...extras2
448
+ })
449
+ )
450
+ );
451
+ }
452
+ };
453
+ var getParamResolver = (input) => {
454
+ if (input instanceof ParamResolverBase) {
455
+ return input;
456
+ }
457
+ return new ParamResolver(input);
458
+ };
459
+ var createResolver = (_options) => {
460
+ return getParamResolver(_options).toResolverFunction();
461
+ };
462
+ var ApiFromResolver = (_options, extras = {}) => {
463
+ return getParamResolver(_options).toApiPropertyDecorator(extras)();
318
464
  };
319
465
 
320
466
  // src/token.guard.ts
321
467
  import { MetadataSetter, Reflector } from "typed-reflector";
322
- import { ModuleRef } from "@nestjs/core";
468
+ import { ModuleRef as ModuleRef2 } from "@nestjs/core";
323
469
  var reflector = new Reflector();
324
470
  var Metadata = new MetadataSetter();
325
471
  var defaultHeaderName = "x-server-token";
@@ -345,9 +491,10 @@ var TokenGuard = class {
345
491
  config.resolver || { paramType: "header", paramName: defaultHeaderName }
346
492
  );
347
493
  const tokenSource = config.tokenSource || defaultConfigName;
494
+ const req = context.switchToHttp().getRequest();
348
495
  const [tokenFromClient, tokenFromConfig] = await Promise.all([
349
- resolver(context, this.moduleRef),
350
- typeof tokenSource === "function" ? tokenSource(context, this.moduleRef) : this.config.get(tokenSource)
496
+ resolver(req, this.moduleRef),
497
+ typeof tokenSource === "function" ? tokenSource(req, this.moduleRef) : this.config.get(tokenSource)
351
498
  ]);
352
499
  if (tokenFromConfig && tokenFromConfig !== tokenFromClient) {
353
500
  throw new BlankReturnMessageDto(
@@ -359,9 +506,9 @@ var TokenGuard = class {
359
506
  }
360
507
  };
361
508
  TokenGuard = __decorateClass([
362
- Injectable(),
363
- __decorateParam(0, Inject(ConfigService)),
364
- __decorateParam(1, Inject(ModuleRef))
509
+ Injectable2(),
510
+ __decorateParam(0, Inject2(ConfigService)),
511
+ __decorateParam(1, Inject2(ModuleRef2))
365
512
  ], TokenGuard);
366
513
  var RequireToken = (options = {}) => {
367
514
  const swaggerDec = options.resolver ? ApiFromResolver(options.resolver, {
@@ -385,7 +532,7 @@ var RequireToken = (options = {}) => {
385
532
 
386
533
  // src/abort-utils.ts
387
534
  import { Observable, takeUntil } from "rxjs";
388
- import { createParamDecorator } from "@nestjs/common";
535
+ import { createParamDecorator as createParamDecorator2 } from "@nestjs/common";
389
536
 
390
537
  // src/utility/abort-http-signal.ts
391
538
  function toRawReq(req) {
@@ -451,7 +598,7 @@ var takeUntilAbort = (signal) => {
451
598
  );
452
599
  };
453
600
  };
454
- var As = createParamDecorator(
601
+ var As = createParamDecorator2(
455
602
  (_data, ctx) => {
456
603
  const req = ctx.switchToHttp().getRequest();
457
604
  return createAbortSignalFromHttp(req);
@@ -459,37 +606,27 @@ var As = createParamDecorator(
459
606
  );
460
607
 
461
608
  // src/abortable-module/abortable.token.ts
462
- import { Inject as Inject3, Scope as Scope2 } from "@nestjs/common";
609
+ import { Inject as Inject4, Scope as Scope3 } from "@nestjs/common";
463
610
  import { abortable } from "nfkit";
464
611
 
465
612
  // 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
613
+ import { Inject as Inject3, Scope as Scope2 } from "@nestjs/common";
614
+ import { REQUEST as REQUEST2 } from "@nestjs/core";
478
615
  var ABORT_SIGNAL = Symbol(
479
616
  "ABORT_SIGNAL"
480
617
  );
481
618
  var AbortSignalProvider = createProvider(
482
619
  {
483
620
  provide: ABORT_SIGNAL,
484
- scope: Scope.REQUEST,
485
- inject: [REQUEST]
621
+ scope: Scope2.REQUEST,
622
+ inject: [REQUEST2]
486
623
  },
487
624
  createAbortSignalFromHttp
488
625
  );
489
- var InjectAbortSignal = () => Inject2(ABORT_SIGNAL);
626
+ var InjectAbortSignal = () => Inject3(ABORT_SIGNAL);
490
627
 
491
628
  // src/abortable-module/abortable.token.ts
492
- import { ContextIdFactory, ModuleRef as ModuleRef2, REQUEST as REQUEST2 } from "@nestjs/core";
629
+ import { ContextIdFactory, ModuleRef as ModuleRef3, REQUEST as REQUEST3 } from "@nestjs/core";
493
630
  var tokenMemo = /* @__PURE__ */ new Map();
494
631
  var abortableToken = (token) => {
495
632
  if (tokenMemo.has(token)) return tokenMemo.get(token);
@@ -510,7 +647,7 @@ function InjectAbortable(token) {
510
647
  );
511
648
  }
512
649
  }
513
- Inject3(abortableToken(actualToken))(target, propertyKey, parameterIndex);
650
+ Inject4(abortableToken(actualToken))(target, propertyKey, parameterIndex);
514
651
  };
515
652
  }
516
653
  function createAbortableProvider(token, opts) {
@@ -518,8 +655,8 @@ function createAbortableProvider(token, opts) {
518
655
  return createProvider(
519
656
  {
520
657
  provide,
521
- scope: Scope2.REQUEST,
522
- inject: [ModuleRef2, REQUEST2, ABORT_SIGNAL]
658
+ scope: Scope3.REQUEST,
659
+ inject: [ModuleRef3, REQUEST3, ABORT_SIGNAL]
523
660
  },
524
661
  async (moduleRef, req, signal) => {
525
662
  const ctxId = ContextIdFactory.getByRequest(req);
@@ -566,8 +703,8 @@ AbortableModule = __decorateClass([
566
703
  import {
567
704
  ConsoleLogger,
568
705
  HttpException as HttpException2,
569
- Inject as Inject4,
570
- Injectable as Injectable2
706
+ Inject as Inject5,
707
+ Injectable as Injectable3
571
708
  } from "@nestjs/common";
572
709
 
573
710
  // src/i18n-module/i18n-token.ts
@@ -576,7 +713,7 @@ var { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBu
576
713
  var I18nModuleOptionsToken = MODULE_OPTIONS_TOKEN;
577
714
 
578
715
  // src/i18n-module/i18n.service.ts
579
- import { ModuleRef as ModuleRef3 } from "@nestjs/core";
716
+ import { ModuleRef as ModuleRef4 } from "@nestjs/core";
580
717
  import { I18n } from "nfkit";
581
718
  var I18nService = class extends I18n {
582
719
  constructor(i18nServiceOptions, moduleRef) {
@@ -611,18 +748,20 @@ var I18nService = class extends I18n {
611
748
  return this;
612
749
  }
613
750
  async getExactLocaleFromRequest(ctx) {
614
- const locale = await this.resolver(ctx, this.moduleRef);
751
+ const req = ctx.switchToHttp().getRequest();
752
+ const locale = await this.resolver(req, this.moduleRef);
615
753
  return this.getExactLocale(locale);
616
754
  }
617
755
  async translateRequest(ctx, obj) {
618
- const locale = await this.resolver(ctx, this.moduleRef);
756
+ const req = ctx.switchToHttp().getRequest();
757
+ const locale = await this.resolver(req, this.moduleRef);
619
758
  return this.translate(locale, obj, ctx);
620
759
  }
621
760
  };
622
761
  I18nService = __decorateClass([
623
- Injectable2(),
624
- __decorateParam(0, Inject4(I18nModuleOptionsToken)),
625
- __decorateParam(1, Inject4(ModuleRef3))
762
+ Injectable3(),
763
+ __decorateParam(0, Inject5(I18nModuleOptionsToken)),
764
+ __decorateParam(1, Inject5(ModuleRef4))
626
765
  ], I18nService);
627
766
 
628
767
  // src/i18n-module/i18n.module.ts
@@ -630,11 +769,11 @@ import { Global, Module as Module2 } from "@nestjs/common";
630
769
 
631
770
  // src/i18n-module/locale.pipe.ts
632
771
  import {
633
- createParamDecorator as createParamDecorator2,
634
- Inject as Inject5,
635
- Injectable as Injectable3
772
+ createParamDecorator as createParamDecorator3,
773
+ Inject as Inject6,
774
+ Injectable as Injectable4
636
775
  } from "@nestjs/common";
637
- import { ModuleRef as ModuleRef4 } from "@nestjs/core";
776
+ import { ModuleRef as ModuleRef5 } from "@nestjs/core";
638
777
  var LocalePipe = class {
639
778
  constructor(i18nService, moduleRef) {
640
779
  this.i18nService = i18nService;
@@ -644,7 +783,8 @@ var LocalePipe = class {
644
783
  const resolver = ctx.resolver;
645
784
  if (resolver) {
646
785
  const _resolver = createResolver(resolver);
647
- const locale = await _resolver(ctx.ctx, this.moduleRef);
786
+ const req = ctx.ctx.switchToHttp().getRequest();
787
+ const locale = await _resolver(req, this.moduleRef);
648
788
  return this.i18nService.getExactLocale(locale);
649
789
  } else {
650
790
  return this.i18nService.getExactLocaleFromRequest(ctx.ctx);
@@ -652,13 +792,15 @@ var LocalePipe = class {
652
792
  }
653
793
  };
654
794
  LocalePipe = __decorateClass([
655
- Injectable3(),
656
- __decorateParam(0, Inject5(I18nService)),
657
- __decorateParam(1, Inject5(ModuleRef4))
795
+ Injectable4(),
796
+ __decorateParam(0, Inject6(I18nService)),
797
+ __decorateParam(1, Inject6(ModuleRef5))
658
798
  ], LocalePipe);
659
- var _dec = createParamDecorator2((resolver, ctx) => {
660
- return { ctx, resolver };
661
- });
799
+ var _dec = createParamDecorator3(
800
+ (resolver, ctx) => {
801
+ return { ctx, resolver };
802
+ }
803
+ );
662
804
  var PutLocale = (resolver) => _dec(resolver, LocalePipe);
663
805
 
664
806
  // src/i18n-module/i18n.module.ts
@@ -678,8 +820,8 @@ import { UseInterceptors } from "@nestjs/common";
678
820
  // src/i18n-module/i18n.interceptor.ts
679
821
  import {
680
822
  HttpException as HttpException3,
681
- Inject as Inject6,
682
- Injectable as Injectable4
823
+ Inject as Inject7,
824
+ Injectable as Injectable5
683
825
  } from "@nestjs/common";
684
826
  import { from, throwError } from "rxjs";
685
827
  import { catchError, mergeMap } from "rxjs/operators";
@@ -710,8 +852,8 @@ var I18nInterceptor = class {
710
852
  }
711
853
  };
712
854
  I18nInterceptor = __decorateClass([
713
- Injectable4(),
714
- __decorateParam(0, Inject6(I18nService))
855
+ Injectable5(),
856
+ __decorateParam(0, Inject7(I18nService))
715
857
  ], I18nInterceptor);
716
858
 
717
859
  // src/i18n-module/i18n-decorator.ts
@@ -756,6 +898,7 @@ export {
756
898
  As,
757
899
  BlankPaginatedReturnMessageDto,
758
900
  BlankReturnMessageDto,
901
+ CombinedParamResolver,
759
902
  DataBody,
760
903
  DataPipe,
761
904
  DataQuery,
@@ -775,6 +918,9 @@ export {
775
918
  MergeParameterDecorators,
776
919
  MergePropertyDecorators,
777
920
  PaginatedReturnMessageDto,
921
+ ParamResolver,
922
+ ParamResolverBase,
923
+ ParamResolverPipe,
778
924
  PutLocale,
779
925
  RenameClass,
780
926
  RequireToken,
@@ -790,6 +936,7 @@ export {
790
936
  fromAbortable,
791
937
  getApiProperty,
792
938
  getClassFromClassOrArray,
939
+ getParamResolver,
793
940
  takeUntilAbort
794
941
  };
795
942
  //# sourceMappingURL=index.mjs.map