@abinnovision/nestjs-configx (英语)
使用 NestJS 的类型安全配置 标准Schema。 。 。 .
安装
yarn add @abinnovision/nestjs-configx
目标
- 类型安全配置从
process.env - 通过标准Schema(Zod, ArkType等)进行Schema-agnostic
- 最小的 NestJS 集成( 仅仅是一个提供者, 没有模块)
- 启动时失败快速验证
非目标
- 多个配置来源( 至少还没有)
- Async 计划验证
- 运行时配置重装
- 保密管理/加密
快速启动
1. 定义您的配置
import { configx } from "@abinnovision/nestjs-configx";
import { z } from "zod";
export class AppConfigx extends configx(
z.object({
PORT: z.string().default("3000").transform(Number),
HOST: z.string().default("0.0.0.0"),
}),
) {}
2. 登记为提供者
import { Module } from "@nestjs/common";
import { AppConfigx } from "./app.configx";
@Module({
providers: [AppConfigx],
exports: [AppConfigx],
})
export class AppModule {}
3. 注射和使用
@Injectable()
export class AppService {
constructor(private readonly config: AppConfigx) {}
getPort() {
return this.config.PORT; // number - fully typed
}
}
Schema 图书馆
任意 标准Schema 兼容的图书馆工作。 试验过:
ArkType 实例
import { configx } from "@abinnovision/nestjs-configx";
import { type } from "arktype";
export class AppConfigx extends configx(
type({
PORT: "string.numeric.parse = '3000'",
HOST: "string = '0.0.0.0'",
}),
) {}
处理错误
无效的配置抛出 InvalidConfigError 在即时提示中, 详细了解哪些字段验证失败 .
许可证
阿帕奇 - 2.0