Skip to content

Commit f5d45e9

Browse files
author
rcatoio
committed
feat: add an option to disable throwing an error when request.ok is not true
1 parent 7df956c commit f5d45e9

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
@@ -67,6 +67,7 @@ program
6767
.option("--disableProxy", "disabled proxy", false)
6868
.option("--axios", "generate axios http client", false)
6969
.option("--unwrap-response-data", "unwrap the data item from the response", false)
70+
.option("--disable-throw-on-error", "Do not throw an error when response.ok is not true", false)
7071
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
7172
.option("--silent", "Output only errors to console", false)
7273
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
@@ -102,6 +103,7 @@ const {
102103
cleanOutput,
103104
defaultResponse,
104105
unwrapResponseData,
106+
disableThrowOnError,
105107
singleHttpClient,
106108
axios,
107109
silent,
@@ -118,6 +120,7 @@ generateApi({
118120
defaultResponseAsSuccess: defaultAsSuccess,
119121
defaultResponseType: defaultResponse,
120122
unwrapResponseData: unwrapResponseData,
123+
disableThrowOnError: disableThrowOnError,
121124
generateUnionEnums: unionEnums,
122125
generateResponses: responses,
123126
extractRequestParams: !!extractRequestParams,

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
templatePaths: {
6364
/** `templates/base` */
6465
base: "",

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = {
4545
extractRequestBody = config.extractRequestBody,
4646
defaultResponseType = config.defaultResponseType,
4747
unwrapResponseData = config.unwrapResponseData,
48+
disableThrowOnError = config.disableThrowOnError,
4849
singleHttpClient = config.singleHttpClient,
4950
prettier: prettierOptions = constants.PRETTIER_OPTIONS,
5051
hooks: rawHooks,
@@ -79,6 +80,7 @@ module.exports = {
7980
cleanOutput,
8081
defaultResponseType,
8182
unwrapResponseData,
83+
disableThrowOnError,
8284
singleHttpClient,
8385
constants,
8486
silent,

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)