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

Incorrect response type in afterHandle #1012

Open
aryzing opened this issue Jan 12, 2025 · 1 comment
Open

Incorrect response type in afterHandle #1012

aryzing opened this issue Jan 12, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@aryzing
Copy link

aryzing commented Jan 12, 2025

What version of Elysia is running?

1.2.10

What platform is your computer?

Linux 6.8.0-50-generic x86_64 x86_64

What steps can reproduce the bug?

Create a simple handler with an afterHandle such as,

import Elysia from "elysia";

export const app = new Elysia()
  .get(
    "/test1",
    () => {
      return { message: "Hello, World!" };
    },
    {
      afterHandle: ({ response }) => {
        console.log(response);
      },
    }
  )

The response prop is typed as never, although it's actually the object returned from the handler.

What is the expected behavior?

The type of response in afterHandle should match the returned object from the handler.

What do you see instead?

The type is never

Details

image

Additional information

No response

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

yes

@aryzing aryzing added the bug Something isn't working label Jan 12, 2025
@aryzing
Copy link
Author

aryzing commented Jan 12, 2025

Also, for some reason, when returning an error() from the route handler, the response value fails an instanceof check against ElysiaCustomStatusResponse:

function myAfterHandle({ response }) {
  if (response instanceof ElysiaCustomStatusResponse) { // <-- This always returns false
    console.log("Yes");
    return;
  }
}

Using the constructor name to get around the instanceof fail does work. Is there a better approach though?

function myAfterHandle2({ response }: { response: unknown }) {
  const constructorName = (() => {
    try {
      return response.constructor.name;
    } catch {
      return null;
    }
  })();

  if (constructorName === "ElysiaCustomStatusResponse") {
    return;
  }

  return new Response(customLogic(response));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant