diff --git a/src/compose.ts b/src/compose.ts index e8cf2f56..1dd6be6b 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -678,15 +678,13 @@ export const composeHandler = ({ } if (hasQuery) { - const destructured = < - { - key: string - isArray: boolean - isNestedObjectArray: boolean - isObject: boolean - anyOf: boolean - }[] - >[] + const destructured = [] as { + key: string + isArray: boolean + isNestedObjectArray: boolean + isObject: boolean + anyOf: boolean + }[] // @ts-ignore if (validator.query && validator.query.schema.type === 'object') { @@ -748,7 +746,7 @@ export const composeHandler = ({ } else { fnLiteral += 'if(c.qi!==-1){' + - `let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))\n` + `let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1).replace(/\\+/g,' '))\n` let index = 0 for (const { @@ -776,8 +774,8 @@ export const composeHandler = ({ `else\n` + `a${index}+=','\n` + `let temp\n` + - `if(memory===-1)temp=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` + - `else temp=decodeURIComponent(url.slice(start, memory).replace(/\\+/g,' '))\n` + + `if(memory===-1)temp=decodeURIComponent(url.slice(start))\n` + + `else temp=decodeURIComponent(url.slice(start, memory))\n` + `const charCode = temp.charCodeAt(0)\n` + `if(charCode !== 91 && charCode !== 123)\n` + `temp='"'+temp+'"'\n` + @@ -800,10 +798,10 @@ export const composeHandler = ({ `if(a${index}===undefined)` + `a${index}=[]\n` + `if(memory===-1){` + - `a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+/g,' '))\n` + + `a${index}.push(decodeURIComponent(url.slice(start)))\n` + `break` + `}` + - `else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+/g,' '))\n` + + `else a${index}.push(decodeURIComponent(url.slice(start, memory)))\n` + `memory=url.indexOf('&${key}=',memory)\n` + `if(memory===-1) break\n` + `}` @@ -813,8 +811,8 @@ export const composeHandler = ({ `if(memory!==-1){` + `const start=memory+${key.length + 2}\n` + `memory=url.indexOf('&',start)\n` + - `if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))` + - `else a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+/g,' '))` + + `if(memory===-1)a${index}=decodeURIComponent(url.slice(start))` + + `else a${index}=decodeURIComponent(url.slice(start,memory))` + `if(a${index}!==undefined)` + `try{` + `a${index}=JSON.parse(a${index})` + @@ -827,9 +825,9 @@ export const composeHandler = ({ `if(memory!==-1){` + `const start=memory+${key.length + 2}\n` + `memory=url.indexOf('&',start)\n` + - `if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` + + `if(memory===-1)a${index}=decodeURIComponent(url.slice(start))\n` + `else{` + - `a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+/g,' '))` + `a${index}=decodeURIComponent(url.slice(start,memory))` if (anyOf) fnLiteral += @@ -842,8 +840,8 @@ export const composeHandler = ({ `if(first)first=false\n` + `else deepMemory = url.indexOf('&', start)\n` + `let value\n` + - `if(deepMemory===-1)value=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` + - `else value=decodeURIComponent(url.slice(start, deepMemory).replace(/\\+/g,' '))\n` + + `if(deepMemory===-1)value=decodeURIComponent(url.slice(start))\n` + + `else value=decodeURIComponent(url.slice(start, deepMemory))\n` + `const vStart=value.charCodeAt(0)\n` + `const vEnd=value.charCodeAt(value.length - 1)\n` + `if((vStart===91&&vEnd===93)||(vStart===123&&vEnd===125))\n` + diff --git a/test/validator/query.test.ts b/test/validator/query.test.ts index 5c384b8f..73042b8d 100644 --- a/test/validator/query.test.ts +++ b/test/validator/query.test.ts @@ -332,7 +332,6 @@ describe('Query Validator', () => { check() { const { state } = ctx.query - // @ts-expect-error if (!checker.check(ctx, name, state ?? ctx.query.state)) throw new Error('State mismatch') } @@ -704,7 +703,7 @@ describe('Query Validator', () => { }) }) - it('parse + in query', async () => { + it('parse query with space', async () => { const api = new Elysia().get('', ({ query }) => query, { query: t.Object({ keyword: t.String() @@ -712,15 +711,35 @@ describe('Query Validator', () => { }) const url = new URL('http://localhost:3000/') - url.searchParams.append('keyword', 'hello world') - console.log(url.href) //http://localhost:3000/?keyword=hello+world + url.searchParams.append('keyword', 'with space') + console.log(url.href) // http://localhost:3000/?keyword=with+space const result = await api .handle(new Request(url.href)) .then((response) => response.json()) expect(result).toEqual({ - keyword: 'hello world' + keyword: 'with space' + }) + }) + + it('parse query with +', async () => { + const api = new Elysia().get('', ({ query }) => query, { + query: t.Object({ + keyword: t.String() + }) + }) + + const url = new URL("http://localhost:3000/"); + url.searchParams.append("keyword", "with+plus"); + console.log(url.href) // http://localhost:3000/?keyword=with%2Bplus + + const result = await api + .handle(new Request(url.href)) + .then((response) => response.json()) + + expect(result).toEqual({ + keyword: 'with+plus' }) })