elysiajs / elysia-swagger

A plugin for Elysia to auto-generate Swagger page
MIT License
90 stars 46 forks source link

Merge guard and route's response schema #75

Closed Xu-pixel closed 1 month ago

Xu-pixel commented 11 months ago

I added a response with a status code of 400 to the guard hook and a response of 200 to the route. I want to automatically merge rather than overwrite guard hooks in swagger

import { Elysia, t } from "elysia";
import jwt from "../middleware/1.jwt";
import { find } from "lodash-es";

const users = [
  {
    name: "xxx",
    passwd: "123",
  },
];

export default new Elysia({ prefix: "/user" }).use(jwt).guard(
  {
    detail: {
      tags: ["User"],
    },
    error: ({ error }) => {
      return {
        message: error.message,
      };
    },
    response: {
      "400": t.Object({
        message: t.String(),
      }),
    },
  },
  (app) =>
    app.post(
      "/login",
      async ({ jwt, body: { name, passwd } }) => {
        const user = find(users, { name, passwd });
        if (!user) {
          throw new Error("mm");
        }
        return {
          token: await jwt.sign({
            name: user.name,
          }),
        };
      },
      {
        body: t.Object({
          name: t.String(),
          passwd: t.String(),
        }),
        response: {
          200: t.Object({ token: t.String() }),
        },
      }
    )
);

image