forked from Savory/Danet-Swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.ts
242 lines (213 loc) · 4.43 KB
/
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { Swagger } from './swagger.ts';
import RequestBody = Swagger.RequestBody;
import Schema = Swagger.Schema;
import Response = Swagger.Response;
import Header = Swagger.Header;
import ServerVariable = Swagger.ServerVariable;
import SecurityRequirementObject = Swagger.SecurityRequirementObject;
export class RequestBodyBuilder {
private requestBody: RequestBody = {
content: {},
required: true,
};
jsonContent(schema: Schema) {
this.requestBody.content = {
'application/json': {
schema,
},
};
return this;
}
setDescription(description: string) {
this.requestBody.description = description;
return this;
}
get() {
return { ...this.requestBody };
}
}
export class ResponseBuilder {
private response: Response = {
description: '',
};
jsonContent(schema: Schema) {
this.response.content = {
'application/json': {
schema,
},
};
return this;
}
setDescription(description: string) {
this.response.description = description;
return this;
}
setHeader(headers: { [name: string]: Header }) {
this.response.headers = headers;
return this;
}
get() {
return this.response;
}
}
export class SpecBuilder {
private spec: Swagger.Spec = {
info: {
title: '',
},
tags: [],
servers: [],
openapi: '3.0.3',
components: {},
paths: {},
};
public setTitle(title: string): this {
this.spec.info.title = title;
return this;
}
public setDescription(description: string): this {
this.spec.info.description = description;
return this;
}
public setVersion(version: string): this {
this.spec.info.version = version;
return this;
}
public setTermsOfService(termsOfService: string): this {
this.spec.info.termsOfService = termsOfService;
return this;
}
public setContact(name: string, url: string, email: string): this {
this.spec.info.contact = { name, url, email };
return this;
}
public setLicense(name: string, url: string): this {
this.spec.info.license = { name, url };
return this;
}
public addServer(
url: string,
description?: string,
variables?: Record<string, ServerVariable>,
): this {
this.spec.servers.push({ url, description, variables });
return this;
}
public setExternalDoc(description: string, url: string): this {
this.spec.externalDocs = { description, url };
return this;
}
public addTag(
name: string,
description = '',
externalDocs?: Swagger.ExternalDocs,
): this {
const tag: Swagger.Tag = {
name,
description,
};
if (externalDocs) {
tag.externalDocs = externalDocs;
}
this.spec.tags!.push(tag);
return this;
}
public addSecurity(name: string, options: Swagger.SecurityScheme): this {
this.spec.components.securitySchemes = {
...this.spec.components.securitySchemes,
[name]: options,
};
return this;
}
public addSecurityRequirements(
name: string | SecurityRequirementObject,
requirements: string[] = [],
): this {
let securityRequirement: SecurityRequirementObject;
if (typeof name === 'string') {
securityRequirement = { [name]: requirements };
} else {
securityRequirement = name;
}
this.spec.security = {
...this.spec.security,
...securityRequirement,
};
return this;
}
public addBearerAuth(
options: Partial<Swagger.SecurityScheme> = {
type: 'http',
},
name = 'bearer',
): this {
this.addSecurity(name, {
...options,
scheme: 'bearer',
bearerFormat: 'JWT',
} as Swagger.SecurityScheme);
return this;
}
public addOAuth2(
options: Partial<Swagger.SecurityScheme> = {
type: 'oauth2',
},
name = 'oauth2',
): this {
this.addSecurity(name, {
...options,
type: 'oauth2',
flows: {
implicit: {
scopes: {},
},
},
});
return this;
}
public addApiKey(
options: Partial<Swagger.SecurityScheme> = {
type: 'apiKey',
},
name = 'api_key',
): this {
this.addSecurity(name, {
...options,
type: 'apiKey',
in: 'header',
name,
});
return this;
}
public addBasicAuth(
options: Partial<Swagger.SecurityScheme> = {
type: 'http',
},
name = 'basic',
): this {
this.addSecurity(name, {
...options,
type: 'http',
scheme: 'basic',
});
return this;
}
public addCookieAuth(
cookieName = 'connect.sid',
options: Partial<Swagger.SecurityScheme> = {
type: 'apiKey',
},
securityName = 'cookie',
): this {
this.addSecurity(securityName, {
...options,
type: 'apiKey',
in: 'cookie',
name: cookieName,
});
return this;
}
public build() {
return this.spec;
}
}