-
-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): introduce
factory
helper (#1434)
* feat(helper): introduce `factory` helper * denoify
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Env, Input, MiddlewareHandler } from '../../types.ts' | ||
|
||
/** | ||
* @experimental | ||
* `middleware()` is an experimental feature. | ||
* The API might be changed. | ||
*/ | ||
export const middleware = <E extends Env = Env, P extends string = string, I extends Input = {}>( | ||
middleware: MiddlewareHandler<E, P, I> | ||
) => middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { hc } from '../../client' | ||
import { Hono } from '../../index' | ||
import { middleware } from './index' | ||
|
||
describe('middleware', () => { | ||
type Env = { Variables: { foo: string } } | ||
const app = new Hono<Env>() | ||
|
||
const mw = (message: string) => | ||
middleware<Env>(async (c, next) => { | ||
c.set('foo', 'bar') | ||
await next() | ||
c.header('X-Message', message) | ||
}) | ||
|
||
const route = app.get('/message', mw('Hello Middleware'), (c) => { | ||
return c.text(`Hey, ${c.var.foo}`) | ||
}) | ||
|
||
it('Should return the correct header and the content', async () => { | ||
const res = await app.request('/message') | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('x-message')).toBe('Hello Middleware') | ||
expect(await res.text()).toBe('Hey, bar') | ||
}) | ||
|
||
it('Should provide the correct types', async () => { | ||
const client = hc<typeof route>('http://localhost') | ||
const url = client.message.$url() | ||
expect(url.pathname).toBe('/message') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Env, Input, MiddlewareHandler } from '../../types' | ||
|
||
/** | ||
* @experimental | ||
* `middleware()` is an experimental feature. | ||
* The API might be changed. | ||
*/ | ||
export const middleware = <E extends Env = Env, P extends string = string, I extends Input = {}>( | ||
middleware: MiddlewareHandler<E, P, I> | ||
) => middleware |