-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.ts
156 lines (150 loc) · 3.07 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
import { TObject } from "./deps.ts";
import { TInfo, TOpenApi, TSecurity, TTagObject } from "./types.ts";
export class DocumentBuilder {
private _doc: TOpenApi = {
openapi: "3.0.0",
info: {
title: "",
description: "",
version: "1.0.0",
contact: {},
},
tags: [],
servers: [],
components: {
schemas: {},
},
definitions: {},
};
public setHost(str: string) {
this._doc.host = str;
return this;
}
public addSchemes(str: string) {
this._doc.schemes = (this._doc.schemes || []).concat([str]);
return this;
}
public addTags(object: TTagObject) {
this._doc.tags = (this._doc.tags || []).concat([object]);
return this;
}
public useSwagger(version: string = "2.0.0") {
this._doc.swagger = version;
if (this._doc.openapi) {
delete this._doc.openapi;
}
return this;
}
public useOpenApi(version: string = "3.0.0") {
this._doc.openapi = version;
if (this._doc.swagger) {
delete this._doc.swagger;
}
return this;
}
public setInfo(info: TInfo) {
this._doc.info = info;
return this;
}
public addServer(
url: string,
description?: string,
variables?: TObject,
) {
this._doc.servers.push({ url, description, variables });
return this;
}
public setExternalDoc(description: string, url: string) {
this._doc.externalDocs = { description, url };
return this;
}
public addSecurity(name: string, opts: TSecurity) {
this._doc.components.securitySchemes = {
...(this._doc.components.securitySchemes || {}),
[name]: opts,
};
return this;
}
public addSecurityRequirements(
name: string | TObject,
requirements: string[] = [],
) {
let obj: TObject;
if (typeof name === "string") {
obj = { [name]: requirements };
} else {
obj = name;
}
this._doc.security = (this._doc.security || []).concat({
...obj,
});
return this;
}
public addBearerAuth(
options: TSecurity = {
type: "http",
},
name = "bearerAuth",
) {
this.addSecurity(name, {
scheme: "bearer",
bearerFormat: "JWT",
...options,
});
return this;
}
public addOAuth2(
options: TSecurity = {
type: "oauth2",
},
name = "oauth2",
) {
this.addSecurity(name, {
flows: {},
...options,
});
return this;
}
public addApiKey(
options: TSecurity = {
type: "apiKey",
},
name = "api_key",
) {
this.addSecurity(name, {
in: "header",
name,
...options,
});
return this;
}
public addBasicAuth(
options: TSecurity = {
type: "http",
},
name = "basicAuth",
) {
this.addSecurity(name, {
scheme: "basic",
...options,
});
return this;
}
public addCookieAuth(
cookieName = "connect.sid",
options: TSecurity = {
type: "apiKey",
},
name = "cookieAuth",
) {
this.addSecurity(name, {
in: "cookie",
name: cookieName,
...options,
});
return this;
}
public build() {
return this._doc;
}
}