Skip to content
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

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

Merged
merged 4 commits into from
Jun 19, 2020

Conversation

msutkowski
Copy link
Contributor

@msutkowski msutkowski commented Jun 4, 2020

Description

  • Prefers extending the base interfaces for Firestore/FirebaseExtendedInstance instead of using intersections throughout the types
  • Adds FirebaseDatabaseService types so that you can achieve:
...
const firebase = useFirebase();
firebase.database.ServerValue.TIMESTAMP
...
  • Correct types for FirestoreStatics so you can achieve:
const firestore = useFirestore();
firestore.FieldValue.serverTimestamp()

Previously, you would've gotten 'serverTimestamp() is a static member of FieldValue'

  • Fixes useFirestore() types - previously withFirestore would've been close, but useFirestore was missing statics.

We've also added inference for plugins, such as performance, analytics, remoteConfig, etc. When a user imports those plugins, TS will automatically allow a user to access those properties. This basically works by saying, 'if we see this known key on the firebase instance, use the types that we know firebase merged into the namespace'

Check List

If not relevant to pull request, check off as complete

  • All tests passing
  • Docs updated with any changes or examples if applicable
  • Added tests to ensure new feature(s) work properly

Relevant Issues

@codecov
Copy link

codecov bot commented Jun 4, 2020

Codecov Report

Merging #957 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #957   +/-   ##
=======================================
  Coverage   88.33%   88.33%           
=======================================
  Files          29       29           
  Lines         797      797           
=======================================
  Hits          704      704           
  Misses         93       93           

@msutkowski msutkowski marked this pull request as ready for review June 4, 2020 01:40
@prescottprue
Copy link
Owner

This is awesome, nice work! Do you foresee this causing any issues for folks? It doesn't appear so

Asking so we can figure out the version for release

@msutkowski
Copy link
Contributor Author

@prescottprue It should not as it just 'fixes' those few TS issues. The only potential issue I can think of is in regard to not using intersections which doesn't apply here - if there was a property collision, that'd throw a type error if they differed. But as it sits, there are no overlaps here that would cause that kind of issue I can see (and we'd also see that in the build).

We could look at introducing some type tests, but being that all this is doing is setting up basic interfaces pointing to the provided Google types, I don't know if that's worth it. If you want to go down that path, I'm open to it and can get a 2nd/3rd opinion as well :)

@msutkowski
Copy link
Contributor Author

@prescottprue Side note: I just quickly scrubbed the APIs for Firebase and compared it what's currently returned by ExtendedFirebaseInstance. There are a few more missing elements that I can tack on to ExtendedFirebaseInstance quickly:

    messaging(): firebase.messaging.Messaging;
    functions(region?: string): firebase.functions.Functions;
    performance(): firebase.performance.Performance;
    analytics(): firebase.analytics.Analytics;
    remoteConfig(): firebase.remoteConfig.RemoteConfig; 

If you'd like to chat about any of this, I'm on the Reactiflux discord under the same user name (often found in #redux). I'm happy to contribute more if you're looking for additional help. Thanks!

@msutkowski
Copy link
Contributor Author

msutkowski commented Jun 5, 2020

@prescottprue Just updated to handle those scenarios. If you include plugins, they'll automatically be inferred on the instance. Example:

import firebase from "firebase/app";
import "firebase/auth";
import "firebase/database";
import "firebase/firestore";
import "@firebase/messaging";
import "@firebase/performance";
import "@firebase/functions";
import "@firebase/analytics";
// import "@firebase/remote-config";


In some component:
const firebase = useFirebase();
console.log('$', firebase.analytics(), firebase.messaging(), '...'); // firebase will have firebase.analytics(), firebase.messaging(), etc..
firebase.remoteConfig() // will throw a type error as it's not imported

Thanks to @themindoverall & @phryneas for the help!

@msutkowski
Copy link
Contributor Author

@prescottprue I think this is good to go. Let me know if there's anything else you'd like done here.

/**
* 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

@prescottprue prescottprue changed the title Type fixes: Firestore statics, add Firebase data service, misc feat(types): add database and firestore statics Jun 19, 2020
@prescottprue prescottprue changed the base branch from master to v3.6.0 June 19, 2020 02:32
@prescottprue prescottprue merged commit 8d57e1d into prescottprue:v3.6.0 Jun 19, 2020
@prescottprue prescottprue mentioned this pull request Jun 19, 2020
3 tasks
prescottprue added a commit that referenced this pull request Jun 19, 2020
* feat(types): add database and firestore statics (#957) - @msutkowski, @themindoverall, @phryneas
* feat(types): add type predicate to `isLoaded` (#956) - @lukeKonopka
* chore(docs): formatting fixes in firestore docs section (#959) - @gregfenton
* feat(docs): add firestore populate docs (#954) - @harveychow

Co-authored-by: gregfenton <[email protected]>
Co-authored-by: Matt Sutkowski <[email protected]>
Co-authored-by: Chris Serino <[email protected]>
Co-authored-by: Harvey Chow <[email protected]>
Co-authored-by: Luke Konopka <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants