Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–36why, one step back
1# Migration Guide
23## v4.3.11 to v4.4.045### `deno.land/x` to JSR67There 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.89If you migrate, replace the path `deno.land/x` with JSR's.1011```ts12// From13import { Hono } from 'https://deno.land/x/hono/mod.ts'1415// To16import { Hono } from 'jsr:@hono/hono'17```1819You can see more details on our website: https://hono.dev/getting-started/deno2021## v3.12.x to v4.0.02223There are some breaking changes.2425### Removal of deprecated features2627- AWS Lambda Adapter - `LambdaFunctionUrlRequestContext` is obsolete. Use `ApiGatewayRequestContextV2` instead.28- Next.js Adapter - `hono/nextjs` is obsolete. Use `hono/vercel` instead.29- Context - `c.jsonT()` is obsolete. Use `c.json()` instead.30- Context - `c.stream()` and `c.streamText()` are obsolete. Use `stream()` and `streamText()` in `hono/streaming` instead.31- Context - `c.env()` is obsolete. Use `getRuntimeKey()` in `hono/adapter` instead.32- Hono - `app.showRoutes()` is obsolete. Use `showRoutes()` in `hono/dev` instead.33- Hono - `app.routerName` is obsolete. Use `getRouterName()` in `hono/dev` instead.34- Hono - `app.head()` is no longer used. `app.get()` implicitly handles the HEAD method.35- Hono - `app.handleEvent()` is obsolete. Use `app.fetch()` instead.36- HonoRequest - `req.cookie()` is obsolete. Use `getCookie()` in `hono/cookie` instead.

docs(MIGRATION): fix req.raw.headers reference (property, not method) (#5047)EduardF1before adoptioned8795e

line 37why, one step back
37- HonoRequest - `headers()`, `body()`, `bodyUsed()`, `integrity()`, `keepalive()`, `referrer()`, and `signal()` are obsolete. Use the equivalent properties on `req.raw` such as `req.raw.headers`.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 38–295why, one step back
38
39### `serveStatic` in Cloudflare Workers Adapter requires `manifest`4041If you use the Cloudflare Workers adapter's `serve-static`, you should specify the `manifest` option.4243```ts44import manifest from '__STATIC_CONTENT_MANIFEST'4546// ...4748app.use('/static/*', serveStatic({ root: './assets', manifest }))49```5051### Others5253- The default value of the `docType` option in JSX Renderer Middleware is now `true`.54- `FC` in `hono/jsx` does not pass `children`. Use `PropsWithChildren`.55- Some Mime Types are removed https://github.com/honojs/hono/pull/2119.56- Types for chaining routes with middleware matter: https://github.com/honojs/hono/pull/2046.57- Types for the validator matter: https://github.com/honojs/hono/pull/2130.5859## v2.7.8 to v3.0.06061There are some breaking changes.62In addition to the following, type mismatches may occur.6364### `c.req` is now `HonoRequest`6566`c.req` becomes `HonoRequest`, not `Request`.67Although APIs are almost the same, if you want to access `Request`, use `c.req.raw`.6869```ts70app.post('/', async (c) => {71 const metadata = c.req.raw.cf?.hostMetadata?72 ...73})74```7576### StaticRouter is obsolete7778You can't use `StaticRouter`.7980### Validator has been changed8182Previous Validator Middleware is obsolete.83You can still use `hono/validator`, but the API has been changed.84See [the document](https://hono.dev).8586### `serveStatic` is provided from the Adapter8788Serve Static Middleware is obsolete. Use Adapters instead.8990```ts91// For Cloudflare Workers92import { serveStatic } from 'hono/cloudflare-workers'9394// For Bun95// import { serveStatic } from 'hono/bun'9697// For Deno98// import { serveStatic } from 'npm:hono/deno'99100// ...101102app.get('/static/*', serveStatic({ root: './' }))103```104105### `serveStatic` for Cloudflare Workers "Service Worker mode" is obsolete106107For Cloudflare Workers, the `serveStatic` is obsolete in Service Worker mode.108109Note: Service Worker mode is that using `app.fire()`.110We recommend use "Module Worker" mode with `export default app`.111112### Use `type` to define the Generics for `new Hono`113114You must use `type` to define the Generics for `new Hono`. Do not use `interface`.115116```ts117// Should use `type`118type Bindings = {119 TOKEN: string120}121122const app = new Hono<{ Bindings: Bindings }>()123```124125## v2.7.1 - v2.x.x126127### Current Validator Middleware is deprecated128129At 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).130131```ts132import * as z from 'zod'133134//...135136const schema = z.object({137 title: z.string().max(100),138})139140app.post('/posts', async (c) => {141 const body = await c.req.parseBody()142 const res = schema.safeParse(body)143 if (!res.success) {144 return c.text('Invalid!', 400)145 }146 return c.text('Valid!')147})148```149150## v2.2.5 to v2.3.0151152There is a breaking change associated with the security update.153154### Basic Auth Middleware and Bearer Auth Middleware155156If you are using Basic Auth and Bearer Auth in your Handler (nested), change as follows:157158```ts159app.use('/auth/*', async (c, next) => {160 const auth = basicAuth({ username: c.env.USERNAME, password: c.env.PASSWORD })161 return auth(c, next) // Older: `await auth(c, next)`162})163```164165## v2.0.9 to v2.1.0166167There are two BREAKING CHANGES.168169### `c.req.parseBody` does not parse JSON, text, and ArrayBuffer170171**DO NOT** use `c.req.parseBody` for parsing **JSON**, **text**, or **ArrayBuffer**.172173`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()`.174175```ts176// `multipart/form` or `application/x-www-form-urlencoded`177const data = await c.req.parseBody()178179const jsonData = await c.req.json() // for JSON body180const text = await c.req.text() // for text body181const arrayBuffer = await c.req.arrayBuffer() // for ArrayBuffer182```183184### The arguments of Generics for `new Hono` have been changed185186Now, the constructor of "Hono" receives `Variables` and `Bindings`.187"Bindings" is for types of environment variables for Cloudflare Workers. "Variables" is for types of `c.set`/`c.get`188189```ts190type Bindings = {191 KV: KVNamespace192 Storage: R2Bucket193}194195type WebClient = {196 user: string197 pass: string198}199200type Variables = {201 client: WebClient202}203204const app = new Hono<{ Variables: Variables; Bindings: Bindings }>()205206app.get('/foo', (c) => {207 const client = c.get('client') // client is WebClient208 const kv = c.env.KV // kv is KVNamespace209 //...210})211```212213## v1.6.4 to v2.0.0214215There are many BREAKING CHANGES. Please follow the instructions below.216217### The way to import Middleware on Deno has been changed218219**DO NOT** import middleware from `hono/mod.ts`.220221```ts222import { Hono, poweredBy } from 'https://deno.land/x/hono/mod.ts' // <--- NG223```224225`hono/mod.ts` does not export middleware.226To import middleware, use `hono/middleware.ts`:227228```ts229import { Hono } from 'https://deno.land/x/hono/mod.ts'230import { poweredBy, basicAuth } from 'https://deno.land/x/hono/middleware.ts'231```232233### Cookie middleware is obsolete234235**DO NOT** use `cookie` middleware.236237```ts238import { cookie } from 'hono/cookie' // <--- Obsolete!239```240241You do not have to use Cookie middleware to parse or set cookies.242They become default functions:243244```ts245// Parse cookie246app.get('/entry/:id', (c) => {247 const value = c.req.cookie('name')248 ...249})250```251252```ts253app.get('/', (c) => {254 c.cookie('delicious_cookie', 'choco')255 return c.text('Do you like cookie?')256})257```258259### Body parse middleware is obsolete260261**DO NOT** use `body-parse` middleware.262263```ts264import { bodyParse } from 'hono/body-parse' // <--- Obsolete!265```266267You do not have to use Body parse middleware to parse the request body. Use `c.req.parseBody()` method instead.268269```ts270// Parse Request body271app.post('', (c) => {272 const body = c.req.parseBody()273 ...274})275```276277### GraphQL Server middleware is obsolete278279**DO NOT** use `graphql-server` middleware.280281```ts282import { graphqlServer } from 'hono/graphql-server' // <--- Obsolete!283```284285It might be distributed as third-party middleware.286287### Mustache middleware is obsolete288289**DO NOT** use `mustache` middleware.290291```ts292import { mustache } from 'hono/mustache' // <--- Obsolete!293```294295It will no longer be implemented.