Last active
December 17, 2020 08:49
-
-
Save armedi/346c79b857864eadf5a190e357a09f14 to your computer and use it in GitHub Desktop.
workers-kv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ... */ | |
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