samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.84k stars 95 forks source link

When I use generic in `@typedException` it's not work. #790

Closed 8471919 closed 9 months ago

8471919 commented 9 months ago

Bug Report

Summary

Write a short summary of the bug in here.

Write detailed description in here.

Code occuring the bug

  // error_message
export const ERROR_MESSAGE = {
  AUTH: {
    FORBIDDEN: {
      code: 403,
      message: '금지된 접근입니다.',
    },
  },
  TOKEN: {
    NOT_FOUND_TOKEN: {
      code: 404,
      message: '토큰을 찾을 수 없습니다.',
    },
  },
} as const;

export type ErrorMessageType = {
  code: number;
  message: string;
};

// error response type
export type ErrorResponseForm<T extends ErrorMessageType> = {
  success: false;
  timestamp: string & tags.Format<'date-time'>;
  path: string & tags.Format<'url'>;
} & T;

  // controller.ts
  @TypedRoute.Get(['/:domainId', '/domain/:domainId'])
  @TypedException<ErrorResponseForm<typeof ERROR_MESSAGE.AUTH.FORBIDDEN>>(
    ERROR_MESSAGE.AUTH.FORBIDDEN.code,
    ERROR_MESSAGE.AUTH.FORBIDDEN.message,
  )
  @TypedException<
    ErrorResponseForm<typeof ERROR_MESSAGE.TOKEN.NOT_FOUND_TOKEN>
  >(
    ERROR_MESSAGE.TOKEN.NOT_FOUND_TOKEN.code,
    ERROR_MESSAGE.TOKEN.NOT_FOUND_TOKEN.message,
  )
  async a() {
    ...
    return res;
  }

when I use @TypedException with Type that has generic like @TypedException<ErrorResponseForm<T>>(args), swagger docs read only last decorator generic type. this is the example picture. (after npx nestia swagger) image

this is swagger.json image

you have any solution of this?

samchon commented 9 months ago

Seems not any problem in your example code and picture, but I may understand what you want.

If you want to define same status numbered error type multiply, just do it with union type.

@TypedException<X|Y|Z>(403, "description")
8471919 commented 9 months ago

@samchon as looking the first picture, 403 and 404 example value's code and message is same. that's two examples are different error each other though. If possible, can you solve this problem? I always feel really thanks for you to make this super-convenient libarary.

samchon commented 9 months ago

Use explicit type, then no problem.