diff --git a/src/adapter/bun/compose.ts b/src/adapter/bun/compose.ts index 97d0910b..fb3e6334 100644 --- a/src/adapter/bun/compose.ts +++ b/src/adapter/bun/compose.ts @@ -35,14 +35,16 @@ const createContext = ( `s=u.indexOf('/',${standardHostname ? 11 : 7}),` + `qi=u.indexOf('?', s + 1)\n` - if (inference.query) fnLiteral += getQi + const needsQuery = inference.query || !!route.hooks.query || !!route.standaloneValidators?.find((x) => x.query) + + if (needsQuery) fnLiteral += getQi const getPath = !inference.path ? '' : !isDynamic ? `path:'${route.path}',` : `get path(){` + - (inference.query ? '' : getQi) + + (needsQuery ? '' : getQi) + `if(qi===-1)return u.substring(s)\n` + `return u.substring(s,qi)\n` + `},` @@ -51,12 +53,12 @@ const createContext = ( allocateIf(`const c=`, !isInline) + `{request,` + `store,` + - allocateIf(`qi,`, inference.query) + + allocateIf(`qi,`, needsQuery) + allocateIf(`params:request.params,`, isDynamic) + getPath + allocateIf( `url:request.url,`, - hasTrace || inference.url || inference.query + hasTrace || inference.url || needsQuery ) + `redirect,` + `error:status,` + @@ -122,8 +124,10 @@ export const createBunRouteHandler = (app: AnyElysia, route: InternalRoute) => { fnLiteral += `${app.event.request?.find(isAsync) ? 'async' : ''} function map(request){` + const needsQuery = inference.query || !!route.hooks.query || !!route.standaloneValidators?.find((x) => x.query) + // inference.query require declaring const 'qi' - if (hasTrace || inference.query || app.event.request?.length) { + if (hasTrace || needsQuery) { fnLiteral += createContext(app, route, inference) fnLiteral += createOnRequestHandler(app) diff --git a/test/adapter/bun/index.test.ts b/test/adapter/bun/index.test.ts new file mode 100644 index 00000000..59333c8c --- /dev/null +++ b/test/adapter/bun/index.test.ts @@ -0,0 +1,49 @@ +import { describe, it, expect } from 'bun:test' +import { Elysia, t } from '../../../src' + +describe('Bun adapter', () => { + it('handle query guard', async () => { + const app = new Elysia() + .guard(({ + query: t.Object({ a: t.String() }), + })) + .get("/works-with", ({ query }) => "Works" + query.a) + .get("/works-without", () => "Works without") + .listen(0) + + const query = await fetch( + `http://localhost:${app.server!.port}/works-with?a=with` + ).then((x) => x.text()) + + expect(query).toEqual("Workswith") + + const query2 = await fetch( + `http://localhost:${app.server!.port}/works-without?a=1` + ).then((x) => x.text()) + + expect(query2).toEqual("Works without") + }) + + it('handle standalone query guard', async () => { + const app = new Elysia() + .guard(({ + query: t.Object({ a: t.String() }), + schema: "standalone" + })) + .get("/works-with", ({ query }) => "Works" + query.a) + .get("/works-without", () => "Works without") + .listen(0) + + const query = await fetch( + `http://localhost:${app.server!.port}/works-with?a=with` + ).then((x) => x.text()) + + expect(query).toEqual("Workswith") + + const query2 = await fetch( + `http://localhost:${app.server!.port}/works-without?a=1` + ).then((x) => x.text()) + + expect(query2).toEqual("Works without") + }) +})