From 2cd426612aea6af99734a8f05cbe7ed26c87a8fd Mon Sep 17 00:00:00 2001 From: SolarisUp <153609965+SolarisUp@users.noreply.github.com> Date: Wed, 4 Sep 2024 13:03:46 +0300 Subject: [PATCH] feat(cloudflare-kv-http): support `ttl` for `setItem` (#448) --- docs/2.drivers/cloudflare.md | 4 ++++ src/drivers/cloudflare-kv-http.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/2.drivers/cloudflare.md b/docs/2.drivers/cloudflare.md index b8229cb3..f1e53846 100644 --- a/docs/2.drivers/cloudflare.md +++ b/docs/2.drivers/cloudflare.md @@ -106,6 +106,10 @@ const storage = createStorage({ - `apiURL`: Custom API URL. Default is `https://api.cloudflare.com`. - `base`: Adds prefix to all stored keys +**Transaction options:** + +- `ttl`: Supported for `setItem(key, value, { ttl: number /* seconds min 60 */ })` + **Supported methods:** - `getItem`: Maps to [Read key-value pair](https://api.cloudflare.com/#workers-kv-namespace-read-key-value-pair) `GET accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values/:key_name` diff --git a/src/drivers/cloudflare-kv-http.ts b/src/drivers/cloudflare-kv-http.ts index 7a54414b..37763343 100644 --- a/src/drivers/cloudflare-kv-http.ts +++ b/src/drivers/cloudflare-kv-http.ts @@ -140,8 +140,12 @@ export default defineDriver((opts) => { } }; - const setItem = async (key: string, value: any) => { - return await kvFetch(`/values/${r(key)}`, { method: "PUT", body: value }); + const setItem = async (key: string, value: any, opt: any) => { + return await kvFetch(`/values/${r(key)}`, { + method: "PUT", + body: value, + query: opt?.ttl ? { expiration_ttl: opt?.ttl } : {}, + }); }; const removeItem = async (key: string) => {