Skip to content

Commit c6b9622

Browse files
committed
🔧 fix: #548 accessing all query params doesn't work
1 parent fdfd301 commit c6b9622

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

example/a.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
import { Elysia, error, t } from '../src'
2+
import { sucrose } from '../src/sucrose'
23
import { post, req } from '../test/utils'
34

4-
const app = new Elysia()
5-
.resolve(() => {
6-
return error(418)
7-
8-
return {
9-
a: 'a'
10-
}
5+
console.log(
6+
sucrose({
7+
handler: function ({ query }) {
8+
query.a
9+
},
10+
afterHandle: [],
11+
beforeHandle: [],
12+
error: [
13+
function a({
14+
query,
15+
query: { a, c: d },
16+
headers: { hello },
17+
...rest
18+
}) {
19+
query.b
20+
rest.query.e
21+
},
22+
({ query: { f } }) => {}
23+
],
24+
mapResponse: [],
25+
onResponse: [],
26+
parse: [],
27+
request: [],
28+
start: [],
29+
stop: [],
30+
trace: [],
31+
transform: []
1132
})
12-
.get('/', ({ a }) => a)
13-
.listen(3000)
14-
15-
app.handle(req('/'))
16-
.then((x) => x.status)
17-
.then(console.log)
33+
)

src/sucrose.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ export const findAlias = (type: string, body: string, depth = 0) => {
324324
return aliases
325325
}
326326

327+
const accessor = <T extends string, P extends string>(parent: T, prop: P) =>
328+
[
329+
parent + '.' + prop,
330+
parent + '["' + prop + '"]',
331+
parent + "['" + prop + "']"
332+
] as const
333+
327334
export const extractMainParameter = (parameter: string) => {
328335
if (!parameter) return
329336

@@ -357,6 +364,8 @@ export const inferBodyReference = (
357364
code.includes(alias + "['" + type + "']")
358365

359366
for (let alias of aliases) {
367+
if (!alias) continue
368+
360369
if (alias.charCodeAt(0) === 123) {
361370
alias = retrieveRootParamters(alias)
362371

@@ -391,7 +400,18 @@ export const inferBodyReference = (
391400

392401
if (!inference.query && access('query', alias)) inference.query = true
393402

394-
if (inference.query)
403+
if (
404+
['return'].some((type) =>
405+
accessor(type + ' ' + alias, 'query').some((key) =>
406+
code.includes(key)
407+
)
408+
)
409+
) {
410+
inference.query = true
411+
inference.unknownQueries = true
412+
}
413+
414+
if (inference.query && !inference.unknownQueries)
395415
while (true) {
396416
let keyword = alias + '.'
397417
if (code.includes(keyword + 'query')) keyword = alias + '.query'
@@ -677,6 +697,19 @@ export const sucrose = (
677697
inferBodyReference(body, aliases, inference)
678698
}
679699

700+
const context = rootParameters || mainParameter
701+
if (
702+
context &&
703+
['', 'return '].some((type) =>
704+
accessor(type + context, 'query').some((key) =>
705+
body.includes(key)
706+
)
707+
)
708+
) {
709+
inference.query = true
710+
inference.unknownQueries = true
711+
}
712+
680713
if (inference.query) {
681714
inferBodyReference(body, ['query'], inference)
682715

test/sucrose/query.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Elysia } from '../../src'
2+
3+
import { describe, expect, it } from 'bun:test'
4+
5+
const req = () => new Request(`http://localhost/?name=sucrose`)
6+
7+
describe('Query', () => {
8+
it('access all using property name', async () => {
9+
const app = new Elysia().get('/', (ctx) => ctx.query)
10+
const response = await app.handle(req())
11+
12+
expect(await response.json()).toEqual({ name: 'sucrose' })
13+
})
14+
15+
it('access all using destructuring', async () => {
16+
const app = new Elysia().get('/', ({ query }) => query)
17+
const response = await app.handle(req())
18+
19+
expect(await response.json()).toEqual({ name: 'sucrose' })
20+
})
21+
22+
it('access single param using property name', async () => {
23+
const app = new Elysia().get('/', (ctx) => ctx.query.name)
24+
const response = await app.handle(req())
25+
26+
expect(await response.text()).toEqual('sucrose')
27+
})
28+
29+
it('access single param using destructuring', async () => {
30+
const app = new Elysia().get('/', ({ query: { name } }) => name)
31+
const response = await app.handle(req())
32+
33+
expect(await response.text()).toEqual('sucrose')
34+
})
35+
})

0 commit comments

Comments
 (0)