0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 02:07:30 +01:00

docs(jsdoc): add jsdoc of some modules (#2836)

* add jsdoc of .route

* chore: format

* add jsdoc of .mount

* add

* chore: format

* chore fix

* change to format same as ".mount"

* chore fix

* chore fix

* docs: change jsdoc of .basePath

* docs: change jsdoc of onError

* same as doc

* delete

* docs: change jsdoc of .notFound

* docs: change jsdoc of .notFound

* docs: change jsdoc of .fetch

* chore: format

* docs: change jsdoc of .fire

* docs: change jsdoc of .fire

* docs: change jsdoc of hono-options

* docs: change jsdoc of http-exception

* docs: change jsdoc of hono-request

* docs: change jsdoc of context

* delete missing jsdoc
This commit is contained in:
Ame_x 2024-06-01 04:00:13 +09:00 committed by GitHub
parent 19f859d398
commit 0dfb794df7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 163 additions and 49 deletions

View File

@ -223,6 +223,9 @@ export class Context<
req: HonoRequest<P, I['out']>
/**
* `.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<E> = (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<E> = (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 = <T extends JSONValue | Simplify<unknown>, 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<Response> => {
return this.notFoundHandler(this)

View File

@ -51,20 +51,28 @@ type GetPath<E extends Env> = (request: Request, options?: { env?: E['Bindings']
export type HonoOptions<E extends Env> = {
/**
* `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<E extends Env> = {
* // headers: { host: 'www1.example.com' },
* // })
* ```
* @see https://hono.dev/api/routing#routing-with-host-header-value
*/
getPath?: GetPath<E>
}
@ -174,6 +181,24 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
private notFoundHandler: NotFoundHandler = notFoundHandler
private errorHandler: ErrorHandler = errorHandler
/**
* `.route()` allows grouping other Hono instance in routes.
*
* @see {@link https://hono.dev/api/routing#grouping}
*
* @param {string} path - base Path
* @param {Hono} app - other Hono instance
* @returns {Hono} routed Hono instnace
*
* @example
* ```ts
* const app = new Hono()
* const app2 = new Hono()
*
* app2.get("/user", (c) => c.text("user"))
* app.route("/api", app2) // GET /api/user
* ```
*/
route<
SubPath extends string,
SubEnv extends Env,
@ -206,11 +231,16 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
/**
* `.basePath()` allows base paths to be specified.
*
* @see {@link https://hono.dev/api/routing#base-path}
*
* @param {string} path - base Path
* @returns {Hono} changed Hono instance
*
* @example
* ```ts
* const api = new Hono().basePath('/api')
* ```
* @see https://hono.dev/api/routing#base-path
*/
basePath<SubPath extends string>(path: SubPath): Hono<E, S, MergePath<BasePath, SubPath>> {
const subApp = this.clone()
@ -220,6 +250,13 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
/**
* `.onError()` handles an error and returns a customized Response.
*
* @see {@link https://hono.dev/api/hono#error-handling}
*
* @param {ErrorHandler} handler - request Handler for error
* @returns {Hono} changed Hono instance
*
* @example
* ```ts
* app.onError((err, c) => {
* console.error(`${err}`)
@ -234,12 +271,18 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
/**
* `.notFound()` allows you to customize a Not Found Response.
*
* @see {@link https://hono.dev/api/hono#not-found}
*
* @param {NotFoundHandler} handler - request handler for not-found
* @returns {Hono} changed Hono instance
*
* @example
* ```ts
* app.notFound((c) => {
* return c.text('Custom 404 Message', 404)
* })
* ```
* @see https://hono.dev/api/hono#not-found
*/
notFound = (handler: NotFoundHandler<E>): Hono<E, S, BasePath> => {
this.notFoundHandler = handler
@ -247,21 +290,27 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
}
/**
* Mounts an external application handler to the specified path.
* `.mount()` allows you to mount applications built with other frameworks into your Hono application.
*
* @param path - The path where the external application handler should be mounted.
* @param applicationHandler - The external application handler function that processes requests.
* @param optionHandler - Optional function to handle additional options, such as environment variables and execution context.
* @see {@link https://hono.dev/api/hono#mount}
*
* @returns The current instance of Hono for chaining.
* @param {string} path - base Path
* @param {Function} applicationHandler - other Request Handler
* @param {Function | undefined} optionHandler - other Request Handler with Hono Context
* @returns {Hono} mounted Hono instance
*
* @example
* ```ts
* import { Router as IttyRouter } from 'itty-router'
* import { Hono } from 'hono'
* // Create itty-router application
* const ittyRouter = IttyRouter()
* // GET /itty-router/hello
* ittyRouter.get('/hello', () => 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<E extends Env = Env, S extends Schema = {}, BasePath extends string =
/**
* `.fetch()` will be entry point of your app.
* @see https://hono.dev/api/hono#fetch
*
* @see {@link https://hono.dev/api/hono#fetch}
*
* @param {Request} request - reuqest Object of request
* @param {Env} Env - env Object
* @param {ExecutionContext} - context of execution
* @returns {Response | Promise<Response>} response of request
*
*/
fetch: (
request: Request,

View File

@ -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

View File

@ -26,6 +26,9 @@ type BodyCache = Partial<Body & { parsedBody: BodyData }>
export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<P extends string = '/', I extends Input['out'] = {}> {
* ...
* })
* ```
* @see https://hono.dev/api/request#raw
*/
raw: Request
@ -43,13 +45,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
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<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<P2 extends ParamKeys<P> = ParamKeys<P>>(key: P2 extends `${infer _}?` ? never : P2): string
param<P2 extends RemoveQuestion<ParamKeys<P>> = RemoveQuestion<ParamKeys<P>>>(
@ -112,6 +118,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.query()` can get querystring parameters.
*
* @see {@link https://hono.dev/api/request#query}
*
* @example
* ```ts
* // Query params
@ -124,7 +133,6 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* const { q, limit, offset } = c.req.query()
* })
* ```
* @see https://hono.dev/api/request#query
*/
query(key: string): string | undefined
query(): Record<string, string>
@ -134,6 +142,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<P extends string = '/', I extends Input['out'] = {}> {
* const tags = c.req.queries('tags')
* })
* ```
* @see https://hono.dev/api/request#queries
*/
queries(key: string): string[] | undefined
queries(): Record<string, string[]>
@ -151,13 +161,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<string, string>
@ -175,13 +187,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<Options extends Partial<ParseBodyOptions>, T extends BodyData<Options>>(
options?: Options
@ -225,13 +239,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<T = any>(): Promise<T> {
return this.cachedBody('json')
@ -239,13 +255,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<string> {
return this.cachedBody('text')
@ -253,13 +271,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<ArrayBuffer> {
return this.cachedBody('arrayBuffer')
@ -318,6 +338,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<P extends string = '/', I extends Input['out'] = {}> {
* ...
* })
* ```
* @see https://hono.dev/api/request#url
*/
get url(): string {
return this.raw.url
@ -333,13 +355,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.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<P extends string = '/', I extends Input['out'] = {}> {
* })
* })
* ```
* @see https://hono.dev/api/request#matchedroutes
*/
get matchedRoutes(): RouterRoute[] {
return this.#matchResult[0].map(([[, route]]) => route)
@ -372,13 +398,15 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `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