Skip to content

Commit

Permalink
feat(helper): introduce factory helper (#1434)
Browse files Browse the repository at this point in the history
* feat(helper): introduce `factory` helper

* denoify
  • Loading branch information
yusukebe authored Sep 10, 2023
1 parent a6d00dd commit e4c4322
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
10 changes: 10 additions & 0 deletions deno_dist/helper/factory/index.ts
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
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@
"import": "./dist/helper/adapter/index.js",
"require": "./dist/cjs/helper/adapter/index.js"
},
"./factory": {
"types": "./dist/types/helper/factory/index.d.ts",
"import": "./dist/helper/factory/index.js",
"require": "./dist/cjs/helper/factory/index.js"
},
"./cloudflare-workers": {
"types": "./dist/types/adapter/cloudflare-workers/index.d.ts",
"import": "./dist/adapter/cloudflare-workers/index.js",
Expand Down Expand Up @@ -322,6 +327,9 @@
"adapter": [
"./dist/types/helper/adapter/index.d.ts"
],
"factory": [
"./dist/types/helper/factory/index.d.ts"
],
"cloudflare-workers": [
"./dist/types/adapter/cloudflare-workers"
],
Expand Down Expand Up @@ -423,4 +431,4 @@
"node": ">=16.0.0"
},
"dependencies": {}
}
}
32 changes: 32 additions & 0 deletions src/helper/factory/index.test.ts
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')
})
})
10 changes: 10 additions & 0 deletions src/helper/factory/index.ts
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

0 comments on commit e4c4322

Please sign in to comment.