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

Fastify-auth0-verify with Typescript #354

Closed
lleonori opened this issue May 16, 2024 · 10 comments · Fixed by #357
Closed

Fastify-auth0-verify with Typescript #354

lleonori opened this issue May 16, 2024 · 10 comments · Fixed by #357
Assignees

Comments

@lleonori
Copy link

lleonori commented May 16, 2024

I use this plugin with Typescript, and when I register it:

import fastifyAuth0Verifiy from "fastify-auth0-verify";

app.register(fastifyAuth0Verifiy, {
    domain: process.env.domain,
    secret: process.env.secret,
});

the application still run, but typescript notify an error:
https://pastebin.com/WeN1hbPm

The export default is present inside the index.d.ts and I use it:

export default fastifyAuth0Verify

@ilteoood
Copy link
Contributor

Can you provide a repro?

https://stackoverflow.com/help/minimal-reproducible-example

@simoneb
Copy link
Member

simoneb commented May 16, 2024

@lleonori please check #355. I added (or actually, modified) a test to cover exactly your case. Unless we're doing something wrong with the types, it should work fine.

@lleonori
Copy link
Author

@ilteoood I try to provide a repro, and it's so strange, there aren't any errors.
https://github.com/lleonori/repro-fastify-auth0

@simoneb LGTM

Maybe there are some library conflict?
For verify this case I link my side project, if is not a problem.
https://github.com/lleonori/gymnasium-fastify

@simoneb
Copy link
Member

simoneb commented May 17, 2024

Can you show us the error?

@lleonori
Copy link
Author

lleonori commented May 17, 2024

import fastifyAuth0Verifiy from "fastify-auth0-verify";

app.register(fastifyAuth0Verifiy, {
// domain: process.env.DOMAIN,
// secret: process.env.SECRET,
});

this is the line and the error is https://pastebin.com/WeN1hbPm

@faizplus faizplus self-assigned this May 17, 2024
@faizplus
Copy link
Contributor

The Problem

@lleonori This is a problem with your typescript configuration and its type compatibility with fastify-auth0-verify as you are using "module": "NodeNext" and "moduleResolution": "NodeNext"

https://github.com/lleonori/gymnasium-fastify/blob/1d62511bbc95b09ace1ce4f9852b1100abc5cac7/tsconfig.json#L28-L30

While fastify-auth0-verify is setup for "target": "es6", "module": "commonjs",

"target": "es6",
"module": "commonjs",

The Solution

There are two ways that I can think of to solve this problem if I don't want to rewrite this package in esmodule from commonjs.

  1. Use dynamic import inside your async function
    import type { FastifyPluginCallback } from "fastify";
    import { FastifyAuth0VerifyOptions } from "fastify-auth0-verify";
    ...
    
    const fastifyAuth0Verifiy = (await import("fastify-auth0-verify"))
       .default as unknown as FastifyPluginCallback<FastifyAuth0VerifyOptions>;
  2. Change your tsconfig.json's module and moduleResolution to
    {
       "module": "CommonJS",
       "moduleResolution": "Node"
    }
    Note: 2nd option will break your code as few things will not work like import.meta.url and you will need to change this to their commonjs counterparts

@simoneb
Copy link
Member

simoneb commented May 17, 2024

@faizplus shall our code not be compatible with a standard TS setup instead?

@faizplus
Copy link
Contributor

@simoneb definitely the code should be compatible, I will try to find a way to make it work. I have provided quick solutions only.

@ilteoood
Copy link
Contributor

What about using tsup to compile to both esm and cjs?
IMO it would be a good strategy to slowly making esm a first class citizen.

@simoneb
Copy link
Member

simoneb commented May 17, 2024

I'm sure there must be a way to expose CJS code in a way that's usable from ESM/TS without much effort.

faizplus added a commit that referenced this issue May 20, 2024
Fixes: #354
Updated module export statements & type declarations inspired from fastify-jwt
faizplus added a commit that referenced this issue May 20, 2024
Fixes: #354
Updated module export statements & type declarations inspired from fastify-jwt
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.

4 participants