Skip to content

Commit 5e24567

Browse files
RoCatrcatoiojs2me
authored
feat: add an option to disable throwing an error when request.ok is not true (#297)
Co-authored-by: rcatoio <[email protected]> Co-authored-by: Sergey S. Volkov <[email protected]>
1 parent 4627926 commit 5e24567

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ program
5858
.option("--disableProxy", "disabled proxy", false)
5959
.option("--axios", "generate axios http client", false)
6060
.option("--unwrap-response-data", "unwrap the data item from the response", false)
61+
.option("--disable-throw-on-error", "Do not throw an error when response.ok is not true", false)
6162
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
6263
.option("--silent", "Output only errors to console", false)
6364
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
@@ -90,6 +91,7 @@ const {
9091
cleanOutput,
9192
defaultResponse,
9293
unwrapResponseData,
94+
disableThrowOnError,
9395
sortTypes,
9496
singleHttpClient,
9597
axios,
@@ -108,6 +110,7 @@ generateApi({
108110
defaultResponseAsSuccess: defaultAsSuccess,
109111
defaultResponseType: defaultResponse,
110112
unwrapResponseData: unwrapResponseData,
113+
disableThrowOnError: disableThrowOnError,
111114
sortTypes: sortTypes,
112115
generateUnionEnums: unionEnums,
113116
generateResponses: responses,

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const config = {
5959
singleHttpClient: false,
6060
httpClientType: HTTP_CLIENT.FETCH,
6161
unwrapResponseData: false,
62+
disableThrowOnError: false,
6263
sortTypes: false,
6364
templatePaths: {
6465
/** `templates/base` */

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports = {
4646
extractRequestBody = config.extractRequestBody,
4747
defaultResponseType = config.defaultResponseType,
4848
unwrapResponseData = config.unwrapResponseData,
49+
disableThrowOnError = config.disableThrowOnError,
4950
sortTypes = config.sortTypes,
5051
singleHttpClient = config.singleHttpClient,
5152
prettier: prettierOptions = getPrettierOptions(),
@@ -82,6 +83,7 @@ module.exports = {
8283
cleanOutput,
8384
defaultResponseType,
8485
unwrapResponseData,
86+
disableThrowOnError,
8587
sortTypes,
8688
singleHttpClient,
8789
constants,

templates/base/http-clients/fetch-http-client.eta

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ export class HttpClient<SecurityDataType = unknown> {
209209
this.abortControllers.delete(cancelToken);
210210
}
211211

212+
<% if (!config.disableThrowOnError) { %>
212213
if (!response.ok) throw data;
214+
<% } %>
213215
<% if (config.unwrapResponseData) { %>
214216
return data.data;
215217
<% } else { %>

0 commit comments

Comments
 (0)