Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace TS declaration merging with a generic function for getting the typed envs object #182

Closed
2 tasks done
edocrippaofficial opened this issue May 1, 2024 · 2 comments · Fixed by #183
Closed
2 tasks done

Comments

@edocrippaofficial
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

The doc suggests me to write my own declaration merging snippet to set the type of my envs object.

I would find more simple to just create a function getEnvs() for the FastifyInstance that returns the object. This function will be generic so the user can simply write something like:

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

and the plugin will take care of performing the declaration merging stuff for them.

The Envs type can also be generated with json-schema-to-ts so that it is always synchronized with the actual definition.

Of course this is not meant to be breaking since the users can still write their own properties for FastifyInstance and access them as they do now.

In addition, it would also be useful to have it as a FastifyRequest decorator, so that I can access my envs from within a handler with ease.

Motivation

I find more simple to use a generic function to retrieve the typed envs object than writing a declaration merging for the FastifyInstance.

Example

No response

@mcollina
Copy link
Member

mcollina commented May 2, 2024

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. We use tsd for TS

@edocrippaofficial
Copy link
Contributor Author

Yep sure 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants