nesties 1.1.21 → 1.1.23

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/README.md CHANGED
@@ -492,7 +492,7 @@ By composing multiple middlewares (dictionaries, database lookups, remote APIs),
492
492
 
493
493
  ### 8. ParamResolver
494
494
 
495
- `ParamResolver` and `CombinedParamResolver` provide a small, composable abstraction over headers and query parameters. They are used internally by `TokenGuard`, the i18n utilities, and can also be used directly in controllers, pipes, and guards.
495
+ `ParamResolver` provide a small, composable abstraction over headers and query parameters. They are used internally by `TokenGuard`, the i18n utilities, and can also be used directly in controllers, pipes, and guards.
496
496
 
497
497
  #### Static header / query resolvers
498
498
 
@@ -635,6 +635,119 @@ When used as a decorator, the combined resolver:
635
635
  - Returns a typed object where each key corresponds to the original resolver
636
636
  - Emits merged Swagger metadata for all headers / queries involved
637
637
 
638
+ #### Transforming resolved values with `TransformParamResolver`
639
+
640
+ Sometimes reading a header or query parameter is only the first step. You often want to **normalize**, **validate**, or **enrich** that value before it reaches your handler—especially when the transformation needs access to request-scoped dependencies via `ModuleRef`.
641
+
642
+ `TransformParamResolver` is a thin wrapper around any `ParamResolverBase<T>` that applies a function `T -> U`:
643
+
644
+ - It **reuses** the base resolver’s extraction logic (header/query/dynamic).
645
+ - It runs a **transform function** that can be sync or async.
646
+ - It receives `(value, moduleRef, req)` so you can:
647
+ - normalize formats (`'zh-hant' -> 'zh-Hant'`)
648
+ - parse primitives (`string -> number/boolean/date`)
649
+ - validate and throw `HttpException` / `BlankReturnMessageDto`
650
+ - hydrate values (e.g., `token -> user`) by resolving services from DI
651
+ - Swagger metadata is **inherited** from the base resolver, so the API contract stays accurate and not duplicated.
652
+
653
+ ##### Basic example: normalize a header value
654
+
655
+ ```ts
656
+ import { Controller, Get } from '@nestjs/common';
657
+ import { ParamResolver, TransformParamResolver } from 'nesties';
658
+
659
+ const rawLang = new ParamResolver({
660
+ paramType: 'header',
661
+ paramName: 'accept-language',
662
+ });
663
+
664
+ const normalizedLang = new TransformParamResolver(
665
+ rawLang,
666
+ (value) => {
667
+ if (!value) return undefined;
668
+ const v = value.split(',')[0]?.trim() ?? value;
669
+ const lower = v.toLowerCase();
670
+ if (lower === 'zh-hant' || lower === 'zh-tw') return 'zh-Hant';
671
+ if (lower === 'en-us') return 'en-US';
672
+ return v;
673
+ },
674
+ );
675
+
676
+ const Lang = normalizedLang.toParamDecorator();
677
+
678
+ @Controller()
679
+ export class LocaleController {
680
+ @Get('lang')
681
+ getLang(@Lang() lang: string | undefined) {
682
+ return { lang };
683
+ }
684
+ }
685
+ ```
686
+
687
+ ##### Advanced example: resolve a request-scoped service inside the transform
688
+
689
+ Because `TransformParamResolver` receives `ModuleRef` and `req`, you can resolve request-scoped providers using `ContextIdFactory.getByRequest(req)`.
690
+
691
+ ```ts
692
+ import { Injectable, Scope } from '@nestjs/common';
693
+ import { ContextIdFactory, ModuleRef } from '@nestjs/core';
694
+ import { ParamResolver, TransformParamResolver } from 'nesties';
695
+
696
+ @Injectable({ scope: Scope.REQUEST })
697
+ class LocaleService {
698
+ normalize(input?: string) {
699
+ if (!input) return undefined;
700
+ const lower = input.toLowerCase();
701
+ if (lower === 'zh-hant') return 'zh-Hant';
702
+ return input;
703
+ }
704
+ }
705
+
706
+ const rawLocale = new ParamResolver({
707
+ paramType: 'query',
708
+ paramName: 'locale',
709
+ });
710
+
711
+ const localeWithService = new TransformParamResolver(
712
+ rawLocale,
713
+ async (value, ref: ModuleRef, req) => {
714
+ const ctxId = ContextIdFactory.getByRequest(req);
715
+ const svc = await ref.resolve(LocaleService, ctxId, { strict: false });
716
+ return svc.normalize(value);
717
+ },
718
+ );
719
+ ```
720
+
721
+ ##### Composing multiple transforms (nesting)
722
+
723
+ `TransformParamResolver` can be used as the base resolver of another `TransformParamResolver`, forming a pipeline of transformations.
724
+
725
+ ```ts
726
+ import { TransformParamResolver } from 'nesties';
727
+
728
+ // rawLocale: ParamResolverBase<string | undefined>
729
+
730
+ // Step 1: normalize
731
+ const normalized = new TransformParamResolver(rawLocale, (v) =>
732
+ v ? v.trim() : undefined,
733
+ );
734
+
735
+ // Step 2: validate / coerce
736
+ const validated = new TransformParamResolver(normalized, (v) => {
737
+ if (!v) return 'en-US';
738
+ return v;
739
+ });
740
+
741
+ // validated resolves to string
742
+ const Locale = validated.toParamDecorator();
743
+ ```
744
+
745
+ ##### Notes
746
+
747
+ - `TransformParamResolver` does **not** change where the parameter comes from—only how the resolved value is shaped.
748
+ - Swagger decorators are inherited from the base resolver, so documentation remains consistent even when you compose multiple transforms.
749
+ - For multi-field inputs, you can first use `CombinedParamResolver`, then transform the combined object into a richer type (e.g., `{ lang, token } -> { locale, user }`).
750
+
638
751
  #### Request-scoped providers from resolvers
639
752
 
640
753
  Sometimes you want to treat the resolved value itself as an injectable request-scoped provider. You can derive such a provider from any `ParamResolver` or `CombinedParamResolver` using `toRequestScopedProvider()`: