elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.44k stars 223 forks source link

Sveltekit integration doesn't work #772

Closed wineTGH closed 2 months ago

wineTGH commented 2 months ago

What version of Elysia is running?

1.1.5

What platform is your computer?

Linux 6.9.12-200.fc40.x86_64 x86_64 unknown (I'm running Fedora 40)

What steps can reproduce the bug?

Hi! I've followed this page and I have a realy weird behavior. Every time I try to make navigate to route, it downloads content of page as a file. When I try to navigate to page with a swagger plugin, app just crashes with this error:

6628 |           if (plugin2 instanceof _Elysia) return this._use(plugin2);
6629 |           if (typeof plugin2.default === "function")
6630 |             return plugin2.default(this);
6631 |           if (plugin2.default instanceof _Elysia)
6632 |             return this._use(plugin2.default);
6633 |           throw new Error(
                       ^
error: Invalid plugin type. Expected Elysia instance, function, or module with "default" as Elysia instance or function that returns Elysia instance.
      at /home/winet/Documents/JS Projects/enxperts/node_modules/elysia/dist/index.mjs:6633:17

Here the code that I'm using:

// /src/routes/api/[...slugs]/+server.ts
import type { RequestHandler } from '@sveltejs/kit';
import { app } from "$api";

export const GET: RequestHandler = ({ request }) => app.handle(request);
export const PUT: RequestHandler = ({ request }) => app.handle(request);
export const DELETE: RequestHandler = ({ request }) => app.handle(request);
export const POST: RequestHandler = ({  request }) => app.handle(request);
export const PATCH: RequestHandler = ({  request }) => app.handle(request);
export const fallback: RequestHandler = ({  request }) => app.handle(request);
// /src/lib/server/api/index.ts ($api)
import { swagger } from "@elysiajs/swagger";
import { Elysia } from "elysia";

export const app = new Elysia({prefix: '/api'})
    .use(swagger())
    .get('/', () => 'hi')
    .get('/hello', () => 'world')

What is the expected behavior?

Just work™

What do you see instead?

image

Additional information

I'm starting app with this command bun --bun run dev

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

yes

tguelcan commented 2 months ago

@wineTGH the error comes from swagger and the execution of the bun script within Elysia. Bun seems to behave differently here. There is a promise response from which is not executed with sveltekit. So if you change .use(swagger()) to .use(await swagger()), it will work.

wineTGH commented 2 months ago

@tguelcan Oh god, that actually worked, thank you! It solved all of the problems. If only you'd come into my life sooner :) I had to use +server files because of the deadline.