Skip to content
Myr

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

6 files+47 added−4 removed

modified:deno_dist/utils/url.ts+5 added
72 unmodified lines
73 const queryIndex = url.indexOf('?', 8)74 return url.slice(url.indexOf('/', 8), queryIndex === -1 ? undefined : queryIndex)75}7677export const getQueryStrings = (url: string): string => {78 const queryIndex = url.indexOf('?', 8)79 return queryIndex === -1 ? '' : '?' + url.slice(queryIndex + 1)80}8182export const getPathNoStrict = (request: Request): string => {83 const result = getPath(request)8485 // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same
155 unmodified lines
modified:deno_dist/hono-base.ts+7 added-2 removed
18 unmodified lines
19 MergeSchemaPath,20 FetchEventLike,21} from './types.ts'22import type { RemoveBlankRecord } from './utils/types.ts'23import { getPath, getPathNoStrict, mergePath } from './utils/url.ts'23import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'2425type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE2627interface RouterRoute {
187 unmodified lines
215 executionContext = c.executionCtx216 } catch {} // Do nothing217 const options = optionHandler ? optionHandler(c) : [c.env, executionContext]218 const optionsArray = Array.isArray(options) ? options : [options]219220 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 )228229 if (res) return res
154 unmodified lines
modified:src/utils/url.test.ts+15 added
6 unmodified lines
7 mergePath,8 checkOptionalParameter,9 getQueryParam,10 getQueryParams,11 getQueryStrings,12} from './url'1314describe('url', () => {15 it('splitPath', () => {
68 unmodified lines
84 expect(path).toBe('/hello/hey/')85 })86 })8788 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 })101102 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 lines
modified:src/utils/url.ts+5 added
72 unmodified lines
73 const queryIndex = url.indexOf('?', 8)74 return url.slice(url.indexOf('/', 8), queryIndex === -1 ? undefined : queryIndex)75}7677export const getQueryStrings = (url: string): string => {78 const queryIndex = url.indexOf('?', 8)79 return queryIndex === -1 ? '' : '?' + url.slice(queryIndex + 1)80}8182export const getPathNoStrict = (request: Request): string => {83 const result = getPath(request)8485 // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same
155 unmodified lines
modified:src/hono-base.ts+7 added-2 removed
18 unmodified lines
19 MergeSchemaPath,20 FetchEventLike,21} from './types'22import type { RemoveBlankRecord } from './utils/types'23import { getPath, getPathNoStrict, mergePath } from './utils/url'23import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'2425type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE2627interface RouterRoute {
187 unmodified lines
215 executionContext = c.executionCtx216 } catch {} // Do nothing217 const options = optionHandler ? optionHandler(c) : [c.env, executionContext]218 const optionsArray = Array.isArray(options) ? options : [options]219220 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 )228229 if (res) return res
154 unmodified lines
modified:src/hono.test.ts+8 added
1923 unmodified lines
1924 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 lines
1992 expect(res.status).toBe(404)1993 expect(res.headers.get('x-message')).toBe('Foo')1994 expect(await res.text()).toBe('Not Found from AnotherApp')19951996 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')19992000 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