netlify-cli
Version:
Netlify command line tool
27 lines (24 loc) • 856 B
text/typescript
// example of async handler using async-await
// https://github.com/netlify/netlify-lambda/issues/43#issuecomment-444618311
import fetch from 'node-fetch'
import { Context } from 'aws-lambda'
export async function handler(event: any, context: 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)
}
}
}