Skip to content

feat(ts): add toString() serialization to API responses#1376

Merged
ChiragAgg5k merged 2 commits intomasterfrom
feat/ts-response-tostring-serialization
Mar 7, 2026
Merged

feat(ts): add toString() serialization to API responses#1376
ChiragAgg5k merged 2 commits intomasterfrom
feat/ts-response-tostring-serialization

Conversation

@ChiragAgg5k
Copy link
Member

@ChiragAgg5k ChiragAgg5k commented Mar 7, 2026

Summary

  • Adds toString() to response objects returned by client.call() in web, node, and react-native SDKs (CLI inherits from node)
  • String interpolation now produces JSON instead of [object Object]
  • Simple property assignment on the plain object — fully non-breaking (preserves prototype, isPlainObject, constructor === Object, etc.)

Problem

const response = await databases.listDocuments('db', 'col');
console.log(`Result: ${response}`);
// Before: "Result: [object Object]"
// After:  "Result: {"total":43,"documents":[...]}"

Change

3 lines added before return data in each SDK's client.call():

if (data && typeof data === 'object') {
    data.toString = () => JSONbig.stringify(data);
}

Files changed

Template Change
templates/{web,node,react-native}/src/client.ts.twig Added toString() assignment before returning response data

Index files are reverted to original — no new exports needed.

Test plan

  • Verified string interpolation, String(), and concatenation produce JSON
  • Verified property access, JSON.stringify, spread/destructuring unchanged
  • Verified Object.getPrototypeOf(data) === Object.prototype (still plain object)
  • Verified constructor === Object (still true)
  • Regenerated all affected SDKs (web, node, react-native, cli)

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced API response data serialization to ensure proper handling of large numeric values across all supported client platforms (Node, React Native, and Web).

…ization

Wrap API responses in a Response class across web, node, and react-native
SDKs so that string interpolation (e.g. `${response}`) returns JSON instead
of "[object Object]". The Response.fromPayload() helper preserves property
access, JSON.stringify, and passes through primitives and ArrayBuffer unchanged.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cd776508-e3c8-48a9-bf47-3758faae1976

📥 Commits

Reviewing files that changed from the base of the PR and between ba64453 and be870e4.

📒 Files selected for processing (3)
  • templates/node/src/client.ts.twig
  • templates/react-native/src/client.ts.twig
  • templates/web/src/client.ts.twig
🚧 Files skipped from review as they are similar to previous changes (3)
  • templates/web/src/client.ts.twig
  • templates/react-native/src/client.ts.twig
  • templates/node/src/client.ts.twig

📝 Walkthrough

Walkthrough

Three client template files (Node, React Native, and Web) are enhanced with a runtime augmentation that attaches a custom toString method to response data objects, enabling serialization via JSONbig.stringify for improved string representation of complex objects.

Changes

Cohort / File(s) Summary
Client Response Enhancement
templates/node/src/client.ts.twig, templates/react-native/src/client.ts.twig, templates/web/src/client.ts.twig
Added runtime attachment of a toString method to response data objects, enabling JSONbig.stringify serialization for improved string interpolation of complex objects. Applied consistently across all three platform templates.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 Hoppy thoughts on strings so grand,
JSONbig now works as planned,
Objects speak with voices clear,
stringify makes data dear!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding toString() serialization to API responses, which is the core objective across all three template files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/ts-response-tostring-serialization

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Add toString() to response objects returned by client.call() in web,
node, and react-native SDKs so that string interpolation produces JSON
instead of "[object Object]". Uses a simple property assignment on the
plain object to remain fully non-breaking (preserves prototype, isPlainObject, etc).
@ChiragAgg5k ChiragAgg5k changed the title feat(ts): add Response class with toString() for string serialization feat(ts): add toString() serialization to API responses Mar 7, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@templates/node/src/client.ts.twig`:
- Around line 66-70: The fromPayload<T>(data: T): T implementation should not
replace the payload's prototype (avoid
Object.assign(Object.create(Response.prototype), data)) because that breaks
checks like Object.getPrototypeOf(...) === Object.prototype and Array.isArray
for top-level arrays; instead preserve the original payload shape by returning
the original data unchanged for objects/arrays and move any
serialization/response helper behavior into a separate wrapper/utility (e.g., a
Response.serialize or Response.wrap function) so generated service methods
(which return this.client.call(...)) keep plain objects/arrays intact; update
fromPayload to simply return data and implement any needed serialization helpers
without mutating prototypes, ensuring callers use the new helper when they need
Response-specific behavior.

In `@templates/react-native/src/client.ts.twig`:
- Around line 137-141: fromPayload currently re-prototypes payloads via
Object.create(Response.prototype), which breaks plain-object/array shapes;
instead preserve the original payload object/array and attach Response behavior
as non-enumerable properties. Modify static fromPayload<T>(data: T) to, when
data is an object/array, iterate Object.getOwnPropertyNames(Response.prototype)
(skip "constructor") and use Object.defineProperty to add each method/property
from Response.prototype onto the original data as non-enumerable bound functions
(or accessors) so Array.isArray(data) and plain-object checks still work while
exposing Response methods without changing the payload prototype.

In `@templates/web/src/client.ts.twig`:
- Around line 334-338: The fromPayload<T>(data: T) helper currently replaces the
payload's prototype (via Object.create(Response.prototype) + Object.assign),
breaking array identity and plain-object expectations; instead, preserve the
original shape and attach a non-enumerable serializer/marker to the existing
value. Update fromPayload to return the original data unchanged for
primitives/ArrayBuffer, and for object/array payloads use Object.defineProperty
on data to add a non-enumerable symbol or property (e.g., a RESPONSE_SERIALIZER
or similar) that references the Response serializer method, leaving Arrays
intact so Array.isArray and length remain correct; keep the Response.prototype
untouched and adjust any consumers of fromPayload/call() to use that
non-enumerable serializer when present.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d6f87cea-b04f-4443-8950-60c1a77d2d9b

📥 Commits

Reviewing files that changed from the base of the PR and between d63e5a3 and ba64453.

📒 Files selected for processing (6)
  • templates/node/src/client.ts.twig
  • templates/node/src/index.ts.twig
  • templates/react-native/src/client.ts.twig
  • templates/react-native/src/index.ts.twig
  • templates/web/src/client.ts.twig
  • templates/web/src/index.ts.twig

@ChiragAgg5k ChiragAgg5k merged commit 1d39f64 into master Mar 7, 2026
55 checks passed
@ChiragAgg5k ChiragAgg5k deleted the feat/ts-response-tostring-serialization branch March 7, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants