1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.DocumentBuilder = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const lodash_1 = require("lodash");
|
6 | const document_base_1 = require("./fixtures/document.base");
|
7 | class DocumentBuilder {
|
8 | constructor() {
|
9 | this.logger = new common_1.Logger(DocumentBuilder.name);
|
10 | this.document = document_base_1.buildDocumentBase();
|
11 | }
|
12 | setTitle(title) {
|
13 | this.document.info.title = title;
|
14 | return this;
|
15 | }
|
16 | setDescription(description) {
|
17 | this.document.info.description = description;
|
18 | return this;
|
19 | }
|
20 | setVersion(version) {
|
21 | this.document.info.version = version;
|
22 | return this;
|
23 | }
|
24 | setTermsOfService(termsOfService) {
|
25 | this.document.info.termsOfService = termsOfService;
|
26 | return this;
|
27 | }
|
28 | setContact(name, url, email) {
|
29 | this.document.info.contact = { name, url, email };
|
30 | return this;
|
31 | }
|
32 | setLicense(name, url) {
|
33 | this.document.info.license = { name, url };
|
34 | return this;
|
35 | }
|
36 | addServer(url, description, variables) {
|
37 | this.document.servers.push({ url, description, variables });
|
38 | return this;
|
39 | }
|
40 | setExternalDoc(description, url) {
|
41 | this.document.externalDocs = { description, url };
|
42 | return this;
|
43 | }
|
44 | setBasePath(path) {
|
45 | 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.');
|
46 | return this;
|
47 | }
|
48 | addTag(name, description = '', externalDocs) {
|
49 | this.document.tags = this.document.tags.concat(lodash_1.pickBy({
|
50 | name,
|
51 | description,
|
52 | externalDocs
|
53 | }, lodash_1.negate(lodash_1.isUndefined)));
|
54 | return this;
|
55 | }
|
56 | addSecurity(name, options) {
|
57 | this.document.components.securitySchemes = Object.assign(Object.assign({}, (this.document.components.securitySchemes || {})), { [name]: options });
|
58 | return this;
|
59 | }
|
60 | addSecurityRequirements(name, requirements = []) {
|
61 | let securityRequirement;
|
62 | if (lodash_1.isString(name)) {
|
63 | securityRequirement = { [name]: requirements };
|
64 | }
|
65 | else {
|
66 | securityRequirement = name;
|
67 | }
|
68 | this.document.security = (this.document.security || []).concat(Object.assign({}, securityRequirement));
|
69 | return this;
|
70 | }
|
71 | addBearerAuth(options = {
|
72 | type: 'http'
|
73 | }, name = 'bearer') {
|
74 | this.addSecurity(name, Object.assign({ scheme: 'bearer', bearerFormat: 'JWT' }, options));
|
75 | return this;
|
76 | }
|
77 | addOAuth2(options = {
|
78 | type: 'oauth2'
|
79 | }, name = 'oauth2') {
|
80 | this.addSecurity(name, Object.assign({ type: 'oauth2', flows: {} }, options));
|
81 | return this;
|
82 | }
|
83 | addApiKey(options = {
|
84 | type: 'apiKey'
|
85 | }, name = 'api_key') {
|
86 | this.addSecurity(name, Object.assign({ type: 'apiKey', in: 'header', name }, options));
|
87 | return this;
|
88 | }
|
89 | addBasicAuth(options = {
|
90 | type: 'http'
|
91 | }, name = 'basic') {
|
92 | this.addSecurity(name, Object.assign({ type: 'http', scheme: 'basic' }, options));
|
93 | return this;
|
94 | }
|
95 | addCookieAuth(cookieName = 'connect.sid', options = {
|
96 | type: 'apiKey'
|
97 | }, securityName = 'cookie') {
|
98 | this.addSecurity(securityName, Object.assign({ type: 'apiKey', in: 'cookie', name: cookieName }, options));
|
99 | return this;
|
100 | }
|
101 | build() {
|
102 | return this.document;
|
103 | }
|
104 | }
|
105 | exports.DocumentBuilder = DocumentBuilder;
|