From 3b9492ce46859f4c6a6331357d5994daf608256c Mon Sep 17 00:00:00 2001 From: Matt Sutkowski Date: Wed, 3 Jun 2020 18:25:16 -0700 Subject: [PATCH 1/5] Fix Firestore statics, add Firebase data service, misc --- index.d.ts | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/index.d.ts b/index.d.ts index 89dd2e33e..628b0b53a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -139,12 +139,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 ExtendedFirebaseInstance + extends DatabaseTypes.FirebaseDatabase, + FirebaseDatabaseService, + ExtendedAuthInstance, + ExtendedStorageInstance { initializeAuth: VoidFunction firestore: () => ExtendedFirestoreInstance @@ -330,7 +350,7 @@ export function createFirebaseInstance( firebase: any, configs: Partial, dispatch: Dispatch -): ExtendedFirebaseInstance & ExtendedAuthInstance & ExtendedStorageInstance +): ExtendedFirebaseInstance export type QueryParamOption = | 'orderByKey' @@ -447,7 +467,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 @@ -532,7 +554,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 @@ -543,18 +565,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 } @@ -803,9 +821,7 @@ export interface UploadFileOptions { } export interface WithFirebaseProps { - firebase: ExtendedAuthInstance & - ExtendedStorageInstance & - ExtendedFirebaseInstance + firebase: ExtendedFirebaseInstance } /** @@ -878,9 +894,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 @@ -919,9 +933,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 From 87f59028a5c67448fbca58562781f9a984df185e Mon Sep 17 00:00:00 2001 From: Matt Sutkowski Date: Fri, 5 Jun 2020 10:04:35 -0700 Subject: [PATCH 2/5] Extract optional firebase plugins Co-authored-by: Chris Serino --- index.d.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 628b0b53a..996861b28 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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' @@ -160,7 +161,7 @@ interface FirebaseDatabaseService { * redux actions. * @see https://react-redux-firebase.com/docs/api/firebaseInstance.html */ -interface ExtendedFirebaseInstance +interface BaseExtendedFirebaseInstance extends DatabaseTypes.FirebaseDatabase, FirebaseDatabaseService, ExtendedAuthInstance, @@ -338,6 +339,29 @@ interface ExtendedFirebaseInstance ) => Promise } +/** + * OptionalOverride is left here in the event that any of the optional properties below need to be extended in the future. + * Example: OptionalOverride + */ +type OptionalOverride = b extends keyof T ? P : {}; +type OptionalPick = { [k in (b extends keyof T ? b : never)]: T[k] }; + +type ExtendedFirebaseInstance = BaseExtendedFirebaseInstance & OptionalPick + +/** + * Create an extended firebase instance that has methods attached + * which dispatch redux actions. + * @param firebase - Firebase instance which to extend + * @param configs - Configuration object + * @param dispatch - Action dispatch function + * @see https://react-redux-firebase.com/docs/api/firebaseInstance.html + */ +export function createFirebaseInstance( + firebase: FirebaseNamespace, + configs: Partial, + dispatch: Dispatch +): ExtendedFirebaseInstance; + /** * Create an extended firebase instance that has methods attached * which dispatch redux actions. From da2baac84664fbc29dd80f93ea5f1d6b5c3f7316 Mon Sep 17 00:00:00 2001 From: Matt Sutkowski Date: Fri, 5 Jun 2020 10:49:37 -0700 Subject: [PATCH 3/5] Simplify OptionalPick --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 996861b28..973cf8a6f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -344,7 +344,7 @@ interface BaseExtendedFirebaseInstance * Example: OptionalOverride */ type OptionalOverride = b extends keyof T ? P : {}; -type OptionalPick = { [k in (b extends keyof T ? b : never)]: T[k] }; +type OptionalPick = Pick type ExtendedFirebaseInstance = BaseExtendedFirebaseInstance & OptionalPick From 42f607382bda3d800c9c554f3b1e30a1aa3d16fc Mon Sep 17 00:00:00 2001 From: Matt Sutkowski Date: Fri, 5 Jun 2020 11:01:40 -0700 Subject: [PATCH 4/5] Cleanup --- index.d.ts | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index 973cf8a6f..a9978abcd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -357,24 +357,11 @@ type ExtendedFirebaseInstance = BaseExtendedFirebaseInstance & OptionalPick, dispatch: Dispatch ): ExtendedFirebaseInstance; -/** - * Create an extended firebase instance that has methods attached - * which dispatch redux actions. - * @param firebase - Firebase instance which to extend - * @param configs - Configuration object - * @param dispatch - Action dispatch function - * @see https://react-redux-firebase.com/docs/api/firebaseInstance.html - */ -export function createFirebaseInstance( - firebase: any, - configs: Partial, - dispatch: Dispatch -): ExtendedFirebaseInstance export type QueryParamOption = | 'orderByKey' From 14c98bc0a2861f1562380a4321296a19086bfa83 Mon Sep 17 00:00:00 2001 From: Matt Sutkowski Date: Tue, 9 Jun 2020 18:44:10 -0700 Subject: [PATCH 5/5] Update ordered and data types for FirestoreReducer.Reducer --- index.d.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index a9978abcd..75753386a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -889,10 +889,11 @@ export function firestoreConnect( * @param action.data - Data associated with action * @see https://react-redux-firebase.com/docs/api/reducer.html */ -export function firestoreReducer( +export function firestoreReducer = {} +>( state: any, action: any -): FirestoreReducer.Reducer +): Reducer> /** * Fix path by adding "/" to path if needed @@ -1246,16 +1247,35 @@ export namespace FirebaseReducer { } } + export namespace FirestoreReducer { - export interface Reducer { - composite?: Data> - data: Data> + declare const entitySymbol: unique symbol + + export type Entity = T & { + [entitySymbol]: never + } + export type EntityWithId = T & { id: string } + export type FirestoreData> = { + [T in keyof Schema]: Record< + string, + Schema[T] extends Entity ? V : FirestoreData + > + } + + export type OrderedData> = { + [T in keyof Schema]: Schema[T] extends Entity + ? EntityWithId[] + : OrderedData>[] + } + + export interface Reducer = {}> { errors: { allIds: string[] byQuery: any[] } listeners: Listeners - ordered: Ordered + data: FirestoreData + ordered: OrderedData queries: Data | any)> status: { requested: Dictionary