Skip to content

Commit

Permalink
chore: added responsstring instead of stringified data
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragAgg5k committed Jan 28, 2025
1 parent a3eddaf commit fbc1eb1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions templates/node/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class Client {
const { uri, options } = this.prepareRequest(method, url, headers, params);

let data: any = null;
let responseString: string = '';

const response = await fetch(uri, options);

Expand All @@ -271,18 +272,19 @@ class Client {
warnings.split(';').forEach((warning: string) => console.warn('Warning: ' + warning));
}

responseString = await response.text();
if (response.headers.get('content-type')?.includes('application/json')) {
data = await response.json();
data = JSON.parse(responseString);
} else if (responseType === 'arrayBuffer') {
data = await response.arrayBuffer();
data = new TextEncoder().encode(responseString).buffer;
} else {
data = {
message: await response.text()
message: responseString
};
}

if (400 <= response.status) {
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, JSON.stringify(data));
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseString);
}

return data;
Expand Down
9 changes: 6 additions & 3 deletions templates/react-native/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -412,23 +412,26 @@ class Client {

try {
let data = null;
let responseString = '';

const response = await fetch(url.toString(), options);

const warnings = response.headers.get('x-{{ spec.title | lower }}-warning');
if (warnings) {
warnings.split(';').forEach((warning: string) => console.warn('Warning: ' + warning));
}

responseString = await response.text();
if (response.headers.get('content-type')?.includes('application/json')) {
data = await response.json();
data = JSON.parse(responseString);
} else {
data = {
message: await response.text()
message: responseString
};
}

if (400 <= response.status) {
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, JSON.stringify(data));
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseString);
}

const cookieFallback = response.headers.get('X-Fallback-Cookies');
Expand Down

0 comments on commit fbc1eb1

Please sign in to comment.