docs(jsdoc): add jsdoc of some modules (#2836)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 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
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+163 added−49 removed
- other:+163 added−49 removed
Provenance
- Commit
0dfb794- Tree
a33fbad- Author
Ame_x <121654029+EdamAme-x@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
19f859d
4 files changed+163 added−49 removed
modified:src/context.ts+42 added-20 removed
221 unmodified lines222 */223 req: HonoRequest<P, I['out']>224 /**225 * `.env` can get bindings (environment variables, secrets, KV namespaces, D1 database, R2 bucket etc.) in Cloudflare Workers.226+ *227+ * @see {@link https://hono.dev/api/context#env}228+ *229 * @example230 * ```ts231 * // Environment object for Cloudflare Workers232 * app.get('*', async c => {233 * const counter = c.env.COUNTER234 * })235 * ```233- * @see https://hono.dev/api/context#env236 */237 env: E['Bindings'] = {}238 private _var: E['Variables'] = {}239 finalized: boolean = false240 /**241 * `.error` can get the error object from the middleware if the Handler throws an error.242+ *243+ * @see {@link https://hono.dev/api/context#error}244+ *245 * @example246 * ```ts247 * app.use('*', async (c, next) => {248 * await next()1 unmodified line250 * // do something...251 * }252 * })253 * ```249- * @see https://hono.dev/api/context#error254 */255 error: Error | undefined = undefined256 257 #status: StatusCode = 20023 unmodified lines281 }282 }283 284 /**285+ * @see {@link https://hono.dev/api/context#event}286 * The FetchEvent associated with the current request.287 *288 * @throws Will throw an error if the context does not have a FetchEvent.284- *285- * @see https://hono.dev/api/context#event289 */290 get event(): FetchEventLike {291 if (this.#executionCtx && 'respondWith' in this.#executionCtx) {292 return this.#executionCtx2 unmodified lines295 }296 }297 298 /**299+ * @see {@link https://hono.dev/api/context#executionctx}300 * The ExecutionContext associated with the current request.301 *302 * @throws Will throw an error if the context does not have an ExecutionContext.299- *300- * @see https://hono.dev/api/context#executionctx303 */304 get executionCtx(): ExecutionContext {305 if (this.#executionCtx) {306 return this.#executionCtx as ExecutionContext2 unmodified lines309 }310 }311 312 /**313+ * @see {@link https://hono.dev/api/context#res}314 * The Response object for the current request.312- *313- * @see https://hono.dev/api/context#res315 */316 get res(): Response {317 this.#isFresh = false318 return (this.#res ||= new Response('404 Not Found', { status: 404 }))24 unmodified lines343 this.finalized = true344 }345 346 /**346- * Renders a response within a layout.347+ * `.render()` can create a response within a layout.348 *349+ * @see {@link https://hono.dev/api/context#render-setrenderer}350+ *351 * @example352 * ```ts353 * app.get('/', (c) => {354 * return c.render('Hello!')355 * })356 * ```354- * @see https://hono.dev/api/context#render-setrenderer357 */358 render: Renderer = (...args) => this.renderer(...args)359 360 /**18 unmodified lines379 getLayout = () => this.layout380 381 /**382 * `.setRenderer()` can set the layout in the custom middleware.383+ *384+ * @see {@link https://hono.dev/api/context#render-setrenderer}385+ *386 * @example387 * ```tsx388 * app.use('*', async (c, next) => {389 * c.setRenderer((content) => {7 unmodified lines397 * })398 * await next()399 * })400 * ```396- * @see https://hono.dev/api/context#render-setrenderer401 */402 setRenderer = (renderer: Renderer): void => {403 this.renderer = renderer404 }405 406 /**407 * `.header()` can set headers.408+ *409+ * @see {@link https://hono.dev/api/context#body}410+ *411 * @example412 * ```ts413 * app.get('/welcome', (c) => {414 * // Set headers2 unmodified lines417 *418 * return c.body('Thank you for coming')419 * })420 * ```414- * @see https://hono.dev/api/context#body421 */422 header = (name: string, value: string | undefined, options?: { append?: boolean }): void => {423 // Clear the header424 if (value === undefined) {39 unmodified lines464 }465 466 /**467 * `.set()` can set the value specified by the key.468+ * 469+ * @see {@link https://hono.dev/api/context#set-get}470+ * 471 * @example472 * ```ts473 * app.use('*', async (c, next) => {474 * c.set('message', 'Hono is cool!!')475 * await next()476 * })477 * ```469- * @see https://hono.dev/api/context#set-get478 ```479 */480 set: Set<E> = (key: string, value: unknown) => {481 this._var ??= {}1 unmodified line483 }484 485 /**486 * `.get()` can use the value specified by the key.487+ *488+ * @see {@link https://hono.dev/api/context#set-get}489+ *490 * @example491 * ```ts492 * app.get('/', (c) => {493 * const message = c.get('message')494 * return c.text(`The message is "${message}"`)495 * })496 * ```486- * @see https://hono.dev/api/context#set-get497 */498 get: Get<E> = (key: string) => {499 return this._var ? this._var[key] : undefined500 }501 502 /**503 * `.var` can access the value of a variable.504+ *505+ * @see {@link https://hono.dev/api/context#var}506+ *507 * @example508 * ```ts509 * const result = c.var.client.oneMethod()510 * ```498- * @see https://hono.dev/api/context#var511 */512 // c.var.propName is a read-only513 get var(): Readonly<514 // eslint-disable-next-line @typescript-eslint/no-explicit-any71 unmodified lines586 /**587 * `.body()` can return the HTTP response.588 * You can set headers with `.header()` and set HTTP status code with `.status`.589 * This can also be set in `.text()`, `.json()` and so on.590+ *591+ * @see {@link https://hono.dev/api/context#body}592+ *593 * @example594 * ```ts595 * app.get('/welcome', (c) => {596 * // Set headers5 unmodified lines602 * // Return the response body603 * return c.body('Thank you for coming')604 * })605 * ```591- * @see https://hono.dev/api/context#body606 */607 body: BodyRespond = (608 data: Data | null,609 arg?: StatusCode | ResponseInit,5 unmodified lines615 }616 617 /**618 * `.text()` can render text as `Content-Type:text/plain`.619+ *620+ * @see {@link https://hono.dev/api/context#text}621+ *622 * @example623 * ```ts624 * app.get('/say', (c) => {625 * return c.text('Hello!')626 * })627 * ```611- * @see https://hono.dev/api/context#text628 */629 text: TextRespond = (630 text: string,631 arg?: StatusCode | ResponseInit,16 unmodified lines648 }649 650 /**651 * `.json()` can render JSON as `Content-Type:application/json`.652+ *653+ * @see {@link https://hono.dev/api/context#json}654+ *655 * @example656 * ```ts657 * app.get('/api', (c) => {658 * return c.json({ message: 'Hello!' })659 * })660 * ```642- * @see https://hono.dev/api/context#json661 */662 json: JSONRespond = <T extends JSONValue | Simplify<unknown>, U extends StatusCode>(663 object: T,664 arg?: U | ResponseInit,37 unmodified lines702 }703 704 /**705 * `.redirect()` can Redirect, default status code is 302.706+ *707+ * @see {@link https://hono.dev/api/context#redirect}708+ *709 * @example710 * ```ts711 * app.get('/redirect', (c) => {712 * return c.redirect('/')1 unmodified line714 * app.get('/redirect-permanently', (c) => {715 * return c.redirect('/', 301)716 * })717 * ```697- * @see https://hono.dev/api/context#redirect718 */719 redirect = (location: string, status: RedirectStatusCode = 302): Response => {720 this.#headers ??= new Headers()721 this.#headers.set('Location', location)1 unmodified line723 }724 725 /**726 * `.notFound()` can return the Not Found Response.727+ *728+ * @see {@link https://hono.dev/api/context#notfound}729+ *730 * @example731 * ```ts732 * app.get('/notfound', (c) => {733 * return c.notFound()734 * })735 * ```713- * @see https://hono.dev/api/context#notfound736 */737 notFound = (): Response | Promise<Response> => {738 return this.notFoundHandler(this)739 }1 unmodified linemodified:src/hono-base.ts+70 added-14 removed
49 unmodified lines50 51 export type HonoOptions<E extends Env> = {52 /**53 * `strict` option specifies whether to distinguish whether the last path is a directory or not.54+ *55+ * @see {@link https://hono.dev/api/hono#strict-mode}56+ *57 * @default true55- * @see https://hono.dev/api/hono#strict-mode58 */59 strict?: boolean60 /**61 * `router` option specifices which router to use.62+ *63+ * @see {@link https://hono.dev/api/hono#router-option}64+ *65+ * @example66 * ```ts67 * const app = new Hono({ router: new RegExpRouter() })68 * ```63- * @see https://hono.dev/api/hono#router-option69 */70 router?: Router<[H, RouterRoute]>71 /**72 * `getPath` can handle the host header value.73+ *74+ * @see {@link https://hono.dev/api/routing#routing-with-host-header-value}75+ *76 * @example77 * ```ts78 * const app = new Hono({79 * getPath: (req) =>6 unmodified lines86 * // new Request('http://www1.example.com/hello', {87 * // headers: { host: 'www1.example.com' },88 * // })89 * ```82- * @see https://hono.dev/api/routing#routing-with-host-header-value90 */91 getPath?: GetPath<E>92 }93 86 unmodified lines180 181 private notFoundHandler: NotFoundHandler = notFoundHandler182 private errorHandler: ErrorHandler = errorHandler183 184+ /**185+ * `.route()` allows grouping other Hono instance in routes.186+ *187+ * @see {@link https://hono.dev/api/routing#grouping}188+ *189+ * @param {string} path - base Path190+ * @param {Hono} app - other Hono instance191+ * @returns {Hono} routed Hono instnace192+ *193+ * @example194+ * ```ts195+ * const app = new Hono()196+ * const app2 = new Hono()197+ *198+ * app2.get("/user", (c) => c.text("user"))199+ * app.route("/api", app2) // GET /api/user200+ * ```201+ */202 route<203 SubPath extends string,204 SubEnv extends Env,205 SubSchema extends Schema,24 unmodified lines230 }231 232 /**233 * `.basePath()` allows base paths to be specified.234+ *235+ * @see {@link https://hono.dev/api/routing#base-path}236+ *237+ * @param {string} path - base Path238+ * @returns {Hono} changed Hono instance239+ *240 * @example241 * ```ts242 * const api = new Hono().basePath('/api')243 * ```213- * @see https://hono.dev/api/routing#base-path244 */245 basePath<SubPath extends string>(path: SubPath): Hono<E, S, MergePath<BasePath, SubPath>> {246 const subApp = this.clone()247 subApp._basePath = mergePath(this._basePath, path)1 unmodified line249 }250 251 /**252 * `.onError()` handles an error and returns a customized Response.253+ *254+ * @see {@link https://hono.dev/api/hono#error-handling}255+ *256+ * @param {ErrorHandler} handler - request Handler for error257+ * @returns {Hono} changed Hono instance258+ *259+ * @example260 * ```ts261 * app.onError((err, c) => {262 * console.error(`${err}`)263 * return c.text('Custom Error Message', 500)6 unmodified lines270 }271 272 /**273 * `.notFound()` allows you to customize a Not Found Response.274+ *275+ * @see {@link https://hono.dev/api/hono#not-found}276+ *277+ * @param {NotFoundHandler} handler - request handler for not-found278+ * @returns {Hono} changed Hono instance279+ *280+ * @example281 * ```ts282 * app.notFound((c) => {283 * return c.text('Custom 404 Message', 404)284 * })285 * ```242- * @see https://hono.dev/api/hono#not-found286 */287 notFound = (handler: NotFoundHandler<E>): Hono<E, S, BasePath> => {288 this.notFoundHandler = handler289 return this290 }291 292 /**250- * Mounts an external application handler to the specified path.293+ * `.mount()` allows you to mount applications built with other frameworks into your Hono application.294 *252- * @param path - The path where the external application handler should be mounted.253- * @param applicationHandler - The external application handler function that processes requests.254- * @param optionHandler - Optional function to handle additional options, such as environment variables and execution context.295+ * @see {@link https://hono.dev/api/hono#mount}296 *256- * @returns The current instance of Hono for chaining.297+ * @param {string} path - base Path298+ * @param {Function} applicationHandler - other Request Handler299+ * @param {Function | undefined} optionHandler - other Request Handler with Hono Context300+ * @returns {Hono} mounted Hono instance301 *302 * @example303 * ```ts304+ * import { Router as IttyRouter } from 'itty-router'305+ * import { Hono } from 'hono'306+ * // Create itty-router application307+ * const ittyRouter = IttyRouter()308+ * // GET /itty-router/hello309+ * ittyRouter.get('/hello', () => new Response('Hello from itty-router'))310+ *311 * const app = new Hono()261- * const externalAppHandler = () => new Response('External App')262- * app.mount('/external', externalAppHandler)312+ * app.mount('/itty-router', ittyRouter.handle)313 * ```264- * @see https://hono.dev/api/hono#mount314 */315 mount(316 path: string,317 applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,109 unmodified lines427 }428 429 /**430 * `.fetch()` will be entry point of your app.382- * @see https://hono.dev/api/hono#fetch431+ *432+ * @see {@link https://hono.dev/api/hono#fetch}433+ *434+ * @param {Request} request - reuqest Object of request435+ * @param {Env} Env - env Object436+ * @param {ExecutionContext} - context of execution437+ * @returns {Response | Promise<Response>} response of request438+ *439 */440 fetch: (441 request: Request,442 Env?: E['Bindings'] | {},52 unmodified linesmodified:src/http-exception.ts+9 added-1 removed
17 unmodified lines18192021222324252627282930313233346 unmodified lines41424344364546474830 unmodified lines17 unmodified lines}
/** * `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' *6 unmodified lines * } * await next() * }) * ``` * @see https://hono.dev/api/exception */export class HTTPException extends Error { readonly res?: Response readonly status: StatusCode30 unmodified linesmodified:src/request.ts+42 added-14 removed
24 unmodified lines25 26 export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {27 /**28 * `.raw` can get the raw Request object.29+ *30+ * @see {@link https://hono.dev/api/request#raw}31+ *32 * @example33 * ```ts34 * // For Cloudflare Workers35 * app.post('/', async (c) => {36 * const metadata = c.req.raw.cf?.hostMetadata?37 * ...38 * })39 * ```37- * @see https://hono.dev/api/request#raw40 */41 raw: Request42 43 #validatedData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData44 #matchResult: Result<[unknown, RouterRoute]>45 routeIndex: number = 046 /**47 * `.path` can get the pathname of the request.48+ *49+ * @see {@link https://hono.dev/api/request#path}50+ *51 * @example52 * ```ts53 * app.get('/about/me', (c) => {54 * const pathname = c.req.path // `/about/me`55 * })56 * ```52- * @see https://hono.dev/api/request#path57 */58 path: string59 bodyCache: BodyCache = {}60 9 unmodified lines70 }71 72 /**73 * `.req.param()` gets the path parameters.74+ *75+ * @see {@link https://hono.dev/api/routing#path-parameter}76+ *77 * @example78 * ```ts79 * const name = c.req.param('name')80 * // or all parameters at once81 * const { id, comment_id } = c.req.param()82 * ```76- * @see https://hono.dev/api/routing#path-parameter83 */84 param<P2 extends ParamKeys<P> = ParamKeys<P>>(key: P2 extends `${infer _}?` ? never : P2): string85 param<P2 extends RemoveQuestion<ParamKeys<P>> = RemoveQuestion<ParamKeys<P>>>(86 key: P230 unmodified lines117 }118 119 /**120 * `.query()` can get querystring parameters.121+ *122+ * @see {@link https://hono.dev/api/request#query}123+ *124 * @example125 * ```ts126 * // Query params127 * app.get('/search', (c) => {4 unmodified lines132 * app.get('/search', (c) => {133 * const { q, limit, offset } = c.req.query()134 * })135 * ```127- * @see https://hono.dev/api/request#query136 */137 query(key: string): string | undefined138 query(): Record<string, string>139 query(key?: string) {1 unmodified line141 }142 143 /**144 * `.queries()` can get multiple querystring parameter values, e.g. /search?tags=A&tags=B145+ *146+ * @see {@link https://hono.dev/api/request#queries}147+ *148 * @example149 * ```ts150 * app.get('/search', (c) => {151 * // tags will be string[]152 * const tags = c.req.queries('tags')153 * })154 * ```144- * @see https://hono.dev/api/request#queries155 */156 queries(key: string): string[] | undefined157 queries(): Record<string, string[]>158 queries(key?: string) {1 unmodified line160 }161 162 /**163 * `.header()` can get the request header value.164+ *165+ * @see {@link https://hono.dev/api/request#header}166+ *167 * @example168 * ```ts169 * app.get('/', (c) => {170 * const userAgent = c.req.header('User-Agent')171 * })172 * ```160- * @see https://hono.dev/api/request#header173 */174 header(name: string): string | undefined175 header(): Record<string, string>176 header(name?: string) {9 unmodified lines186 }187 188 /**189 * `.parseBody()` can parse Request body of type `multipart/form-data` or `application/x-www-form-urlencoded`190+ *191+ * @see {@link https://hono.dev/api/request#parsebody}192+ *193 * @example194 * ```ts195 * app.post('/entry', async (c) => {196 * const body = await c.req.parseBody()197 * })198 * ```184- * @see https://hono.dev/api/request#parsebody199 */200 async parseBody<Options extends Partial<ParseBodyOptions>, T extends BodyData<Options>>(201 options?: Options202 ): Promise<T>35 unmodified lines238 }239 240 /**241 * `.json()` can parse Request body of type `application/json`242+ *243+ * @see {@link https://hono.dev/api/request#json}244+ *245 * @example246 * ```ts247 * app.post('/entry', async (c) => {248 * const body = await c.req.json()249 * })250 * ```234- * @see https://hono.dev/api/request#json251 */252 json<T = any>(): Promise<T> {253 return this.cachedBody('json')254 }255 256 /**257 * `.text()` can parse Request body of type `text/plain`258+ *259+ * @see {@link https://hono.dev/api/request#text}260+ *261 * @example262 * ```ts263 * app.post('/entry', async (c) => {264 * const body = await c.req.text()265 * })266 * ```248- * @see https://hono.dev/api/request#text267 */268 text(): Promise<string> {269 return this.cachedBody('text')270 }271 272 /**273 * `.arrayBuffer()` parse Request body as an `ArrayBuffer`274+ *275+ * @see {@link https://hono.dev/api/request#arraybuffer}276+ *277 * @example278 * ```ts279 * app.post('/entry', async (c) => {280 * const body = await c.req.arrayBuffer()281 * })282 * ```262- * @see https://hono.dev/api/request#arraybuffer283 */284 arrayBuffer(): Promise<ArrayBuffer> {285 return this.cachedBody('arrayBuffer')286 }50 unmodified lines337 }338 339 /**340 * `.url()` can get the request url strings.341+ *342+ * @see {@link https://hono.dev/api/request#url}343+ *344 * @example345 * ```ts346 * app.get('/about/me', (c) => {347 * const url = c.req.url // `http://localhost:8787/about/me`348 * ...349 * })350 * ```328- * @see https://hono.dev/api/request#url351 */352 get url(): string {353 return this.raw.url354 }355 356 /**357 * `.method()` can get the method name of the request.358+ *359+ * @see {@link https://hono.dev/api/request#method}360+ *361 * @example362 * ```ts363 * app.get('/about/me', (c) => {364 * const method = c.req.method // `GET`365 * })366 * ```342- * @see https://hono.dev/api/request#method367 */368 get method(): string {369 return this.raw.method370 }371 372 /**373 * `.matchedRoutes()` can return a matched route in the handler374+ *375+ * @see {@link https://hono.dev/api/request#matchedroutes}376+ *377 * @example378 * ```ts379 * app.use('*', async function logger(c, next) {380 * await next()9 unmodified lines390 * )391 * })392 * })393 * ```367- * @see https://hono.dev/api/request#matchedroutes394 */395 get matchedRoutes(): RouterRoute[] {396 return this.#matchResult[0].map(([[, route]]) => route)397 }398 399 /**400 * `routePath()` can retrieve the path registered within the handler401+ *402+ * @see {@link https://hono.dev/api/request#routepath}403+ *404 * @example405 * ```ts406 * app.get('/posts/:id', (c) => {407 * return c.json({ path: c.req.routePath })408 * })409 * ```381- * @see https://hono.dev/api/request#routepath410 */411 get routePath(): string {412 return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path413 }1 unmodified line