fastify-env icon indicating copy to clipboard operation
fastify-env copied to clipboard

README instructions seem to be wrong for TS

Open georgiosd opened this issue 2 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

Latest

Plugin version

Latest

Node.js version

18

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Description

For TS, the docs say that we just have to augment the fastify interface but I had to do more to get the plugin working without TS errors:

import FastifyEnv from '@fastify/env'
import { JSONSchemaType } from 'env-schema'

<redacted>

type Env = {
  JWT_SECRET: string
}

const envSchema: JSONSchemaType<Env> = {
  type: 'object',
  properties: {
    JWT_SECRET: {
      type: 'string',
    },
  },
  required: ['JWT_SECRET'],
  additionalProperties: false,
}

const app: FastifyPluginAsync<AppOptions> = async (
  fastify,
  opts
): Promise<void> => {
  await fastify.register(FastifyEnv, {
    schema: envSchema,
    dotenv: {
      path: path.join(__dirname, isProduction ? '.env' : '.env.development'),
      debug: isProduction === false,
    },
  })
 
  <redacted>
}

Without all this, either the runtime was complaining about the schema passed to the options, or TS was complaining about the overload used.

Also, there was nothing in the docs about how to copy the .env file to dist/ - I know it's not directly your responsibility but would be nice to add.

Steps to Reproduce

  • Create project with fastify-cli --lang=ts --esm
  • Add code from TS instructions

Expected Behavior

No response

georgiosd avatar Aug 31 '23 16:08 georgiosd

Thanks for reporting! Would you like to send a Pull Request to address this issue? It's likely just a docs issue.

mcollina avatar Sep 04 '23 08:09 mcollina

@mcollina it's on my TODO :)

georgiosd avatar Feb 03 '24 08:02 georgiosd