Skip to content

Commit

Permalink
use http client and remove axios
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Feb 13, 2025
1 parent cab36a6 commit 9c2e44c
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 560 deletions.
3 changes: 1 addition & 2 deletions src/Generator/Client/Language/typescript-client.ts.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios, {AxiosRequestConfig} from "axios";
import {ClientAbstract, CredentialsInterface, TokenStoreInterface} from "sdkgen-client"
import {ClientAbstract, CredentialsInterface, TokenStoreInterface, HttpRequest} from "sdkgen-client"
{% if security.type == 'httpBasic' %}
import {HttpBasic} from "sdkgen-client"
{% elseif security.type == 'httpBearer' %}
Expand Down
46 changes: 21 additions & 25 deletions src/Generator/Client/Language/typescript-operation.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% endfor %}
});

let params: AxiosRequestConfig = {
let request: HttpRequest = {
url: url,
method: '{{ operation.method }}',
headers: {
Expand All @@ -37,40 +37,36 @@
'{{ realName }}',
{% endfor %}
]),
{% if operation.return.contentShape == 'application/octet-stream' %}
responseType: 'arraybuffer',
{% elseif operation.return.contentShape == 'application/json' %}
responseType: 'json',
{% elseif operation.return.contentShape == 'text/plain' or operation.return.contentShape == 'application/xml' %}
responseType: 'text',
{% endif %}
{% if operation.bodyName %}
data: {{ operation.bodyName }}
{% endif %}
};

try {
const response = await this.httpClient.request{% if operation.return %}<{{ operation.return.schema.type|raw }}>{% endif %}(params);
const response = await this.httpClient.request(request);
if (response.ok) {
{% if operation.return %}
return response.data;
return {{ _self.response(operation.return, false) }};
{% endif %}
} catch (error) {
if (error instanceof ClientException) {
throw error;
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;
}

const statusCode = response.status;
{% for code, throw in operation.throws %}
if ({% if code == 999 %}statusCode >= 0 && statusCode <= 999{% elseif code == 499 %}statusCode >= 400 && statusCode <= 499{% elseif code == 599 %}statusCode >= 500 && statusCode <= 599{% else %}statusCode === {{ code }}{% endif %}) {
throw new {{ throw.className|raw }}(error.response.data);
}
if ({% if code == 999 %}statusCode >= 0 && statusCode <= 999{% elseif code == 499 %}statusCode >= 400 && statusCode <= 499{% elseif code == 599 %}statusCode >= 500 && statusCode <= 599{% else %}statusCode === {{ code }}{% endif %}) {
throw new {{ throw.className|raw }}({{ _self.response(throw, true) }});
}

{% endfor %}
throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
}
throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
}

{% endfor %}


{% macro response(payload) %}
{% if payload.contentShape == 'application/octet-stream' %}
await response.arrayBuffer(){% elseif payload.contentShape == 'application/x-www-form-urlencoded' %}
new URLSearchParams(await response.formData() as any){% elseif payload.contentShape == 'application/json' %}
await response.json(){% elseif payload.contentShape == 'multipart/form-data' %}
await response.formData(){% elseif payload.contentShape == 'text/plain' or payload.contentShape == 'application/xml' %}
await response.text(){% else %}
await response.json() as {{ payload.schema.type|raw }}{% endif %}
{% endmacro %}
3 changes: 1 addition & 2 deletions src/Generator/Client/Language/typescript-tag.ts.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios, {AxiosRequestConfig} from "axios";
import {TagAbstract} from "sdkgen-client"
import {TagAbstract, HttpRequest} from "sdkgen-client"
import {ClientException, UnknownStatusCodeException} from "sdkgen-client";

{% for file, className in imports %}
Expand Down
147 changes: 54 additions & 93 deletions tests/Generator/Client/resource/typescript/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* {@link https://sdkgen.app}
*/

import axios, {AxiosRequestConfig} from "axios";
import {ClientAbstract, CredentialsInterface, TokenStoreInterface} from "sdkgen-client"
import {ClientAbstract, CredentialsInterface, TokenStoreInterface, HttpRequest} from "sdkgen-client"
import {HttpBearer} from "sdkgen-client"
import {Anonymous} from "sdkgen-client"
import {ClientException, UnknownStatusCodeException} from "sdkgen-client";
Expand Down Expand Up @@ -33,7 +32,7 @@ export class Client extends ClientAbstract {
'type': type,
});

let params: AxiosRequestConfig = {
let request: HttpRequest = {
url: url,
method: 'GET',
headers: {
Expand All @@ -50,22 +49,14 @@ export class Client extends ClientAbstract {
]),
};

try {
const response = await this.httpClient.request<EntryCollection>(params);
return response.data;
} catch (error) {
if (error instanceof ClientException) {
throw error;
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
const response = await this.httpClient.request(request);
if (response.ok) {
return await response.json() as EntryCollection;
}
}

const statusCode = response.status;
throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
}
/**
* @returns {Promise<EntryMessage>}
* @throws {EntryMessageException}
Expand All @@ -77,7 +68,7 @@ export class Client extends ClientAbstract {
'type': type,
});

let params: AxiosRequestConfig = {
let request: HttpRequest = {
url: url,
method: 'POST',
headers: {
Expand All @@ -89,30 +80,22 @@ export class Client extends ClientAbstract {
data: payload
};

try {
const response = await this.httpClient.request<EntryMessage>(params);
return response.data;
} catch (error) {
if (error instanceof ClientException) {
throw error;
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;

if (statusCode === 400) {
throw new EntryMessageException(error.response.data);
}
const response = await this.httpClient.request(request);
if (response.ok) {
return await response.json() as EntryMessage;
}

if (statusCode === 500) {
throw new EntryMessageException(error.response.data);
}
const statusCode = response.status;
if (statusCode === 400) {
throw new EntryMessageException(await response.json() as EntryMessage);
}

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
if (statusCode === 500) {
throw new EntryMessageException(await response.json() as EntryMessage);
}
}

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
}
/**
* @returns {Promise<Record<string, EntryMessage>>}
* @throws {EntryMessageException}
Expand All @@ -125,7 +108,7 @@ export class Client extends ClientAbstract {
'type': type,
});

let params: AxiosRequestConfig = {
let request: HttpRequest = {
url: url,
method: 'PUT',
headers: {
Expand All @@ -137,30 +120,22 @@ export class Client extends ClientAbstract {
data: payload
};

try {
const response = await this.httpClient.request<Record<string, EntryMessage>>(params);
return response.data;
} catch (error) {
if (error instanceof ClientException) {
throw error;
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;

if (statusCode === 400) {
throw new EntryMessageException(error.response.data);
}
const response = await this.httpClient.request(request);
if (response.ok) {
return await response.json() as Record<string, EntryMessage>;
}

if (statusCode === 500) {
throw new MapEntryMessageException(error.response.data);
}
const statusCode = response.status;
if (statusCode === 400) {
throw new EntryMessageException(await response.json() as EntryMessage);
}

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
if (statusCode === 500) {
throw new MapEntryMessageException(await response.json() as Record<string, EntryMessage>);
}
}

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
}
/**
* @returns {Promise<void>}
* @throws {ClientException}
Expand All @@ -171,7 +146,7 @@ export class Client extends ClientAbstract {
'type': type,
});

let params: AxiosRequestConfig = {
let request: HttpRequest = {
url: url,
method: 'DELETE',
headers: {
Expand All @@ -181,21 +156,13 @@ export class Client extends ClientAbstract {
]),
};

try {
const response = await this.httpClient.request(params);
} catch (error) {
if (error instanceof ClientException) {
throw error;
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
const response = await this.httpClient.request(request);
if (response.ok) {
}
}

const statusCode = response.status;
throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
}
/**
* @returns {Promise<Array<EntryMessage>>}
* @throws {EntryMessageException}
Expand All @@ -208,7 +175,7 @@ export class Client extends ClientAbstract {
'type': type,
});

let params: AxiosRequestConfig = {
let request: HttpRequest = {
url: url,
method: 'PATCH',
headers: {
Expand All @@ -220,32 +187,26 @@ export class Client extends ClientAbstract {
data: payload
};

try {
const response = await this.httpClient.request<Array<EntryMessage>>(params);
return response.data;
} catch (error) {
if (error instanceof ClientException) {
throw error;
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;

if (statusCode === 400) {
throw new EntryMessageException(error.response.data);
}
const response = await this.httpClient.request(request);
if (response.ok) {
return await response.json() as Array<EntryMessage>;
}

if (statusCode === 500) {
throw new ArrayEntryMessageException(error.response.data);
}
const statusCode = response.status;
if (statusCode === 400) {
throw new EntryMessageException(await response.json() as EntryMessage);
}

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
if (statusCode === 500) {
throw new ArrayEntryMessageException(await response.json() as Array<EntryMessage>);
}

throw new UnknownStatusCodeException('The server returned an unknown status code: ' + statusCode);
}




public static build(token: string): Client
{
return new Client('http://api.foo.com', new HttpBearer(token));
Expand Down
Loading

0 comments on commit 9c2e44c

Please sign in to comment.