elysiajs / eden

Fully type-safe Elysia client
MIT License
147 stars 37 forks source link

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

Open bogeychan opened 3 months ago

bogeychan commented 3 months ago

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