-
Notifications
You must be signed in to change notification settings - Fork 42
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
Able to assign host
and port
for emualtor
#35
Changes from all commits
2c11321
20f11f8
46749f3
fbe67f2
31b748d
a3d1a82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
part of '../app.dart'; | ||
|
||
/// Represents a Firebase Emulator. | ||
/// Now for Auth and Firestore. | ||
class Emulator { | ||
/// If there is no environment variable set, please use this constructor. | ||
/// For example, gcloud firestore emulators. | ||
const Emulator(this.host, this.port); | ||
|
||
/// Creates an [Emulator] from an environment variable. | ||
/// For example, env key for auth is: FIREBASE_AUTH_EMULATOR_HOST | ||
@internal | ||
factory Emulator.fromEnvString(String envString) { | ||
final parts = envString.split(':'); | ||
if (parts.length != 2) { | ||
throw ArgumentError.value(envString, 'envString', 'Invalid format'); | ||
} | ||
final host = parts[0]; | ||
final port = int.tryParse(parts[1]); | ||
if (port == null) { | ||
throw ArgumentError.value(envString, 'envString', 'Invalid port'); | ||
} | ||
return Emulator(host, port); | ||
} | ||
|
||
/// The default Auth Emulator. | ||
const Emulator._defaultAuth() | ||
: host = '127.0.0.1', | ||
port = 9099; | ||
|
||
/// The default Firestore Emulator. | ||
const Emulator._defaultFirestore() | ||
: host = '127.0.0.1', | ||
port = 8080; | ||
|
||
/// Try to get the Auth Emulator from the environment variable. | ||
/// If not found, use the default Auth Emulator. | ||
factory Emulator.auth() { | ||
if (!Platform.environment.containsKey('FIREBASE_AUTH_EMULATOR_HOST')) { | ||
return const Emulator._defaultAuth(); | ||
} else { | ||
return Emulator.fromEnvString( | ||
Platform.environment['FIREBASE_AUTH_EMULATOR_HOST']!, | ||
); | ||
} | ||
} | ||
|
||
/// Try to get the Firestore Emulator from the environment variable. | ||
/// If not found, use the default Firestore Emulator. | ||
factory Emulator.firestore() { | ||
if (!Platform.environment.containsKey('FIRESTORE_EMULATOR_HOST')) { | ||
return const Emulator._defaultFirestore(); | ||
} else { | ||
return Emulator.fromEnvString( | ||
Platform.environment['FIRESTORE_EMULATOR_HOST']!, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
); | ||
} | ||
} | ||
|
||
final String host; | ||
final int port; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,46 @@ class FirebaseAdminApp { | |
/// The [Credential] used to authenticate the Admin SDK. | ||
final Credential credential; | ||
|
||
bool get isUsingEmulator => _isUsingEmulator; | ||
var _isUsingEmulator = false; | ||
bool get isUsingAuthEmulator => _isUsingAuthEmulator; | ||
|
||
bool get isUsingFirestoreEmulator => _isUsingFirestoreEmulator; | ||
|
||
bool get isUsingEmulator => _isUsingAuthEmulator || _isUsingFirestoreEmulator; | ||
|
||
var _isUsingAuthEmulator = false; | ||
var _isUsingFirestoreEmulator = false; | ||
|
||
@internal | ||
Uri authApiHost = Uri.https('identitytoolkit.googleapis.com', '/'); | ||
@internal | ||
Uri firestoreApiHost = Uri.https('firestore.googleapis.com', '/'); | ||
|
||
/// Use the Firebase Emulator Suite to run the app locally. | ||
void useEmulator() { | ||
_isUsingEmulator = true; | ||
authApiHost = Uri.http('127.0.0.1:9099', 'identitytoolkit.googleapis.com/'); | ||
firestoreApiHost = Uri.http('127.0.0.1:8080', '/'); | ||
/// Use the Firebase Emulator suite to run the app locally. | ||
void useEmulator({ | ||
Emulator? authEmulator, | ||
Emulator? firestoreEmulator, | ||
}) { | ||
useAuthEmulator(emulator: authEmulator); | ||
useFirestoreEmulator(emulator: firestoreEmulator); | ||
} | ||
|
||
/// Use the Firebase Auth Emulator to run the app locally. | ||
void useAuthEmulator({ | ||
Emulator? emulator | ||
}) { | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't a simpler API be this? void useAuthEmulator([Uri? uri]) We can then remove that |
||
_isUsingAuthEmulator = true; | ||
emulator ??= Emulator.auth(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have the environment logic directly in here, instead of a constructor inside a separate Emulator class. |
||
authApiHost = Uri.http( | ||
'${emulator.host}:${emulator.port}', 'identitytoolkit.googleapis.com/'); | ||
} | ||
|
||
/// Use the Firebase Firestore Emulator to run the app locally. | ||
void useFirestoreEmulator({ | ||
Emulator? emulator, | ||
}) { | ||
_isUsingFirestoreEmulator = true; | ||
emulator ??= Emulator.firestore(); | ||
firestoreApiHost = Uri.http('${emulator.host}:${emulator.port}', '/'); | ||
} | ||
|
||
@internal | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this
!
?This is possible by doing something like: