feat: add first-class QUERY method support (#5070)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by haki · committed by Yusuke Wada · parent 5fc8aad · Browse files · 5 files·+45 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+45 added−2 removed
- tests:+42 added−0 removed
- other:+3 added−2 removed
Provenance
- Commit
6f101a7- Tree
4871667- Author
haki <165590443+shellhaki@users.noreply.github.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
5fc8aad
5 files changed+45 added−2 removed
modified:src/client/types.test.ts+1 added
115 unmodified linesExpand all11611711811912012112212312422 unmodified lines115 unmodified linesExpand all expectTypeOf(client['all-route']).toHaveProperty('$put') expectTypeOf(client['all-route']).toHaveProperty('$delete') expectTypeOf(client['all-route']).toHaveProperty('$options') expectTypeOf(client['all-route']).toHaveProperty('$patch') expectTypeOf(client['all-route']).toHaveProperty('$query') })
it('should have correct return type for expanded methods', async () => { // The response type should match the original handler's return type22 unmodified linesmodified:src/client/types.ts+1 added-1 removed
10 unmodified lines11 type MethodNameAll = `$${typeof METHOD_NAME_ALL_LOWERCASE}`12 13 /**14 * Type representing all standard HTTP methods prefixed with '$'15- * e.g., '$get' | '$post' | '$put' | '$delete' | '$options' | '$patch'15+ * e.g., '$get' | '$post' | '$put' | '$delete' | '$options' | '$patch' | '$query'16 */17 type StandardMethods = `$${(typeof METHODS)[number]}`18 19 /**374 unmodified linesmodified:src/hono-base.ts+1 added
105 unmodified lines106 put!: HandlerInterface<E, 'put', S, BasePath, CurrentPath>107 delete!: HandlerInterface<E, 'delete', S, BasePath, CurrentPath>108 options!: HandlerInterface<E, 'options', S, BasePath, CurrentPath>109 patch!: HandlerInterface<E, 'patch', S, BasePath, CurrentPath>110+ query!: HandlerInterface<E, 'query', S, BasePath, CurrentPath>111 all!: HandlerInterface<E, 'all', S, BasePath, CurrentPath>112 on: OnHandlerInterface<E, S, BasePath>113 use: MiddlewareHandlerInterface<E, S, BasePath>114 432 unmodified linesmodified:src/hono.test.ts+41 added
2077 unmodified lines2078 expect(res.status).toBe(404)2079 })2080 })2081 2082+describe('Using QUERY with `app.query` and `app.on`', () => {2083+ it('Should handle QUERY method with app.query()', async () => {2084+ const app = new Hono()2085+2086+ app.query('/query', (c) => c.text('Accepted', 202))2087+2088+ const req = new Request('http://localhost/query', {2089+ method: 'QUERY',2090+ })2091+ const res = await app.request(req)2092+ expect(res.status).toBe(202)2093+ expect(await res.text()).toBe('Accepted')2094+ })2095+2096+ it('Should handle QUERY method with RegExpRouter', async () => {2097+ const app = new Hono({ router: new RegExpRouter() })2098+2099+ app.on('QUERY', '/query', (c) => c.text('Accepted', 202))2100+2101+ const req = new Request('http://localhost/query', {2102+ method: 'QUERY',2103+ })2104+ const res = await app.request(req)2105+ expect(res.status).toBe(202)2106+ expect(await res.text()).toBe('Accepted')2107+ })2108+2109+ it('Should handle QUERY method with TrieRouter', async () => {2110+ const app = new Hono({ router: new TrieRouter() })2111+2112+ app.on('QUERY', '/query', (c) => c.text('Accepted', 202))2113+2114+ const req = new Request('http://localhost/query', {2115+ method: 'QUERY',2116+ })2117+ const res = await app.request(req)2118+ expect(res.status).toBe(202)2119+ expect(await res.text()).toBe('Accepted')2120+ })2121+})2122+2123 describe('Multiple paths with one handler', () => {2124 const app = new Hono()2125 2126 const paths = ['/hello', '/ja/hello', '/en/hello']1674 unmodified linesmodified:src/router.ts+1 added-1 removed
12 unmodified lines13 export const METHOD_NAME_ALL_LOWERCASE = 'all' as const14 /**15 * Array of supported HTTP methods.16 */17-export const METHODS = ['get', 'post', 'put', 'delete', 'options', 'patch'] as const17+export const METHODS = ['get', 'post', 'put', 'delete', 'options', 'patch', 'query'] as const18 /**19 * Error message indicating that a route cannot be added because the matcher is already built.20 */21 export const MESSAGE_MATCHER_IS_ALREADY_BUILT =82 unmodified lines