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

Env var not found: (getter|setter) #141

Closed
shrujalshah28 opened this issue Mar 28, 2021 · 1 comment
Closed

Env var not found: (getter|setter) #141

shrujalshah28 opened this issue Mar 28, 2021 · 1 comment

Comments

@shrujalshah28
Copy link

shrujalshah28 commented Mar 28, 2021

I am trying to decorate env variable with cleanEnv output in Fastify plugin.

const fastifyEnv: FastifyPluginAsync<{ env: Env }> = async (fastify, options) => {
  fastify.decorate('env', options.env);
};

Currently, this throws the following error in strict mode.

ReferenceError: [envalid] Env var not found: getter
        at Object.get ([...]\node_modules\envalid\dist\envalid.cjs.development.js:351:15)
        at decorate ([...]\node_modules\fastify\lib\decorate.js:30:24)
        at Object.decorateFastify [as decorate] ([...]\node_modules\fastify\lib\decorate.js:49:3)
        at fastifyEnv (file:///E:/Projects/shrujal/node-typescript-fastify/src/plugins/envalid.ts:13:11)
        at Plugin.exec ([...]\node_modules\avvio\plugin.js:131:17)
        at Boot.loadPlugin ([...]\node_modules\avvio\plugin.js:266:10)
        at [...]\node_modules\avvio\plugin.js:256:18

and

ReferenceError: [envalid] Env var not found: setter
        at Object.get ([...]\node_modules\envalid\dist\envalid.cjs.development.js:351:15)
        at decorate ([...]\node_modules\fastify\lib\decorate.js:30:59)
        at Object.decorateFastify [as decorate] ([...]\node_modules\fastify\lib\decorate.js:49:3)
        at fastifyEnv (file:///E:/Projects/shrujal/node-typescript-fastify/src/plugins/envalid.ts:13:11)
        at Plugin.exec ([...]\node_modules\avvio\plugin.js:131:17)
        at Boot.loadPlugin ([...]\node_modules\avvio\plugin.js:266:10)
        at [...]\node_modules\avvio\plugin.js:256:18

This is happening due to Fastify decorate try to check getter and setter property on the object.
https://github.com/fastify/fastify/blob/cc5440ba24de119516597356249aa625f58312bc/lib/decorate.js#L28-L36


Ref: #74, #76, #103, #104

@af
Copy link
Owner

af commented Apr 3, 2021

This behavior makes sense because of how envalid's strictProxyMiddleware() works. You can work around this by providing your own applyMiddleware() option that doesn't use the proxy middleware.

Alternately, you could insert your own forked version of the proxy middleware (allowing use of getter and setter) into the returned value of applyMiddleware()` if you want the rest of the proxy behavior. Hope that helps!

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