MIGRATION.md
This file
Path
docs/MIGRATION.md
295 lines 路 7.8 KiB 路 Markdown
Last changed by
docs(MIGRATION): fix req.raw.headers reference (property, not method) (#5047)EduardF1before adoptioned8795e
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296# Migration Guide
## v4.3.11 to v4.4.0
### `deno.land/x` to JSR
There is no breaking change, but we no longer publish the module from `deno.land/x`. If you want to use Hono on Deno, use JSR instead of it.
If you migrate, replace the path `deno.land/x` with JSR's.
```ts// Fromimport { Hono } from 'https://deno.land/x/hono/mod.ts'
// Toimport { Hono } from 'jsr:@hono/hono'```
You can see more details on our website: https://hono.dev/getting-started/deno
## v3.12.x to v4.0.0
There are some breaking changes.
### Removal of deprecated features
- AWS Lambda Adapter - `LambdaFunctionUrlRequestContext` is obsolete. Use `ApiGatewayRequestContextV2` instead.- Next.js Adapter - `hono/nextjs` is obsolete. Use `hono/vercel` instead.- Context - `c.jsonT()` is obsolete. Use `c.json()` instead.- Context - `c.stream()` and `c.streamText()` are obsolete. Use `stream()` and `streamText()` in `hono/streaming` instead.- Context - `c.env()` is obsolete. Use `getRuntimeKey()` in `hono/adapter` instead.- Hono - `app.showRoutes()` is obsolete. Use `showRoutes()` in `hono/dev` instead.- Hono - `app.routerName` is obsolete. Use `getRouterName()` in `hono/dev` instead.- Hono - `app.head()` is no longer used. `app.get()` implicitly handles the HEAD method.- Hono - `app.handleEvent()` is obsolete. Use `app.fetch()` instead.- HonoRequest - `req.cookie()` is obsolete. Use `getCookie()` in `hono/cookie` instead.- HonoRequest - `headers()`, `body()`, `bodyUsed()`, `integrity()`, `keepalive()`, `referrer()`, and `signal()` are obsolete. Use the equivalent properties on `req.raw` such as `req.raw.headers`.
### `serveStatic` in Cloudflare Workers Adapter requires `manifest`
If you use the Cloudflare Workers adapter's `serve-static`, you should specify the `manifest` option.
```tsimport manifest from '__STATIC_CONTENT_MANIFEST'
// ...
app.use('/static/*', serveStatic({ root: './assets', manifest }))```
### Others
- The default value of the `docType` option in JSX Renderer Middleware is now `true`.- `FC` in `hono/jsx` does not pass `children`. Use `PropsWithChildren`.- Some Mime Types are removed https://github.com/honojs/hono/pull/2119.- Types for chaining routes with middleware matter: https://github.com/honojs/hono/pull/2046.- Types for the validator matter: https://github.com/honojs/hono/pull/2130.
## v2.7.8 to v3.0.0
There are some breaking changes.In addition to the following, type mismatches may occur.
### `c.req` is now `HonoRequest`
`c.req` becomes `HonoRequest`, not `Request`.Although APIs are almost the same, if you want to access `Request`, use `c.req.raw`.
```tsapp.post('/', async (c) => { const metadata = c.req.raw.cf?.hostMetadata? ...})```
### StaticRouter is obsolete
You can't use `StaticRouter`.
### Validator has been changed
Previous Validator Middleware is obsolete.You can still use `hono/validator`, but the API has been changed.See [the document](https://hono.dev).
### `serveStatic` is provided from the Adapter
Serve Static Middleware is obsolete. Use Adapters instead.
```ts// For Cloudflare Workersimport { serveStatic } from 'hono/cloudflare-workers'
// For Bun// import { serveStatic } from 'hono/bun'
// For Deno// import { serveStatic } from 'npm:hono/deno'
// ...
app.get('/static/*', serveStatic({ root: './' }))```
### `serveStatic` for Cloudflare Workers "Service Worker mode" is obsolete
For Cloudflare Workers, the `serveStatic` is obsolete in Service Worker mode.
Note: Service Worker mode is that using `app.fire()`.We recommend use "Module Worker" mode with `export default app`.
### Use `type` to define the Generics for `new Hono`
You must use `type` to define the Generics for `new Hono`. Do not use `interface`.
```ts// Should use `type`type Bindings = { TOKEN: string}
const app = new Hono<{ Bindings: Bindings }>()```
## v2.7.1 - v2.x.x
### Current Validator Middleware is deprecated
At the next major version, Validator Middleware will be changed with "breaking changes". Therefore, the current Validator Middleware will be deprecated; please use 3rd-party Validator libraries such as [Zod](https://zod.dev) or [TypeBox](https://github.com/sinclairzx81/typebox).
```tsimport * as z from 'zod'
//...
const schema = z.object({ title: z.string().max(100),})
app.post('/posts', async (c) => { const body = await c.req.parseBody() const res = schema.safeParse(body) if (!res.success) { return c.text('Invalid!', 400) } return c.text('Valid!')})```
## v2.2.5 to v2.3.0
There is a breaking change associated with the security update.
### Basic Auth Middleware and Bearer Auth Middleware
If you are using Basic Auth and Bearer Auth in your Handler (nested), change as follows:
```tsapp.use('/auth/*', async (c, next) => { const auth = basicAuth({ username: c.env.USERNAME, password: c.env.PASSWORD }) return auth(c, next) // Older: `await auth(c, next)`})```
## v2.0.9 to v2.1.0
There are two BREAKING CHANGES.
### `c.req.parseBody` does not parse JSON, text, and ArrayBuffer
**DO NOT** use `c.req.parseBody` for parsing **JSON**, **text**, or **ArrayBuffer**.
`c.req.parseBody` now only parses FormData with content type `multipart/form` or `application/x-www-form-urlencoded`. If you want to parse JSON, text, or ArrayBuffer, use `c.req.json()`, `c.req.text()`, or `c.req.arrayBuffer()`.
```ts// `multipart/form` or `application/x-www-form-urlencoded`const data = await c.req.parseBody()
const jsonData = await c.req.json() // for JSON bodyconst text = await c.req.text() // for text bodyconst arrayBuffer = await c.req.arrayBuffer() // for ArrayBuffer```
### The arguments of Generics for `new Hono` have been changed
Now, the constructor of "Hono" receives `Variables` and `Bindings`."Bindings" is for types of environment variables for Cloudflare Workers. "Variables" is for types of `c.set`/`c.get`
```tstype Bindings = { KV: KVNamespace Storage: R2Bucket}
type WebClient = { user: string pass: string}
type Variables = { client: WebClient}
const app = new Hono<{ Variables: Variables; Bindings: Bindings }>()
app.get('/foo', (c) => { const client = c.get('client') // client is WebClient const kv = c.env.KV // kv is KVNamespace //...})```
## v1.6.4 to v2.0.0
There are many BREAKING CHANGES. Please follow the instructions below.
### The way to import Middleware on Deno has been changed
**DO NOT** import middleware from `hono/mod.ts`.
```tsimport { Hono, poweredBy } from 'https://deno.land/x/hono/mod.ts' // <--- NG```
`hono/mod.ts` does not export middleware.To import middleware, use `hono/middleware.ts`:
```tsimport { Hono } from 'https://deno.land/x/hono/mod.ts'import { poweredBy, basicAuth } from 'https://deno.land/x/hono/middleware.ts'```
### Cookie middleware is obsolete
**DO NOT** use `cookie` middleware.
```tsimport { cookie } from 'hono/cookie' // <--- Obsolete!```
You do not have to use Cookie middleware to parse or set cookies.They become default functions:
```ts// Parse cookieapp.get('/entry/:id', (c) => { const value = c.req.cookie('name') ...})```
```tsapp.get('/', (c) => { c.cookie('delicious_cookie', 'choco') return c.text('Do you like cookie?')})```
### Body parse middleware is obsolete
**DO NOT** use `body-parse` middleware.
```tsimport { bodyParse } from 'hono/body-parse' // <--- Obsolete!```
You do not have to use Body parse middleware to parse the request body. Use `c.req.parseBody()` method instead.
```ts// Parse Request bodyapp.post('', (c) => { const body = c.req.parseBody() ...})```
### GraphQL Server middleware is obsolete
**DO NOT** use `graphql-server` middleware.
```tsimport { graphqlServer } from 'hono/graphql-server' // <--- Obsolete!```
It might be distributed as third-party middleware.
### Mustache middleware is obsolete
**DO NOT** use `mustache` middleware.
```tsimport { mustache } from 'hono/mustache' // <--- Obsolete!```
It will no longer be implemented.