This example demonstrates an HTTP Trigger Function providing YAML parsing with js-yaml. Default safeLoad and safeDump actions are supported along with restricted options.
Example uses the Express framework.
See file functions/index.js for the code.
The dependencies are listed in functions/package.json.
- Clone or download this repo and open the
yaml-parser
directory. - If you don't already have one, create a Firebase Project using the Firebase Console.
- If the Firebase CLI is not install, install it with
npm install -g firebase-tools
and then configure it withfirebase login
. - Configure the CLI locally by using
firebase use --add
and select your project in the list. - Install dependencies locally by running:
cd functions; npm install; cd -
IMPORTANT:
Cloud Function only supports POST requests.
Requests to Cloud Function should specify header contentType
of application/json
and contain the following fields:
- content - YAML content (for LOAD) / JSON content (for DUMP)
- action - action to be performed by Cloud Function (possible values: "LOAD" or "DUMP")
- (optional) options
Example LOAD Request (YAML):
{
"content": "cats:\n- rats\n- mats\n- hats",
"action": "LOAD"
}
Example LOAD Response (JSON):
{
"cats": [
"rats",
"mats",
"hats"
]
}
Example DUMP Request (JSON):
{
"content":{
"cats":["rats", "mats", "hats"]
},
"action":"dump",
"options": {
"indent": 4
}
}
Example DUMP Response (YAML):
cats:
- rats
- mats
- hats
Deploy project using firebase deploy --only functions
Example Google Apps Script to call the deployed Cloud Function to LOAD YAML and return as JSON.
...
function loadYaml() {
var response, responseCode;
// DEVELOPER TODO: Replace "functionUrl" with the deployed Cloud Function URL from the Firebase Console
// Consider storing URL in a Google Apps Script Properties Store
var functionUrl = "https://<FIREBASE_CLOUD_FUNCTION_URL>/yamlParser";
// DEVELOPER TODO: Replace "content" with YAML content to be LOADed to JSON
var yaml = "cats:\n- rats\n- mats\n- hats";
var requestBody = {
content: yaml,
action: "LOAD"
};
var params = {
'method': 'POST',
'contentType': 'application/json',
'payload' : JSON.stringify(requestBody)
};
response = UrlFetchApp.fetch(functionUrl + "/", params);
responseCode = response.getResponseCode();
if (responseCode == 200) {
// Success fetching file from Cloud Function
return JSON.parse(response.getContentText());
} else {
// Error returned from Cloud Function
Logger.log('Error loading YAML. Response code: ' + responseCode);
return null;
}
}
...
Example Google Apps Script to call the deployed Cloud Function to DUMP JSON and return as YAML.
...
function dumpYaml() {
var response, responseCode;
// DEVELOPER TODO: Replace "functionUrl" with the deployed Cloud Function URL from the Firebase Console
// Consider storing URL in a Google Apps Script Properties Store
var functionUrl = "https://<FIREBASE_CLOUD_FUNCTION_URL>/yamlParser";
// DEVELOPER TODO: Replace "content" with JSON content to be DUMPed to YAML
var json = { cats: ["rats", "mats","hats"] };
var requestBody = {
content: json,
action: "DUMP",
options: {
indent: 4
}
};
var params = {
'method': 'POST',
'contentType': 'application/json',
'payload' : JSON.stringify(requestBody)
};
response = UrlFetchApp.fetch(functionUrl + "/", params);
responseCode = response.getResponseCode();
if (responseCode == 200) {
// Success fetching file from Cloud Function
return response.getContentText();
} else {
// Error returned from Cloud Function
Logger.log('Error dumping YAML. Response code: ' + responseCode);
return null;
}
}
...
© Laura Taylor. Licensed under an MIT license.