Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/adapter/bun/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` +
`},`
Expand All @@ -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,` +
Expand Down Expand Up @@ -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)

Expand Down
49 changes: 49 additions & 0 deletions test/adapter/bun/index.test.ts
Original file line number Diff line number Diff line change
@@ -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")
})
})