@ fastify/env (法语)
快速检查环境变量的插件
安装
npm i @fastify/env
兼容性
| 插件版本 | 快速化版本 |
|---|---|
>=5.x | ^5.x |
^4.x | ^4.x |
>=2.x <4.x | ^3.x |
>=0.x 2.x | ^2.x |
>=0.x 2.x | ^1.x |
请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 快速化 LTS 政策 更多细节.
使用量
const fastify = require('fastify')()
const fastifyEnv = require('@fastify/env')
const schema = {
type: 'object',
required: [ 'PORT' ],
properties: {
PORT: {
type: 'string',
default: 3000
}
}
}
const options = {
confKey: 'config', // optional, default: 'config'
schema: schema,
data: data // optional, default: process.env
}
fastify
.register(fastifyEnv, options)
.ready((err) => {
if (err) console.error(err)
console.log(fastify.config) // or fastify[options.confKey]
console.log(fastify.getEnvs())
// output: { PORT: 3000 }
})
您也可以使用此函数 getEnvs() 从处理器函数内获取的请求:
fastify.get('/', (request, reply) => {
console.log(request.getEnvs())
// output: { PORT: 3000 }
})
注意 getEnvs 如果装饰已经存在, 则不会添加装饰 .
此模块是一个包装器周围 环境图。 。 。 .
读一个 .env 您必须设置的文件 dotenv 选项中:
const options = {
dotenv: true // will read .env in root folder
}
// or, pass config options supported by env-schema
const options = {
dotenv: {
path: `${__dirname}/.env`,
encoding: 'utf8'
}
}
格式和自定义 Ajv 选项
@fastify/env 以其 拥有 Ajv 实例 (以千美元计) 环境图),独立于路线的快用.
在快捷实例上配置的格式或选项( E)Fastify({ ajv: ... })),或来自类型提供者,如TypeBox,是 没有 应用到环境图中,所以使用 format 关键词结果如下:
unknown format "uuid" ignored in schema at path "#/properties/ID"
用插件的 Ajv 实例注册格式 ajv.customOptions 选项,它可以是 Ajv 选项对象,也可以是接收并返回 Ajv 实例的函数.
用于标准格式 ajv 格式 (单位:千美元)npm i ajv-formats,它不是此插件的依赖性 :
await fastify.register(fastifyEnv, {
schema: {
type: 'object',
required: ['ID'],
properties: {
ID: { type: 'string', format: 'uuid' }
}
},
ajv: {
customOptions (ajvInstance) {
require('ajv-formats')(ajvInstance)
// or: ajvInstance.addFormat('myFormat', (value) => /* ... */)
return ajvInstance
}
}
})
与 TypeBox (1.x,即:. @fastify/type-provider-typebox QQ 6 您可以再使用 TypeBox 中包含的格式 :
import { Format, Type } from '@fastify/type-provider-typebox'
await fastify.register(fastifyEnv, {
schema: Type.Object({
ID: Type.String({ format: 'uuid' })
}),
ajv: {
customOptions (ajvInstance) {
for (const entry of Format.Entries()) {
ajvInstance.addFormat(...entry)
}
// Or if you only want to register a specific format:
// ajvInstance.addFormat('uuid', Format.Get('uuid'))
return ajvInstance
}
}
})
见 env-sema 文档 所有支持的 Ajv 选项.
使用 @ fastify/ env 配置其他插件
这个 @fastify/env 插件同步加载。 如果您想要在启动序列前在不同的插件中使用其值,则需要确保:
@fastify/env首先登记.- 等待插件注册或等待后 ()
await fastify.register(fastifyEnv)
// fastify.config can be used in here
或
fastify.register(fastifyEnv)
await fastify
// fastify.config can be used in here
编号 用于此插件的额外属性支持已禁用, 其间 additionalProperties 设置为 false 内部.
字体
要输入封存实例,请:
- 使用该
declaration merging技术,加强FastifyInstance在选项中定义的属性及其密钥类型 :
declare module 'fastify' {
interface FastifyInstance {
config: { // this should be the same as the confKey in options
// specify your typing here
FOO: string
};
}
}
const fastify = Fastify()
fastify.register(fastifyEnv)
fastify.config.FOO // will be a string
fastify.config.BAR // error: Property BAR does not exist on type { FOO: string }
- 使用通用函数
getEnvs()以获取已输入的对象 :
type Envs = {
FOO: string
}
const fastify = Fastify()
await fastify.register(fastifyEnv)
const envs = fastify.getEnvs<Envs>() // envs will be of type Envs
envs.FOO // will be a string
envs.BAR // error: Property BAR does not exist on type Envs
如果是这样,建议使用 json-schema-to-ts (日语) 使该类型始终与实际计划同步.
承认
请赞助人: 米娅平台。 。 。 .
许可证
许可证根据 麻省理工学院。 。 。 .