-
Notifications
You must be signed in to change notification settings - Fork 0
implemented namespaced config #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * @file inject-config.decorator.ts | ||
| * @description | ||
| * Parameter decorator for injecting a typed namespace config slice into | ||
| * NestJS constructors. | ||
| * | ||
| * Usage: | ||
| * ```typescript | ||
| * constructor( | ||
| * @InjectConfig('auth') private cfg: z.output<typeof authConfig.definition.schema> | ||
| * ) {} | ||
| * ``` | ||
| * | ||
| * Under the hood it is just `@Inject(getNamespaceToken(namespace))` — a thin | ||
| * wrapper so consumers never have to know about the internal token format. | ||
| * | ||
| * Contents: | ||
| * - InjectConfig() — parameter decorator factory | ||
| */ | ||
|
|
||
| import { Inject } from "@nestjs/common"; | ||
|
|
||
| import { getNamespaceToken } from "@/constants"; | ||
|
|
||
| // ───────────────────────────────────────────────────────────────────────────── | ||
| // @InjectConfig | ||
| // ───────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| /** | ||
| * Parameter decorator that injects the validated config slice for a namespace. | ||
| * | ||
| * Must be used in constructors of NestJS providers (services, controllers, etc.) | ||
| * inside a module that has imported ConfigModule and added the corresponding | ||
| * `NamespacedConfig.asProvider()` to its `providers` array. | ||
| * | ||
| * The injected value is a frozen, fully-typed object — the Zod output of the | ||
| * schema passed to `defineNamespace()`. No `string | undefined` values. | ||
| * | ||
| * @param namespace - The namespace name used in `defineNamespace(namespace, schema)`. | ||
| * Must match exactly (case-sensitive). | ||
| * @returns A NestJS `@Inject()` parameter decorator bound to the namespace token. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // auth/auth.config.ts | ||
| * export const authConfig = defineNamespace('auth', z.object({ | ||
| * JWT_SECRET: z.string().min(32), | ||
| * JWT_EXPIRES_IN: z.string().default('7d'), | ||
| * })); | ||
| * | ||
| * // auth/auth.module.ts | ||
| * @Module({ providers: [authConfig.asProvider(), AuthService] }) | ||
| * export class AuthModule {} | ||
| * | ||
| * // auth/auth.service.ts | ||
| * @Injectable() | ||
| * export class AuthService { | ||
| * constructor( | ||
| * // Injects the validated { JWT_SECRET: string, JWT_EXPIRES_IN: string } object | ||
| * @InjectConfig('auth') private cfg: z.output<typeof authConfig.definition.schema> | ||
| * ) {} | ||
| * | ||
| * getSecret(): string { | ||
| * return this.cfg.JWT_SECRET; // string — never undefined | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function InjectConfig(namespace: string): ParameterDecorator { | ||
| // Resolve the namespace to its unique DI token and delegate to NestJS @Inject | ||
| return Inject(getNamespaceToken(namespace)); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The namespace registry is created as
new Set<string>()perConfigModule.register()/registerAsync()call. That means duplicate namespace detection only works within the scope of a single ConfigModule instance; importing ConfigModule multiple times will create multiple registries and allow duplicate namespaces across module graphs. If app-wide uniqueness is required, consider using a shared singleton registry (or enforce a single ConfigModule import) and document the expected usage.