awslabs / aws-jwt-verify

JS library for verifying JWTs signed by Amazon Cognito, and any OIDC-compatible IDP that signs JWTs with RS256, RS384, and RS512
Apache License 2.0
606 stars 42 forks source link

Add exports of important types #42

Closed ottokruse closed 2 years ago

ottokruse commented 2 years ago

Issue #, if available: #40

Description of changes: Add exports of important types, so users to define variables separately from the making the CognitoJwtVerifier.create call, as requested in #40. [Since changing these types would probably be breaking anyway, as they're return values, I see no reason to not export them and let users use them if they want.]

Usage as follows:

import {
  CognitoJwtVerifierSingleUserPool,
  CognitoJwtVerifier, // Import this from the subnamespace now too, not from the top namespace (!)
} from "aws-jwt-verify/cognito-verifier";

// For the generic type parameter, provide the type of the object 
// that you will pass to the actual `create` call later:
let verifier: CognitoJwtVerifierSingleUserPool<{
  userPoolId: string;
  tokenUse: "id";
}>;

verifier = CognitoJwtVerifier.create({
  userPoolId: "<user pool id>",
  tokenUse: "id",
});

@elorzafe about your question. I'm slightly inclined to keep it like it is now: the type should be imported from the subnamespace "aws-jwt-verify/cognito-verifier", not from "aws-jwt-verify/cognito-verifier".

This keeps the top level namespace clean, which I like, and consider the type import above a bit more advanced, for which it would be okay to do the import from the subnamespace. The downside of this is, that it is harder to find that import. For me this tradeoff slightly favors cleanness of top level namespace. What do you think?

(Note there's multiple types/interfaces that would have to be available in top level namespace too. Would grow from 2 symbols to 10 symbols)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.