From 42d3c35c13671afa7b779482fce07e6bb7153988 Mon Sep 17 00:00:00 2001 From: swyx Date: Thu, 6 Dec 2018 18:10:20 +0800 Subject: [PATCH] add async example --- README.md | 2 +- package.json | 1 + src/App.js | 9 ++++++--- src/lambda/async-chuck-norris.js | 25 +++++++++++++++++++++++++ yarn.lock | 5 +++++ 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/lambda/async-chuck-norris.js diff --git a/README.md b/README.md index 6493e2a..73ea314 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This project is based on [Create React App v2](https://github.com/facebookincuba The main addition is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment. -As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`. +As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`. We've also included an async lambda example using async/await syntax in `async-chuck-norris.js`. [![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda) diff --git a/package.json b/package.json index 359d2e1..45d86e8 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.4.0", "private": true, "dependencies": { + "node-fetch": "^2.3.0", "react": "^16.6.3", "react-dom": "^16.6.3", "react-scripts": "^2.1.1" diff --git a/src/App.js b/src/App.js index c903672..adf8cf6 100644 --- a/src/App.js +++ b/src/App.js @@ -8,11 +8,11 @@ class LambdaDemo extends Component { this.state = { loading: false, msg: null }; } - handleClick = e => { + handleClick = api => e => { e.preventDefault(); this.setState({ loading: true }); - fetch('/.netlify/functions/hello') + fetch('/.netlify/functions/' + api) .then(response => response.json()) .then(json => this.setState({ loading: false, msg: json.msg })); }; @@ -22,9 +22,12 @@ class LambdaDemo extends Component { return (

- +
{msg}

diff --git a/src/lambda/async-chuck-norris.js b/src/lambda/async-chuck-norris.js new file mode 100644 index 0000000..b9ace2a --- /dev/null +++ b/src/lambda/async-chuck-norris.js @@ -0,0 +1,25 @@ +// example of async handler using async-await +// https://github.com/netlify/netlify-lambda/issues/43#issuecomment-444618311 + +import fetch from 'node-fetch'; +export async function handler(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) + }; + } +} diff --git a/yarn.lock b/yarn.lock index 29cebc4..4224596 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6046,6 +6046,11 @@ no-case@^2.2.0: dependencies: lower-case "^1.1.1" +node-fetch@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5" + integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA== + node-forge@0.7.5: version "0.7.5" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.5.tgz#6c152c345ce11c52f465c2abd957e8639cd674df"