Appearance
工厂助手
¥Factory Helper
Factory Helper 提供了创建 Hono 组件(如中间件)的有用功能。有时很难设置正确的 TypeScript 类型,但此辅助程序可以简化此操作。
¥The Factory Helper provides useful functions for creating Hono's components such as Middleware. Sometimes it's difficult to set the proper TypeScript types, but this helper facilitates that.
导入
¥Import
ts
import { Hono } from 'hono'
import { createFactory, createMiddleware } from 'hono/factory'
createFactory()
createFactory()
将创建 Factory 类的实例。
¥createFactory()
will create an instance of the Factory class.
ts
import { createFactory } from 'hono/factory'
const factory = createFactory()
你可以将 Env 类型作为泛型传递:
¥You can pass your Env types as Generics:
ts
type Env = {
Variables: {
foo: string
}
}
const factory = createFactory<Env>()
选项
¥Options
optional defaultAppOptions:HonoOptions
传递给 createApp()
创建的 Hono 应用的默认选项。
¥The default options to pass to the Hono application created by createApp()
.
ts
const factory = createFactory({
defaultAppOptions: { strict: false },
})
const app = factory.createApp() // `strict: false` is applied
createMiddleware()
createMiddleware()
是 factory.createMiddleware()
的快捷方式。此函数将创建你的自定义中间件。
¥createMiddleware()
is shortcut of factory.createMiddleware()
. This function will create your custom middleware.
ts
const messageMiddleware = createMiddleware(async (c, next) => {
await next()
c.res.headers.set('X-Message', 'Good morning!')
})
提示:如果你想要获取像 message
这样的参数,你可以像下面这样将其创建为一个函数。
¥Tip: If you want to get an argument like message
, you can create it as a function like the following.
ts
const messageMiddleware = (message: string) => {
return createMiddleware(async (c, next) => {
await next()
c.res.headers.set('X-Message', message)
})
}
app.use(messageMiddleware('Good evening!'))
factory.createHandlers()
createHandlers()
有助于在与 app.get('/')
不同的地方定义处理程序。
¥createHandlers()
helps to define handlers in a different place than app.get('/')
.
ts
import { createFactory } from 'hono/factory'
import { logger } from 'hono/logger'
// ...
const factory = createFactory()
const middleware = factory.createMiddleware(async (c, next) => {
c.set('foo', 'bar')
await next()
})
const handlers = factory.createHandlers(logger(), middleware, (c) => {
return c.json(c.var.foo)
})
app.get('/api', ...handlers)
factory.createApp()
createApp()
帮助创建具有适当类型的 Hono 实例。如果你将此方法与 createFactory()
一起使用,则可以避免 Env
类型的定义中的冗余。
¥createApp()
helps to create an instance of Hono with the proper types. If you use this method with createFactory()
, you can avoid redundancy in the definition of the Env
type.
如果你的应用是这样的,你必须在两个地方设置 Env
:
¥If your application is like this, you have to set the Env
in two places:
ts
import { createMiddleware } from 'hono/factory'
type Env = {
Variables: {
myVar: string
}
}
// 1. Set the `Env` to `new Hono()`
const app = new Hono<Env>()
// 2. Set the `Env` to `createMiddleware()`
const mw = createMiddleware<Env>(async (c, next) => {
await next()
})
app.use(mw)
通过使用 createFactory()
和 createApp()
,你只能在一个地方设置 Env
。
¥By using createFactory()
and createApp()
, you can set the Env
only in one place.
ts
import { createFactory } from 'hono/factory'
// ...
// Set the `Env` to `createFactory()`
const factory = createFactory<Env>()
const app = factory.createApp()
// factory also has `createMiddleware()`
const mw = factory.createMiddleware(async (c, next) => {
await next()
})
createFactory()
可以接收 initApp
选项来初始化由 createApp()
创建的 app
。以下是使用该选项的示例。
¥createFactory()
can receive the initApp
option to initialize an app
created by createApp()
. The following is an example that uses the option.
ts
// factory-with-db.ts
type Env = {
Bindings: {
MY_DB: D1Database
}
Variables: {
db: DrizzleD1Database
}
}
export default createFactory<Env>({
initApp: (app) => {
app.use(async (c, next) => {
const db = drizzle(c.env.MY_DB)
c.set('db', db)
await next()
})
},
})
ts
// crud.ts
import factoryWithDB from './factory-with-db'
const app = factoryWithDB.createApp()
app.post('/posts', (c) => {
c.var.db.insert()
// ...
})