Setup something simple and straight-forward
This commit is contained in:
parent
079f25673b
commit
5ec7cd193e
7 changed files with 85 additions and 4 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,5 @@
|
|||
.DS_Store
|
||||
node_modules
|
||||
|
||||
# Local Netlify folder
|
||||
.netlify
|
||||
|
|
11
netlify.toml
Normal file
11
netlify.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[build]
|
||||
publish = "public"
|
||||
command = "echo No build command"
|
||||
node_bundler = "esbuild"
|
||||
|
||||
[dev]
|
||||
publish = "public"
|
||||
node_bundler = "esbuild"
|
||||
|
||||
[functions]
|
||||
node_bundler = "esbuild"
|
33
netlify/functions/hello-world.ts
Normal file
33
netlify/functions/hello-world.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { Handler } from '@netlify/functions'
|
||||
import { api } from "./shared/api"
|
||||
|
||||
const handler: Handler = async (event, context) => {
|
||||
console.log(event)
|
||||
let lastfm = await api<{
|
||||
recenttracks: {
|
||||
track: {
|
||||
artist: {
|
||||
"#text": string
|
||||
},
|
||||
image: {
|
||||
size: string,
|
||||
"#text": string
|
||||
}[]
|
||||
album: {
|
||||
"#text": string
|
||||
},
|
||||
name: string,
|
||||
"@attr"?: {
|
||||
nowplaying?: string
|
||||
}
|
||||
}[]
|
||||
}
|
||||
}>
|
||||
(`http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=TTTaevas&api_key=${process.env["API_LASTFM"]}&format=json&limit=1`)
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(lastfm.recenttracks.track[0])
|
||||
}
|
||||
}
|
||||
|
||||
export { handler }
|
10
netlify/functions/shared/api.ts
Normal file
10
netlify/functions/shared/api.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
// Standard variation
|
||||
export async function api<T>(url: string): Promise<T> {
|
||||
return fetch(url)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText)
|
||||
}
|
||||
return response.json() as Promise<T>
|
||||
})
|
||||
}
|
5
package.json
Normal file
5
package.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"@netlify/functions": "^1.4.0"
|
||||
}
|
||||
}
|
|
@ -4,10 +4,10 @@
|
|||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Jamstack Explorers - Up and Running with Serverless Functions</title>
|
||||
<title>Simple last.fm thingie</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Up and Running with Serverless Functions</h1>
|
||||
<h1>Simple last.fm thingie</h1>
|
||||
<button id="fetch-btn">Fetch</button>
|
||||
<p id="response-output">Response Placeholder</p>
|
||||
|
||||
|
@ -16,8 +16,12 @@
|
|||
const fetchBtn = document.getElementById('fetch-btn')
|
||||
const responseText = document.getElementById('response-output')
|
||||
|
||||
fetchBtn.addEventListener('click', () => {
|
||||
responseText.innerText = 'Static Text Replacement'
|
||||
fetchBtn.addEventListener('click', async () => {
|
||||
const response = await fetch('/.netlify/functions/hello-world')
|
||||
.then(response => response.json()
|
||||
)
|
||||
|
||||
responseText.innerText = JSON.stringify(response)
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
|
15
yarn.lock
Normal file
15
yarn.lock
Normal file
|
@ -0,0 +1,15 @@
|
|||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@netlify/functions@^1.4.0":
|
||||
version "1.4.0"
|
||||
resolved "https://registry.yarnpkg.com/@netlify/functions/-/functions-1.4.0.tgz#027a2e5d54df5519ccbd14cf450231e97bbbf93a"
|
||||
integrity sha512-gy7ULTIRroc2/jyFVGx1djCmmBMVisIwrvkqggq5B6iDcInRSy2Tpkm+V5C63hKJVkNRskKWtLQKm9ecCaQTjA==
|
||||
dependencies:
|
||||
is-promise "^4.0.0"
|
||||
|
||||
is-promise@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-4.0.0.tgz#42ff9f84206c1991d26debf520dd5c01042dd2f3"
|
||||
integrity sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==
|
Loading…
Add table
Add a link
Reference in a new issue