Skip to content

feat(types): add database and firestore statics #957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { FirebaseNamespace } from "@firebase/app-types";
import * as FirestoreTypes from '@firebase/firestore-types'
import * as DatabaseTypes from '@firebase/database-types'
import * as StorageTypes from '@firebase/storage-types'
Expand Down Expand Up @@ -139,12 +140,32 @@ interface RemoveOptions {
dispatchAction: boolean
}

/**
* https://firebase.google.com/docs/reference/js/firebase.database
*/

interface FirebaseDatabaseService {
database: {
(app?: string): DatabaseTypes.FirebaseDatabase
Reference: DatabaseTypes.Reference
Query: DatabaseTypes.Query
DataSnapshot: DatabaseTypes.DataSnapshot
enableLogging: typeof DatabaseTypes.enableLogging
ServerValue: DatabaseTypes.ServerValue
Database: typeof DatabaseTypes.FirebaseDatabase
}
}

/**
* Firestore instance extended with methods which dispatch
* redux actions.
* @see https://react-redux-firebase.com/docs/api/firebaseInstance.html
*/
interface ExtendedFirebaseInstance extends DatabaseTypes.FirebaseDatabase {
interface BaseExtendedFirebaseInstance
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really like that you combined this all into one it much more clearly matches the name

extends DatabaseTypes.FirebaseDatabase,
FirebaseDatabaseService,
ExtendedAuthInstance,
ExtendedStorageInstance {
initializeAuth: VoidFunction

firestore: () => ExtendedFirestoreInstance
Expand Down Expand Up @@ -318,6 +339,15 @@ interface ExtendedFirebaseInstance extends DatabaseTypes.FirebaseDatabase {
) => Promise<any>
}

/**
* OptionalOverride is left here in the event that any of the optional properties below need to be extended in the future.
* Example: OptionalOverride<FirebaseNamespace, 'messaging', { messaging: ExtendedMessagingInstance }>
*/
type OptionalOverride<T, b extends string, P> = b extends keyof T ? P : {};
type OptionalPick<T, b extends string> = Pick<T, b & keyof T>

type ExtendedFirebaseInstance = BaseExtendedFirebaseInstance & OptionalPick<FirebaseNamespace, 'messaging' | 'performance' | 'functions' | 'analytics' | 'remoteConfig'>

/**
* Create an extended firebase instance that has methods attached
* which dispatch redux actions.
Expand All @@ -330,7 +360,8 @@ export function createFirebaseInstance(
firebase: any,
configs: Partial<ReduxFirestoreConfig>,
dispatch: Dispatch
): ExtendedFirebaseInstance & ExtendedAuthInstance & ExtendedStorageInstance
): ExtendedFirebaseInstance;


export type QueryParamOption =
| 'orderByKey'
Expand Down Expand Up @@ -447,7 +478,9 @@ export type ReduxFirestoreQueriesFunction = (
* Firestore instance extended with methods which dispatch redux actions.
* @see https://github.com/prescottprue/redux-firestore#api
*/
interface ExtendedFirestoreInstance extends FirestoreTypes.FirebaseFirestore {
interface ExtendedFirestoreInstance
extends FirestoreTypes.FirebaseFirestore,
FirestoreStatics {
/**
* Get data from firestore.
* @see https://github.com/prescottprue/redux-firestore#get
Expand Down Expand Up @@ -532,7 +565,7 @@ interface ExtendedFirestoreInstance extends FirestoreTypes.FirebaseFirestore {
* @see https://github.com/prescottprue/redux-firestore#other-firebase-statics
*/
interface FirestoreStatics {
FieldValue: FirestoreTypes.FieldValue
FieldValue: typeof FirestoreTypes.FieldValue
FieldPath: FirestoreTypes.FieldPath
setLogLevel: (logLevel: FirestoreTypes.LogLevel) => void
Blob: FirestoreTypes.Blob
Expand All @@ -543,18 +576,14 @@ interface FirestoreStatics {
Query: FirestoreTypes.Query
QueryDocumentSnapshot: FirestoreTypes.QueryDocumentSnapshot
QuerySnapshot: FirestoreTypes.QuerySnapshot
Timestamp: FirestoreTypes.FieldValue
Timestamp: typeof FirestoreTypes.FieldValue
Transaction: FirestoreTypes.Transaction
WriteBatch: FirestoreTypes.WriteBatch
}

export interface WithFirestoreProps {
firestore: FirestoreTypes.FirebaseFirestore &
ExtendedFirestoreInstance &
FirestoreStatics
firebase: ExtendedFirebaseInstance &
ExtendedAuthInstance &
ExtendedStorageInstance
firestore: ExtendedFirestoreInstance
firebase: ExtendedFirebaseInstance
dispatch: Dispatch
}

Expand Down Expand Up @@ -803,9 +832,7 @@ export interface UploadFileOptions<T extends File | Blob> {
}

export interface WithFirebaseProps<ProfileType> {
firebase: ExtendedAuthInstance &
ExtendedStorageInstance &
ExtendedFirebaseInstance
firebase: ExtendedFirebaseInstance
}

/**
Expand Down Expand Up @@ -878,9 +905,7 @@ export function fixPath(path: string): string
* integrations into external libraries such as redux-thunk and redux-observable.
* @see https://react-redux-firebase.com/docs/api/getFirebase.html
*/
export function getFirebase(): ExtendedFirebaseInstance &
ExtendedAuthInstance &
ExtendedStorageInstance
export function getFirebase(): ExtendedFirebaseInstance

/**
* Get a value from firebase using slash notation. This enables an easy
Expand Down Expand Up @@ -919,9 +944,7 @@ export function isLoaded(...args: any[]): boolean
* instance is gathered from `ReactReduxFirebaseContext`.
* @see https://react-redux-firebase.com/docs/api/useFirebase.html
*/
export function useFirebase(): ExtendedFirebaseInstance &
ExtendedAuthInstance &
ExtendedStorageInstance
export function useFirebase(): ExtendedFirebaseInstance

/**
* React hook that automatically listens/unListens
Expand Down