Skip to content

Commit

Permalink
Update docs to include Hono type hints across all pages
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Nov 13, 2024
1 parent 804c05d commit d6bead4
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 65 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class GetPageNumber extends OpenAPIRoute {
}
}

// Star a Hono app
// Start a Hono app
const app = new Hono()

// Setup OpenAPI registry
Expand All @@ -95,12 +95,7 @@ Workers and define a pattern to allow everyone to
have an OpenAPI-compliant schema without worrying about implementation details or reinventing the wheel.

chanfana is considered stable and production ready and is being used with
the [Radar 2.0 public API](https://developers.cloudflare.com/radar/).

Currently this package is maintained by the [Cloudflare Radar Team](https://radar.cloudflare.com/) and features are
prioritized based on the Radar roadmap.

Nonetheless you can still open pull requests or issues in this repository and they will get reviewed.
the [Radar 2.0 public API](https://developers.cloudflare.com/radar/) and many other Cloudflare products.

You can also talk to us in the [Cloudflare Community](https://community.cloudflare.com/) or
the [Radar Discord Channel](https://discord.com/channels/595317990191398933/1035553707116478495)
6 changes: 4 additions & 2 deletions docs/pages/advanced-user-guide/custom-error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ In order to customize the zod error formats, just overwrite the `handleValidatio

```ts
import { OpenAPIRoute } from 'chanfana'
import { Context } from 'hono'

export class ToDoList extends OpenAPIRoute {
schema = {
Expand All @@ -18,7 +19,7 @@ export class ToDoList extends OpenAPIRoute {
})
}

async handle(request: Request, env: any, context: any) {
async handle(c: Context) {
// ...
}
}
Expand Down Expand Up @@ -48,13 +49,14 @@ Then, in your endpoint extend from the new class

```ts
import { MyProjectRoute } from './route'
import { Context } from 'hono'

export class ToDoList extends MyProjectRoute {
schema = {
// ...
}

async handle(request: Request, env: any, context: any, data: any) {
async handle(c: Context) {
// ...
}
}
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/advanced-user-guide/custom-response-formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

```ts
import { OpenAPIRoute, Str } from 'chanfana'
import { Context } from 'hono'

export class ToDoList extends OpenAPIRoute {
schema = {
Expand All @@ -18,7 +19,7 @@ export class ToDoList extends OpenAPIRoute {
},
}

async handle(request: Request, env: any, context: any) {
async handle(c: Context) {
// ...
}
}
Expand All @@ -28,6 +29,7 @@ export class ToDoList extends OpenAPIRoute {

```ts
import { OpenAPIRoute, Str } from 'chanfana'
import { Context } from 'hono'

export class ToDoList extends OpenAPIRoute {
schema = {
Expand All @@ -49,7 +51,7 @@ export class ToDoList extends OpenAPIRoute {
},
}

async handle(request: Request, env: any, context: any, data: any) {
async handle(c: Context) {
// ...
}
}
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/advanced-user-guide/custom-response-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

```ts
import { OpenAPIRoute, Str } from 'chanfana'
import { Context } from 'hono'

export class ToDoList extends OpenAPIRoute {
schema = {
Expand All @@ -27,7 +28,7 @@ export class ToDoList extends OpenAPIRoute {
}


async handle(request: Request, env: any, context: any, data: any) {
async handle(c: Context) {
// ...
}
}
Expand All @@ -37,6 +38,7 @@ export class ToDoList extends OpenAPIRoute {

```ts
import { OpenAPIRoute, Str } from 'chanfana'
import { Context } from 'hono'

export class ToDoList extends OpenAPIRoute {
schema = {
Expand All @@ -61,7 +63,7 @@ export class ToDoList extends OpenAPIRoute {
}


async handle(request: Request, env: any, context: any, data: any) {
async handle(c: Context) {
// ...
}
}
Expand Down
10 changes: 3 additions & 7 deletions docs/pages/advanced-user-guide/streaming-responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ as soon as the they get generated by the model, instead of waiting for the compl

```ts
import { OpenAPIRoute, OpenAPIRouter } from 'chanfana'
import {z} from 'zod'
import { z } from 'zod'
import OpenAI from 'openai'
import { Context } from 'hono'

export class ChatGPT extends OpenAPIRoute {
schema = {
Expand All @@ -23,12 +24,7 @@ export class ChatGPT extends OpenAPIRoute {
},
}

async handle(
request: Request,
env: any,
context: any,
data: any
) {
async handle(c: Context) {
const { question } = data.query

const { readable, writable } = new TransformStream()
Expand Down
7 changes: 1 addition & 6 deletions docs/pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ Workers and define a pattern to allow everyone to
have an OpenAPI-compliant schema without worrying about implementation details or reinventing the wheel.

chanfana is considered stable and production ready and is being used with
the [Radar 2.0 public API](https://developers.cloudflare.com/radar/).

Currently this package is maintained by the [Cloudflare Radar Team](https://radar.cloudflare.com/) and features are
prioritized based on the Radar roadmap.

Nonetheless you can still open pull requests or issues in this repository and they will get reviewed.
the [Radar 2.0 public API](https://developers.cloudflare.com/radar/) and many other Cloudflare products.

You can also talk to us in the [Cloudflare Community](https://community.cloudflare.com/) or
the [Radar Discord Channel](https://discord.com/channels/595317990191398933/1035553707116478495)
3 changes: 2 additions & 1 deletion docs/pages/type-hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ To enable this, simply pass your endpoint schema to the generic type when retrie

```ts hl_lines="17"
import { OpenAPIRoute, Str } from 'chanfana'
import { Context } from 'hono'

export class TaskFetch extends OpenAPIRoute {
schema = {
Expand All @@ -68,7 +69,7 @@ export class TaskFetch extends OpenAPIRoute {
},
}

async handle(request: Request, env: any, context: any) {
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

// you now get type hints, when accessing the data argument
Expand Down
12 changes: 8 additions & 4 deletions docs/pages/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Then use it in a endpoint like this:

```ts
import { OpenAPIRoute } from 'chanfana'
import { Context } from 'hono'

export class Example extends OpenAPIRoute {
schema = {
Expand All @@ -46,8 +47,9 @@ export class Example extends OpenAPIRoute {
}
}

async handle(request: Request, env: any, context: any) {
// TODO data
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

return {
validatedLimit: data.query.limit
}
Expand Down Expand Up @@ -95,6 +97,7 @@ in every place you could use a Zod types, like this:

```ts
import { Str, Int, OpenAPIRoute } from 'chanfana'
import { Context } from 'hono'

const queryDescription = Str({description: 'a valid email address'})

Expand All @@ -107,8 +110,9 @@ export class Example extends OpenAPIRoute {
}
}

async handle(request: Request, env: any, context: any) {
// todo data
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

return {
validatedDescription: data.query.description
}
Expand Down
11 changes: 4 additions & 7 deletions docs/pages/user-guide/path-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ You can declare `params` parameters in the `request` property of your endpoint s

The validated data is available under `data.params.<name>`.

```ts hl_lines="9-11"
```ts hl_lines="10-12"
import { OpenAPIRoute } from 'chanfana'
import {z} from 'zod'
import { z } from 'zod'
import { Context } from 'hono'

export class ToDoFetch extends OpenAPIRoute {
schema = {
Expand All @@ -20,11 +21,7 @@ export class ToDoFetch extends OpenAPIRoute {
}
}

async handle(
request: Request,
env: any,
context: any,
) {
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

// You get full type inference when accessing the data variable
Expand Down
11 changes: 4 additions & 7 deletions docs/pages/user-guide/query-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ You can declare `query` parameters in the `request` property of your endpoint sc

The validated data is available under `data.query.<name>`.

```ts hl_lines="9-11"
```ts hl_lines="10-12"
import { OpenAPIRoute } from 'chanfana'
import {z} from 'zod'
import { z } from 'zod'
import { Context } from 'hono'

export class ToDoFetch extends OpenAPIRoute {
schema = {
Expand All @@ -20,11 +21,7 @@ export class ToDoFetch extends OpenAPIRoute {
}
}

async handle(
request: Request,
env: any,
context: any,
) {
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

// You get full type inference when accessing the data variable
Expand Down
27 changes: 9 additions & 18 deletions docs/pages/user-guide/request-body.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ For nested objects the validated data will be under the same position as defined

In this example, the description can be either a string or an array of strings.

```ts hl_lines="7-11"
```ts hl_lines="10-25"
import { OpenAPIRoute, Str, Enumeration } from 'chanfana'
import { z } from 'zod'
import { Context } from 'hono'

export class ToDoCreate extends OpenAPIRoute {
schema = {
Expand All @@ -37,11 +38,7 @@ export class ToDoCreate extends OpenAPIRoute {
},
}

async handle(
request: Request,
env: any,
context: any,
) {
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

const newToDo = data.body
Expand All @@ -63,9 +60,10 @@ Notice that when using `legacyTypeIntoZod` typescript inference is not available

Here's an example of a response schema using variables

```ts
```ts hl_lines="10-19"
import { OpenAPIRoute, legacyTypeIntoZod } from 'chanfana'
import { z } from 'zod'
import { Context } from 'hono'

export class ToDoCreate extends OpenAPIRoute {
schema = {
Expand All @@ -85,11 +83,7 @@ export class ToDoCreate extends OpenAPIRoute {
},
}

async handle(
request: Request,
env: any,
context: any,
) {
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

const newToDo = data.body
Expand All @@ -105,9 +99,10 @@ under the hood, for you to have even more flexibility.

Notice that when using `contentJson` typescript inference is not available!

```ts
```ts hl_lines="10-13"
import { contentJson, OpenAPIRoute } from 'chanfana'
import { z } from 'zod'
import { Context } from 'hono'

export class ToDoCreate extends OpenAPIRoute {
schema = {
Expand All @@ -121,11 +116,7 @@ export class ToDoCreate extends OpenAPIRoute {
}
}

async handle(
request: Request,
env: any,
context: any,
) {
async handle(c: Context) {
const data = await this.getValidatedData<typeof this.schema>()

const newToDo = data.body
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/user-guide/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ endpoints.
For this add the `security` component to your main `router` schema customization field

Notice that the key used in security must be the same used to register the component
```ts hl_lines="5 13"
```ts hl_lines="6 14"
const app = new Hono()
const openapi = fromHono(app, {
schema: {
Expand Down Expand Up @@ -60,7 +60,7 @@ openapi.registry.registerComponent(
For this add the `security` component to your main `endpoint` schema customization field

Notice that the key used in security must be the same used to register the component
```ts hl_lines="14 26"
```ts hl_lines="16 29"
export class ScanMetadataCreate extends OpenAPIRoute {
schema = {
tags: ['Scans'],
Expand Down

0 comments on commit d6bead4

Please sign in to comment.