cloudflare-typescript icon indicating copy to clipboard operation
cloudflare-typescript copied to clipboard

Regression in [email protected] KV SDK: metadata is being stored inside value unexpectedly

Open sanjithacks opened this issue 9 months ago • 1 comments

Confirm this is a Typescript library issue and not an underlying Cloudflare API issue

  • [x] This is an issue with the Typescript library

Describe the bug

🐛 Bug Report

Summary

In [email protected], the kv.namespaces.values.update() method incorrectly stores both metadata and value inside the KV value, rather than keeping metadata separate. This results in unexpected behavior where the stored data is:

{
  "metadata": "...",
  "value": "{...double-encoded JSON...}"
}


### To Reproduce

## 1. Install the latest SDK:
```bash
npm install [email protected]

2.Run the following code:

import Cloudflare from "cloudflare";
const new Cloudflare({
  apiEmail: config?.cloudflare?.email,
  apiKey: config?.cloudflare?.access_key,
});
await client.kv.namespaces.values.update('namespace_id', 'my-key', {
  account_id: 'my-account-id',
  metadata: '{"note": "example"}',
  value: JSON.stringify({
    ID: "123",
    NAME: "Test",
  })
});

3.Retrieve the stored value:

const result = await client.kv.namespaces.values.get('namespace_id', 'my-key');
console.log(result);

✅ Expected Behavior

The value stored in KV should be:

{
  "ID": "123",
  "NAME": "Test"
}

❌ Actual Behavior (in [email protected])

The returned value looks like:

{
  "metadata": "",
  "value": "{\"ID\":\"123\",\"NAME\":\"Test\"}"
}

This results in:

  • Double-encoded JSON.
  • TypeScript type mismatches.
  • Unexpected data format requiring manual JSON.parse(result.value) to access the actual object.

✅ Works As Expected In

[email protected]

⚠️ Impact

  • Breaks backward compatibility.
  • Makes KV value unusable without additional parsing.
  • Violates expectations based on earlier SDK versions and API documentation.

💡 Suggested Fix

Ensure the value is stored directly as the KV content, and metadata is handled separately at the key level — not embedded into the stored value.

Code snippets


OS

linux

Runtime version

Deno 2.3.1

Library version

v4.2.0

sanjithacks avatar May 08 '25 06:05 sanjithacks

I've opened an internal issue for the team to have a look. Thanks!

jhutchings1 avatar May 12 '25 21:05 jhutchings1

Dupe of #2593. This is fixed in https://github.com/cloudflare/cloudflare-typescript/commit/d4ce21e135415971b6b4578151304289f5b7c4ef.

1000hz avatar Jul 10 '25 20:07 1000hz