Gathering detailed insights and metrics for @fastify/env
Gathering detailed insights and metrics for @fastify/env
Gathering detailed insights and metrics for @fastify/env
Gathering detailed insights and metrics for @fastify/env
npm install @fastify/env
94.2
Supply Chain
100
Quality
86.2
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
205 Stars
224 Commits
26 Forks
20 Watching
2 Branches
37 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (88.87%)
TypeScript (11.13%)
Cumulative downloads
Total Downloads
Last day
-6.2%
6,903
Compared to previous day
Last week
7.2%
41,747
Compared to previous week
Last month
5.1%
170,834
Compared to previous month
Last year
86.6%
1,581,489
Compared to previous year
2
8
Fastify plugin to check environment variables
npm i @fastify/env
1const fastify = require('fastify')() 2const fastifyEnv = require('@fastify/env') 3 4const schema = { 5 type: 'object', 6 required: [ 'PORT' ], 7 properties: { 8 PORT: { 9 type: 'string', 10 default: 3000 11 } 12 } 13} 14 15const options = { 16 confKey: 'config', // optional, default: 'config' 17 schema: schema, 18 data: data // optional, default: process.env 19} 20 21fastify 22 .register(fastifyEnv, options) 23 .ready((err) => { 24 if (err) console.error(err) 25 26 console.log(fastify.config) // or fastify[options.confKey] 27 console.log(fastify.getEnvs()) 28 // output: { PORT: 3000 } 29 })
You can also use the function getEnvs()
of the Request from within a handler function:
1fastify.get('/', (request, reply) => { 2 console.log(request.getEnvs()) 3 // output: { PORT: 3000 } 4})
Note that the getEnvs
decorators will not be added if they already exist.
This module is a wrapper around env-schema.
To read an .env
file you must set dotenv
in the options:
1const options = { 2 dotenv: true // will read .env in root folder 3} 4 5// or, pass config options available on dotenv module 6const options = { 7 dotenv: { 8 path: `${__dirname}/.env`, 9 debug: true 10 } 11} 12
The @fastify/env
plugin loads asynchronously. If you wish to use its values in a different plugin before the boot sequence, you need to make sure that:
@fastify/env
is registered first.1await fastify.register(fastifyEnv) 2// fastify.config can be used in here
OR
1fastify.register(fastifyEnv) 2await fastify 3// fastify.config can be used in here
NB Support for additional properties in the schema is disabled for this plugin, with the additionalProperties
flag set to false
internally.
In order to have typing for the fastify instance, you should either:
declaration merging
technique to enhance the FastifyInstance
type with the property and its keys you have defined in the options:1declare module 'fastify' { 2 interface FastifyInstance { 3 config: { // this should be same as the confKey in options 4 // specify your typing here 5 FOO: string 6 }; 7 } 8} 9 10const fastify = Fastify() 11fastify.register(fastifyEnv) 12 13fastify.config.FOO // will be a string 14fastify.config.BAR // error: Property BAR does not exist on type { FOO: string }
getEnvs()
to get the already typed object:1type Envs = { 2 FOO: string 3} 4 5const fastify = Fastify() 6await fastify.register(fastifyEnv) 7 8const envs = fastify.getEnvs<Envs>() // envs will be of type Envs 9 10envs.FOO // will be a string 11envs.BAR // error: Property BAR does not exist on type Envs
If this is the case it is suggested to use json-schema-to-ts to have the type always synchronized with the actual schema.
Kindly sponsored by Mia Platform
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 6
Details
Reason
7 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 8/19 approved changesets -- score normalized to 4
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More