Skip to content

Commit

Permalink
supports auth
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Aug 15, 2024
1 parent 6f892a0 commit 4e357fc
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 14 deletions.
2 changes: 2 additions & 0 deletions firebase_functions/lib/firebase_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export 'package:tekartik_firebase_firestore/firestore.dart'
export 'package:tekartik_firebase_functions/src/express_http_request.dart'
show
ExpressHttpRequest,
ExpressHttpRequestExt,
ExpressHttpResponse,
ExpressHttpRequestWrapperBase,
ExpressHttpResponseWrapperBase,
Expand All @@ -18,6 +19,7 @@ export 'package:tekartik_firebase_functions/src/firebase_functions.dart'
CallHandler,
CallRequest,
CallRequestMixin,
CallRequestExt,
CallContext,
CallContextAuth,
CallContextAuthMixin,
Expand Down
19 changes: 15 additions & 4 deletions firebase_functions/lib/src/express_http_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import 'package:tekartik_http/src/utils.dart'; // ignore: implementation_imports
String requestBodyAsText(dynamic body) {
if (body is String) {
return body;
} else if (body is List) {
} else if (body is Uint8List) {
return utf8.decode(body.cast<int>());
} else if (body is Map) {
return jsonEncode(body);
}
throw 'body $body not text';
}

Map<String, dynamic>? requestBodyAsJsonObject(dynamic body) {
Map<String, Object?>? requestBodyAsJsonObject(dynamic body) {
if (body == null) {
return null;
} else if (body is Map) {
return body.cast<String, dynamic>();
return body.cast<String, Object?>();
} else if (body is String) {
try {
return (json.decode(body) as Map?)?.cast<String, dynamic>();
return (json.decode(body) as Map?)?.cast<String, Object?>();
} catch (_) {
return null;
}
Expand All @@ -47,6 +49,15 @@ abstract class ExpressHttpRequest {
Uri get requestedUri;
}

/// Extension to get the body as a map
extension ExpressHttpRequestExt on ExpressHttpRequest {
/// Get the body as a map
Map<String, Object?>? get bodyAsMap => requestBodyAsJsonObject(body);

/// Get the body as a text
String? get bodyAsText => requestBodyAsText(body);
}

abstract class ExpressHttpResponse {
/// send closes too
/// Node only supports this.
Expand Down
12 changes: 12 additions & 0 deletions firebase_functions/lib/src/firebase_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,25 @@ abstract class CallRequest {

/// Call request
mixin CallRequestMixin implements CallRequest {
@override
CallContext get context => throw UnimplementedError('context');

@override
String? get text => data?.toString();

@override
Object? get data => null;
}

/// Extension to get the body as a map
extension CallRequestExt on CallRequest {
/// Get the body as a map
Map<String, Object?>? get dataAsMap => requestBodyAsJsonObject(data);

/// Get the body as a text
String? get dataAsText => requestBodyAsText(data);
}

/// Call request handler.
///
/// The object returned is the response. Could be a map object
Expand Down
14 changes: 14 additions & 0 deletions firebase_functions/test/mock_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,25 @@ class DocumentBuilderMock
with DocumentBuilderDefaultMixin
implements DocumentBuilder {}

class CallRequestMock with CallRequestMixin {
@override
final Object? data;
CallRequestMock({this.data});
}

void main() {
test('mock', () {
ScheduleEventMock();
SchedulerFunctionsMock();
FirebaseFunctionsMock();
FirestoreFunctionsMock();
});
test('call_request', () {
var request = CallRequestMock();
expect(request.data, null);
request = CallRequestMock(data: <String, Object?>{});
expect(request.dataAsText, '{}');
request = CallRequestMock(data: '{}');
expect(request.dataAsMap, <String, Object?>{});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import 'package:path/path.dart' as p;
import 'package:tekartik_firebase_functions/ff_server.dart';
import 'package:tekartik_firebase_functions_test/firebase_functions_test.dart'; // ignore: depend_on_referenced_packages

import '../ff_server.dart';
import '../firebase_functions_memory.dart';
import '../src/import.dart';

class FirebaseFunctionsTestContextHttp extends FirebaseFunctionsTestContext
with
FirebaseFunctionsTestServerContextHttpMixin,
Expand All @@ -31,9 +27,7 @@ mixin FirebaseFunctionsTestServerContextHttpMixin
/// Serve locally
@override
Future<FfServer> serve() async {
var server =
await (firebaseFunctions as FirebaseFunctionsHttp).serveHttp(port: 0);
return this.server = FfServerHttp(server);
return server = await firebaseFunctions.serve();
}
}
mixin FirebaseFunctionsTestClientContextHttpMixin
Expand Down
1 change: 1 addition & 0 deletions functions_call/lib/functions_call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export 'src/functions_call.dart'
FirebaseFunctionsCallable,
FirebaseFunctionsCallableOptions,
FirebaseFunctionsCallableResult,
FirebaseFunctionsCallableResultExt,
FirebaseFunctionsCallableResultDefaultMixin;
15 changes: 13 additions & 2 deletions functions_call/lib/src/functions_call.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:tekartik_firebase/firebase.dart';
import 'package:tekartik_firebase/firebase_mixin.dart';
import 'package:tekartik_firebase_functions/firebase_functions.dart';

/// Interface representing an HttpsCallable instance's options,
class FirebaseFunctionsCallableOptions {
Expand Down Expand Up @@ -53,7 +54,7 @@ abstract class FirebaseFunctionsCallable {
/// automatically includes a Firebase Instance ID token to identify the app
/// instance. If a user is logged in with Firebase Auth, an auth ID token for
/// the user is also automatically included.
Future<FirebaseFunctionsCallableResult> call<T>([Object? parameters]);
Future<FirebaseFunctionsCallableResult<T>> call<T>([Object? parameters]);
}

/// Firebase functions callable result.
Expand All @@ -62,6 +63,16 @@ abstract class FirebaseFunctionsCallableResult<T> {
T get data;
}

/// Firebase functions callable result helper extension
extension FirebaseFunctionsCallableResultExt
on FirebaseFunctionsCallableResult {
/// Get the body as a map
Map<String, Object?>? get dataAsMap => requestBodyAsJsonObject(data);

/// Get the body as a text
String? get dataAsText => requestBodyAsText(data);
}

/// Firebase functions callable result default mixin
mixin FirebaseFunctionsCallableResultDefaultMixin<T>
implements FirebaseFunctionsCallableResult<T> {
Expand Down Expand Up @@ -104,7 +115,7 @@ mixin FirebaseFunctionsCallServiceDefaultMixin
mixin FirebaseFunctionsCallableDefaultMixin
implements FirebaseFunctionsCallable {
@override
Future<FirebaseFunctionsCallableResult> call<T>([Object? parameters]) {
Future<FirebaseFunctionsCallableResult<T>> call<T>([Object? parameters]) {
throw UnimplementedError('FirebaseFunctionsCallable.call');
}
}
Expand Down
2 changes: 1 addition & 1 deletion functions_call_http/lib/src/functions_call_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class FirebaseFunctionsCallableHttp implements FirebaseFunctionsCallable {
FirebaseFunctionsCallableHttp(this.functionsCallHttp, this.name);

@override
Future<FirebaseFunctionsCallableResultHttp> call<T>(
Future<FirebaseFunctionsCallableResultHttp<T>> call<T>(
[Object? parameters]) async {
var service = functionsCallHttp.service;
var httpClient = service.httpClientFactory.newClient();
Expand Down

0 comments on commit 4e357fc

Please sign in to comment.