Skip to content

Commit 3a36a06

Browse files
docs(openapi): add global responses docs
1 parent 60577ef commit 3a36a06

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

Diff for: content/openapi/operations.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ Let's open the browser and verify the generated `Cat` model:
115115

116116
<figure><img src="/assets/swagger-response-type.png" /></figure>
117117

118+
Instead of defining responses for each endpoint or controller individually, you can define a global response for all endpoints using the `DocumentBuilder` class. This approach is useful when you want to define a global response for all endpoints in your application (e.g., for errors like `401 Unauthorized` or `500 Internal Server Error`).
119+
120+
```typescript
121+
const config = new DocumentBuilder()
122+
.addGlobalResponse({
123+
status: 500,
124+
description: 'Internal server error',
125+
})
126+
// other configurations
127+
.build();
128+
```
129+
118130
#### File upload
119131

120132
You can enable file upload for a specific method with the `@ApiBody` decorator together with `@ApiConsumes()`. Here's a full example using the [File Upload](/techniques/file-upload) technique:
@@ -299,7 +311,9 @@ findAll(): Observable<{ total: number, limit: number, offset: number, results: C
299311
As you can see, the **Return Type** here is ambiguous. To workaround this issue, you can add a `title` property to the `schema` for `ApiPaginatedResponse`:
300312

301313
```typescript
302-
export const ApiPaginatedResponse = <TModel extends Type<any>>(model: TModel) => {
314+
export const ApiPaginatedResponse = <TModel extends Type<any>>(
315+
model: TModel,
316+
) => {
303317
return applyDecorators(
304318
ApiOkResponse({
305319
schema: {

Diff for: content/openapi/other-features.md

+22-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ const document = SwaggerModule.createDocument(app, options, {
1414

1515
#### Global parameters
1616

17-
You can add parameter definitions to all routes using `DocumentBuilder`:
17+
You can define parameters for all routes using `DocumentBuilder`, as shown below:
1818

1919
```typescript
20-
const options = new DocumentBuilder().addGlobalParameters({
21-
name: 'tenantId',
22-
in: 'header',
23-
});
20+
const config = new DocumentBuilder()
21+
.addGlobalParameters({
22+
name: 'tenantId',
23+
in: 'header',
24+
})
25+
// other configurations
26+
.build();
27+
```
28+
29+
#### Global responses
30+
31+
You can define global responses for all routes using `DocumentBuilder`. This is useful for setting up consistent responses across all endpoints in your application, such as error codes like `401 Unauthorized` or `500 Internal Server Error`.
32+
33+
```typescript
34+
const config = new DocumentBuilder()
35+
.addGlobalResponse({
36+
status: 500,
37+
description: 'Internal server error',
38+
})
39+
// other configurations
40+
.build();
2441
```
2542

2643
#### Multiple specifications

0 commit comments

Comments
 (0)