diff --git a/src/context.ts b/src/context.ts index 3d656d07..6f35b5ee 100644 --- a/src/context.ts +++ b/src/context.ts @@ -223,6 +223,9 @@ export class Context< req: HonoRequest /** * `.env` can get bindings (environment variables, secrets, KV namespaces, D1 database, R2 bucket etc.) in Cloudflare Workers. + * + * @see {@link https://hono.dev/api/context#env} + * * @example * ```ts * // Environment object for Cloudflare Workers @@ -230,13 +233,15 @@ export class Context< * const counter = c.env.COUNTER * }) * ``` - * @see https://hono.dev/api/context#env */ env: E['Bindings'] = {} private _var: E['Variables'] = {} finalized: boolean = false /** * `.error` can get the error object from the middleware if the Handler throws an error. + * + * @see {@link https://hono.dev/api/context#error} + * * @example * ```ts * app.use('*', async (c, next) => { @@ -246,7 +251,6 @@ export class Context< * } * }) * ``` - * @see https://hono.dev/api/context#error */ error: Error | undefined = undefined @@ -278,11 +282,10 @@ export class Context< } /** + * @see {@link https://hono.dev/api/context#event} * The FetchEvent associated with the current request. * * @throws Will throw an error if the context does not have a FetchEvent. - * - * @see https://hono.dev/api/context#event */ get event(): FetchEventLike { if (this.#executionCtx && 'respondWith' in this.#executionCtx) { @@ -293,11 +296,10 @@ export class Context< } /** + * @see {@link https://hono.dev/api/context#executionctx} * The ExecutionContext associated with the current request. * * @throws Will throw an error if the context does not have an ExecutionContext. - * - * @see https://hono.dev/api/context#executionctx */ get executionCtx(): ExecutionContext { if (this.#executionCtx) { @@ -308,9 +310,8 @@ export class Context< } /** + * @see {@link https://hono.dev/api/context#res} * The Response object for the current request. - * - * @see https://hono.dev/api/context#res */ get res(): Response { this.#isFresh = false @@ -343,7 +344,9 @@ export class Context< } /** - * Renders a response within a layout. + * `.render()` can create a response within a layout. + * + * @see {@link https://hono.dev/api/context#render-setrenderer} * * @example * ```ts @@ -351,7 +354,6 @@ export class Context< * return c.render('Hello!') * }) * ``` - * @see https://hono.dev/api/context#render-setrenderer */ render: Renderer = (...args) => this.renderer(...args) @@ -378,6 +380,9 @@ export class Context< /** * `.setRenderer()` can set the layout in the custom middleware. + * + * @see {@link https://hono.dev/api/context#render-setrenderer} + * * @example * ```tsx * app.use('*', async (c, next) => { @@ -393,7 +398,6 @@ export class Context< * await next() * }) * ``` - * @see https://hono.dev/api/context#render-setrenderer */ setRenderer = (renderer: Renderer): void => { this.renderer = renderer @@ -401,6 +405,9 @@ export class Context< /** * `.header()` can set headers. + * + * @see {@link https://hono.dev/api/context#body} + * * @example * ```ts * app.get('/welcome', (c) => { @@ -411,7 +418,6 @@ export class Context< * return c.body('Thank you for coming') * }) * ``` - * @see https://hono.dev/api/context#body */ header = (name: string, value: string | undefined, options?: { append?: boolean }): void => { // Clear the header @@ -459,6 +465,9 @@ export class Context< /** * `.set()` can set the value specified by the key. + * + * @see {@link https://hono.dev/api/context#set-get} + * * @example * ```ts * app.use('*', async (c, next) => { @@ -466,7 +475,6 @@ export class Context< * await next() * }) * ``` - * @see https://hono.dev/api/context#set-get ``` */ set: Set = (key: string, value: unknown) => { @@ -476,6 +484,9 @@ export class Context< /** * `.get()` can use the value specified by the key. + * + * @see {@link https://hono.dev/api/context#set-get} + * * @example * ```ts * app.get('/', (c) => { @@ -483,7 +494,6 @@ export class Context< * return c.text(`The message is "${message}"`) * }) * ``` - * @see https://hono.dev/api/context#set-get */ get: Get = (key: string) => { return this._var ? this._var[key] : undefined @@ -491,11 +501,13 @@ export class Context< /** * `.var` can access the value of a variable. + * + * @see {@link https://hono.dev/api/context#var} + * * @example * ```ts * const result = c.var.client.oneMethod() * ``` - * @see https://hono.dev/api/context#var */ // c.var.propName is a read-only get var(): Readonly< @@ -575,6 +587,9 @@ export class Context< * `.body()` can return the HTTP response. * You can set headers with `.header()` and set HTTP status code with `.status`. * This can also be set in `.text()`, `.json()` and so on. + * + * @see {@link https://hono.dev/api/context#body} + * * @example * ```ts * app.get('/welcome', (c) => { @@ -588,7 +603,6 @@ export class Context< * return c.body('Thank you for coming') * }) * ``` - * @see https://hono.dev/api/context#body */ body: BodyRespond = ( data: Data | null, @@ -602,13 +616,15 @@ export class Context< /** * `.text()` can render text as `Content-Type:text/plain`. + * + * @see {@link https://hono.dev/api/context#text} + * * @example * ```ts * app.get('/say', (c) => { * return c.text('Hello!') * }) * ``` - * @see https://hono.dev/api/context#text */ text: TextRespond = ( text: string, @@ -633,13 +649,15 @@ export class Context< /** * `.json()` can render JSON as `Content-Type:application/json`. + * + * @see {@link https://hono.dev/api/context#json} + * * @example * ```ts * app.get('/api', (c) => { * return c.json({ message: 'Hello!' }) * }) * ``` - * @see https://hono.dev/api/context#json */ json: JSONRespond = , U extends StatusCode>( object: T, @@ -685,6 +703,9 @@ export class Context< /** * `.redirect()` can Redirect, default status code is 302. + * + * @see {@link https://hono.dev/api/context#redirect} + * * @example * ```ts * app.get('/redirect', (c) => { @@ -694,7 +715,6 @@ export class Context< * return c.redirect('/', 301) * }) * ``` - * @see https://hono.dev/api/context#redirect */ redirect = (location: string, status: RedirectStatusCode = 302): Response => { this.#headers ??= new Headers() @@ -704,13 +724,15 @@ export class Context< /** * `.notFound()` can return the Not Found Response. + * + * @see {@link https://hono.dev/api/context#notfound} + * * @example * ```ts * app.get('/notfound', (c) => { * return c.notFound() * }) * ``` - * @see https://hono.dev/api/context#notfound */ notFound = (): Response | Promise => { return this.notFoundHandler(this) diff --git a/src/hono-base.ts b/src/hono-base.ts index 8740dfa4..f3e9ad03 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -51,20 +51,28 @@ type GetPath = (request: Request, options?: { env?: E['Bindings'] export type HonoOptions = { /** * `strict` option specifies whether to distinguish whether the last path is a directory or not. + * + * @see {@link https://hono.dev/api/hono#strict-mode} + * * @default true - * @see https://hono.dev/api/hono#strict-mode */ strict?: boolean /** * `router` option specifices which router to use. + * + * @see {@link https://hono.dev/api/hono#router-option} + * + * @example * ```ts * const app = new Hono({ router: new RegExpRouter() }) * ``` - * @see https://hono.dev/api/hono#router-option */ router?: Router<[H, RouterRoute]> /** * `getPath` can handle the host header value. + * + * @see {@link https://hono.dev/api/routing#routing-with-host-header-value} + * * @example * ```ts * const app = new Hono({ @@ -79,7 +87,6 @@ export type HonoOptions = { * // headers: { host: 'www1.example.com' }, * // }) * ``` - * @see https://hono.dev/api/routing#routing-with-host-header-value */ getPath?: GetPath } @@ -174,6 +181,24 @@ class Hono c.text("user")) + * app.route("/api", app2) // GET /api/user + * ``` + */ route< SubPath extends string, SubEnv extends Env, @@ -206,11 +231,16 @@ class Hono(path: SubPath): Hono> { const subApp = this.clone() @@ -220,6 +250,13 @@ class Hono { * console.error(`${err}`) @@ -234,12 +271,18 @@ class Hono { * return c.text('Custom 404 Message', 404) * }) * ``` - * @see https://hono.dev/api/hono#not-found */ notFound = (handler: NotFoundHandler): Hono => { this.notFoundHandler = handler @@ -247,21 +290,27 @@ class Hono new Response('Hello from itty-router')) + * * const app = new Hono() - * const externalAppHandler = () => new Response('External App') - * app.mount('/external', externalAppHandler) + * app.mount('/itty-router', ittyRouter.handle) * ``` - * @see https://hono.dev/api/hono#mount */ mount( path: string, @@ -379,7 +428,14 @@ class Hono} response of request + * */ fetch: ( request: Request, diff --git a/src/http-exception.ts b/src/http-exception.ts index da56f55a..d858c0a1 100644 --- a/src/http-exception.ts +++ b/src/http-exception.ts @@ -19,6 +19,15 @@ type HTTPExceptionOptions = { /** * `HTTPException` must be used when a fatal error such as authentication failure occurs. + * + * @see {@link https://hono.dev/api/exception} + * + * @param {StatusCode} status - status code of HTTPException + * @param {HTTPExceptionOptions} options - options of HTTPException + * @param {HTTPExceptionOptions["res"]} options.res - response of options of HTTPException + * @param {HTTPExceptionOptions["message"]} options.message - message of options of HTTPException + * @param {HTTPExceptionOptions["cause"]} options.cause - cause of options of HTTPException + * * @example * ```ts * import { HTTPException } from 'hono/http-exception' @@ -33,7 +42,6 @@ type HTTPExceptionOptions = { * await next() * }) * ``` - * @see https://hono.dev/api/exception */ export class HTTPException extends Error { readonly res?: Response diff --git a/src/request.ts b/src/request.ts index 73caa3b1..bd131ad0 100644 --- a/src/request.ts +++ b/src/request.ts @@ -26,6 +26,9 @@ type BodyCache = Partial export class HonoRequest

{ /** * `.raw` can get the raw Request object. + * + * @see {@link https://hono.dev/api/request#raw} + * * @example * ```ts * // For Cloudflare Workers @@ -34,7 +37,6 @@ export class HonoRequest

{ * ... * }) * ``` - * @see https://hono.dev/api/request#raw */ raw: Request @@ -43,13 +45,15 @@ export class HonoRequest

{ routeIndex: number = 0 /** * `.path` can get the pathname of the request. + * + * @see {@link https://hono.dev/api/request#path} + * * @example * ```ts * app.get('/about/me', (c) => { * const pathname = c.req.path // `/about/me` * }) * ``` - * @see https://hono.dev/api/request#path */ path: string bodyCache: BodyCache = {} @@ -67,13 +71,15 @@ export class HonoRequest

{ /** * `.req.param()` gets the path parameters. + * + * @see {@link https://hono.dev/api/routing#path-parameter} + * * @example * ```ts * const name = c.req.param('name') * // or all parameters at once * const { id, comment_id } = c.req.param() * ``` - * @see https://hono.dev/api/routing#path-parameter */ param = ParamKeys

>(key: P2 extends `${infer _}?` ? never : P2): string param> = RemoveQuestion>>( @@ -112,6 +118,9 @@ export class HonoRequest

{ /** * `.query()` can get querystring parameters. + * + * @see {@link https://hono.dev/api/request#query} + * * @example * ```ts * // Query params @@ -124,7 +133,6 @@ export class HonoRequest

{ * const { q, limit, offset } = c.req.query() * }) * ``` - * @see https://hono.dev/api/request#query */ query(key: string): string | undefined query(): Record @@ -134,6 +142,9 @@ export class HonoRequest

{ /** * `.queries()` can get multiple querystring parameter values, e.g. /search?tags=A&tags=B + * + * @see {@link https://hono.dev/api/request#queries} + * * @example * ```ts * app.get('/search', (c) => { @@ -141,7 +152,6 @@ export class HonoRequest

{ * const tags = c.req.queries('tags') * }) * ``` - * @see https://hono.dev/api/request#queries */ queries(key: string): string[] | undefined queries(): Record @@ -151,13 +161,15 @@ export class HonoRequest

{ /** * `.header()` can get the request header value. + * + * @see {@link https://hono.dev/api/request#header} + * * @example * ```ts * app.get('/', (c) => { * const userAgent = c.req.header('User-Agent') * }) * ``` - * @see https://hono.dev/api/request#header */ header(name: string): string | undefined header(): Record @@ -175,13 +187,15 @@ export class HonoRequest

{ /** * `.parseBody()` can parse Request body of type `multipart/form-data` or `application/x-www-form-urlencoded` + * + * @see {@link https://hono.dev/api/request#parsebody} + * * @example * ```ts * app.post('/entry', async (c) => { * const body = await c.req.parseBody() * }) * ``` - * @see https://hono.dev/api/request#parsebody */ async parseBody, T extends BodyData>( options?: Options @@ -225,13 +239,15 @@ export class HonoRequest

{ /** * `.json()` can parse Request body of type `application/json` + * + * @see {@link https://hono.dev/api/request#json} + * * @example * ```ts * app.post('/entry', async (c) => { * const body = await c.req.json() * }) * ``` - * @see https://hono.dev/api/request#json */ json(): Promise { return this.cachedBody('json') @@ -239,13 +255,15 @@ export class HonoRequest

{ /** * `.text()` can parse Request body of type `text/plain` + * + * @see {@link https://hono.dev/api/request#text} + * * @example * ```ts * app.post('/entry', async (c) => { * const body = await c.req.text() * }) * ``` - * @see https://hono.dev/api/request#text */ text(): Promise { return this.cachedBody('text') @@ -253,13 +271,15 @@ export class HonoRequest

{ /** * `.arrayBuffer()` parse Request body as an `ArrayBuffer` + * + * @see {@link https://hono.dev/api/request#arraybuffer} + * * @example * ```ts * app.post('/entry', async (c) => { * const body = await c.req.arrayBuffer() * }) * ``` - * @see https://hono.dev/api/request#arraybuffer */ arrayBuffer(): Promise { return this.cachedBody('arrayBuffer') @@ -318,6 +338,9 @@ export class HonoRequest

{ /** * `.url()` can get the request url strings. + * + * @see {@link https://hono.dev/api/request#url} + * * @example * ```ts * app.get('/about/me', (c) => { @@ -325,7 +348,6 @@ export class HonoRequest

{ * ... * }) * ``` - * @see https://hono.dev/api/request#url */ get url(): string { return this.raw.url @@ -333,13 +355,15 @@ export class HonoRequest

{ /** * `.method()` can get the method name of the request. + * + * @see {@link https://hono.dev/api/request#method} + * * @example * ```ts * app.get('/about/me', (c) => { * const method = c.req.method // `GET` * }) * ``` - * @see https://hono.dev/api/request#method */ get method(): string { return this.raw.method @@ -347,6 +371,9 @@ export class HonoRequest

{ /** * `.matchedRoutes()` can return a matched route in the handler + * + * @see {@link https://hono.dev/api/request#matchedroutes} + * * @example * ```ts * app.use('*', async function logger(c, next) { @@ -364,7 +391,6 @@ export class HonoRequest

{ * }) * }) * ``` - * @see https://hono.dev/api/request#matchedroutes */ get matchedRoutes(): RouterRoute[] { return this.#matchResult[0].map(([[, route]]) => route) @@ -372,13 +398,15 @@ export class HonoRequest

{ /** * `routePath()` can retrieve the path registered within the handler + * + * @see {@link https://hono.dev/api/request#routepath} + * * @example * ```ts * app.get('/posts/:id', (c) => { * return c.json({ path: c.req.routePath }) * }) * ``` - * @see https://hono.dev/api/request#routepath */ get routePath(): string { return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path