amplify-js icon indicating copy to clipboard operation
amplify-js copied to clipboard

Better typings

Open jordanmonier opened this issue 4 years ago • 6 comments

Is this related to a new or existing framework?

No response

Is this related to a new or existing API?

Authentication

Is this related to another service?

No response

Describe the feature you'd like to request

The typings aren't correct, they only have to methods and they often returns any. Maybe we are missing something but in a Typescript environment this is very painful to deal with.

Describe the solution you'd like

Includes all the property in the class declaration.

export class CognitoUserPool {
	constructor(
		data: ICognitoUserPoolData,
		wrapRefreshSessionCallback?: (
			target: NodeCallback.Any
		) => NodeCallback.Any
	);

        ...
       
        public advancedSecurityDataCollectionFlag: boolean
        public clientId: string
        public userPoolId: string

	...
}

Describe alternatives you've considered

Declaring our own typings for @aws-amplify/auth

Additional context

No response

Is this something that you'd be interested in working on?

  • [X] 👋 I may be able to implement this feature request
  • [ ] ⚠️ This feature might incur a breaking change

jordanmonier avatar Sep 30 '21 12:09 jordanmonier

I just wrote some types if you want to use them :

declare module 'amazon-cognito-identity-js' {
  enum AuthenticationFlowType {
    USER_SRP_AUTH = 'USER_SRP_AUTH',
    USER_PASSWORD_AUTH = 'USER_PASSWORD_AUTH',
    CUSTOM_AUTH = 'CUSTOM_AUTH',
  }

  interface CognitoUser {
    attributes: {
      email: string
      email_verified: true

      family_name: string
      given_name: string
      phone_number: string
      phone_number_verified: boolean

      sub: string
    }

    pool: CognitoUserPool

    authenticationFlowType: AuthenticationFlowType
    keyPrefix: string
    signInUserSession: CognitoUserSession
    userDataKey: 'CognitoIdentityServiceProvider'
    username: string
  }

  interface CognitoUserPool {
    advancedSecurityDataCollectionFlag: boolean
    clientId: string
    userPoolId: string
  }

  interface CognitoUserSession {
    clockDrift: number
    idToken: CognitoIdToken
    accessToken: CognitoAccessToken
    refreshToken: CognitoRefreshToken
  }

  interface CognitoIdToken {
    jwtToken: string
    payload: {
      sub: string
      email_verified: boolean
      iss: string
      phone_number_verified: boolean
      'cognito:username': string
      given_name: string
      origin_jti: string
      aud: string
      event_id: string
      token_use: string
      auth_time: number
      phone_number: string
      exp: number
      iat: number
      family_name: 'Monier'
      jti: string
      email: string
    }
  }

  interface CognitoAccessToken {
    jwtToken: string
    payload: {
      origin_jti: string
      sub: string
      event_id: string
      token_use: string
      scope: string
      auth_time: number
      iss: string
      exp: number
      iat: number
      jti: string
      client_id: string
      username: string
    }
  }

  interface CognitoRefreshToken {
    token: string
  }
}

jordanmonier avatar Sep 30 '21 12:09 jordanmonier

Can we please get better Typescript typings 🙏

joelzwarrington avatar Oct 25 '21 21:10 joelzwarrington

@jordanmonier hey Jordan, according to your code, I apply it to my code and got this error in the payload Property:

(property) CognitoIdToken.payload: {
    [key: string]: any;
}
Subsequent property declarations must have the same type.  Property 'payload' must be of type '{ [key: string]: any; }', but here has type '{ sub: string; email_verified: boolean; iss: string; phone_number_verified: boolean; 'cognito:username': string; given_name: string; origin_jti: string; aud: string; event_id: string; token_use: string; ... 6 more ...; email: string; }'.ts(2717)
index.d.ts(356, 3): 'payload' was also declared here.

Can anyone have a better typescript typings please tell us know!

hoa-nguyen-bk avatar May 24 '22 02:05 hoa-nguyen-bk

+1

ShawnCockburn avatar Jun 30 '22 13:06 ShawnCockburn

+1

wmoa avatar Nov 18 '22 00:11 wmoa

These types are all defined, and in many cases as Classes not just interfaces. They're just in transitive dependencies that are annoyingly flagged as "internal" and for some reason aren't always available for imports. I think aws-amplify really needs to be re-exporting these types if developers are expected to not directly install the transitive dependencies.

For example I'm able to use the following:

import { CognitoUser } from '@aws-amplify/auth'
import { CognitoUserSession, CognitoIdToken } from 'amazon-cognito-identity-js'

So long as I have @aws-amplify/auth in my dependencies:

    "aws-amplify": "5.3.16",
    "@aws-amplify/auth": "5.6.10",

I'm not sure why I have to explicitly depend on @aws-amplify/auth but I do.

sflanker avatar Oct 17 '24 02:10 sflanker