Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 05ca7a5

Browse files
author
sw-yx
committed
add token hider function template
1 parent 97f8242 commit 05ca7a5

File tree

6 files changed

+150
-6
lines changed

6 files changed

+150
-6
lines changed

src/commands/functions/create.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ async function downloadFromURL(flags, args, functionsDir) {
278278
const { onComplete, addons = [] } = require(fnTemplateFile);
279279

280280
await installAddons.call(this, addons, path.resolve(fnFolder));
281-
if (onComplete) onComplete();
281+
if (onComplete) await onComplete.call(this);
282282
fs.unlinkSync(fnTemplateFile); // delete
283283
}
284284
}
@@ -293,7 +293,7 @@ async function installDeps(functionPath) {
293293

294294
// no --url flag specified, pick from a provided template
295295
async function scaffoldFromTemplate(flags, args, functionsDir) {
296-
const chosentemplate = await pickTemplate(); // pull the rest of the metadata from the template
296+
const chosentemplate = await pickTemplate.call(this); // pull the rest of the metadata from the template
297297
if (chosentemplate === "url") {
298298
const { chosenurl } = await inquirer.prompt([
299299
{
@@ -375,7 +375,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
375375
}
376376

377377
installAddons.call(this, addons, path.resolve(functionPath));
378-
if (onComplete) onComplete(); // do whatever the template wants to do after it is scaffolded
378+
if (onComplete) await onComplete.call(this); // do whatever the template wants to do after it is scaffolded
379379
});
380380
}
381381
}

src/detect-server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ module.exports.serverSettings = async devConfig => {
2020
settings = settingsArr[0];
2121
settings.args = settings.possibleArgsArrs[0]; // just pick the first one
2222
if (!settings.args) {
23-
console.error(
24-
"empty args assigned, this is an internal Netlify Dev bug, please report your settings and scripts so we can improve"
25-
);
2623
const { scripts } = JSON.parse(
2724
fs.readFileSync("package.json", { encoding: "utf8" })
2825
);
26+
console.error(
27+
"empty args assigned, this is an internal Netlify Dev bug, please report your settings and scripts so we can improve",
28+
{ scripts, settings }
29+
);
2930
process.exit(1);
3031
}
3132
} else if (settingsArr.length > 1) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const chalk = require("chalk");
2+
const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
3+
4+
module.exports = {
5+
name: "token-hider",
6+
description: "Token Hider: access APIs without exposing your API keys",
7+
async onComplete() {
8+
console.log(
9+
`${NETLIFYDEV} ${chalk.yellow(
10+
"token-hider"
11+
)} function created from template!`
12+
);
13+
console.log(
14+
`${NETLIFYDEV} note this function requires ${chalk.yellow(
15+
"API_URL"
16+
)} and ${chalk.yellow(
17+
"API_TOKEN"
18+
)} build environment variables set in your Netlify Site.`
19+
);
20+
21+
let siteData = { name: "YOURSITENAMEHERE" };
22+
try {
23+
siteData = await this.netlify.api.getSite({
24+
siteId: this.netlify.site.id
25+
});
26+
} catch (e) {
27+
// silent error, not important
28+
}
29+
console.log(
30+
`${NETLIFYDEV} Set them at: https://app.netlify.com/sites/${
31+
siteData.name
32+
}/settings/deploys#build-environment-variables (must have CD setup)`
33+
);
34+
}
35+
};

src/functions-templates/js/token-hider/package-lock.json

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "token-hider",
3+
"version": "1.0.0",
4+
"description": "netlify functions:create - how to hide API tokens from your users",
5+
"main": "token-hider.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"netlify",
11+
"serverless",
12+
"apis",
13+
"js"
14+
],
15+
"author": "Netlify",
16+
"license": "MIT",
17+
"dependencies": {
18+
"axios": "^0.18.0",
19+
"qs": "^6.7.0"
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const axios = require("axios");
2+
const qs = require("qs");
3+
4+
export function handler(event, context, callback) {
5+
// apply our function to the queryStringParameters and assign it to a variable
6+
const API_PARAMS = qs.stringify(event.queryStringParameters);
7+
// Get env var values defined in our Netlify site UI
8+
const { API_TOKEN, API_URL } = process.env;
9+
// In this example, the API Key needs to be passed in the params with a key of key.
10+
// We're assuming that the ApiParams var will contain the initial ?
11+
const URL = `${API_URL}?${API_PARAMS}&key=${API_TOKEN}`;
12+
13+
// Let's log some stuff we already have.
14+
console.log("Injecting token to", API_URL);
15+
console.log("logging event.....", event);
16+
console.log("Constructed URL is ...", URL);
17+
18+
// Here's a function we'll use to define how our response will look like when we call callback
19+
const pass = body => {
20+
callback(null, {
21+
statusCode: 200,
22+
body: JSON.stringify(body)
23+
});
24+
};
25+
26+
// Perform the API call.
27+
const get = () => {
28+
axios
29+
.get(URL)
30+
.then(response => {
31+
console.log(response.data);
32+
pass(response.data);
33+
})
34+
.catch(err => pass(err));
35+
};
36+
if (event.httpMethod == "GET") {
37+
get();
38+
}
39+
}

0 commit comments

Comments
 (0)