ajaishankar / openapi-typescript-fetch

A typed fetch client for openapi-typescript
MIT License
233 stars 54 forks source link

Missing responses interfaces #63

Open kirkas opened 1 year ago

kirkas commented 1 year ago

I have an openapi spec for authentification with few responses types:

"/authentication": {
  "post": {
    "tags": [
      "authenticate_user"
    ],
    "operationId": "authenticate",
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Auth"
          }
        }
      },
      "required": true
    },
    "responses": {
      "200": {
        "description": "Returns current authentication state.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/AuthResponse"
            }
          }
        }
      },
      "4XX": {
        "description": "",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponseObject"
            }
          }
        }
      },
      "5XX": {
        "description": "",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponseObject"
            }
          }
        }
      }
    }
  }
}

The AuthResponse & ErrorResponseObject are correctly created, but ErrorResponseObject is not used in case of 4XX or 5XX errors.

This is the generated file:

import * as runtime from '../runtime';
import type {
  Auth,
  AuthResponse,
  ErrorResponseObject,
} from '../models';
import {
    AuthFromJSON,
    AuthToJSON,
    AuthResponseFromJSON,
    AuthResponseToJSON,
    ErrorResponseObjectFromJSON,
    ErrorResponseObjectToJSON,
} from '../models';

export interface AuthenticateRequest {
    auth: Auth;
}

/**
 * 
 */
export class AuthenticateUserApi extends runtime.BaseAPI {

    /**
     */
    async authenticateRaw(requestParameters: AuthenticateRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<AuthResponseErrorResponseObject>> {
        if (requestParameters.auth === null || requestParameters.auth === undefined) {
            throw new runtime.RequiredError('auth','Required parameter requestParameters.auth was null or undefined when calling authenticate.');
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        headerParameters['Content-Type'] = 'application/json';

        const response = await this.request({
            path: `/authentication`,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: AuthToJSON(requestParameters.auth),
        }, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => AuthResponseFromJSON(jsonValue));
    }

    /**
     */
    async authenticate(requestParameters: AuthenticateRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<AuthResponse> {
        const response = await this.authenticateRaw(requestParameters, initOverrides);
        return await response.value();
    }

}

As you can see ErrorResponseObject is imported but never used.

This creates multiple issues down the line. The main one is that I cannot use the ErrorResponseObject attributes to let the user know what the error actually is.

kirkas commented 1 year ago

other generator seems to have a isCodeInRange function that checks for specific statuses.