|
1 | 1 | // example of async handler using async-await
|
2 | 2 | // https://github.com/netlify/netlify-lambda/issues/43#issuecomment-444618311
|
3 | 3 |
|
4 |
| -import fetch from 'node-fetch'; |
5 |
| -export async function handler(event, context) { |
| 4 | +const fetch = require("node-fetch") |
| 5 | +exports.handler = async function handler(event, context) { |
6 | 6 | if (!context.clientContext && !context.clientContext.identity) {
|
7 | 7 | return {
|
8 | 8 | statusCode: 500,
|
9 | 9 | body: JSON.stringify({
|
10 | 10 | msg:
|
11 |
| - 'No identity instance detected. Did you enable it? Also, Netlify Identity is not supported on local dev yet.' |
| 11 | + "No identity instance detected. Did you enable it? Also, Netlify Identity is not supported on local dev yet." |
12 | 12 | }) // Could be a custom message or object i.e. JSON.stringify(err)
|
13 |
| - }; |
| 13 | + } |
14 | 14 | }
|
15 |
| - const { identity, user } = context.clientContext; |
| 15 | + const { identity, user } = context.clientContext |
16 | 16 | try {
|
17 |
| - const response = await fetch('https://api.chucknorris.io/jokes/random'); |
| 17 | + const response = await fetch("https://api.chucknorris.io/jokes/random") |
18 | 18 | if (!response.ok) {
|
19 | 19 | // NOT res.status >= 200 && res.status < 300
|
20 |
| - return { statusCode: response.status, body: response.statusText }; |
| 20 | + return { statusCode: response.status, body: response.statusText } |
21 | 21 | }
|
22 |
| - const data = await response.json(); |
| 22 | + const data = await response.json() |
23 | 23 |
|
24 | 24 | return {
|
25 | 25 | statusCode: 200,
|
26 | 26 | body: JSON.stringify({ identity, user, msg: data.value })
|
27 |
| - }; |
| 27 | + } |
28 | 28 | } catch (err) {
|
29 |
| - console.log(err); // output to netlify function log |
| 29 | + console.log(err) // output to netlify function log |
30 | 30 | return {
|
31 | 31 | statusCode: 500,
|
32 | 32 | body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err)
|
33 |
| - }; |
| 33 | + } |
34 | 34 | }
|
35 | 35 | }
|
0 commit comments