openapistack / openapi-client-axios

JavaScript client library for consuming OpenAPI-enabled APIs with axios
https://openapistack.co
MIT License
558 stars 67 forks source link

Typegen generated responses contains errors which are thrown and not returned. #72

Closed mat813 closed 2 years ago

mat813 commented 3 years ago

Say I have this path:

"/auth": {
  "post": {
    "description": "Autentification d'un utilisateur",
    "operationId": "auth.auth",
    "security": [],
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/authLogin"
          }
        },
        "application/x-www-form-urlencoded": {
          "schema": {
            "$ref": "#/components/schemas/authLogin"
          }
        }
      }
    },
    "responses": {
      "200": {
        "description": "Auth ok",
        "content": {
          "application/json": {
            "schema": {
        "$ref": "#/components/schemas/authToken"
            }
          }
        }
      },
      "400": {
        "description": "Bad request",
        "content": {
          "application/json": {
            "schema": {
        "$ref": "#/components/schemas/error"
            }
          }
        }
      },
      "401": {
        "description": "Bad Auth token",
        "content": {
          "application/json": {
            "schema": {
        "$ref": "#/components/schemas/error"
            }
          },
        }
      }
    }
  }
},

The error type is this:

      "error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "$ref": "#/components/schemas/non_empty_string"
          }
        },
        "example": {
          "error": "Email and or Password invalid."
        }
      },

typegen will generate this:

  /**
   * auth.auth - Autentification d'un utilisateur
   */
  'authAuth'(
    parameters?: undefined | null,
    data?: Paths.AuthAuth.RequestBody,
    config?: AxiosRequestConfig
  ): OperationResponse<
    | Paths.AuthAuth.Responses.$200
    | Paths.AuthAuth.Responses.$400
    | Paths.AuthAuth.Responses.$401
  >;

But only Paths.AuthAuth.Responses.$200 will ever be returned by client.authAuth as all status codes outside of 200..300 generate an exception. (see lib/adapters/xhr.js for example)

I think only the responses in the 2xx range should be added there. If TypeScript ever gets a "throws" keyword to describe what a function can throw, then they can be added back in there, but adding it as the return values feels wrong.

arutkowski00 commented 2 years ago

Here is the diff that solved my problem:

diff --git a/node_modules/openapi-client-axios-typegen/typegen.js b/node_modules/openapi-client-axios-typegen/typegen.js
index 82d87c5..35b4714 100644
--- a/node_modules/openapi-client-axios-typegen/typegen.js
+++ b/node_modules/openapi-client-axios-typegen/typegen.js
@@ -180,7 +180,7 @@ function generateMethodForOperation(methodName, operation, exportTypes) {
     var responseTypePaths = lodash_1.default.chain(exportTypes)
         .filter(function (_a) {
         var schemaRef = _a.schemaRef;
-        return schemaRef.startsWith("#/paths/" + normalizedOperationId + "/responses");
+        return schemaRef.startsWith("#/paths/" + normalizedOperationId + "/responses/2");
     })
         .map(function (_a) {
         var path = _a.path;

You can apply it with patch-package.