Skip to content

Instantly share code, notes, and snippets.

@armedi
Last active December 17, 2020 08:49
Show Gist options
  • Save armedi/346c79b857864eadf5a190e357a09f14 to your computer and use it in GitHub Desktop.
Save armedi/346c79b857864eadf5a190e357a09f14 to your computer and use it in GitHub Desktop.
workers-kv
/* ... */
declare const REDIRECTS: KVNamespace
interface Redirect {
to: string
from: string
}
const save = async (data: Redirect): Promise<void> => {
return REDIRECTS.put(data.from, JSON.stringify(data))
}
const get = async (from: string): Promise<Redirect> => {
const response: Redirect | null = await REDIRECTS.get(from, 'json')
if (response === null) {
throw new Error('not found')
}
return response
}
const exists = async (from: string): Promise<boolean> => {
const response = await REDIRECTS.get(from)
return !!response
}
/* ... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment