From f5d45e940aaa981eccfa1ef526a4002695dd64e4 Mon Sep 17 00:00:00 2001 From: rcatoio Date: Mon, 27 Sep 2021 16:35:45 +0200 Subject: [PATCH] feat: add an option to disable throwing an error when request.ok is not true --- index.js | 3 +++ src/config.js | 1 + src/index.js | 2 ++ templates/base/http-clients/fetch-http-client.eta | 2 ++ 4 files changed, 8 insertions(+) diff --git a/index.js b/index.js index a42b61a50..7b277d4eb 100755 --- a/index.js +++ b/index.js @@ -67,6 +67,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) @@ -102,6 +103,7 @@ const { cleanOutput, defaultResponse, unwrapResponseData, + disableThrowOnError, singleHttpClient, axios, silent, @@ -118,6 +120,7 @@ generateApi({ defaultResponseAsSuccess: defaultAsSuccess, defaultResponseType: defaultResponse, unwrapResponseData: unwrapResponseData, + disableThrowOnError: disableThrowOnError, generateUnionEnums: unionEnums, generateResponses: responses, extractRequestParams: !!extractRequestParams, diff --git a/src/config.js b/src/config.js index b9f3d63a5..115576b50 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, templatePaths: { /** `templates/base` */ base: "", diff --git a/src/index.js b/src/index.js index 6ccbdd3ab..6f8e55ca2 100644 --- a/src/index.js +++ b/src/index.js @@ -45,6 +45,7 @@ module.exports = { extractRequestBody = config.extractRequestBody, defaultResponseType = config.defaultResponseType, unwrapResponseData = config.unwrapResponseData, + disableThrowOnError = config.disableThrowOnError, singleHttpClient = config.singleHttpClient, prettier: prettierOptions = constants.PRETTIER_OPTIONS, hooks: rawHooks, @@ -79,6 +80,7 @@ module.exports = { cleanOutput, defaultResponseType, unwrapResponseData, + disableThrowOnError, singleHttpClient, constants, silent, diff --git a/templates/base/http-clients/fetch-http-client.eta b/templates/base/http-clients/fetch-http-client.eta index 0477e0b70..b1b04a786 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 { %>