Skip to content

Commit

Permalink
feat(cloudflare-kv-http): support ttl for setItem (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarisUp authored Sep 4, 2024
1 parent f817e0a commit 2cd4266
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/2.drivers/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 6 additions & 2 deletions src/drivers/cloudflare-kv-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ export default defineDriver<KVHTTPOptions>((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) => {
Expand Down

0 comments on commit 2cd4266

Please sign in to comment.