Skip to content

Commit

Permalink
fix(mount): include query params (#1224)
Browse files Browse the repository at this point in the history
* fix(mount): include query params

* denoify
  • Loading branch information
yusukebe authored Jul 8, 2023
1 parent 9fd3cd3 commit cef0adb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
9 changes: 7 additions & 2 deletions deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
FetchEventLike,
} from './types.ts'
import type { RemoveBlankRecord } from './utils/types.ts'
import { getPath, getPathNoStrict, mergePath } from './utils/url.ts'
import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'

type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE

Expand Down Expand Up @@ -216,8 +216,13 @@ class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends d
} catch {} // Do nothing
const options = optionHandler ? optionHandler(c) : [c.env, executionContext]
const optionsArray = Array.isArray(options) ? options : [options]

const queryStrings = getQueryStrings(c.req.url)
const res = await applicationHandler(
new Request(new URL(c.req.path.slice(pathPrefixLength) || '/', c.req.url), c.req.raw),
new Request(
new URL((c.req.path.slice(pathPrefixLength) || '/') + queryStrings, c.req.url),
c.req.raw
),
...optionsArray
)

Expand Down
5 changes: 5 additions & 0 deletions deno_dist/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export const getPath = (request: Request): string => {
return url.slice(url.indexOf('/', 8), queryIndex === -1 ? undefined : queryIndex)
}

export const getQueryStrings = (url: string): string => {
const queryIndex = url.indexOf('?', 8)
return queryIndex === -1 ? '' : '?' + url.slice(queryIndex + 1)
}

export const getPathNoStrict = (request: Request): string => {
const result = getPath(request)

Expand Down
9 changes: 7 additions & 2 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
FetchEventLike,
} from './types'
import type { RemoveBlankRecord } from './utils/types'
import { getPath, getPathNoStrict, mergePath } from './utils/url'
import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'

type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE

Expand Down Expand Up @@ -216,8 +216,13 @@ class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends d
} catch {} // Do nothing
const options = optionHandler ? optionHandler(c) : [c.env, executionContext]
const optionsArray = Array.isArray(options) ? options : [options]

const queryStrings = getQueryStrings(c.req.url)
const res = await applicationHandler(
new Request(new URL(c.req.path.slice(pathPrefixLength) || '/', c.req.url), c.req.raw),
new Request(
new URL((c.req.path.slice(pathPrefixLength) || '/') + queryStrings, c.req.url),
c.req.raw
),
...optionsArray
)

Expand Down
8 changes: 8 additions & 0 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,10 @@ describe('app.mount()', () => {
const message = req.headers.get('x-message')
return new Response(message)
}
if (path === '/with-query') {
const queryStrings = new URL(req.url).searchParams.toString()
return new Response(queryStrings)
}
if (path == '/with-params') {
return new Response(
JSON.stringify({
Expand Down Expand Up @@ -1989,6 +1993,10 @@ describe('app.mount()', () => {
expect(res.headers.get('x-message')).toBe('Foo')
expect(await res.text()).toBe('Not Found from AnotherApp')

res = await app.request('/another-app/with-query?foo=bar&baz=qux')
expect(res.status).toBe(200)
expect(await res.text()).toBe('foo=bar&baz=qux')

res = await app.request('/another-app/with-params')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
Expand Down
15 changes: 15 additions & 0 deletions src/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
checkOptionalParameter,
getQueryParam,
getQueryParams,
getQueryStrings,
} from './url'

describe('url', () => {
Expand Down Expand Up @@ -84,6 +85,20 @@ describe('url', () => {
})
})

describe('getQueryStrings', () => {
it('getQueryStrings', () => {
let qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20')
expect(qs).toBe('?name=foo&name=bar&age=20')
qs = getQueryStrings('https://example.com/hello?')
expect(qs).toBe('?')
qs = getQueryStrings('https://example.com/hello')
expect(qs).toBe('')
// Allows to contain hash
qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20#hash')
expect(qs).toBe('?name=foo&name=bar&age=20#hash')
})
})

describe('getPathNoStrict', () => {
it('getPathNoStrict - no strict is false', () => {
let path = getPathNoStrict(new Request('https://example.com/hello/'))
Expand Down
5 changes: 5 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export const getPath = (request: Request): string => {
return url.slice(url.indexOf('/', 8), queryIndex === -1 ? undefined : queryIndex)
}

export const getQueryStrings = (url: string): string => {
const queryIndex = url.indexOf('?', 8)
return queryIndex === -1 ? '' : '?' + url.slice(queryIndex + 1)
}

export const getPathNoStrict = (request: Request): string => {
const result = getPath(request)

Expand Down

0 comments on commit cef0adb

Please sign in to comment.