|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +# OpenAPI Documentation |
| 6 | + |
| 7 | +This page contains the OpenAPI documentation for the Yelix API. The documentation is generated using the OpenAPI specification and is available in JSON format. |
| 8 | + |
| 9 | +## Getting Started |
| 10 | + |
| 11 | +Let's get started by initializing the OpenAPI documentation. |
| 12 | + |
| 13 | +```ts title="main.ts" |
| 14 | +import { Yelix } from "jsr:@murat/yelix"; |
| 15 | +import * as path from "jsr:@std/path@1.0.8"; |
| 16 | + |
| 17 | +async function main() { |
| 18 | + // Port is 3030 by default |
| 19 | + const app = new Yelix(); |
| 20 | + |
| 21 | + // Load endpoints from a 'api' folder |
| 22 | + const currentDir = Deno.cwd(); |
| 23 | + const API_Folder = path.join(currentDir, 'api'); |
| 24 | + await app.loadEndpointsFromFolder(API_Folder); |
| 25 | + |
| 26 | + // highlight-start |
| 27 | + app.initOpenAPI({ |
| 28 | + path: "/docs", |
| 29 | + title: "Yelix Testing API", |
| 30 | + description: "This is a testing API for Yelix", |
| 31 | + servers: [ |
| 32 | + { |
| 33 | + url: "http://localhost:3030", |
| 34 | + description: "Local Server", |
| 35 | + }, |
| 36 | + ], |
| 37 | + }); |
| 38 | + // highlight-end |
| 39 | + |
| 40 | + app.serve(); |
| 41 | +} |
| 42 | + |
| 43 | +await main(); |
| 44 | +``` |
| 45 | + |
| 46 | +#### Overview |
| 47 | + |
| 48 | +When you initialize the OpenAPI documentation, endpoints are automatically documented. The documentation is available at the `/docs` endpoint. |
| 49 | + |
| 50 | +```ts title="hello.ts" |
| 51 | +// ...imports... |
| 52 | + |
| 53 | +export async function GET(ctx: Ctx) { |
| 54 | + const requestData = ctx.get("dataValidation").user; |
| 55 | + const query: QueryType = requestData.query; |
| 56 | + |
| 57 | + const data = "Hello, " + query.name; |
| 58 | + return await ctx.text(data, 200); |
| 59 | +} |
| 60 | + |
| 61 | +export const path = "/api/hello-no-cache"; |
| 62 | +export const middlewares = ["dataValidation"]; |
| 63 | + |
| 64 | +export const validation: ValidationType = { |
| 65 | + query: { |
| 66 | + name: z.string(), |
| 67 | + }, |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +Without extra documentation, the OpenAPI documentation will look like this: |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +Yelix automaticly takes some information when serving your endpoints. |
| 76 | +- Method: `GET` - Taken from the function name |
| 77 | +- Path: `/api/hello-no-cache` - Defined in the `path` variable |
| 78 | +- Validation: `query` - Defined in the `validation` variable |
| 79 | + |
| 80 | +## Configuration (openAPI export) |
| 81 | + |
| 82 | +```ts title="hello.ts" |
| 83 | +export const openAPI = { |
| 84 | + description: 'Returns a greeting message', |
| 85 | + tags: ['General'], |
| 86 | + responses: { |
| 87 | + 200: { |
| 88 | + type: 'application/json', |
| 89 | + zodSchema: z.object({ |
| 90 | + message: z.string(), |
| 91 | + }), |
| 92 | + }, |
| 93 | + }, |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | +With the `openAPI` export, you can provide additional information about the endpoint. This information will be displayed in the OpenAPI documentation. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +#### Automatic Description Generation |
| 102 | + |
| 103 | +If you don't provide a description, Yelix will generate one for you based on the data validation. For example, if you have a query validation like this: |
| 104 | + |
| 105 | +```ts title="hello.ts" |
| 106 | +export const validation: ValidationType = { |
| 107 | + query: { |
| 108 | + name: z.string() |
| 109 | + // highlight-next-line |
| 110 | + .min(3).max(255).email(), |
| 111 | + }, |
| 112 | +}; |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +`query: { [key: string]: { description: string } }` is also supported. If you don't provide a description, I will generate one for you with data validation(If you don't have extra validation, will be empty). Lemme do one example for you to understand better. Don't forget, all descriptions supports CommonMarkdown. |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +```ts title="hello.ts" |
| 122 | +export const openAPI = { |
| 123 | + description: 'Returns a greeting message', |
| 124 | + tags: ['General'], |
| 125 | + // highlight-start |
| 126 | + query: { |
| 127 | + name: { |
| 128 | + description: 'Name of the person', |
| 129 | + } |
| 130 | + }, |
| 131 | + // highlight-end |
| 132 | + responses: { |
| 133 | + 200: { |
| 134 | + type: 'application/json', |
| 135 | + zodSchema: z.object({ |
| 136 | + message: z.string(), |
| 137 | + }), |
| 138 | + }, |
| 139 | + }, |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +## Responses |
| 146 | + |
| 147 | +In openAPI export, you can define responses for the endpoint. The responses are displayed in the OpenAPI documentation. |
| 148 | + |
| 149 | +```ts title="hello.ts" |
| 150 | +export const openAPI = { |
| 151 | + description: 'Register a new user', |
| 152 | + tags: ['General'], |
| 153 | + responses: { |
| 154 | + 200: { |
| 155 | + type: 'application/json', |
| 156 | + zodSchema: z.object({ |
| 157 | + username: z.string(), |
| 158 | + email: z.string().email(), |
| 159 | + age: z.number().min(18).max(99), |
| 160 | + friendNames: z.array(z.string()), |
| 161 | + country: z.enum(['USA', 'UK', 'India']), |
| 162 | + }), |
| 163 | + }, |
| 164 | + }, |
| 165 | +}; |
| 166 | +``` |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +## Hide Endpoint |
| 171 | + |
| 172 | +If you want to hide an endpoint from the OpenAPI documentation, you can use the `hide` in openAPI export. |
| 173 | + |
| 174 | +```ts title="hello.ts" |
| 175 | +export const openAPI = { |
| 176 | + hide: true, |
| 177 | +}; |
| 178 | +``` |
0 commit comments