Comments (13)
Try to import jwks-rsa as default : import jwksRsa from "jwks-rsa";
I can confirm this works with tsc test.ts --esModuleInterop
:
import jwksRsa from "jwks-rsa";
console.info(jwksRsa({jwksUri: ""}));
But it would be nice if it worked normally.
The following fails despite compiling:
import {JwksClient} from "jwks-rsa";
console.info(new JwksClient({jwksUri: ""}));
This works but violates common linting rules and has high wtf/min in code reviews: 😁
import * as jwks from "jwks-rsa";
import {ClientOptions, JwksClient} from "jwks-rsa";
const createClient = (jwks as unknown) as (options: ClientOptions) => JwksClient;
console.info(createClient({jwksUri: ""}));
I think the problem is this in your index.d.ts:
declare function JwksRsa(options: JwksRsa.ClientOptions): JwksRsa.JwksClient;
declare namespace JwksRsa {
class JwksClient {
constructor(options: ClientOptions);
}
}
export = JwksRsa;
Having a duplicate identifier seems strange, and the class with constructor probably doesn't match reality since calling the constructor throws a TypeError
that it isn't a constructor.
from node-jwks-rsa.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇♂️
from node-jwks-rsa.
Having read up on the issue again some more with a clearer head, I agree this ticket can't really go anywhere new, @adamjmcgrath . The problem is talked about plenty online, but there seem to be no "clever tricks" or anything aside from making a hybrid library with multiple exposed files to import from (each with a different module system). esModuleInterop
is simply the way to go with any commonjs being imported into es6. Thanks for your input and patience. :)
from node-jwks-rsa.
import * as jwks from "jwks-rsa"; requires enabling allowSyntheticDefaultImports if I'm not mistaken. It's not as bad as moduleInterop, but still configuration creep.
This works for me with zero config, see https://repl.it/@AdamMcGrath/ProbableHomelyMonitor
I don't think there's really a question about what the technical issue is anymore, it's more about what can be done to improve UX (i.e. make jwks-rsa all-compatible)
We do the same as express (and probably koa, hapi, etc..). I don't think we can do more than that (without converting the library itself to es modules)
jwks-rsa
declare function JwksRsa(options: JwksRsa.ClientOptions): JwksRsa.JwksClient;
declare namespace JwksRsa {
class JwksClient { ... }
...
}
export = JwksRsa;
https://github.com/auth0/node-jwks-rsa/blob/master/index.d.ts
@types/express
declare function e(): core.Express;
declare namespace e {
var json: typeof bodyParser.json;
...
}
export = e;
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts
from node-jwks-rsa.
Thanks @adamjmcgrath and @leaumar for working this one out.
from node-jwks-rsa.
bump
from node-jwks-rsa.
Try to import jwks-rsa as default : import jwksRsa from "jwks-rsa";
from node-jwks-rsa.
Using esModuleInterop cannot be considered a solution because it creates incompatibilities with other libraries. I've used the workaround proposed by @leaumar but, as they say, it has high wtf/min reaction.
from node-jwks-rsa.
Hi @llienard @leaumar @nicolaracco
This library exports commonjs. In order to model this in typescript we've used export =
.
When exporting a module using export =
, TypeScript-specific import module = require("module")
should be used to import the module (see: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require). So the following would work:
import jwksRsa = require("jwks-rsa");
console.info(jwksRsa({jwksUri: ""}));
If you want to have this common js library interoperate with es modules (eg you want to use it with a default import) - then you'll need to use the esModuleInterop
flag.
Having a duplicate identifier seems strange
This is expected, have a look at the function with extra fields example in https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html
from node-jwks-rsa.
@adamjmcgrath
Using require
violates my project's rules:
TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
ESLint: Arequire()
style import is forbidden.(@typescript-eslint/no-require-imports)
Is there no change/addition you could make to the library that would be fully backward compatible (obviously) but also allow it to work hassle-free (including no compatibility settings like esModuleInterop
) in the type of projects it currently doesn't work in? This is the only library I have this issue with. I'm not in a position to offer the solution myself because I'm not a pro on the whole module ecosystem and compatibility tricks.
from node-jwks-rsa.
@adamjmcgrath Using esModuleInterop
is not standard nor recommended in typescript. Using it creates problems with other libraries (in my case I was already trying with esModuleInterop
and got errors when using sharp if I recall correctly).
from node-jwks-rsa.
This library is not an ES module, it's a CJS module, so if you want to use it like an ES Module with a default import import jwksRsa from "jwks-rsa";
- you need to use esModuleInterop
@adamjmcgrath Using esModuleInterop is not standard nor recommended in typescript.
I'm not aware of any recommendation from typescript against the use of esModuleInterop
or any TypeScript standards that don't include it, but I'd be happy to take a look at any.
If you don't want to use esModuleInterop
you can try:
import * as jwks from "jwks-rsa";
console.log(jwks({jwksUri: ""}));
This is the only library I have this issue with.
@leaumar - are you able to share your project? Do you use express, hapi or koa? They're CJS modules and I'd be interested to know how you import them
from node-jwks-rsa.
I checked the work project that made me reply on this thread, someone has enabled moduleInterop there in the meantime to stop having these problems (with koa and hapi indeed).
In some private projects, where I want stricter rules, I got around the issue by... finding alternative libraries for the few that gave me this.
import * as jwks from "jwks-rsa";
requires enabling allowSyntheticDefaultImports
if I'm not mistaken. It's not as bad as moduleInterop, but still configuration creep.
I don't think there's really a question about what the technical issue is anymore, it's more about what can be done to improve UX (i.e. make jwks-rsa all-compatible):
Is there no change/addition you could make to the library that would be fully backward compatible (obviously) but also allow it to work hassle-free (including no compatibility settings like esModuleInterop) in the type of projects it currently doesn't work in?
^ honest question
Edit: I think it's safe to say "this kind of projects" = vanilla es6 imports? The goal for me at least is to use all my libs with vanilla config, no special settings just for any library.
Sorry if my answers are maybe getting inconsistent, my attention is spread over twice as many projects as I have fingers, so yeah... I've said everything I had to add. :)
from node-jwks-rsa.
Related Issues (20)
- Types for JwksClient.getSigningKey still allows callbacks HOT 2
- cache doesn't work for the expressJwtSecret function HOT 3
- Types conflict between [email protected] and [email protected] HOT 2
- FIX BUG TYPES WITH TYPESCRIPT AND AUTH 0 HOT 1
- The JWKS endpoint did not contain any signing keys HOT 2
- cb is not a function HOT 2
- Add pre-fetch keys / tweaks to caching HOT 2
- strictSsl property not available jwksRsa.hapiJwt2KeyAsync HOT 2
- Please upgrade dependencies HOT 5
- I can't login to my wallet I tried everything else but it is hopeless 😔 I hope you can understand that I created Bitcoin with satoshi nakamoto in 2008
- error in secret or public key callback: The JWKS endpoint did not contain any signing keys HOT 3
- Consider outputting ESM HOT 2
- types referred in dependencies section of package json HOT 2
- Make jwks-rsa resilient in the face of inability to access the underlying JWKS HOT 1
- Provide a way to prevent `getKeysInterceptor` falling back to `jwksUri` when the result doesn't contain the `kid` HOT 2
- No support for Cloudflare Workers HOT 3
- Can't match types definition in @types/[email protected] HOT 2
- error TS2688: Cannot find type definition file for 'express-unless'.
- Bump jose to v5 HOT 3
- Add module-info.java
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-jwks-rsa.