This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from netlify/addInquirer
add working basic templates for netlify functions:create
- Loading branch information
Showing
6 changed files
with
136 additions
and
65 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## note to devs | ||
|
||
place new templates here and our CLI will pick it up. currently only works for single file `.js` templates. | ||
|
||
## why place it in this separate folder | ||
|
||
we dont colocate this inside `src/commands/functions` because oclif will think it's a new command. | ||
|
||
## providing metadata (and other functionality) | ||
|
||
we split the file based on the `// --- Netlify Template Below -- //` string. everything below it is cloned as the template. everything above it can be required and run as a module for configuring the template. for now we simply export a `metadata` object that fits [`inquirer's choices spec`](https://www.npmjs.com/package/inquirer#question). in future we can think about other options we may want to offer. | ||
|
||
## future dev thoughts | ||
|
||
we will want a way to scale this to TS and Go as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// --- Netlify Template Metadata -- // | ||
exports.metadata = { | ||
name: 'Basic Hello World function: shows async/await usage, and proper formatting with statusCode and body', | ||
value: 'hello-world', | ||
short: 'hello-world' | ||
} | ||
// --- Netlify Template Below -- // | ||
async function hello() { | ||
return Promise.resolve('Hello, World') | ||
} | ||
|
||
exports.handler = async function(event, context) { | ||
try { | ||
const body = await hello() | ||
return { statusCode: 200, body } | ||
} catch (err) { | ||
return { statusCode: 500, body: err.toString() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// --- Netlify Template Metadata -- // | ||
exports.metadata = { | ||
name: 'Fetch function: uses node-fetch to hit an external API without CORS issues', | ||
value: 'node-fetch', | ||
short: 'node-fetch' | ||
} | ||
// --- Netlify Template Below -- // | ||
const fetch = require('node-fetch') | ||
exports.handler = async function(event, context) { | ||
try { | ||
const response = await fetch('https://api.chucknorris.io/jokes/random') | ||
if (!response.ok) { | ||
// NOT res.status >= 200 && res.status < 300 | ||
return { statusCode: response.status, body: response.statusText } | ||
} | ||
const data = await response.json() | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ msg: data.value }) | ||
} | ||
} catch (err) { | ||
console.log(err) // output to netlify function log | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err) | ||
} | ||
} | ||
} |