Skip to content

Types for macro with transform are broken #1673

@Kurzdor

Description

@Kurzdor

What version of Elysia is running?

1.4.21

What platform is your computer?

Microsoft Windows 11 10.0.26200.0 x64

What environment are you using

Bun 1.3.5

Are you using dynamic mode?

AOT is enabled

What steps can reproduce the bug?

import { Elysia, t } from 'elysia';
import { jwt } from '@elysiajs/jwt';

const example = new Elysia({ name: 'example' })
  .use(
    jwt({
      name: 'jwt',
      secret: Bun.env.SECRET,
      exp: '7d',
    }),
  )
  .macro({
    requiresAuth: {
      // this works, i.e. I can send 401 before validation but type safety breaks
      // UNCOMMENT ME AND COMMENT resolve handler underneath
      // async transform({ jwt, cookie: { session }, status }) {
      //   const token: string = session.value as string;
      //   if (!token) return status(401, {});
      //   const user = await jwt.verify(token);
      //   // more logic
      // },
      // this works but if validation fails then response will be validation error instead of unauthorized as it should be
      async resolve({ jwt, cookie: { session }, status }) {
        const token: string = session.value as string;
        if (!token) return status(401, {});
        const user = await jwt.verify(token);
        // more logic
      },
    },
  })
  .get(
    '/foo/:id',
    ({ params: { bar } }) => {
      return 'foo';
    },
    {
      requiresAuth: true,
      params: t.Object({
        bar: t.Number(),
      }),
    },
  );

What is the expected behavior?

Type safety should not break when using macro/transform

What do you see instead?

Issues related to type safety in macro/transform, macro/resolve works like a charm but that behaviour is not what I desire

Additional information

Explanation: I am implementing macro to protect routes from accessing unless request contains valid credentials (JWT token from cookies in this example). When validation fails then it just throws 422 status when it must be before validation (i.e. I should return 401 instead). I tried to use transform as it is described in docs because it fires before/during validation stage but it looks like it break type safety.

P.S. I might be really wrong with thinking in Elysia way so I am sorry about that especially considering this is my first TS project.

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions