Skip to content

Commit ca68475

Browse files
authored
feat(metadata): documentation, fixes and improvements (#2006)
* feat(openapi): add comments and improve documentation with twoslash * feat(metadata): merge properly operations together
1 parent 713ea1b commit ca68475

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1163
-84
lines changed

.changeset/dry-flies-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-metadata": patch
3+
---
4+
5+
Add comments for better documentation and add new exports

.changeset/honest-coats-relax.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-metadata": patch
3+
---
4+
5+
Merge properly operations together

docs/.vitepress/config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import en from "./en";
33
import zh from "./zh";
44
import ja from "./ja";
55
import shared from "./shared";
6+
import { transformerTwoslash } from "@shikijs/vitepress-twoslash";
7+
import { ModuleKind, ModuleResolutionKind } from "typescript";
68

79
// https://vitepress.dev/reference/site-config
810
export default defineConfig({
@@ -12,4 +14,16 @@ export default defineConfig({
1214
zh: { label: "简体中文", ...zh },
1315
ja: { label: "日本語", ...ja },
1416
},
17+
markdown: {
18+
codeTransformers: [
19+
transformerTwoslash({
20+
twoslashOptions: {
21+
compilerOptions: {
22+
experimentalDecorators: true,
23+
moduleResolution: ModuleResolutionKind.Bundler,
24+
},
25+
},
26+
}),
27+
],
28+
},
1529
});

docs/.vitepress/en.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,23 @@ export default defineConfig({
102102
},
103103
{
104104
text: "openapi-metadata",
105-
base: "/openapi-metadata",
106105
items: [
107-
{ text: "Getting Started", link: "/" },
108-
{ text: "Decorators", link: "/decorators" },
109-
{ text: "Metadata", link: "/metadata" },
110-
{ text: "Type loader", link: "/type-loader" },
111-
{ text: "UI Integrations", link: "/ui" },
112-
{ text: "Examples", link: "/examples" },
113-
{ text: "About", link: "/about" },
106+
{ text: "Getting Started", link: "/openapi-metadata" },
107+
{ text: "Decorators", link: "/openapi-metadata/decorators" },
108+
{ text: "Metadata", link: "/openapi-metadata/metadata" },
109+
{ text: "Type loader", link: "/openapi-metadata/type-loader" },
110+
{ text: "UI Integrations", link: "/openapi-metadata/ui" },
111+
{
112+
text: "Integrations",
113+
items: [
114+
{
115+
text: "AdonisJS",
116+
link: "https://friendsofadonis.com/docs/openapi",
117+
},
118+
],
119+
},
120+
{ text: "Examples", link: "/openapi-metadata/examples" },
121+
{ text: "About", link: "/openapi-metadata/about" },
114122
],
115123
},
116124
],

docs/.vitepress/theme/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import type { Theme } from "vitepress";
33
import DefaultTheme from "vitepress/theme";
44
import CustomLayout from "./CustomLayout.vue";
55
import SponsorList from "./SponsorList.vue";
6+
import TwoslashFloatingVue from "@shikijs/vitepress-twoslash/client";
67
import "./style.css";
8+
import "@shikijs/vitepress-twoslash/style.css";
79

810
export default {
911
extends: DefaultTheme,
1012
Layout: CustomLayout,
1113
enhanceApp({ app, router, siteData }) {
1214
app.component("SponsorList", SponsorList);
15+
app.use(TwoslashFloatingVue);
1316
},
1417
} satisfies Theme;

docs/data/contributors.json

+1-1
Large diffs are not rendered by default.

docs/openapi-metadata/decorators.md

+45-15
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,48 @@ Decorators are used to enrich your OpenAPI specifications. They can be applied o
88

99
> For more information about the decorators, you can directly refer to the [source code](https://github.com/openapi-ts/openapi-typescript/packages/openapi-metadata/src/decorators).
1010
11-
| Decorator | Usage | Description |
12-
| ----------------------- | ------------------- | ------------------------------------------------------------------------ |
13-
| `@ApiBody` | Method | Sets the requestBody of the operation. |
14-
| `@ApiCookie` | Controller / Method | Adds a cookie parameter to the operation(s). |
15-
| `@ApiExcludeController` | Method | Excludes the operations of this controller from the document generation. |
16-
| `@ApiExcludeOperation` | Method | Excludes this operation from the document generation. |
17-
| `@ApiExtraModels` | Controller | Adds extra models to be loaded in the schema. |
18-
| `@ApiHeader` | Controller / Method | Adds a header parameter to the operation(s). |
19-
| `@ApiOperation` | Method | Configures an operation. |
20-
| `@ApiParam` | Controller / Method | Adds a path parameter to the operation(s). |
21-
| `@ApiProperty` | Model | Configures a schema property property. |
22-
| `@ApiQuery` | Controller / Method | Adds a query parameter to the operation(s). |
23-
| `@ApiResponse` | Controller / Method | Adds a response to the operation(s). |
24-
| `@ApiSecurity` | Controller / Method | Sets the security scheme to the operation(s). |
25-
| `@ApiTags` | Controller / Method | Adds tags to the operation(s). |
11+
_You can hover the following code snippet to get information about each decorator._
12+
13+
```ts twoslash
14+
// @noErrors
15+
// ---cut---
16+
import {
17+
ApiBody,
18+
ApiCookie,
19+
ApiExcludeController,
20+
ApiExcludeOperation,
21+
ApiExtraModels,
22+
ApiHeader,
23+
ApiOperation,
24+
ApiParam,
25+
ApiProperty,
26+
ApiPropertyOptional,
27+
ApiQuery,
28+
ApiResponse,
29+
ApiSecurity,
30+
ApiBasicAuth,
31+
ApiOauth2,
32+
ApiBearerAuth,
33+
ApiCookieAuth,
34+
ApiTags
35+
} from 'openapi-metadata/decorators'
36+
// ---cut---
37+
@ApiBody()
38+
@ApiCookie()
39+
@ApiExcludeController()
40+
@ApiExcludeOperation()
41+
@ApiExtraModels()
42+
@ApiHeader()
43+
@ApiOperation()
44+
@ApiParam()
45+
@ApiProperty()
46+
@ApiPropertyOptional()
47+
@ApiQuery()
48+
@ApiResponse()
49+
@ApiSecurity()
50+
@ApiBasicAuth()
51+
@ApiOAuth2()
52+
@ApiBearerAuth()
53+
@ApiCookieAuth()
54+
@ApiTags()
55+
```

docs/openapi-metadata/examples.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ This library is made to be used through an integration with your favorite framew
88

99
## Express
1010

11-
```ts
11+
```ts twoslash
12+
// @noErrors
1213
import express from "express";
1314
import { generateDocument } from "openapi-metadata";
1415
import { generateScalarUI } from "openapi-metadata/ui";
@@ -29,7 +30,8 @@ app.get("/api/docs", (req, res) => {
2930

3031
## Fastify
3132

32-
```ts
33+
```ts twoslash
34+
// @noErrors
3335
import fastify from "fastify";
3436
import { generateDocument } from "openapi-metadata";
3537
import { generateScalarUI } from "openapi-metadata/ui";

docs/openapi-metadata/index.md

+23-21
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ title: "Getting started"
88

99
::: code-group
1010

11-
```ts [users_controller.ts]
11+
```ts [users_controller.ts] twoslash
12+
// @noErrors
1213
import { ApiOperation, ApiResponse } from "openapi-metadata/decorators";
1314
import User from "./user";
1415

1516
class UsersController {
1617
@ApiOperation({
17-
method: "get",
18-
pattern: "/users",
18+
methods: ["get"],
19+
path: "/users",
1920
summary: "List users"
2021
})
2122
@ApiResponse({ type: [User] })
@@ -25,7 +26,7 @@ class UsersController {
2526
}
2627
```
2728

28-
```ts [user.ts]
29+
```ts [user.ts] twoslash
2930
import { ApiProperty } from "openapi-metadata/decorators";
3031

3132
class User {
@@ -40,7 +41,8 @@ class User {
4041
}
4142
```
4243

43-
```ts [index.ts]
44+
```ts [index.ts] twoslash
45+
// @noErrors
4446
import "reflect-metadata";
4547
import { generateDocument } from "openapi-metadata";
4648
import UsersController from "./users_controller";
@@ -49,7 +51,7 @@ const document = await generateDocument({
4951
controllers: [UsersController],
5052
document: {
5153
info: {
52-
name: "My Api",
54+
title: "My Api",
5355
version: "1.0.0",
5456
},
5557
},
@@ -109,21 +111,21 @@ To get started, you can use the `generateDocument` function to create an (almost
109111

110112
::: code-group
111113

112-
```ts [index.ts]
114+
```ts [index.ts] twoslash
113115
import "reflect-metadata";
114116
import { generateDocument } from "openapi-metadata";
115117

116-
const builder = await generateDocument({
118+
const document = await generateDocument({
117119
controllers: [],
118120
document: {
119121
info: {
120-
name: "My API",
122+
title: "My API",
121123
version: "1.0.0",
122124
},
123125
},
124126
});
125127

126-
console.log(document.build()); // <- Your generated OpenAPI specifications
128+
console.log(document); // <- Your generated OpenAPI specifications
127129
```
128130

129131
:::
@@ -135,14 +137,15 @@ In the following example we have a `UsersController` which declares an operation
135137

136138
::: code-group
137139

138-
```ts [controllers/users_controller.ts]
140+
```ts [controllers/users_controller.ts] twoslash
141+
// @noErrors
139142
import { ApiOperation, ApiResponse } from "openapi-metadata/decorators";
140143
import User from "../schemas/user";
141144

142145
export default class UsersController {
143146
@ApiOperation({
144-
method: "get",
145-
pattern: "/users",
147+
methods: ["get"],
148+
path: "/users",
146149
summary: "List users",
147150
})
148151
@ApiResponse({ type: [User] })
@@ -164,7 +167,7 @@ By using the `@ApiProperty` decorator on class we can define the properties of o
164167
165168
::: code-group
166169

167-
```ts [schemas/user.ts]
170+
```ts [schemas/user.ts] twoslash
168171
import { ApiProperty } from "openapi-metadata/decorators";
169172

170173
export default class User {
@@ -184,18 +187,19 @@ export default class User {
184187

185188
:::
186189

187-
### Add the controller to the generated document
190+
### Register your controller
188191

189-
Now that we have our controller ready, we can use it to generate our document.
192+
Now that we have our controller ready, we can include it when generating our document.
190193

191194
::: code-group
192195

193-
```ts [index.ts]
196+
```ts [index.ts] twoslash
197+
// @noErrors
194198
import "reflect-metadata";
195199
import { generateDocument } from "openapi-metadata";
196200
import UsersController from "./controllers/users_controller.ts";
197201

198-
const builder = await generateDocument({
202+
const document = await generateDocument({
199203
controllers: [UsersController],
200204
document: {
201205
info: {
@@ -205,9 +209,7 @@ const builder = await generateDocument({
205209
},
206210
});
207211

208-
console.log(document.build()); // <- Your generated OpenAPI specifications
212+
console.log(document); // <- Your generated OpenAPI specifications
209213
```
210214

211215
:::
212-
213-
### Going further

docs/openapi-metadata/ui.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ title: UI integrations
88

99
## [Scalar](https://scalar.com)
1010

11-
```ts
11+
```ts twoslash
1212
import { generateScalarUI } from "openapi-metadata/ui";
1313

1414
generateScalarUI("http://localhost:3000/api");
1515
```
1616

1717
## [Swagger UI](https://swagger.io/tools/swagger-ui/)
1818

19-
```ts
19+
```ts twoslash
2020
import { generateSwaggerUI } from "openapi-metadata/ui";
2121

2222
generateSwaggerUI("http://localhost:3000/api");
2323
```
2424

2525
## [Rapidoc](https://rapidocweb.com/)
2626

27-
```ts
27+
```ts twoslash
2828
import { generateRapidocUI } from "openapi-metadata/ui";
2929

3030
generateRapidocUI("http://localhost:3000/api");

docs/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"update-contributors": "node scripts/update-contributors.js"
1010
},
1111
"devDependencies": {
12-
"vitepress": "1.5.0"
12+
"vitepress": "1.5.0",
13+
"@shikijs/vitepress-twoslash": "^1.22.2",
14+
"openapi-metadata": "workspace:*"
1315
}
1416
}

packages/openapi-metadata/src/decorators/api-body.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { type OperationBodyMetadata, OperationBodyMetadataStorage } from "../met
33

44
export type ApiBodyOptions = SetOptional<OperationBodyMetadata, "mediaType">;
55

6+
/**
7+
* Configures the request body.
8+
* Can be applied to Controllers and Operations.
9+
*
10+
* @see https://swagger.io/specification/#request-body-object
11+
*/
612
export function ApiBody(options: ApiBodyOptions): MethodDecorator {
713
return (target, propertyKey) => {
814
OperationBodyMetadataStorage.defineMetadata(

packages/openapi-metadata/src/decorators/api-cookie.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { type OperationParameterMetadata, OperationParameterMetadataStorage } fr
22

33
export type ApiCookieOptions = Omit<OperationParameterMetadata, "in">;
44

5+
/**
6+
* Configures a cookie parameter.
7+
* Can be applied to Operations and Controllers.
8+
*
9+
* @see https://swagger.io/specification/#parameter-object
10+
*/
511
export function ApiCookie(options: ApiCookieOptions) {
612
return (target: Object, propertyKey?: string | symbol) => {
713
OperationParameterMetadataStorage.mergeMetadata(target, [{ in: "cookie", ...options }], propertyKey);

packages/openapi-metadata/src/decorators/api-exclude.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { ExcludeMetadataStorage } from "../metadata/exclude.js";
22

3+
/**
4+
* Exclude this Controller from the generated schema.
5+
* Useful when working with framework integrations that autoload controllers.
6+
*/
37
export function ApiExcludeController(): ClassDecorator {
48
return (target) => {
59
ExcludeMetadataStorage.defineMetadata(target, true);
610
};
711
}
812

13+
/**
14+
* Exclude this Operation from the generated schema.
15+
* Useful when working with framework integrations that autoload controllers.
16+
*/
917
export function ApiExcludeOperation(): MethodDecorator {
1018
return (target, propertyKey) => {
1119
ExcludeMetadataStorage.defineMetadata(target, true, propertyKey);

packages/openapi-metadata/src/decorators/api-extra-models.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { ExtraModelsMetadataStorage } from "../metadata/extra-models.js";
22
import type { Thunk, TypeValue } from "../types.js";
33

4+
/**
5+
* Adds extra models to the generated schema that are not used anywhere else.
6+
* Useful when you want to share models that are not used by your operations.
7+
*/
48
export function ApiExtraModels(...models: (TypeValue | Thunk<TypeValue>)[]) {
59
return (target: Object) => {
610
ExtraModelsMetadataStorage.mergeMetadata(target, models);

0 commit comments

Comments
 (0)