Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi Iwana committed Jun 28, 2020
1 parent 177fd03 commit 5a60933
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
32 changes: 19 additions & 13 deletions src/cognito/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import AWS from 'aws-sdk'
import { AuthOptionsOAuth } from '../shared/types'
import { AdminInitiateAuthResponse } from 'aws-sdk/clients/cognitoidentityserviceprovider'
import { getCredentials } from '../utils/cognito'

export interface AWSCognitoUserPoolOptions {
import { NormalizedCacheObject } from 'apollo-cache-inmemory'
export interface AWSCognitoUserPoolOptions {
clientId: string
userPoolId: string
authParameters: {
Expand All @@ -15,17 +15,23 @@ export interface AWSCognitoUserPoolOptions {
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createCognitoUserPoolAppSyncClient(options: AWSCognitoUserPoolOptions & {
url: string
auth?: {
jwtToken: (provider: AWS.CognitoIdentityServiceProvider, options: AWSCognitoUserPoolOptions) => () => Promise<string>
export function createCognitoUserPoolAppSyncClient(
options: AWSCognitoUserPoolOptions & {
url: string
auth?: {
jwtToken: (
provider: AWS.CognitoIdentityServiceProvider,
options: AWSCognitoUserPoolOptions
) => () => Promise<string>
}
disableOffline?: boolean
}
disableOffline?: boolean
}) {
): AWSAppSyncClient<NormalizedCacheObject> {
const provider = new AWS.CognitoIdentityServiceProvider({
region: options.region
region: options.region,
})
let credentials: AdminInitiateAuthResponse, expired = new Date('01-01-1970')
let credentials: AdminInitiateAuthResponse,
expired = new Date('01-01-1970')

const jwtToken: () => Promise<string | undefined> = async () => {
// Check if we already have credentials or if credentials are expired
Expand All @@ -34,7 +40,8 @@ export function createCognitoUserPoolAppSyncClient(options: AWSCognitoUserPoolOp
credentials = await getCredentials(provider, options)
// Give ourselves a 10 minute leeway here
expired = new Date(
+new Date() + ((credentials?.AuthenticationResult?.ExpiresIn ?? 600) - 600) * 1000
+new Date() +
((credentials?.AuthenticationResult?.ExpiresIn ?? 600) - 600) * 1000
)
}

Expand All @@ -49,6 +56,5 @@ export function createCognitoUserPoolAppSyncClient(options: AWSCognitoUserPoolOp
jwtToken: options.auth?.jwtToken(provider, options) ?? jwtToken,
} as AuthOptionsOAuth,
disableOffline: options.disableOffline ?? true,

})
}
}
18 changes: 10 additions & 8 deletions src/iam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import AWSAppSyncClient from 'aws-appsync'
import AWS from 'aws-sdk'
import { AppsyncClientOptions } from '../shared/interfaces'
import { AuthOptionsIAM } from '../shared/types'

export interface AWSIAMOptions {
import { NormalizedCacheObject } from 'apollo-cache-inmemory'
export interface AWSIAMOptions {
credentials: {
accessKeyId: string
secretAccessKey: string
Expand All @@ -12,19 +12,21 @@ export interface AWSIAMOptions {
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createIAMAppSyncClient(options: AWSIAMOptions & Omit<AppsyncClientOptions<AuthOptionsIAM>, 'auth'>) {
export function createIAMAppSyncClient(
options: AWSIAMOptions & Omit<AppsyncClientOptions<AuthOptionsIAM>, 'auth'>
): AWSAppSyncClient<NormalizedCacheObject> {
AWS.config.update({
region: options.region,
credentials: new AWS.Credentials(options.credentials)
credentials: new AWS.Credentials(options.credentials),
})

return new AWSAppSyncClient({
url: options.url,
region: options.region,
auth: {
type: 'AWS_IAM',
credentials: AWS.config.credentials
credentials: AWS.config.credentials,
} as AuthOptionsIAM,
disableOffline: options.disableOffline ?? true
disableOffline: options.disableOffline ?? true,
})
}
}

0 comments on commit 5a60933

Please sign in to comment.