Skip to content

Support for 400 and 500 series status codes in openapi2aspida response generation #257

@naporin0624

Description

@naporin0624

Description

First of all, thank you for this excellent library. I use openapi2aspida frequently, and it has been incredibly helpful in my projects.

Currently, the openapi2aspida statuscode only supports 200 and 300 series status codes. I would like to ask if it's possible to extend support to the 400 and 500 series status codes.
Relevant code: src/buildV3.ts#L177-L178

Describe the solution you'd like

I would like to see support for 400 and 500 series status codes added to the OAS statuscode generation.

Describe alternatives you've considered

Currently, res.status only returns 2xx or 3xx status codes, so when handling errors such as 400 or 500, I have to insert code that upcasts the status to number. I have been using the following workaround:

const res = await client.xxx.post();

const status: number = res.status;
switch (status) {
  case 200:
    console.info(res.body);
    break;
  case 400:
    console.error('Bad Request');
    break;
  case 403:
    console.error('Forbidden');
    break;
  case 404:
    console.error('Not Found');
    break;
  case 500:
    console.error('Internal Server Error');
    break;
}

Additional context

Below is a sample of the generated method and its usage with openapi2aspida to illustrate the expected behavior:

Generated Method

/* eslint-disable */
import type { DefineMethods } from 'aspida';

export type Methods = DefineMethods<{
  post: {
    status: 200;

    /** 成功 */
    resBody: unknown;
  } | {
    status: 400;
  } | {
    status: 403;
  } | {
    status: 404;
  } | {
    status: 500;
  };
}>;

Usage Example

const res = await client.xxx.post();

switch (res.status) {
  case 200:
    console.info(res.body);
    break;
  case 400:
    console.error('Bad Request');
    break;
  case 403:
    console.error('Forbidden');
    break;
  case 404:
    console.error('Not Found');
    break;
  case 500:
    console.error('Internal Server Error');
    break;
}

This extension would improve error handling by allowing more robust responses for different scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions