Skip to content

Instantly share code, notes, and snippets.

@aciddust
Created May 6, 2023 04:34
Show Gist options
  • Save aciddust/f17f3d1f4e2c988dbca87bf4e56ec260 to your computer and use it in GitHub Desktop.
Save aciddust/f17f3d1f4e2c988dbca87bf4e56ec260 to your computer and use it in GitHub Desktop.
Vercel KV example

README

Do not use like this

The GET method is not good for security
because it exposes your bearer token transmitted as a header to the client.

<html>
<head>
<title>fetch test</title>
</head>
<body>
<div>
<div>session</div>
<div id="session"><!-- will be placed here --></div>
</div>
<script>
const host = "https://SECRET.kv.vercel-storage.com"
const token = "Y0UR_T0K3N"
const cache = (command, key, value) => {
const base_url = `${host}/${command}/${key}`
if (value) return `${base_url}/${value}`
return base_url
}
const proxy_handler = (url, token) => {
return fetch(url, {
headers: {
Authorization: `Bearer ${token}`
}
}).then(response => response.json())
}
window.onload = function() {
var now = Date.now();
const getter = cache("get", "example:test:user_01")
const setter = cache("set", "example:test:user_01", now)
proxy_handler(getter, token).then(data => {
const session = document.getElementById("session");
session.innerHTML = JSON.stringify(data);
})
proxy_handler(setter, token)
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment