Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge guard and route's response schema #75

Open
Xu-pixel opened this issue Dec 2, 2023 · 0 comments
Open

Merge guard and route's response schema #75

Xu-pixel opened this issue Dec 2, 2023 · 0 comments

Comments

@Xu-pixel
Copy link

Xu-pixel commented Dec 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant