Skip to content

Commit be870e4

Browse files
committed
feat(ts): add toString() serialization to API responses
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).
1 parent ba64453 commit be870e4

File tree

6 files changed

+21
-53
lines changed

6 files changed

+21
-53
lines changed

templates/node/src/client.ts.twig

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ type Headers = {
5858
[key: string]: string;
5959
}
6060

61-
class Response {
62-
toString(): string {
63-
return JSONbig.stringify(this);
64-
}
65-
66-
static fromPayload<T>(data: T): T {
67-
if (data === null || data === undefined || typeof data !== 'object' || data instanceof ArrayBuffer) {
68-
return data;
69-
}
70-
return Object.assign(Object.create(Response.prototype), data);
71-
}
72-
}
73-
7461
class {{spec.title | caseUcfirst}}Exception extends Error {
7562
code: number;
7663
response: string;
@@ -356,7 +343,11 @@ class Client {
356343
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseText);
357344
}
358345

359-
return Response.fromPayload(data);
346+
if (data && typeof data === 'object') {
347+
data.toString = () => JSONbig.stringify(data);
348+
}
349+
350+
return data;
360351
}
361352

362353
static flatten(data: Payload, prefix = ''): Payload {
@@ -375,7 +366,7 @@ class Client {
375366
}
376367
}
377368

378-
export { Client, Response, {{spec.title | caseUcfirst}}Exception };
369+
export { Client, {{spec.title | caseUcfirst}}Exception };
379370
export { Query } from './query';
380371
export type { Models, Payload, UploadProgress };
381372
export type { QueryTypes, QueryTypesList } from './query';

templates/node/src/index.ts.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { Client, Response, Query, {{spec.title | caseUcfirst}}Exception } from './client';
1+
export { Client, Query, {{spec.title | caseUcfirst}}Exception } from './client';
22
{% for service in spec.services %}
33
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseKebab}}';
44
{% endfor %}

templates/react-native/src/client.ts.twig

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,6 @@ export type UploadProgress = {
129129
chunksUploaded: number;
130130
}
131131

132-
class Response {
133-
toString(): string {
134-
return JSONbig.stringify(this);
135-
}
136-
137-
static fromPayload<T>(data: T): T {
138-
if (data === null || data === undefined || typeof data !== 'object' || data instanceof ArrayBuffer) {
139-
return data;
140-
}
141-
return Object.assign(Object.create(Response.prototype), data);
142-
}
143-
}
144-
145132
class {{spec.title | caseUcfirst}}Exception extends Error {
146133
code: number;
147134
response: string;
@@ -547,7 +534,11 @@ class Client {
547534
window.localStorage.setItem('cookieFallback', cookieFallback);
548535
}
549536

550-
return Response.fromPayload(data);
537+
if (data && typeof data === 'object') {
538+
data.toString = () => JSONbig.stringify(data);
539+
}
540+
541+
return data;
551542
} catch (e) {
552543
if (e instanceof {{spec.title | caseUcfirst}}Exception) {
553544
throw e;
@@ -557,5 +548,5 @@ class Client {
557548
}
558549
}
559550

560-
export { Client, Response, {{spec.title | caseUcfirst}}Exception };
551+
export { Client, {{spec.title | caseUcfirst}}Exception };
561552
export type { Models, Payload };

templates/react-native/src/index.ts.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { Client, Response, {{spec.title | caseUcfirst}}Exception } from './client';
1+
export { Client, {{spec.title | caseUcfirst}}Exception } from './client';
22
{% for service in spec.services %}
33
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseKebab}}';
44
{% endfor %}

templates/web/src/client.ts.twig

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,6 @@ type UploadProgress = {
321321
chunksUploaded: number;
322322
}
323323

324-
/**
325-
* Response class that wraps API responses to provide proper string serialization.
326-
* When used in string interpolation (e.g. `${response}`), it returns a JSON string
327-
* instead of "[object Object]".
328-
*/
329-
class Response {
330-
toString(): string {
331-
return JSONbig.stringify(this);
332-
}
333-
334-
static fromPayload<T>(data: T): T {
335-
if (data === null || data === undefined || typeof data !== 'object' || data instanceof ArrayBuffer) {
336-
return data;
337-
}
338-
return Object.assign(Object.create(Response.prototype), data);
339-
}
340-
}
341-
342324
/**
343325
* Exception thrown by the {{language.params.packageName}} package
344326
*/
@@ -937,7 +919,11 @@ class Client {
937919
window.localStorage.setItem('cookieFallback', cookieFallback);
938920
}
939921

940-
return Response.fromPayload(data);
922+
if (data && typeof data === 'object') {
923+
data.toString = () => JSONbig.stringify(data);
924+
}
925+
926+
return data;
941927
}
942928

943929
static flatten(data: Payload, prefix = ''): Payload {
@@ -956,7 +942,7 @@ class Client {
956942
}
957943
}
958944

959-
export { Client, Response, {{spec.title | caseUcfirst}}Exception };
945+
export { Client, {{spec.title | caseUcfirst}}Exception };
960946
export { Query } from './query';
961947
export type { Models, Payload, UploadProgress };
962948
export type { RealtimeResponseEvent };

templates/web/src/index.ts.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For older versions, please check
66
* [previous releases](https://github.com/{{sdk.gitUserName}}/{{sdk.gitRepoName}}/releases).
77
*/
8-
export { Client, Response, Query, {{spec.title | caseUcfirst}}Exception } from './client';
8+
export { Client, Query, {{spec.title | caseUcfirst}}Exception } from './client';
99
{% for service in spec.services %}
1010
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseKebab}}';
1111
{% endfor %}

0 commit comments

Comments
 (0)