fastify / env-schema

Validate your env variable using Ajv and dotenv
MIT License
212 stars 25 forks source link

docs: adding reference guide #144

Closed lirantal closed 1 year ago

lirantal commented 1 year ago

Adding a section to list helpful resource guides to get started with building with env-schema

Fixes #142

Checklist

mcollina commented 1 year ago

Could you split creation of the Fastify application with the call to listen in two files, as this enables testing without using globals?

lirantal commented 1 year ago

Sure thing. I was going for simplicity there but have merged a PR to change that to a more modular structure: https://github.com/lirantal/fastify-dotenv-envschema-example/pull/1 (landed in main already)

lirantal commented 1 year ago

It's a good question and I was asking myself the same thing but I thought the ideal place is here because folks using the @fastify/env plugin would probably already know best practices and how to use it, yet new folks landing on this repository might look for how to get started and how they can use env-schema, especially since it doesn't have anything to do with Fastify as the app framework itself, but just happens to live on this org.

So, I'd keep it here to be more helpful but happy to move elsewhere if you think it'd be a better fit.

mcollina commented 1 year ago

I personally find env-schema better and I don't use fastify-env ;).

lirantal commented 1 year ago

fastify-env is pretty small and is anyway just a wrapper around env-schema though, isn't it? I figured if you don't use fastify-env you probably just do the same plugging wrapping yourself.

mcollina commented 1 year ago

I tend to not wrap this into a plugin, but rather read it before Fastify is started. In this way I can customize the options of Fastify itself

jsumners commented 1 year ago

I tend to not wrap this into a plugin, but rather read it before Fastify is started. In this way I can customize the options of Fastify itself

Agreed. I really don't understand the point of fastify-env.

'use strict'

const dotenv = require('dotenv')
const config = dotenv.config()

const fastify = require('fastify')({ /* use stuff from config */})
fastify.decorate('env', {
  getter() { return config }
})