|
| 1 | +--- |
| 2 | +title: 'Getting started' |
| 3 | +--- |
| 4 | + |
| 5 | +# Introduction |
| 6 | + |
| 7 | +`openapi-decorators` 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-adonis/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-adonis/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 { DocumentBuilder } from "openapi-decorators/builders"; |
| 46 | +import { loadController } from "openapi-decorators/loaders"; |
| 47 | +import UsersController from "./users_controller"; |
| 48 | + |
| 49 | +const builder = new DocumentBuilder() |
| 50 | + .setTitle("My API") |
| 51 | + .setVersion("1.0.0"); |
| 52 | + |
| 53 | +await loadController(builder, UsersController); |
| 54 | + |
| 55 | +console.log(document.build()); // <- Your generated OpenAPI specifications |
| 56 | +``` |
| 57 | + |
| 58 | +::: |
| 59 | + |
| 60 | +## Getting started |
| 61 | + |
| 62 | +### Setup |
| 63 | + |
| 64 | +Install `openapi-decorators` and `reflect-metadata` using your favorite package manager. |
| 65 | + |
| 66 | +```bash |
| 67 | +npm install openapi-decorators reflect-metadata |
| 68 | +``` |
| 69 | + |
| 70 | +Import `reflect-metadata` in your main file. |
| 71 | + |
| 72 | +::: code-group |
| 73 | + |
| 74 | +```ts [index.ts] |
| 75 | +import "reflect-metadata"; |
| 76 | + |
| 77 | +// Rest of your app |
| 78 | +``` |
| 79 | + |
| 80 | +::: |
| 81 | + |
| 82 | +Enable `experimentalDecorators` and `experimentalDecorators`. |
| 83 | + |
| 84 | + |
| 85 | +::: code-group |
| 86 | + |
| 87 | +```json [tsconfig.json] |
| 88 | +{ |
| 89 | + "compilerOptions": { |
| 90 | + "emitDecoratorMetadata": true, |
| 91 | + "experimentalDecorators": true, |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +::: |
| 97 | + |
| 98 | +### Create your OpenAPI document |
| 99 | + |
| 100 | +To get started, create a new DocumentBuilder. It will hold all the informations required to generate your OpenAPI specifications. |
| 101 | +By using the method `build()` you can already have an (almost) empty documentation. |
| 102 | + |
| 103 | +::: code-group |
| 104 | + |
| 105 | +```ts [index.ts] |
| 106 | +import "reflect-metadata"; |
| 107 | +import { DocumentBuilder } from "openapi-decorators/builders"; |
| 108 | + |
| 109 | +const builder = new DocumentBuilder() |
| 110 | + .setTitle("My API") |
| 111 | + .setVersion("1.0.0"); |
| 112 | + |
| 113 | +console.log(document.build()); // <- Your generated OpenAPI specifications |
| 114 | +``` |
| 115 | + |
| 116 | +::: |
| 117 | + |
| 118 | +### Create your first model |
| 119 | + |
| 120 | +Using the `apiProperty` decorator on class properties will allow your operations to use the class as a schema. |
| 121 | + |
| 122 | +> 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. |
| 123 | +
|
| 124 | +::: code-group |
| 125 | + |
| 126 | +```ts [user.ts] |
| 127 | +import { apiProperty } from "openapi-decorators/decorators"; |
| 128 | + |
| 129 | +class User { |
| 130 | + @apiProperty() |
| 131 | + declare id: string; |
| 132 | + |
| 133 | + @apiProperty({ example: "John Doe" }) |
| 134 | + declare name: string; |
| 135 | + |
| 136 | + @apiProperty() |
| 137 | + declare email: string; |
| 138 | + |
| 139 | + @apiProperty({ required: false }) |
| 140 | + declare mobile?: string; |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +::: |
| 145 | + |
| 146 | +### Create your first controller |
| 147 | + |
| 148 | +Next we need to define our first operation. We can do this by using a controller. |
| 149 | + |
| 150 | +In the following example we create an operation `GET /users` that returns a list of `User`. |
| 151 | + |
| 152 | +::: code-group |
| 153 | + |
| 154 | +```ts [users_controller.ts] |
| 155 | +import { apiOperation, apiResponse } from "openapi-decorators/decorators"; |
| 156 | +import User from "./user"; |
| 157 | + |
| 158 | +class UsersController { |
| 159 | + @apiOperation({ |
| 160 | + method: "get", |
| 161 | + pattern: "/users", |
| 162 | + summary: "List users" |
| 163 | + }) |
| 164 | + @apiResponse({ type: [User] }) |
| 165 | + async list() { |
| 166 | + ... |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +::: |
| 172 | + |
| 173 | +### Load the controller into your DocumentBuilder |
| 174 | + |
| 175 | +You now simply have to load the controller into your DocumentBuilder and tada 🎉. |
| 176 | + |
| 177 | +::: code-group |
| 178 | + |
| 179 | +```ts [index.ts] |
| 180 | +import "reflect-metadata"; |
| 181 | +import { DocumentBuilder } from "openapi-decorators/builders"; |
| 182 | +import { loadController } from "openapi-decorators/loaders"; |
| 183 | +import UsersController from "./users_controller"; |
| 184 | + |
| 185 | +const builder = new DocumentBuilder() |
| 186 | + .setTitle("My API") |
| 187 | + .setVersion("1.0.0"); |
| 188 | + |
| 189 | +await loadController(builder, UsersController); |
| 190 | + |
| 191 | +console.log(document.build()); // <- Your generated OpenAPI specifications |
| 192 | +``` |
| 193 | + |
| 194 | +::: |
0 commit comments