View package.json
{ | |
"name": "ffnode12", | |
"version": "1.0.0", | |
"main": "index.js", | |
"scripts": { | |
"start": "functions-framework" | |
}, | |
"dependencies": { | |
"@google-cloud/functions-framework": "^1.2.1" | |
} |
View Dockerfile
# Use the official Node.js 12 image. | |
# https://hub.docker.com/_/node | |
FROM node:12 | |
# Create and change to the app directory. | |
WORKDIR /usr/src/app | |
# Copy application dependency manifests to the container image. | |
# A wildcard is used to ensure both package.json AND package-lock.json are copied. | |
# Copying this separately prevents re-running npm install on every code change. | |
COPY package*.json ./ | |
# Install production dependencies. |
View index.js
const {google} = require('googleapis'); | |
/** | |
* Returns a list of Google APIs and their descriptions. | |
* @param {Express.Request} req The API request. | |
* @param {Express.Response} res The API response. | |
*/ | |
exports.listGoogleAPIs = async (req, res) => { | |
// Call the API | |
const apis = await google.discovery('v1').apis.list(); | |
// Build the response |
View settings.json
{ | |
"debug.node.autoAttach": "on" | |
} |
View launch.json
{ | |
"version": "0.2.0", | |
"configurations": [ | |
{ | |
"type": "node", | |
"request": "attach", | |
"name": "Attach", | |
"port": 9229 | |
} | |
] |
View index.js
/** | |
* Returns a list of Google APIs and the link to the API's docs. | |
* @param {Express.Request} req The API request. | |
* @param {Express.Response} res The API response. | |
*/ | |
exports.listGoogleAPIs = async (req, res) => { | |
let sum = 0; | |
for (let i = 0; i < 10; ++i) { | |
sum += i; | |
} |
View btc_sheets.js
/** | |
* Get's the bitcoin price at a date. | |
* | |
* @param {string} date The date in yyyy-MM-dd format. Defaults to today. | |
* @return The value of BTC in USD at the date. From Coinbase's API | |
* @customfunction | |
*/ | |
function BTC_CF(date) { | |
var dateString = Utilities.formatDate(new Date(date), "GMT", "yyyy-MM-dd"); | |
var res = UrlFetchApp.fetch("https://us-central1-new-project-as-test.cloudfunctions.net/btc?date=" + dateString); |
View btc.js
const rp = require('request-promise'); | |
const DEFAULT_DATE = '2019-04-10'; | |
exports.btc = async (req, res) => { | |
const date = req.query.date || DEFAULT_DATE; | |
async function getBTC(date) { | |
const res = await rp(`https://api.coinbase.com/v2/prices/BTC-USD/spot?date=${date}`, { | |
headers: { | |
'CB-VERSION': '2016-10-10', | |
Authorization: `Bearer ${process.env.COINBASE_TOKEN}` |
View index.js
// server | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
// client | |
var b = fetch('https://us-central1-{project}.cloudfunctions.net/{method}').then((response) => { | |
const a = response.json().then(c => {console.log(c)}); | |
}) | |
View index.js
/** | |
* Responds to any HTTP request. | |
* | |
* @param {!express:Request} req HTTP request context. | |
* @param {!express:Response} res HTTP response context. | |
*/ | |
exports.helloWorld = (req, res) => { | |
let message = req.query.message || req.body.message || 'Hello World!'; | |
res.status(200) | |
.set('Content-Type', 'text/html') |