Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudflare-kv-http): support ttl for setItem #448

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
};

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 } : {},
});

Check warning on line 148 in src/drivers/cloudflare-kv-http.ts

View check run for this annotation

Codecov / codecov/patch

src/drivers/cloudflare-kv-http.ts#L144-L148

Added lines #L144 - L148 were not covered by tests
};

const removeItem = async (key: string) => {
Expand Down
Loading