Skip to content

Commit

Permalink
chore: Add _fluttefire_internals and extract firebase_core/src/intern…
Browse files Browse the repository at this point in the history
…als to the new package (#9628)
  • Loading branch information
rrousselGit committed Sep 29, 2022
1 parent 33a71e9 commit 7cd76a1
Show file tree
Hide file tree
Showing 64 changed files with 277 additions and 425 deletions.
3 changes: 3 additions & 0 deletions packages/_flutterfire_internals/CHANGELOG.md
@@ -0,0 +1,3 @@
## 1.0.0

Initial release of \_fluttefire_internals
26 changes: 26 additions & 0 deletions packages/_flutterfire_internals/LICENSE
@@ -0,0 +1,26 @@
Copyright 2017, the Chromium project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4 changes: 4 additions & 0 deletions packages/_flutterfire_internals/README.md
@@ -0,0 +1,4 @@
# \_flutterfire_internals

This package is hosting Dart code shared between FlutterFire plugins. It is not
meant for public consumption.
Expand Up @@ -7,17 +7,38 @@
// This file exports utilities shared between firebase packages, without making
// them public.

import '../firebase_core.dart';
import 'interop_shimmer.dart'
import 'package:firebase_core/firebase_core.dart';
import 'src/interop_shimmer.dart'
if (dart.library.js) 'package:firebase_core_web/firebase_core_web_interop.dart'
as core_interop;

export 'src/exception.dart';

/// An extension that adds utilities for safely casting objects
extension ObjectX<T> on T? {
/// Transform an object if that value is not null.
///
/// Doing:
///
/// ```dart
/// Map? json;
/// var result = json?['key']?.guard((json) => Model.fromJson(json));
/// ```
///
/// is equivalent to doing:
///
/// ```dart
/// Map? json;
/// var key = json?['key'];
/// var result = key == null ? null : Model.fromJson(key);
/// ```
R? guard<R>(R Function(T value) cb) {
if (this is T) return cb(this as T);
return null;
}

/// Safely cast an object, returning `null` if the casted object does not
/// match the casted type.
R? safeCast<R>() {
if (this is R) return this as R;
return null;
Expand Down
53 changes: 53 additions & 0 deletions packages/_flutterfire_internals/lib/src/exception.dart
@@ -0,0 +1,53 @@
// ignore_for_file: require_trailing_commas
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/services.dart';

/// Catches a [PlatformException] and returns an [Exception].
///
/// If the [Exception] is a [PlatformException], a [FirebaseException] is returned.
Never convertPlatformExceptionToFirebaseException(
Object exception,
StackTrace stackTrace, {
required String plugin,
}) {
if (exception is! Exception || exception is! PlatformException) {
Error.throwWithStackTrace(exception, stackTrace);
}

Error.throwWithStackTrace(
platformExceptionToFirebaseException(exception, plugin: plugin),
stackTrace,
);
}

/// Converts a [PlatformException] into a [FirebaseException].
///
/// A [PlatformException] can only be converted to a [FirebaseException] if the
/// `details` of the exception exist. Firebase returns specific codes and messages
/// which can be converted into user friendly exceptions.
FirebaseException platformExceptionToFirebaseException(
PlatformException platformException, {
required String plugin,
}) {
Map<String, String>? details = platformException.details != null
? Map<String, String>.from(platformException.details)
: null;

String? code;
String message = platformException.message ?? '';

if (details != null) {
code = details['code'] ?? code;
message = details['message'] ?? message;
}

return FirebaseException(
plugin: plugin,
code: code,
message: message,
);
}
25 changes: 25 additions & 0 deletions packages/_flutterfire_internals/pubspec.yaml
@@ -0,0 +1,25 @@
name: _flutterfire_internals
description: A package hosting Dart code shared between FlutterFire plugins.
homepage: https://firebase.google.com/docs/firestore
repository: https://github.com/firebase/flutterfire/tree/master/packages/_flutterfire_internals
version: 1.0.0

environment:
sdk: '>=2.16.0 <3.0.0'
flutter: '>=1.12.13+hotfix.5'

dependencies:
cloud_firestore_platform_interface: ^5.7.5
cloud_firestore_web: ^2.8.8
collection: ^1.0.0
firebase_core: ^1.10.2
firebase_core_platform_interface: ^4.5.1
flutter:
sdk: flutter
meta: ^1.3.0

dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^5.0.0
test: any
@@ -1,5 +1,5 @@
import 'package:firebase_core/src/internals.dart';
import 'package:firebase_core/src/interop_shimmer.dart';
import 'package:_flutterfire_internals/_flutterfire_internals.dart';
import 'package:_flutterfire_internals/src/interop_shimmer.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Expand Down
2 changes: 2 additions & 0 deletions packages/cloud_firestore/cloud_firestore/example/pubspec.yaml
Expand Up @@ -15,6 +15,8 @@ dependencies:
http: ^0.13.3

dependency_overrides:
_flutterfire_internals:
path: ../../../_flutterfire_internals
cloud_firestore_platform_interface:
path: ../../cloud_firestore_platform_interface
cloud_firestore_web:
Expand Down
@@ -1,44 +1,18 @@
// ignore_for_file: require_trailing_commas
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:firebase_core/firebase_core.dart';
import 'package:_flutterfire_internals/_flutterfire_internals.dart';
import 'package:flutter/services.dart';

/// Catches a [PlatformException] and returns an [Exception].
///
/// If the [Exception] is a [PlatformException], a [FirebaseException] is returned.
Never convertPlatformException(Object exception, StackTrace stackTrace) {
if (exception is! Exception || exception is! PlatformException) {
Error.throwWithStackTrace(exception, stackTrace);
}

Error.throwWithStackTrace(
platformExceptionToFirebaseException(exception),
convertPlatformExceptionToFirebaseException(
exception,
stackTrace,
plugin: 'cloud_firestore',
);
}

/// Converts a [PlatformException] into a [FirebaseException].
///
/// A [PlatformException] can only be converted to a [FirebaseException] if the
/// `details` of the exception exist. Firebase returns specific codes and messages
/// which can be converted into user friendly exceptions.
FirebaseException platformExceptionToFirebaseException(
PlatformException platformException) {
Map<String, String>? details = platformException.details != null
? Map<String, String>.from(platformException.details)
: null;

String code = 'unknown';
String message = platformException.message ?? '';

if (details != null) {
code = details['code'] ?? code;
message = details['message'] ?? message;
}

return FirebaseException(
plugin: 'cloud_firestore', code: code, message: message);
}
Expand Up @@ -9,6 +9,7 @@ environment:
flutter: ">=1.9.1+hotfix.5"

dependencies:
_flutterfire_internals: ^1.0.0
collection: ^1.15.0
firebase_core: ^1.10.0
flutter:
Expand Down
@@ -1,9 +1,6 @@
// ignore_for_file: require_trailing_commas
export 'package:firebase_core/src/internals.dart' hide guardWebExceptions;

import 'package:firebase_core/firebase_core.dart';
// ignore: implementation_imports
import 'package:firebase_core/src/internals.dart' as internals;
import 'package:_flutterfire_internals/_flutterfire_internals.dart'
as internals;

/// Will return a [FirebaseException] from a thrown web error.
/// Any other errors will be propagated as normal.
Expand Down
1 change: 1 addition & 0 deletions packages/cloud_firestore/cloud_firestore_web/pubspec.yaml
Expand Up @@ -10,6 +10,7 @@ environment:
flutter: ">=1.12.13+hotfix.4"

dependencies:
_flutterfire_internals: ^1.0.0
cloud_firestore_platform_interface: ^5.7.5
collection: ^1.0.0
firebase_core: ^1.10.0
Expand Down
Expand Up @@ -32,6 +32,8 @@ dev_dependencies:
mockito: ^5.0.0

dependency_overrides:
_flutterfire_internals:
path: ../../../_flutterfire_internals
cloud_firestore:
path: ../../../cloud_firestore/cloud_firestore
cloud_firestore_odm:
Expand Down
Expand Up @@ -25,6 +25,8 @@ dev_dependencies:
json_serializable: ">=6.3.0 <7.0.0"

dependency_overrides:
_flutterfire_internals:
path: ../../../_flutterfire_internals
cloud_firestore:
path: ../../../cloud_firestore/cloud_firestore
cloud_firestore_odm:
Expand Down
2 changes: 2 additions & 0 deletions packages/cloud_functions/cloud_functions/example/pubspec.yaml
Expand Up @@ -15,6 +15,8 @@ dependencies:
sdk: flutter

dependency_overrides:
_flutterfire_internals:
path: ../../../_flutterfire_internals
cloud_functions_platform_interface:
path: ../../../cloud_functions/cloud_functions_platform_interface
cloud_functions_web:
Expand Down
Expand Up @@ -14,6 +14,8 @@ dependencies:
sdk: flutter

dependency_overrides:
_flutterfire_internals:
path: ../../../_flutterfire_internals
firebase_analytics_platform_interface:
path: ../../firebase_analytics_platform_interface
firebase_analytics_web:
Expand Down
Expand Up @@ -3,47 +3,16 @@
// found in the LICENSE file.

import 'package:firebase_core/firebase_core.dart' show FirebaseException;
import 'package:_flutterfire_internals/_flutterfire_internals.dart';
import 'package:flutter/services.dart';

/// Catches a [PlatformException] and returns an [Exception].
///
/// If the [Exception] is a [PlatformException], a [FirebaseException] is returned.
Never convertPlatformException(Object exception, StackTrace stackTrace) {
if (exception is! Exception || exception is! PlatformException) {
Error.throwWithStackTrace(exception, stackTrace);
}

Error.throwWithStackTrace(
platformExceptionToFirebaseException(exception, stackTrace),
convertPlatformExceptionToFirebaseException(
exception,
stackTrace,
);
}

/// Converts a [PlatformException] into a [FirebaseException].
///
/// A [PlatformException] can only be converted to a [FirebaseException] if the
/// `details` of the exception exist. Firebase returns specific codes and messages
/// which can be converted into user friendly exceptions.
FirebaseException platformExceptionToFirebaseException(
PlatformException platformException,
StackTrace stackTrace,
) {
Map<String, String>? details = platformException.details != null
? Map<String, String>.from(platformException.details)
: null;

String code = 'unknown';
String message = platformException.message ?? '';

if (details != null) {
code = details['code'] ?? code;
message = details['message'] ?? message;
}

return FirebaseException(
plugin: 'firebase_analytics',
code: code,
message: message,
stackTrace: stackTrace,
);
}
Expand Up @@ -9,6 +9,7 @@ environment:
flutter: '>=1.9.1+hotfix.5'

dependencies:
_flutterfire_internals: ^1.0.0
firebase_core: ^1.10.0
flutter:
sdk: flutter
Expand Down
@@ -1,8 +1,6 @@
export 'package:firebase_core/src/internals.dart' hide guardWebExceptions;

import 'package:firebase_core/firebase_core.dart';
// ignore: implementation_imports
import 'package:firebase_core/src/internals.dart' as internals;
import 'package:_flutterfire_internals/_flutterfire_internals.dart'
as internals;

/// Will return a [FirebaseException] from a thrown web error.
/// Any other errors will be propagated as normal.
Expand Down
Expand Up @@ -9,6 +9,7 @@ environment:
flutter: '>=1.12.13+hotfix.4'

dependencies:
_flutterfire_internals: ^1.0.0
firebase_analytics_platform_interface: ^3.3.5
firebase_core: ^1.10.0
firebase_core_web: ^1.7.2
Expand Down
Expand Up @@ -20,6 +20,8 @@ dependencies:
sdk: flutter

dependency_overrides:
_flutterfire_internals:
path: ../../../_flutterfire_internals
cloud_firestore:
path: ../../../cloud_firestore/cloud_firestore
cloud_firestore_platform_interface:
Expand Down

0 comments on commit 7cd76a1

Please sign in to comment.