adding packages

This commit is contained in:
2026-01-15 14:38:46 -08:00
parent ef86e3ab6a
commit 6869cf47e5
5253 changed files with 726695 additions and 34 deletions
+57
View File
@@ -0,0 +1,57 @@
library dart_firebase.impl.browser;
import 'dart:async';
import 'dart:convert';
import 'dart:html';
import '../../api.dart';
import 'common/http.dart';
class FirestoreClientImpl extends FirestoreHttpClient {
FirestoreClientImpl(String email, String password, App app,
FirestoreAccessToken token, FirestoreApiEndpoints endpoints)
: super(email, password, app, token, endpoints);
@override
Future<dynamic> sendHttpRequest(Uri uri,
{bool needsToken: true,
String? extract,
Map<String, dynamic>? body}) async {
var request = new HttpRequest();
request.open(body == null ? "GET" : "POST", uri.toString());
if (needsToken) {
if (!isCurrentTokenValid(true)) {
await login();
}
request.setRequestHeader("Authorization", "Bearer ${token.accessToken}");
}
if (body != null) {
request.setRequestHeader(
"Content-Type", "application/json; charset=utf-8");
request.send(const JsonEncoder().convert(body));
} else {
request.send();
}
await request.onLoadEnd.first;
var content = request.responseText!;
if (request.status != 200) {
throw new Exception(
"Failed to perform action. (Status Code: ${request.status})\n${content}");
}
var result = const JsonDecoder().convert(content);
if (result is! Map) {
if (extract != null) {
return result[extract];
}
}
return result;
}
@override
Future close() async {}
}
+132
View File
@@ -0,0 +1,132 @@
library dart_firebase.impl.common.http;
import 'dart:async';
import '../../../api.dart';
const String _defaultAppName = "[DEFAULT]";
abstract class FirestoreHttpClient implements FirestoreClient {
FirestoreHttpClient(
this.email, this.password, this.app, this.token, this.endpoints);
@override
final String email;
@override
final String password;
@override
final App app;
@override
final FirestoreApiEndpoints endpoints;
@override
FirestoreAccessToken token;
bool isCurrentTokenValid(bool refreshable) {
if (token == null) {
return false;
}
if (refreshable) {
var now = DateTime.now();
return token.expiresAt.difference(now).abs().inSeconds >= 60;
}
return true;
}
@override
bool get isAuthorized => isCurrentTokenValid(true);
@override
Future login() async {
if (!isCurrentTokenValid(false)) {
var result = await sendHttpRequest(endpoints.getAuthUrl(app),
body: {
"email": email,
"password": password,
"returnSecureToken": true,
},
needsToken: false);
token = FirestoreJsonAccessToken(result, DateTime.now());
return;
}
var result = await sendHttpRequest(endpoints.getRefreshUrl(app),
body: {
"grant_type": "refresh_token",
"refresh_token": token.refreshToken
},
needsToken: false);
token = FirestoreJsonAccessToken(result, DateTime.now());
}
@override
Future<List<DocumentSnapshot>> listDocumentSnapshots(String path) async {
var list = <DocumentSnapshot>[];
var result = await (getJsonList("$path", extract: 'documents') as FutureOr<List<dynamic>>);
for (var item in result) {
list.add(new DocumentSnapshot(this, item));
}
return list;
}
@override
CollectionReference collection(String path) {
assert(path != null);
return CollectionReference(this, path.split('/'));
}
@override
DocumentReference document(String path) {
assert(path != null);
return DocumentReference(this, path.split('/'));
}
@override
Future<DocumentSnapshot> getDocumentSnapshot(String path) async {
final _data = await getJsonMap("$path", extract: null);
return DocumentSnapshot(this, _asStringKeyedMap(_data));
}
Future<Map<String, dynamic>?> getJsonMap(String url,
{Map<String, dynamic>? body,
String? extract: "response",
bool standard: true}) async {
return (await sendHttpRequest(_apiUrl(url, standard),
body: body, extract: extract)) as Map<String, dynamic>?;
}
Future<List<dynamic>?> getJsonList(String url,
{Map<String, dynamic>? body,
String extract: "response",
bool standard: true}) async {
return (await sendHttpRequest(_apiUrl(url, standard),
body: body, extract: extract)) as List<dynamic>?;
}
Future<dynamic> sendHttpRequest(Uri uri,
{bool needsToken: true, String? extract, Map<String, dynamic>? body});
Uri _apiUrl(String path, bool standard) {
path = standard ? "$path" : path;
var uri = endpoints.getFirestoreUrl(app).resolve(path);
return uri;
}
}
Map<String, dynamic>? _asStringKeyedMap(Map<dynamic, dynamic>? map) {
if (map == null) return null;
if (map is Map<String, dynamic>) {
return map;
} else {
return Map<String, dynamic>.from(map);
}
}
+71
View File
@@ -0,0 +1,71 @@
library dart_firebase.impl.io;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import '../../api.dart';
import 'common/http.dart';
final ContentType _jsonContentType =
new ContentType("application", "json", charset: "utf-8");
HttpClient _createHttpClient() {
var client = new HttpClient();
client.userAgent = "Firestore.dart";
return client;
}
class FirestoreClientImpl extends FirestoreHttpClient {
FirestoreClientImpl(String email, String password, App app,
FirestoreAccessToken token, FirestoreApiEndpoints endpoints,
{HttpClient? client})
: this.client = client == null ? _createHttpClient() : client,
super(email, password, app, token, endpoints);
final HttpClient client;
@override
Future<dynamic> sendHttpRequest(Uri uri,
{bool needsToken: true,
String? extract,
Map<String, dynamic>? body}) async {
if (endpoints.enableProxyMode) {
uri = uri.replace(queryParameters: {"__firebase": "api"});
}
var request =
body == null ? await client.getUrl(uri) : await client.postUrl(uri);
request.headers.set("User-Agent", "Firestore.dart");
if (needsToken) {
if (!isCurrentTokenValid(true)) {
await login();
}
request.headers.add("Authorization", "Bearer ${token.accessToken}");
}
if (body != null) {
request.headers.contentType = _jsonContentType;
request.write(const JsonEncoder().convert(body));
}
var response = await request.close();
var content = await response.transform(const Utf8Decoder()).join();
if (response.statusCode != 200) {
throw new Exception(
"Failed to perform action. $uri (Status Code: ${response.statusCode})\n${content}");
}
var result = const JsonDecoder().convert(content);
if (result is Map) {
if (extract != null) {
return result[extract];
}
}
return result;
}
@override
Future close() async {
client.close();
}
}
+68
View File
@@ -0,0 +1,68 @@
library dart_firebase.impl.unsupported;
import 'dart:async';
import '../../api.dart';
class FirestoreClientImpl implements FirestoreClient {
FirestoreClientImpl(String email, String password, App? app,
FirestoreAccessToken? token, FirestoreApiEndpoints endpoints) {
throw "This platform is not supported.";
}
@override
App get app => throw "This platform is not supported.";
@override
String get email => throw "This platform is not supported.";
@override
String get password => throw "This platform is not supported.";
@override
FirestoreAccessToken get token => throw "This platform is not supported.";
@override
set token(FirestoreAccessToken token) =>
throw "This platform is not supported.";
@override
bool get isAuthorized => throw "This platform is not supported.";
@override
FirestoreApiEndpoints get endpoints =>
throw "This platform is not supported.";
@override
Future<DocumentSnapshot> getDocumentSnapshot(String path) {
throw "This platform is not supported.";
}
@override
Future<List<DocumentSnapshot>> listDocumentSnapshots(String path) {
throw "This platform is not supported.";
}
@override
Future login() {
throw "This platform is not supported.";
}
@override
Future close() {
throw "This platform is not supported.";
}
@override
String get apiKey => throw "This platform is not supported.";
@override
CollectionReference collection(String path) {
throw "This platform is not supported.";
}
@override
DocumentReference document(String path) {
throw "This platform is not supported.";
}
}