fix(mount): include query params (#1224)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(mount): include query params * denoify
authored by Yusuke Wada · committed by GitHub · parent 9fd3cd3 · Browse files · 6 files·+47 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+47 added−4 removed
- tests:+23 added−0 removed
- other:+24 added−4 removed
Provenance
- Commit
cef0adb- Tree
a3788a6- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
9fd3cd3
6 files changed+47 added−4 removed
modified:deno_dist/utils/url.ts+5 added
72 unmodified lines73 const queryIndex = url.indexOf('?', 8)74 return url.slice(url.indexOf('/', 8), queryIndex === -1 ? undefined : queryIndex)75 }76 77+export const getQueryStrings = (url: string): string => {78+ const queryIndex = url.indexOf('?', 8)79+ return queryIndex === -1 ? '' : '?' + url.slice(queryIndex + 1)80+}81+82 export const getPathNoStrict = (request: Request): string => {83 const result = getPath(request)84 85 // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same155 unmodified linesmodified:deno_dist/hono-base.ts+7 added-2 removed
18 unmodified lines19 MergeSchemaPath,20 FetchEventLike,21 } from './types.ts'22 import type { RemoveBlankRecord } from './utils/types.ts'23-import { getPath, getPathNoStrict, mergePath } from './utils/url.ts'23+import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'24 25 type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE26 27 interface RouterRoute {187 unmodified lines215 executionContext = c.executionCtx216 } catch {} // Do nothing217 const options = optionHandler ? optionHandler(c) : [c.env, executionContext]218 const optionsArray = Array.isArray(options) ? options : [options]219+220+ const queryStrings = getQueryStrings(c.req.url)221 const res = await applicationHandler(220- new Request(new URL(c.req.path.slice(pathPrefixLength) || '/', c.req.url), c.req.raw),222+ new Request(223+ new URL((c.req.path.slice(pathPrefixLength) || '/') + queryStrings, c.req.url),224+ c.req.raw225+ ),226 ...optionsArray227 )228 229 if (res) return res154 unmodified linesmodified:src/utils/url.test.ts+15 added
6 unmodified lines7 mergePath,8 checkOptionalParameter,9 getQueryParam,10 getQueryParams,11+ getQueryStrings,12 } from './url'13 14 describe('url', () => {15 it('splitPath', () => {68 unmodified lines84 expect(path).toBe('/hello/hey/')85 })86 })87 88+ describe('getQueryStrings', () => {89+ it('getQueryStrings', () => {90+ let qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20')91+ expect(qs).toBe('?name=foo&name=bar&age=20')92+ qs = getQueryStrings('https://example.com/hello?')93+ expect(qs).toBe('?')94+ qs = getQueryStrings('https://example.com/hello')95+ expect(qs).toBe('')96+ // Allows to contain hash97+ qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20#hash')98+ expect(qs).toBe('?name=foo&name=bar&age=20#hash')99+ })100+ })101+102 describe('getPathNoStrict', () => {103 it('getPathNoStrict - no strict is false', () => {104 let path = getPathNoStrict(new Request('https://example.com/hello/'))105 expect(path).toBe('/hello')151 unmodified linesmodified:src/utils/url.ts+5 added
72 unmodified lines73 const queryIndex = url.indexOf('?', 8)74 return url.slice(url.indexOf('/', 8), queryIndex === -1 ? undefined : queryIndex)75 }76 77+export const getQueryStrings = (url: string): string => {78+ const queryIndex = url.indexOf('?', 8)79+ return queryIndex === -1 ? '' : '?' + url.slice(queryIndex + 1)80+}81+82 export const getPathNoStrict = (request: Request): string => {83 const result = getPath(request)84 85 // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same155 unmodified linesmodified:src/hono-base.ts+7 added-2 removed
18 unmodified lines19 MergeSchemaPath,20 FetchEventLike,21 } from './types'22 import type { RemoveBlankRecord } from './utils/types'23-import { getPath, getPathNoStrict, mergePath } from './utils/url'23+import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'24 25 type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE26 27 interface RouterRoute {187 unmodified lines215 executionContext = c.executionCtx216 } catch {} // Do nothing217 const options = optionHandler ? optionHandler(c) : [c.env, executionContext]218 const optionsArray = Array.isArray(options) ? options : [options]219+220+ const queryStrings = getQueryStrings(c.req.url)221 const res = await applicationHandler(220- new Request(new URL(c.req.path.slice(pathPrefixLength) || '/', c.req.url), c.req.raw),222+ new Request(223+ new URL((c.req.path.slice(pathPrefixLength) || '/') + queryStrings, c.req.url),224+ c.req.raw225+ ),226 ...optionsArray227 )228 229 if (res) return res154 unmodified linesmodified:src/hono.test.ts+8 added
1923 unmodified lines1924 if (path === '/header') {1925 const message = req.headers.get('x-message')1926 return new Response(message)1927 }1928+ if (path === '/with-query') {1929+ const queryStrings = new URL(req.url).searchParams.toString()1930+ return new Response(queryStrings)1931+ }1932 if (path == '/with-params') {1933 return new Response(1934 JSON.stringify({1935 params,56 unmodified lines1992 expect(res.status).toBe(404)1993 expect(res.headers.get('x-message')).toBe('Foo')1994 expect(await res.text()).toBe('Not Found from AnotherApp')1995 1996+ res = await app.request('/another-app/with-query?foo=bar&baz=qux')1997+ expect(res.status).toBe(200)1998+ expect(await res.text()).toBe('foo=bar&baz=qux')1999+2000 res = await app.request('/another-app/with-params')2001 expect(res.status).toBe(200)2002 expect(await res.json()).toEqual({2003 params: 'params',148 unmodified lines