Installations
npm install @fastify/env
Score
94.2
Supply Chain
100
Quality
86.2
Maintenance
100
Vulnerability
100
License
Developer
fastify
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
20.8.0
NPM Version
10.1.0
Statistics
205 Stars
224 Commits
26 Forks
20 Watching
2 Branches
37 Contributors
Updated on 21 Nov 2024
Bundle Size
121.08 kB
Minified
35.81 kB
Minified + Gzipped
Languages
JavaScript (88.87%)
TypeScript (11.13%)
Total Downloads
Cumulative downloads
Total Downloads
2,616,533
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
8
@fastify/env
Fastify plugin to check environment variables
Install
npm i @fastify/env
Usage
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
Using @fastify/env to configure other plugins
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.- Await the plugin registration or await after()
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.
Typescript
In order to have typing for the fastify instance, you should either:
- use the
declaration merging
technique to enhance theFastifyInstance
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 }
- use the generic function
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.
Acknowledgements
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/fastify/.github/SECURITY.md:1
- Info: Found linked content: github.com/fastify/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/fastify/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/fastify/.github/SECURITY.md:1
Reason
SAST tool is not run on all commits -- score normalized to 6
Details
- Warn: 15 commits out of 23 are checked with a SAST tool
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
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
5.6
/10
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