feat(ts): add toString() serialization to API responses#1376
feat(ts): add toString() serialization to API responses#1376ChiragAgg5k merged 2 commits intomasterfrom
Conversation
…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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThree client template files (Node, React Native, and Web) are enhanced with a runtime augmentation that attaches a custom Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
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).
There was a problem hiding this comment.
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
📒 Files selected for processing (6)
templates/node/src/client.ts.twigtemplates/node/src/index.ts.twigtemplates/react-native/src/client.ts.twigtemplates/react-native/src/index.ts.twigtemplates/web/src/client.ts.twigtemplates/web/src/index.ts.twig
Summary
toString()to response objects returned byclient.call()in web, node, and react-native SDKs (CLI inherits from node)[object Object]isPlainObject,constructor === Object, etc.)Problem
Change
3 lines added before
return datain each SDK'sclient.call():Files changed
templates/{web,node,react-native}/src/client.ts.twigtoString()assignment before returning response dataIndex files are reverted to original — no new exports needed.
Test plan
String(), and concatenation produce JSONJSON.stringify, spread/destructuring unchangedObject.getPrototypeOf(data) === Object.prototype(still plain object)constructor === Object(still true)Summary by CodeRabbit