Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions example/basic/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,75 @@ final isProduction = defineBoolean(

void main(List<String> args) {
fireUp(args, (firebase) {
// ==========================================================================
// HTTPS Callable Functions (onCall / onCallWithData)
// ==========================================================================

// Basic callable function - untyped data
firebase.https.onCall(
name: 'greet',
(request, response) async {
final data = request.data as Map<String, dynamic>?;
final name = data?['name'] ?? 'World';
return CallableResult({'message': 'Hello, $name!'});
},
);

// Callable function with typed data using fromJson
firebase.https.onCallWithData<GreetRequest, GreetResponse>(
name: 'greetTyped',
fromJson: GreetRequest.fromJson,
(request, response) async {
return GreetResponse(message: 'Hello, ${request.data.name}!');
},
);

// Callable function demonstrating error handling
firebase.https.onCall(
name: 'divide',
(request, response) async {
final data = request.data as Map<String, dynamic>?;
final a = (data?['a'] as num?)?.toDouble();
final b = (data?['b'] as num?)?.toDouble();

if (a == null || b == null) {
throw InvalidArgumentError('Both "a" and "b" are required');
}

if (b == 0) {
throw FailedPreconditionError('Cannot divide by zero');
}

return CallableResult({'result': a / b});
},
);

// Callable function with streaming support
firebase.https.onCall(
name: 'countdown',
options: const CallableOptions(
heartBeatIntervalSeconds: HeartBeatIntervalSeconds(5),
),
(request, response) async {
final data = request.data as Map<String, dynamic>?;
final start = (data?['start'] as num?)?.toInt() ?? 10;

// Stream countdown if client supports it
if (request.acceptsStreaming) {
for (var i = start; i >= 0; i--) {
await response.sendChunk({'count': i});
await Future<void>.delayed(const Duration(milliseconds: 100));
}
}

return CallableResult({'message': 'Countdown complete!'});
},
);

// ==========================================================================
// HTTPS onRequest Functions
// ==========================================================================

// HTTPS onRequest example - using parameterized configuration
firebase.https.onRequest(
name: 'helloWorld',
Expand Down Expand Up @@ -251,3 +320,29 @@ void main(List<String> args) {
print('Functions registered successfully!');
});
}

// =============================================================================
// Data classes for typed callable functions
// =============================================================================

/// Request data for the greetTyped callable function.
class GreetRequest {
GreetRequest({required this.name});

factory GreetRequest.fromJson(Map<String, dynamic> json) {
return GreetRequest(name: json['name'] as String? ?? 'World');
}

final String name;

Map<String, dynamic> toJson() => {'name': name};
}

/// Response data for the greetTyped callable function.
class GreetResponse {
GreetResponse({required this.message});

final String message;

Map<String, dynamic> toJson() => {'message': message};
}
49 changes: 48 additions & 1 deletion example/nodejs_reference/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* generates compatible functions.yaml output.
*/

const { onRequest } = require("firebase-functions/v2/https");
const { onRequest, onCall, HttpsError } = require("firebase-functions/v2/https");
const { onMessagePublished } = require("firebase-functions/v2/pubsub");
const { onDocumentCreated, onDocumentUpdated, onDocumentDeleted, onDocumentWritten } = require("firebase-functions/v2/firestore");
const { onValueCreated, onValueUpdated, onValueDeleted, onValueWritten } = require("firebase-functions/v2/database");
Expand Down Expand Up @@ -33,6 +33,53 @@ const isProduction = defineBoolean("IS_PRODUCTION", {
description: "Whether this is a production deployment",
});

// =============================================================================
// HTTPS Callable Functions (onCall)
// =============================================================================

// Basic callable function - untyped data
exports.greet = onCall((request) => {
const name = request.data?.name ?? "World";
return { message: `Hello, ${name}!` };
});

// Callable function with typed data (same manifest structure as untyped)
exports.greetTyped = onCall((request) => {
const name = request.data?.name ?? "World";
return { message: `Hello, ${name}!` };
});

// Callable function demonstrating error handling
exports.divide = onCall((request) => {
const a = request.data?.a;
const b = request.data?.b;

if (a === undefined || b === undefined) {
throw new HttpsError("invalid-argument", 'Both "a" and "b" are required');
}

if (b === 0) {
throw new HttpsError("failed-precondition", "Cannot divide by zero");
}

return { result: a / b };
});

// Callable function with streaming support
exports.countdown = onCall(
{
// heartbeatSeconds is a runtime option, not in manifest
},
(request) => {
// Streaming is handled at runtime, not in manifest
return { message: "Countdown complete!" };
}
);

// =============================================================================
// HTTPS onRequest Functions
// =============================================================================

// HTTPS onRequest example - using parameterized configuration
exports.helloWorld = onRequest(
{
Expand Down
Loading
Loading