new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Hono · all subjects

api

85 notes in this subject, read out of this brain and free to use. This is page 2 of 2.

Testing POST with multipart/form-data

To send multipart form data in a POST request test, create a FormData instance, append fields to it, and pass it as the body. The Content-Type header is automatically set. Example: const formData = new FormData(); formData.append('message', 'hello'); const res = await app.request('/posts', { method: 'POST', body: formData });

Testing with Request instance

app.request can accept an instance of the Request class as its first parameter instead of a path string. Example: const req = new Request('http://localhost/posts', { method: 'POST' }); const res = await app.request(req);

Testing response headers

Response headers can be tested using res.headers.get(headerName). Example: expect(res.headers.get('X-Custom')).toBe('Thank you');

Testing helper for typed test client

Hono provides a testing helper for a typed test client. See the testing helper documentation at /docs/helpers/testing for more details.

env() function runtime-specific behavior

The env() function retrieves environment variables differently depending on the runtime. On Node.js or Bun, NAME returns process.env.NAME. On Cloudflare, it returns the value written in wrangler.toml or wrangler.jsonc.

env() function imports and usage

The env() function is imported from 'hono/adapter' and facilitates retrieving environment variables across different runtimes. It is called with the context as the first argument: env(c). The function is generic and accepts a type parameter for typing the returned environment variables, for example env<{ NAME: string }>(c).

env() supported runtimes for environment variables

The env() function supports the following runtimes: Cloudflare Workers (wrangler.toml, wrangler.jsonc), Deno (Deno.env, .env file), Bun (Bun.env, process.env), Node.js (process.env), Vercel (Environment Variables on Vercel), AWS Lambda (Environment Variables on AWS Lambda), Lambda@Edge (environment variables not supported, use Lambda@Edge event as alternative), Fastly Compute (use ConfigStore for user-defined data), Netlify (use Netlify Contexts for user-defined data).

env() with explicit runtime key parameter

The env() function accepts an optional second argument to specify the runtime explicitly. For example, env<{ NAME: string }>(c, 'workerd') retrieves environment variables from Cloudflare Workers specifically.

getRuntimeKey() function imports and usage

The getRuntimeKey() function is imported from 'hono/adapter' and returns the identifier of the current runtime. It takes no arguments and returns a string representing the runtime key.

getRuntimeKey() available runtime keys

The getRuntimeKey() function returns one of the following runtime keys: 'workerd' (Cloudflare Workers), 'deno' (Deno), 'bun' (Bun), 'node' (Node.js), 'edge-light' (Vercel Edge Functions), 'fastly' (Fastly Compute), 'other' (other unknown runtimes, some inspired by WinterCG's Runtime Keys).

getRuntimeKey() example usage

The getRuntimeKey() function is used to conditionally execute code based on the current runtime. For example: if (getRuntimeKey() === 'workerd') { return c.text('You are on Cloudflare') } else if (getRuntimeKey() === 'bun') { return c.text('You are on Bun') }

ConnInfo interface definition

ConnInfo is an interface representing HTTP connection information. It contains one property: - remote: NetAddrInfo (required, remote connection information)

ConnInfo helper overview and purpose

The ConnInfo Helper is used to retrieve connection information from requests, such as the client's remote address. It is available across multiple runtimes.

getConnInfo usage example

Call getConnInfo(c) where c is the context to retrieve a ConnInfo object. Access the remote address via info.remote.address. Example: ```ts const app = new Hono() app.get('/', (c) => { const info = getConnInfo(c) // info is `ConnInfo` return c.text(`Your remote address is ${info.remote.address}`) }) ```

AddressType type definition

AddressType is a union type with values: 'IPv6' | 'IPv4' | undefined.

NetAddrInfo type definition

NetAddrInfo is an object type with the following properties: - transport?: 'tcp' | 'udp' (optional, transport protocol type) - port?: number (optional, transport port number) - address?: string (optional, host name such as IP address) - addressType?: AddressType (optional, host name type) At least one of the following must be present for a valid NetAddrInfo: - address: string (required in discriminated union) - addressType: AddressType (required in discriminated union)

createMiddleware() import

createMiddleware is imported from 'hono/factory'.

createFactory() defaultAppOptions option

createFactory() accepts an optional defaultAppOptions parameter of type HonoOptions that specifies default options to pass to the Hono application created by createApp(). Example: createFactory({ defaultAppOptions: { strict: false } }).

createFactory() with Env generic

createFactory() accepts a type parameter Env to specify Variables and other environment types. Example: createFactory<Env>() where Env includes Variables: { foo: string }.

createFactory() basic usage

createFactory() creates an instance of the Factory class that provides methods for creating Hono components with proper TypeScript types.

factory.createHandlers() method

factory.createHandlers() helps define handlers in a different place than the route definition. It accepts middleware and handler functions, returning an array of handlers to spread into route definitions. Example: const handlers = factory.createHandlers(logger(), middleware, (c) => { return c.json(c.var.foo) }); app.get('/api', ...handlers).

createFactory() with initApp option

createFactory() accepts an optional initApp option that receives a callback function taking the app as a parameter to initialize it. Example: createFactory<Env>({ initApp: (app) => { app.use(async (c, next) => { const db = drizzle(c.env.MY_DB); c.set('db', db); await next() }) } }).

Factory pattern for database initialization

A common pattern is exporting a factory instance with initApp configured for database setup, then importing it in other modules to create apps that inherit the database middleware. The factory's createMiddleware() method inherits the Env type from createFactory<Env>(), avoiding redundant type annotations.

createFactory() import

createFactory is imported from 'hono/factory'.

Hono RPC mode with validators and client

The Validator and Hono Client (hc) enable RPC mode, allowing use of favorite validators such as Zod to share server-side API specs with the client and build type-safe applications.

Give your agent this brain