-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathdocument-builder.ts
211 lines (188 loc) · 4.96 KB
/
document-builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { Logger } from '@nestjs/common';
import { isString, isUndefined, negate, pickBy } from 'lodash';
import { buildDocumentBase } from './fixtures/document.base';
import { OpenAPIObject } from './interfaces';
import {
ExternalDocumentationObject,
HttpSecuritySchemeObject,
OAuth2SecuritySchemeObject,
OpenIdSecurityConnectSchemeObject,
ParameterObject,
SecurityRequirementObject,
SecuritySchemeObject,
ServerVariableObject,
TagObject
} from './interfaces/open-api-spec.interface';
import { GlobalParametersStorage } from './storages/global-parameters.storage';
export class DocumentBuilder {
private readonly logger = new Logger(DocumentBuilder.name);
private readonly document: Omit<OpenAPIObject, 'paths'> = buildDocumentBase();
public setTitle(title: string): this {
this.document.info.title = title;
return this;
}
public setDescription(description: string): this {
this.document.info.description = description;
return this;
}
public setVersion(version: string): this {
this.document.info.version = version;
return this;
}
public setTermsOfService(termsOfService: string): this {
this.document.info.termsOfService = termsOfService;
return this;
}
public setContact(name: string, url: string, email: string): this {
this.document.info.contact = { name, url, email };
return this;
}
public setLicense(name: string, url: string): this {
this.document.info.license = { name, url };
return this;
}
public addServer(
url: string,
description?: string,
variables?: Record<string, ServerVariableObject>
): this {
this.document.servers.push({ url, description, variables });
return this;
}
public setExternalDoc(description: string, url: string): this {
this.document.externalDocs = { description, url };
return this;
}
public setBasePath(path: string) {
this.logger.warn(
'The "setBasePath" method has been deprecated. Now, a global prefix is populated automatically. If you want to ignore it, take a look here: https://docs.nestjs.com/recipes/swagger#global-prefix. Alternatively, you can use "addServer" method to set up multiple different paths.'
);
return this;
}
public addTag(
name: string,
description = '',
externalDocs?: ExternalDocumentationObject
): this {
this.document.tags = this.document.tags.concat(
pickBy(
{
name,
description,
externalDocs
},
negate(isUndefined)
) as TagObject
);
return this;
}
public addSecurity(name: string, options: SecuritySchemeObject): this {
this.document.components.securitySchemes = {
...(this.document.components.securitySchemes || {}),
[name]: options
};
return this;
}
public addGlobalParameters(...parameters: ParameterObject[]): this {
GlobalParametersStorage.add(...parameters);
return this;
}
public addSecurityRequirements(
name: string | SecurityRequirementObject,
requirements: string[] = []
): this {
let securityRequirement: SecurityRequirementObject;
if (isString(name)) {
securityRequirement = { [name]: requirements };
} else {
securityRequirement = name;
}
this.document.security = (this.document.security || []).concat({
...securityRequirement
});
return this;
}
public addBearerAuth(
options: HttpSecuritySchemeObject = {
type: 'http'
},
name = 'bearer'
): this {
this.addSecurity(name, {
scheme: 'bearer',
bearerFormat: 'JWT',
...options
});
return this;
}
public addOAuth2(
options: OAuth2SecuritySchemeObject | OpenIdSecurityConnectSchemeObject = {
type: 'oauth2'
},
name = 'oauth2'
): this {
this.addSecurity(name, {
type: 'oauth2',
flows: {},
...options
});
return this;
}
public addOpenIdConnect(
options: Partial<OpenIdSecurityConnectSchemeObject> = {
type: 'openIdConnect'
},
name = 'openIdConnect'
): this {
this.addSecurity(name, {
type: 'openIdConnect',
...options
});
return this;
}
public addApiKey(
options: HttpSecuritySchemeObject = {
type: 'apiKey'
},
name = 'api_key'
): this {
this.addSecurity(name, {
type: 'apiKey',
in: 'header',
name,
...options
});
return this;
}
public addBasicAuth(
options: HttpSecuritySchemeObject = {
type: 'http'
},
name = 'basic'
): this {
this.addSecurity(name, {
type: 'http',
scheme: 'basic',
...options
});
return this;
}
public addCookieAuth(
cookieName = 'connect.sid',
options: HttpSecuritySchemeObject = {
type: 'apiKey'
},
securityName = 'cookie'
): this {
this.addSecurity(securityName, {
type: 'apiKey',
in: 'cookie',
name: cookieName,
...options
});
return this;
}
public build(): Omit<OpenAPIObject, 'paths'> {
return this.document;
}
}