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

treaty doesn't infer guard's response schema #98

Open
bogeychan opened this issue May 27, 2024 · 0 comments
Open

treaty doesn't infer guard's response schema #98

bogeychan opened this issue May 27, 2024 · 0 comments

Comments

@bogeychan
Copy link

bogeychan commented May 27, 2024

Dependencies

{
  "@elysiajs/eden": "^1.0.14",
  "@elysiajs/swagger": "^1.0.5",
  "elysia": "^1.0.22"
}

Code

import { treaty } from "@elysiajs/eden";
import { swagger } from "@elysiajs/swagger";
import { Elysia, t } from "elysia";

const app = new Elysia()
  .use(swagger())
  .guard(
    {
      response: {
        418: t.Union([t.Literal("foo"), t.Literal("bar")]),
      },
    },
    (app) =>
      app
        .onBeforeHandle(async ({ set }) => {
          if (Math.random() > 0.5) {
            set.status = 418;
            return "foo";
          }
        })
        .onBeforeHandle(async ({ set }) => {
          if (Math.random() > 0.5) {
            set.status = 418;
            return "bar";
          }
        })
        .get("/test", () => 1, {
          response: {
            200: t.Number(),
          },
        })
  )
  .listen(8080);

const client = treaty<typeof app>(app.server!.url.toString());

const { error } = await client.test.get();

if (error) {
  type Err = typeof error; // <---
}

Err has type {status: unknown, value: unknown} instead of {status: 418, value: "foo" | "bar"}

Additional Informations

adding type to handler works as expected:

.get("/test", () => 1, {
  response: {
    200: t.Number(),
    418: t.Union([t.Literal("foo"), t.Literal("bar")]),
  },
})

reported on discord

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