[react-native-google-signin] Add Typings (#20505)

* Add typings for react-native-google-signin

* Fix type in configure parameter

* Remove optional in favor of type | null
This commit is contained in:
Jacob Froman
2017-10-12 04:22:49 -05:00
committed by Wesley Wigham
parent 6d187525fa
commit 972e5154d8
4 changed files with 222 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
// Type definitions for react-native-google-signin 0.12
// Project: https://github.com/devfd/react-native-google-signin
// Definitions by: Jacob Froman <https://github.com/j-fro>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import * as React from 'react';
import { ViewProperties } from 'react-native';
export interface GoogleSigninButtonProps extends ViewProperties {
size?: GoogleSigninButton.Size;
color?: GoogleSigninButton.Color;
onPress?(): void;
}
export class GoogleSigninButton extends React.Component<GoogleSigninButtonProps> {
constructor(props: GoogleSigninButtonProps);
}
export namespace GoogleSigninButton {
enum Size {
Standard,
Wide,
Icon
}
enum Color {
Light,
Dark
}
}
export interface HasPlayServicesParams {
/**
* When autoresolve is true, the user will be prompted to install Play
* Services if on Android and they are not installed.
*/
autoResolve?: boolean;
}
export interface ConfigureParams {
/**
* The Google API scopes to request access to. Default is email and profile.
*/
scopes?: string[];
/**
* iOS client ID from Developer Console. Required for iOS.
*/
iosClientId?: string;
/**
* Web client ID from Developer Console. Required for offline access
*/
webClientId?: string;
/**
* Must be true if you wish to access user APIs on behalf of the user from
* your own server
*/
offlineAccess?: boolean;
/**
* Specifies a hosted domain restriction
*/
hostedDomain?: string;
/**
* ANDROID ONLY. Specifies if the consent prompt should be shown at each login.
*/
forceConsentPrompt?: boolean;
/**
* ANDROID ONLY. An account name that should be prioritized.
*/
accountName?: string;
}
export interface User {
id: string | null;
name: string | null;
email: string | null;
scopes?: string[];
photo: string | null;
familyName: string | null;
givenName: string | null;
idToken: string | null;
/**
* IOS ONLY. Use getAccessToken() on Android
*/
accessToken: string;
/**
* IOS ONLY. Use getAccessToken() on Android
*/
accessTokenExpirationDate: number;
/**
* Not null only if a valid webClientId and offlineAccess: true was
* specified in configure().
*/
serverAuthCode: string | null;
}
export namespace GoogleSignin {
/**
* Check if the device has Google Play Services installed. Always resolves
* true on iOS
*/
function hasPlayServices(params?: HasPlayServicesParams): Promise<boolean>;
/**
* Configures the library for login. MUST be called before attempting login
*/
function configure(params?: ConfigureParams): Promise<void>;
/**
* Returns the current signed in user, or null if not signed in.
*/
function currentUser(): User | null;
/**
* Returns a Promise that resolves with the current signed in user, or null
* if not signed in.
*/
function currentUserAsync(): Promise<User | null>;
/**
* Prompts the user to sign in with their Google account. Resolves with the
* user if successful.
*/
function signIn(): Promise<User>;
/**
* Signs the user out.
*/
function signOut(): Promise<void>;
/**
* ANDROID ONLY. Resolves with the current signed in user's access token.
*/
function getAccessToken(): Promise<string | null>;
/**
* Removes your application from the user's authorized applications
*/
function revokeAccess(): Promise<void>;
}

View File

@@ -0,0 +1,51 @@
import * as React from 'react';
import { GoogleSignin, GoogleSigninButton, User } from 'react-native-google-signin';
import { StyleSheet, ViewStyle, View, Text, TouchableHighlight } from 'react-native';
interface State {
user?: User;
}
export default class Signin extends React.Component<{}, State> {
state: State = {};
componentDidMount() {
GoogleSignin.configure({
scopes: ['https://www.googleapis.com/auth/drive.readonly']
}).then(() => GoogleSignin.hasPlayServices({ autoResolve: true }));
}
async handleSigninPress(): Promise<void> {
const user = await GoogleSignin.signIn();
this.setState({ user });
}
handleSignoutPress(): Promise<void> {
return GoogleSignin.signOut();
}
render() {
if (this.state.user) {
return (
<View>
<Text>{this.state.user.name}</Text>
<TouchableHighlight onPress={() => this.handleSignoutPress()}>
<Text>Sign Out</Text>
</TouchableHighlight>
</View>
);
}
return (
<GoogleSigninButton
style={styles.button}
size={GoogleSigninButton.Size.Wide}
color={GoogleSigninButton.Color.Dark}
onPress={() => this.handleSigninPress()}
/>
);
}
}
const styles = StyleSheet.create({
button: { width: 312, height: 48 }
});

View File

@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strictFunctionTypes": true,
"jsx": "react-native"
},
"files": [
"index.d.ts",
"react-native-google-signin-tests.tsx"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }