|
| 1 | +--- |
| 2 | +title: "Getting started" |
| 3 | +--- |
| 4 | + |
| 5 | +# Introduction |
| 6 | + |
| 7 | +`openapi-metadata` is a framework agnostic library to automatically generate OpenAPI schemas and documentation by using Typescript decorators and metadata. |
| 8 | + |
| 9 | +::: code-group |
| 10 | + |
| 11 | +```ts [users_controller.ts] |
| 12 | +import { ApiOperation, ApiResponse } from "openapi-metadata/decorators"; |
| 13 | +import User from "./user"; |
| 14 | + |
| 15 | +class UsersController { |
| 16 | + @ApiOperation({ |
| 17 | + method: "get", |
| 18 | + pattern: "/users", |
| 19 | + summary: "List users" |
| 20 | + }) |
| 21 | + @ApiResponse({ type: [User] }) |
| 22 | + async list() { |
| 23 | + ... |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +```ts [user.ts] |
| 29 | +import { ApiProperty } from "openapi-metadata/decorators"; |
| 30 | + |
| 31 | +class User { |
| 32 | + @ApiProperty() |
| 33 | + declare id: number; |
| 34 | + |
| 35 | + @ApiProperty() |
| 36 | + declare name: string; |
| 37 | + |
| 38 | + @ApiProperty({ required: false }) |
| 39 | + declare mobile?: string; |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```ts [index.ts] |
| 44 | +import "reflect-metadata"; |
| 45 | +import { generateDocument } from "openapi-metadata"; |
| 46 | +import UsersController from "./users_controller"; |
| 47 | + |
| 48 | +const document = await generateDocument({ |
| 49 | + controllers: [UsersController], |
| 50 | + document: { |
| 51 | + info: { |
| 52 | + name: "My Api", |
| 53 | + version: "1.0.0", |
| 54 | + }, |
| 55 | + }, |
| 56 | +}); |
| 57 | + |
| 58 | +console.log(document); // <- Your generated OpenAPI specifications |
| 59 | +``` |
| 60 | + |
| 61 | +::: |
| 62 | + |
| 63 | +- ✅ Fully compliant [OpenAPI V3](https://swagger.io/specification/) |
| 64 | +- ✅ Automatic type inference |
| 65 | +- ✅ Supports [Scalar](https://scalar.com/), [Swagger UI](https://swagger.io/tools/swagger-ui/) and [Rapidoc](https://rapidocweb.com/) |
| 66 | +- ✅ Extensible with custom type loaders |
| 67 | +- ✅ Ready to be integrated with your favorite framework |
| 68 | + |
| 69 | +## Getting started |
| 70 | + |
| 71 | +### Setup |
| 72 | + |
| 73 | +Install `openapi-metadata` and `reflect-metadata` using your favorite package manager. |
| 74 | + |
| 75 | +```bash |
| 76 | +npm install openapi-metadata reflect-metadata |
| 77 | +``` |
| 78 | + |
| 79 | +Import `reflect-metadata` in your main file. |
| 80 | + |
| 81 | +::: code-group |
| 82 | + |
| 83 | +```ts [index.ts] |
| 84 | +import "reflect-metadata"; |
| 85 | + |
| 86 | +// Rest of your app |
| 87 | +``` |
| 88 | + |
| 89 | +::: |
| 90 | + |
| 91 | +Enable `experimentalDecorators` and `experimentalDecorators`. |
| 92 | + |
| 93 | +::: code-group |
| 94 | + |
| 95 | +```json [tsconfig.json] |
| 96 | +{ |
| 97 | + "compilerOptions": { |
| 98 | + "emitDecoratorMetadata": true, |
| 99 | + "experimentalDecorators": true |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +::: |
| 105 | + |
| 106 | +### Create your OpenAPI document |
| 107 | + |
| 108 | +To get started, you can use the `generateDocument` function to create an (almost) empty documentation. You can define a base document that will be merged with the generated one. |
| 109 | + |
| 110 | +::: code-group |
| 111 | + |
| 112 | +```ts [index.ts] |
| 113 | +import "reflect-metadata"; |
| 114 | +import { generateDocument } from "openapi-metadata"; |
| 115 | + |
| 116 | +const builder = await generateDocument({ |
| 117 | + controllers: [], |
| 118 | + document: { |
| 119 | + info: { |
| 120 | + name: "My API", |
| 121 | + version: "1.0.0", |
| 122 | + }, |
| 123 | + }, |
| 124 | +}); |
| 125 | + |
| 126 | +console.log(document.build()); // <- Your generated OpenAPI specifications |
| 127 | +``` |
| 128 | + |
| 129 | +::: |
| 130 | + |
| 131 | +### Create your first controller |
| 132 | + |
| 133 | +A controller is a simple class where each methods could be an Operation. |
| 134 | +In the following example we have a `UsersController` which declares an operation `GET /users` that returns a list of `Users`. |
| 135 | + |
| 136 | +::: code-group |
| 137 | + |
| 138 | +```ts [controllers/users_controller.ts] |
| 139 | +import { ApiOperation, ApiResponse } from "openapi-metadata/decorators"; |
| 140 | +import User from "../schemas/user"; |
| 141 | + |
| 142 | +export default class UsersController { |
| 143 | + @ApiOperation({ |
| 144 | + method: "get", |
| 145 | + pattern: "/users", |
| 146 | + summary: "List users", |
| 147 | + }) |
| 148 | + @ApiResponse({ type: [User] }) |
| 149 | + async list() { |
| 150 | + // ...your logic |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +::: |
| 156 | + |
| 157 | +### Create your first schema |
| 158 | + |
| 159 | +In our controller we define the response of your operation to be `[User]` (a list of users). We now need to create this model. |
| 160 | + |
| 161 | +By using the `@ApiProperty` decorator on class we can define the properties of our schema. |
| 162 | + |
| 163 | +> Unlike other libraries like `@nestjs/swagger`, every element of your OpenAPI schema is lazy-loaded. Your models will only be part of your documentation if it is used. |
| 164 | +
|
| 165 | +::: code-group |
| 166 | + |
| 167 | +```ts [schemas/user.ts] |
| 168 | +import { ApiProperty } from "openapi-metadata/decorators"; |
| 169 | + |
| 170 | +export default class User { |
| 171 | + @ApiProperty() |
| 172 | + declare id: string; |
| 173 | + |
| 174 | + @ApiProperty({ example: "John Doe" }) |
| 175 | + declare name: string; |
| 176 | + |
| 177 | + @ApiProperty() |
| 178 | + declare email: string; |
| 179 | + |
| 180 | + @ApiProperty({ required: false }) |
| 181 | + declare mobile?: string; |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +::: |
| 186 | + |
| 187 | +### Add the controller to the generated document |
| 188 | + |
| 189 | +Now that we have our controller ready, we can use it to generate our document. |
| 190 | + |
| 191 | +::: code-group |
| 192 | + |
| 193 | +```ts [index.ts] |
| 194 | +import "reflect-metadata"; |
| 195 | +import { generateDocument } from "openapi-metadata"; |
| 196 | +import UsersController from "./controllers/users_controller.ts"; |
| 197 | + |
| 198 | +const builder = await generateDocument({ |
| 199 | + controllers: [UsersController], |
| 200 | + document: { |
| 201 | + info: { |
| 202 | + name: "My API", |
| 203 | + version: "1.0.0", |
| 204 | + }, |
| 205 | + }, |
| 206 | +}); |
| 207 | + |
| 208 | +console.log(document.build()); // <- Your generated OpenAPI specifications |
| 209 | +``` |
| 210 | + |
| 211 | +::: |
| 212 | + |
| 213 | +### Going further |
0 commit comments