Skip to content

Commit

Permalink
exp: onCall
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jul 3, 2024
1 parent 61b444d commit a5cd289
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
7 changes: 4 additions & 3 deletions firebase_functions/lib/firebase_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export 'package:tekartik_firebase_functions/src/firebase_functions.dart'
FirebaseFunctionsServiceDefaultMixin,
FirebaseFunctionsDefaultMixin,
FirebaseFunction,
CallFunction,
CallHandler,
CallRequest,
CallRequestMixin,
Expand Down Expand Up @@ -43,10 +42,12 @@ export 'package:tekartik_firebase_functions/src/firebase_functions_https.dart'
show
HttpsFunction,
HttpsFunctions,
HttpsFunctionsMixin,
HttpsFunctionsDefaultMixin,
HttpsError,
HttpsErrorCode,
HttpsOptions;
HttpsOptions,
HttpsCallableOptions,
HttpsCallableFunction;

export 'package:tekartik_http/http_server.dart';

Expand Down
3 changes: 0 additions & 3 deletions firebase_functions/lib/src/firebase_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ typedef CallHandler = FutureOr<Object?> Function(CallRequest request);

abstract class FirebaseFunction {}

/// Https function.
abstract class CallFunction implements FirebaseFunction {}

/// Pubsub function
abstract class PubsubFunction implements FirebaseFunction {}

Expand Down
29 changes: 26 additions & 3 deletions firebase_functions/lib/src/firebase_functions_https.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ abstract class HttpsFunctions {
HttpsFunction onRequestV2(HttpsOptions httpsOptions, RequestHandler handler);

/// call request
CallFunction onCall(CallHandler handler);
HttpsCallableFunction onCall(CallHandler handler,
{HttpsCallableOptions? callableOptions});
}

/// no-op by default to never break compilation
mixin HttpsFunctionsMixin implements HttpsFunctions {
mixin HttpsFunctionsDefaultMixin implements HttpsFunctions {
@override
CallFunction onCall(CallHandler handler) =>
HttpsCallableFunction onCall(CallHandler handler,
{HttpsCallableOptions? callableOptions}) =>
throw UnimplementedError('onCall');

@override
Expand All @@ -37,6 +39,27 @@ mixin HttpsFunctionsMixin implements HttpsFunctions {
/// Https function.
abstract class HttpsFunction implements FirebaseFunction {}

/// Callable function.
abstract class HttpsCallableFunction implements FirebaseFunction {}

/// Callable options
class HttpsCallableOptions extends HttpsOptions {
/// Determines whether Firebase App Check token is consumed on request. Defaults to false.
final bool? consumeAppCheckToken;

/// Determines whether Firebase AppCheck is enforced. When true, requests with invalid tokens autorespond with a 401 (Unauthorized) error. When false, requests with invalid tokens set event.app to undefiend.
final bool? enforceAppCheck;
HttpsCallableOptions(
{this.consumeAppCheckToken,
this.enforceAppCheck,
super.cors,
super.concurrency,
super.memory,
super.region,
super.regions,
super.timeoutSeconds});
}

/// Https options
class HttpsOptions extends GlobalOptions {
/// Set to true to allow cors
Expand Down
11 changes: 6 additions & 5 deletions firebase_functions_http/lib/src/firebase_functions_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ abstract class FirebaseFunctionsHttp implements FirebaseFunctions {
Future onFileRequestHttp(HttpRequest request);
}

class HttpsHttp with HttpsFunctionsMixin implements HttpsFunctions {
class HttpsHttp with HttpsFunctionsDefaultMixin implements HttpsFunctions {
HttpsHttp();

@override
Expand All @@ -172,8 +172,9 @@ class HttpsHttp with HttpsFunctionsMixin implements HttpsFunctions {
}

@override
CallFunction onCall(CallHandler handler) {
return CallFunctionHttp(handler);
HttpsCallableFunction onCall(CallHandler handler,
{HttpsCallableOptions? callableOptions}) {
return HttpsCallableFunctionHttp(handler);
}
}

Expand All @@ -185,11 +186,11 @@ class HttpsFunctionHttp implements HttpsFunction {
HttpsFunctionHttp(this.options, this.handler);
}

class CallFunctionHttp implements CallFunction {
class HttpsCallableFunctionHttp implements HttpsCallableFunction {
// ignore: unused_field
final CallHandler handler;

CallFunctionHttp(this.handler);
HttpsCallableFunctionHttp(this.handler);
}

String rewritePath(String path) {
Expand Down
1 change: 1 addition & 0 deletions firebase_functions_test/lib/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const functionCallName = 'callprv';
13 changes: 9 additions & 4 deletions firebase_functions_test/lib/firebase_functions_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:tekartik_firebase_functions/firebase_functions.dart';
import 'package:tekartik_firebase_functions_test/src/firebase_functions_test_context.dart';
import 'package:tekartik_firebase_functions_test/src/import.dart';

import 'constants.dart';

void echoBytesHandler(ExpressHttpRequest request) {
var body = request.body;
//devPrint('echoBytes ${body?.runtimeType}: ${body}');
Expand Down Expand Up @@ -50,10 +52,10 @@ void echoHeadersHandler(ExpressHttpRequest request) {
request.response.send(sb.toString());
}

FutureOr<dynamic> callHandler(CallRequest request) async {
FutureOr<Object?> callHandler(CallRequest request) async {
//devPrint('request data ${request.text}');
try {
return jsonDecode(request.text!);
return {'uid': request.context.auth?.uid, 'data': request.data};
} catch (_) {
return {'error': 'no_body'};
}
Expand Down Expand Up @@ -97,7 +99,10 @@ T setup<T extends FirebaseFunctionsTestServerContext>(

/// temp out for node testing
try {
firebaseFunctions['call'] = firebaseFunctions.https.onCall(callHandler);
firebaseFunctions[functionCallName] = firebaseFunctions.https.onCall(
callHandler,
callableOptions:
HttpsCallableOptions(cors: true, region: regionBelgium));
} catch (e) {
print('error onCall definition $e');
}
Expand All @@ -113,5 +118,5 @@ var testFunctionNames = [
'echoheaders',
'echoinfo',
'ffinfo',
'call'
functionCallName
];

0 comments on commit a5cd289

Please sign in to comment.