openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] [typescript-axios] Use generated enums in operations

Open chaayac opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

When using API functions generated from this, I want to be able to also use the enum types. The enums are generated for models, but they should also be generated for operations too.

Describe the solution you'd like

Generate and use enum types in operations.

Describe alternatives you've considered

Just hard-coding the enum values when using operations, but that's not great.

Additional context

e.g.

findPetsByStatus: async (status: Array<'available' | 'pending' | 'sold'>, ...

should be

findPetsByStatus: async (status: Array<FindPetsByStatusStatusEnum>, ...
...
export const FindPetsByStatusStatusEnum = {
    Available: 'available',
    Pending: 'pending',
    Sold: 'sold'
} as const;
export type FindPetsByStatusStatusEnum = typeof FindPetsByStatusStatusEnum[keyof typeof FindPetsByStatusStatusEnum];

chaayac avatar Sep 16 '22 00:09 chaayac

up

galihputera avatar Sep 16 '22 06:09 galihputera

this is my .yaml

schemas:
    ErrorCode:
      type: string
      enum:
        - MISSING_PARAMETER
        - MISSING_TOKEN
        - INVALID_TOKEN
        - ACTION_NOT_ALLOWED
        - NOT_FOUND
        - SERVER_ERROR
        - UNEXPECTED_ERROR

I want

export enum ErrorStateEnum {
    MissingParameter = 'MISSING_PARAMETER',
    MissingToken = 'MISSING_TOKEN',
    InvalidToken = 'INVALID_TOKEN',
    ActionNotAllowed = 'ACTION_NOT_ALLOWED',
    NotFound = 'NOT_FOUND',
    ServerError = 'SERVER_ERROR',
    UnexpectedError = 'UNEXPECTED_ERROR'
}

but now

export const ErrorCode = {
    MissingParameter: 'MISSING_PARAMETER',
    MissingToken: 'MISSING_TOKEN',
    InvalidToken: 'INVALID_TOKEN',
    ActionNotAllowed: 'ACTION_NOT_ALLOWED',
    NotFound: 'NOT_FOUND',
    ServerError: 'SERVER_ERROR',
    UnexpectedError: 'UNEXPECTED_ERROR'
} as const;

export type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];

Is there a good solution?

ganbowengo avatar Sep 21 '22 15:09 ganbowengo

bump

Tohusk avatar Sep 26 '22 00:09 Tohusk