diff --git a/index.js b/index.js index 3cd8c484..9eccd3c2 100755 --- a/index.js +++ b/index.js @@ -58,6 +58,7 @@ program .option("--disableProxy", "disabled proxy", false) .option("--axios", "generate axios http client", false) .option("--unwrap-response-data", "unwrap the data item from the response", false) + .option("--disable-throw-on-error", "Do not throw an error when response.ok is not true", false) .option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false) .option("--silent", "Output only errors to console", false) .option("--default-response ", "default type for empty response schema", TS_KEYWORDS.VOID) @@ -90,6 +91,7 @@ const { cleanOutput, defaultResponse, unwrapResponseData, + disableThrowOnError, sortTypes, singleHttpClient, axios, @@ -108,6 +110,7 @@ generateApi({ defaultResponseAsSuccess: defaultAsSuccess, defaultResponseType: defaultResponse, unwrapResponseData: unwrapResponseData, + disableThrowOnError: disableThrowOnError, sortTypes: sortTypes, generateUnionEnums: unionEnums, generateResponses: responses, diff --git a/src/config.js b/src/config.js index 73938fe9..15d6b0bd 100644 --- a/src/config.js +++ b/src/config.js @@ -59,6 +59,7 @@ const config = { singleHttpClient: false, httpClientType: HTTP_CLIENT.FETCH, unwrapResponseData: false, + disableThrowOnError: false, sortTypes: false, templatePaths: { /** `templates/base` */ diff --git a/src/index.js b/src/index.js index c149b8ca..ecb8c59d 100644 --- a/src/index.js +++ b/src/index.js @@ -46,6 +46,7 @@ module.exports = { extractRequestBody = config.extractRequestBody, defaultResponseType = config.defaultResponseType, unwrapResponseData = config.unwrapResponseData, + disableThrowOnError = config.disableThrowOnError, sortTypes = config.sortTypes, singleHttpClient = config.singleHttpClient, prettier: prettierOptions = getPrettierOptions(), @@ -82,6 +83,7 @@ module.exports = { cleanOutput, defaultResponseType, unwrapResponseData, + disableThrowOnError, sortTypes, singleHttpClient, constants, diff --git a/templates/base/http-clients/fetch-http-client.eta b/templates/base/http-clients/fetch-http-client.eta index 0477e0b7..b1b04a78 100644 --- a/templates/base/http-clients/fetch-http-client.eta +++ b/templates/base/http-clients/fetch-http-client.eta @@ -209,7 +209,9 @@ export class HttpClient { this.abortControllers.delete(cancelToken); } +<% if (!config.disableThrowOnError) { %> if (!response.ok) throw data; +<% } %> <% if (config.unwrapResponseData) { %> return data.data; <% } else { %>