-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (63 loc) · 2.45 KB
/
index.js
File metadata and controls
71 lines (63 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Reference Node.js implementation matching the Dart example.
* This is used for snapshot testing to ensure the Dart builder
* generates compatible functions.yaml output.
*/
const { onRequest } = require("firebase-functions/v2/https");
const { onMessagePublished } = require("firebase-functions/v2/pubsub");
const { defineString, defineInt, defineBoolean } = require("firebase-functions/params");
// =============================================================================
// Parameterized Configuration Examples
// =============================================================================
// Define parameters - these are read from environment variables at runtime
// and can be configured at deploy time via .env files or CLI prompts.
const welcomeMessage = defineString("WELCOME_MESSAGE", {
default: "Hello from Dart Functions!",
label: "Welcome Message",
description: "The greeting message returned by the helloWorld function",
});
const minInstances = defineInt("MIN_INSTANCES", {
default: 0,
label: "Minimum Instances",
description: "Minimum number of instances to keep warm",
});
const isProduction = defineBoolean("IS_PRODUCTION", {
default: false,
description: "Whether this is a production deployment",
});
// HTTPS onRequest example - using parameterized configuration
exports.helloWorld = onRequest(
{
// Use parameters in options - evaluated at deploy time
minInstances: minInstances,
},
(request, response) => {
// Access parameter value at runtime
response.send(welcomeMessage.value());
}
);
// Conditional configuration based on boolean parameter
exports.configuredEndpoint = onRequest(
{
// Use thenElse for conditional configuration at deploy time
// isProduction.thenElse(trueValue, falseValue) returns an expression
memory: isProduction.thenElse(2048, 512),
},
(request, response) => {
// Access parameter value at runtime
const env = isProduction.value() ? "production" : "development";
response.send(`Running in ${env} mode`);
}
);
// Pub/Sub trigger example
exports.onMessagePublished_mytopic = onMessagePublished(
"my-topic",
(event) => {
const message = event.data.message;
console.log("Received Pub/Sub message:");
console.log(" ID:", message.messageId);
console.log(" Published:", message.publishTime);
console.log(" Data:", message.data ? Buffer.from(message.data, "base64").toString() : "");
console.log(" Attributes:", message.attributes);
}
);