Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: change jsdoc of context #8

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,25 @@ 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
* app.get('*', async c => {
* 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) => {
Expand All @@ -137,7 +142,6 @@ export class Context<
* }
* })
* ```
* @see https://hono.dev/api/context#error
*/
error: Error | undefined = undefined

Expand All @@ -163,7 +167,7 @@ export class Context<
}

/**
* @see https://hono.dev/api/context#event
* @see {@link https://hono.dev/api/context#event}
*/
get event(): FetchEventLike {
if (this.#executionCtx && 'respondWith' in this.#executionCtx) {
Expand All @@ -174,7 +178,7 @@ export class Context<
}

/**
* @see https://hono.dev/api/context#executionctx
* @see {@link https://hono.dev/api/context#executionctx}
*/
get executionCtx(): ExecutionContext {
if (this.#executionCtx) {
Expand All @@ -185,7 +189,7 @@ export class Context<
}

/**
* @see https://hono.dev/api/context#res
* @see {@link https://hono.dev/api/context#res}
*/
get res(): Response {
this.#isFresh = false
Expand Down Expand Up @@ -214,13 +218,15 @@ export class Context<

/**
* `.render()` can create a response within a layout.
*
* @see {@link https://hono.dev/api/context#render-setrenderer}
*
* @example
* ```ts
* app.get('/', (c) => {
* return c.render('Hello!')
* })
* ```
* @see https://hono.dev/api/context#render-setrenderer
*/
render: Renderer = (...args) => this.renderer(...args)

Expand All @@ -236,6 +242,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) => {
Expand All @@ -251,14 +260,16 @@ export class Context<
* await next()
* })
* ```
* @see https://hono.dev/api/context#render-setrenderer
*/
setRenderer = (renderer: Renderer) => {
this.renderer = renderer
}

/**
* `.header()` can set headers.
*
* @see {@link https://hono.dev/api/context#body}
*
* @example
* ```ts
* app.get('/welcome', (c) => {
Expand All @@ -269,7 +280,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
Expand Down Expand Up @@ -317,14 +327,16 @@ 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) => {
* c.set('message', 'Hono is cool!!')
* await next()
* })
* ```
* @see https://hono.dev/api/context#set-get
```
*/
set: Set<E> = (key: string, value: unknown) => {
Expand All @@ -334,26 +346,30 @@ 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) => {
* const message = c.get('message')
* 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
}

/**
* `.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<
Expand Down Expand Up @@ -433,6 +449,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) => {
Expand All @@ -446,7 +465,6 @@ export class Context<
* return c.body('Thank you for coming')
* })
* ```
* @see https://hono.dev/api/context#body
*/
body: BodyRespond = (
data: Data | null,
Expand All @@ -460,13 +478,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,
Expand All @@ -491,13 +511,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,
Expand Down Expand Up @@ -543,6 +565,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) => {
Expand All @@ -552,7 +577,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()
Expand All @@ -562,13 +586,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)
Expand Down