Skip to content

Commit

Permalink
add createFactory() and createHandlers()
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Dec 3, 2023
1 parent 12fb6c3 commit 2bef606
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
25 changes: 25 additions & 0 deletions guides/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ app.get('/books/:id', (c) => {
})
```

## `factory.createHandlers()` in `hono/factory`

If you still want to create a RoR-like Controller, use `factory.createHandlers()` in [`hono/factory`](/helpers/factory). If you use this, type inference will work correctly.

```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)
```

## Building a larger application

Use `app.route()` to build a larger application without creating "Ruby on Rails-like Controllers".
Expand Down
51 changes: 49 additions & 2 deletions helpers/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,41 @@ The Factory Helper provides useful functions for creating Hono's components such

```ts [npm]
import { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'
import { createFactory, createMiddleware } from 'hono/factory'
```

```ts [Deno]
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { createMiddleware } from 'https://deno.land/x/hono/helper.ts'
import { createFactory, createMiddleware } from 'https://deno.land/x/hono/helper.ts'
```

:::

## `createFactory()`

`createFactory()` will create an instance of Factory class.

```ts
import { createFactory } from 'hono/factory'

const factory = createFactory()
```

You can pass your Env types as Generics:

```ts
type Env = {
Variables: {
foo: string
}
}

const factory = createFactory<Env>()
```

## `createMiddleware()`

`createMiddleware()` is shortcut of `factory.createMiddleware()`.
This function will create your custom middleware.

```ts
Expand All @@ -41,3 +64,27 @@ const messageMiddleware = (message: string) => {

app.use('*', messageMiddleware('Good evening!'))
```

## `factory.createHandlers()` <Badge style="vertical-align: middle;" type="warning" text="Experimental" />

This function 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)
```

0 comments on commit 2bef606

Please sign in to comment.