From b6b3e305bbbe144d79ecc54d8313cda8d2c1a081 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Sat, 25 Sep 2021 17:23:32 +0200 Subject: [PATCH] types: Fix onSuccess callback Currently we have a `code` property on the `GoogleLoginResponse` interface that isn't actually there and is just a hack for people doing the offline flow. Additionally, if using the online flow in TypeScript you can't safely access any of the properties and need to cast the argument of the onSuccess callback to get them. This PR fixes these issues by properly handling the `responseType` property that determines what the `onSuccess` callback's argument is. --- index.d.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/index.d.ts b/index.d.ts index 289263f..12ff52b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,12 @@ /* eslint-disable max-classes-per-file */ -// Type definitions for react-google-login v2.5.4 +// Type definitions for react-google-login v5.2 // Project: https://github.com/anthonyjgrove/react-google-login // Definitions by: Ruslan Ibragimov import {Component, ReactNode, CSSProperties} from 'react'; export as namespace ReactGoogleLogin; +// https://developers.google.com/identity/sign-in/web/reference#gapiauth2authresponse interface AuthResponse { readonly access_token: string; readonly id_token: string; @@ -44,7 +45,6 @@ export interface GoogleLoginResponse { tokenObj: AuthResponse; tokenId: string; accessToken: string; - readonly code?: string;//does not exist but here to satisfy typescript compatibility profileObj: { googleId: string; imageUrl: string; @@ -71,8 +71,8 @@ export interface GoogleLoginResponseOffline { readonly code: string; } -export interface GoogleLoginProps { - readonly onSuccess?: (response: GoogleLoginResponse | GoogleLoginResponseOffline) => void, +export interface GoogleLoginProps { + readonly onSuccess?: T extends 'code' ? (response: GoogleLoginResponseOffline) => void : (response: GoogleLoginResponse) => void, readonly onAutoLoadFinished?: (successLogin: boolean) => void, readonly onFailure?: (error: any) => void, readonly onScriptLoadFailure?: (error: any) => void, @@ -87,7 +87,7 @@ export interface GoogleLoginProps { readonly loginHint?: string, readonly hostedDomain?: string, readonly prompt?: string, - readonly responseType?: string, + readonly responseType?: 'code' | T, readonly children?: ReactNode, readonly style?: CSSProperties, readonly icon?: boolean, @@ -104,7 +104,7 @@ export interface GoogleLoginProps { readonly render?: (props: { onClick: () => void, disabled?: boolean }) => JSX.Element; } -export class GoogleLogin extends Component { +export class GoogleLogin extends Component, unknown> { public signIn(e?: Event): void; } @@ -156,8 +156,8 @@ export interface UseGoogleLoginResponse { loaded: boolean; } -export interface UseGoogleLoginProps { - readonly onSuccess?: (response: GoogleLoginResponse | GoogleLoginResponseOffline) => void, +export interface UseGoogleLoginProps { + readonly onSuccess?: T extends 'code' ? (response: GoogleLoginResponseOffline) => void : (response: GoogleLoginResponse) => void, readonly onFailure?: (error: any) => void, readonly onScriptLoadFailure?: (error: any) => void, readonly onAutoLoadFinished?: (successLogin: boolean) => void, @@ -170,7 +170,7 @@ export interface UseGoogleLoginProps { readonly loginHint?: string, readonly hostedDomain?: string, readonly prompt?: string, - readonly responseType?: string, + readonly responseType?: 'code' | T, readonly autoLoad?: boolean; readonly uxMode?: string; readonly fetchBasicProfile?: boolean; @@ -179,6 +179,6 @@ export interface UseGoogleLoginProps { readonly accessType?: string; } -export function useGoogleLogin(input: UseGoogleLoginProps): UseGoogleLoginResponse; +export function useGoogleLogin(input: UseGoogleLoginProps): UseGoogleLoginResponse; export default GoogleLogin;