Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions templates/node/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ type Headers = {
[key: string]: string;
}

class Response {
toString(): string {
return JSONbig.stringify(this);
}

static fromPayload<T>(data: T): T {
if (data === null || data === undefined || typeof data !== 'object' || data instanceof ArrayBuffer) {
return data;
}
return Object.assign(Object.create(Response.prototype), data);
}
}

class {{spec.title | caseUcfirst}}Exception extends Error {
code: number;
response: string;
Expand Down Expand Up @@ -343,7 +356,7 @@ class Client {
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseText);
}

return data;
return Response.fromPayload(data);
}

static flatten(data: Payload, prefix = ''): Payload {
Expand All @@ -362,7 +375,7 @@ class Client {
}
}

export { Client, {{spec.title | caseUcfirst}}Exception };
export { Client, Response, {{spec.title | caseUcfirst}}Exception };
export { Query } from './query';
export type { Models, Payload, UploadProgress };
export type { QueryTypes, QueryTypesList } from './query';
2 changes: 1 addition & 1 deletion templates/node/src/index.ts.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Client, Query, {{spec.title | caseUcfirst}}Exception } from './client';
export { Client, Response, Query, {{spec.title | caseUcfirst}}Exception } from './client';
{% for service in spec.services %}
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseKebab}}';
{% endfor %}
Expand Down
17 changes: 15 additions & 2 deletions templates/react-native/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ export type UploadProgress = {
chunksUploaded: number;
}

class Response {
toString(): string {
return JSONbig.stringify(this);
}

static fromPayload<T>(data: T): T {
if (data === null || data === undefined || typeof data !== 'object' || data instanceof ArrayBuffer) {
return data;
}
return Object.assign(Object.create(Response.prototype), data);
}
}

class {{spec.title | caseUcfirst}}Exception extends Error {
code: number;
response: string;
Expand Down Expand Up @@ -534,7 +547,7 @@ class Client {
window.localStorage.setItem('cookieFallback', cookieFallback);
}

return data;
return Response.fromPayload(data);
} catch (e) {
if (e instanceof {{spec.title | caseUcfirst}}Exception) {
throw e;
Expand All @@ -544,5 +557,5 @@ class Client {
}
}

export { Client, {{spec.title | caseUcfirst}}Exception };
export { Client, Response, {{spec.title | caseUcfirst}}Exception };
export type { Models, Payload };
2 changes: 1 addition & 1 deletion templates/react-native/src/index.ts.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Client, {{spec.title | caseUcfirst}}Exception } from './client';
export { Client, Response, {{spec.title | caseUcfirst}}Exception } from './client';
{% for service in spec.services %}
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseKebab}}';
{% endfor %}
Expand Down
22 changes: 20 additions & 2 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ type UploadProgress = {
chunksUploaded: number;
}

/**
* Response class that wraps API responses to provide proper string serialization.
* When used in string interpolation (e.g. `${response}`), it returns a JSON string
* instead of "[object Object]".
*/
class Response {
toString(): string {
return JSONbig.stringify(this);
}

static fromPayload<T>(data: T): T {
if (data === null || data === undefined || typeof data !== 'object' || data instanceof ArrayBuffer) {
return data;
}
return Object.assign(Object.create(Response.prototype), data);
}
}

/**
* Exception thrown by the {{language.params.packageName}} package
*/
Expand Down Expand Up @@ -919,7 +937,7 @@ class Client {
window.localStorage.setItem('cookieFallback', cookieFallback);
}

return data;
return Response.fromPayload(data);
}

static flatten(data: Payload, prefix = ''): Payload {
Expand All @@ -938,7 +956,7 @@ class Client {
}
}

export { Client, {{spec.title | caseUcfirst}}Exception };
export { Client, Response, {{spec.title | caseUcfirst}}Exception };
export { Query } from './query';
export type { Models, Payload, UploadProgress };
export type { RealtimeResponseEvent };
Expand Down
2 changes: 1 addition & 1 deletion templates/web/src/index.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For older versions, please check
* [previous releases](https://github.com/{{sdk.gitUserName}}/{{sdk.gitRepoName}}/releases).
*/
export { Client, Query, {{spec.title | caseUcfirst}}Exception } from './client';
export { Client, Response, Query, {{spec.title | caseUcfirst}}Exception } from './client';
{% for service in spec.services %}
export { {{service.name | caseUcfirst}} } from './services/{{service.name | caseKebab}}';
{% endfor %}
Expand Down
Loading