@visulima/jsdoc-open-api 3.0.0-alpha.9 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +436 -0
  2. package/LICENSE.md +34 -0
  3. package/README.md +44 -33
  4. package/bin/index.js +12 -9
  5. package/dist/cli/commander/index.d.ts +1000 -2
  6. package/dist/cli/commander/index.js +1 -0
  7. package/dist/cli/index.d.ts +9 -2
  8. package/dist/cli/index.js +1 -0
  9. package/dist/index.d.ts +266 -7
  10. package/dist/index.js +1 -0
  11. package/dist/packem_shared/DEFAULT_OPTIONS-C7o6qjwP.js +1 -0
  12. package/dist/packem_shared/SpecBuilder-Vbq42UbW.js +1 -0
  13. package/dist/packem_shared/SwaggerCompilerPlugin-PUOALp2i.js +1 -0
  14. package/dist/packem_shared/constants-CdEv9ZcD.js +1 -0
  15. package/dist/packem_shared/customizer-BXFVpImq.js +1 -0
  16. package/dist/packem_shared/generateCommand-C_XPsbS6.js +5 -0
  17. package/dist/packem_shared/generateCommand-ClL9Mnc3.js +1 -0
  18. package/dist/packem_shared/initCommand-Ca1hGdYS.js +35 -0
  19. package/dist/packem_shared/initCommand-TixxsVQU.js +1 -0
  20. package/dist/packem_shared/jsDocumentCommentsToOpenApi-TjPhUj5G.js +1 -0
  21. package/dist/packem_shared/loadDefinition-7r9qpfuE.js +1 -0
  22. package/dist/packem_shared/parseFile-RDlR4tEF.js +1 -0
  23. package/dist/packem_shared/swaggerJsDocumentCommentsToOpenApi-Bm9R1BnW.js +10 -0
  24. package/dist/packem_shared/validate-9Rwe2A3r.js +1 -0
  25. package/dist/packem_shared/yamlLoc-DPq-xUM4.js +1 -0
  26. package/package.json +54 -74
  27. package/dist/cli/command/generate-command.d.cts +0 -7
  28. package/dist/cli/command/generate-command.d.mts +0 -7
  29. package/dist/cli/command/generate-command.d.ts +0 -7
  30. package/dist/cli/command/init-command.d.cts +0 -2
  31. package/dist/cli/command/init-command.d.mts +0 -2
  32. package/dist/cli/command/init-command.d.ts +0 -2
  33. package/dist/cli/commander/command/generate-command.d.cts +0 -3
  34. package/dist/cli/commander/command/generate-command.d.mts +0 -3
  35. package/dist/cli/commander/command/generate-command.d.ts +0 -3
  36. package/dist/cli/commander/command/init-command.d.cts +0 -3
  37. package/dist/cli/commander/command/init-command.d.mts +0 -3
  38. package/dist/cli/commander/command/init-command.d.ts +0 -3
  39. package/dist/cli/commander/index.cjs +0 -11
  40. package/dist/cli/commander/index.d.cts +0 -2
  41. package/dist/cli/commander/index.d.mts +0 -2
  42. package/dist/cli/commander/index.mjs +0 -2
  43. package/dist/cli/index.cjs +0 -11
  44. package/dist/cli/index.d.cts +0 -2
  45. package/dist/cli/index.d.mts +0 -2
  46. package/dist/cli/index.mjs +0 -2
  47. package/dist/constants.d.cts +0 -1
  48. package/dist/constants.d.mts +0 -1
  49. package/dist/constants.d.ts +0 -1
  50. package/dist/exported.d.cts +0 -198
  51. package/dist/exported.d.mts +0 -198
  52. package/dist/exported.d.ts +0 -198
  53. package/dist/index.cjs +0 -19
  54. package/dist/index.d.cts +0 -7
  55. package/dist/index.d.mts +0 -7
  56. package/dist/index.mjs +0 -6
  57. package/dist/jsdoc/comments-to-open-api.d.cts +0 -6
  58. package/dist/jsdoc/comments-to-open-api.d.mts +0 -6
  59. package/dist/jsdoc/comments-to-open-api.d.ts +0 -6
  60. package/dist/packem_shared/SpecBuilder-B7YiF7zR.mjs +0 -61
  61. package/dist/packem_shared/SpecBuilder-CCESkmm5.cjs +0 -63
  62. package/dist/packem_shared/SwaggerCompilerPlugin-BkYJzU4f.cjs +0 -88
  63. package/dist/packem_shared/SwaggerCompilerPlugin-DZ7tIJ8x.mjs +0 -86
  64. package/dist/packem_shared/customizer-CoMXMU7a.mjs +0 -8
  65. package/dist/packem_shared/customizer-DDFwFqJl.cjs +0 -10
  66. package/dist/packem_shared/generateCommand-DRMkfIle.mjs +0 -15
  67. package/dist/packem_shared/generateCommand-MwnCNgEE.cjs +0 -82
  68. package/dist/packem_shared/generateCommand-RY4QFtm7.cjs +0 -17
  69. package/dist/packem_shared/generateCommand-a9_RAbug.mjs +0 -80
  70. package/dist/packem_shared/initCommand-B6Z9EXHP.mjs +0 -15
  71. package/dist/packem_shared/initCommand-Ch-72dKZ.cjs +0 -65
  72. package/dist/packem_shared/initCommand-D3yVEMwO.cjs +0 -17
  73. package/dist/packem_shared/initCommand-zE4VRDGW.mjs +0 -63
  74. package/dist/packem_shared/jsDocumentCommentsToOpenApi-C7yMeXJ5.cjs +0 -337
  75. package/dist/packem_shared/jsDocumentCommentsToOpenApi-DtrIZ2lK.mjs +0 -331
  76. package/dist/packem_shared/parseFile-BE-sKiRK.mjs +0 -35
  77. package/dist/packem_shared/parseFile-Dk7Bw7lt.cjs +0 -42
  78. package/dist/packem_shared/swaggerJsDocumentCommentsToOpenApi-B7g9rVr5.mjs +0 -131
  79. package/dist/packem_shared/swaggerJsDocumentCommentsToOpenApi-BuB7eE94.cjs +0 -138
  80. package/dist/packem_shared/validate-DZHaKH07.mjs +0 -30
  81. package/dist/packem_shared/validate-FKieC3ln.cjs +0 -37
  82. package/dist/packem_shared/yamlLoc-C7iHWvdq.mjs +0 -14
  83. package/dist/packem_shared/yamlLoc-CEvBiR4K.cjs +0 -16
  84. package/dist/parse-file.d.cts +0 -9
  85. package/dist/parse-file.d.mts +0 -9
  86. package/dist/parse-file.d.ts +0 -9
  87. package/dist/spec-builder.d.cts +0 -14
  88. package/dist/spec-builder.d.mts +0 -14
  89. package/dist/spec-builder.d.ts +0 -14
  90. package/dist/swagger-jsdoc/comments-to-open-api.d.cts +0 -6
  91. package/dist/swagger-jsdoc/comments-to-open-api.d.mts +0 -6
  92. package/dist/swagger-jsdoc/comments-to-open-api.d.ts +0 -6
  93. package/dist/swagger-jsdoc/organize-swagger-object.d.cts +0 -7
  94. package/dist/swagger-jsdoc/organize-swagger-object.d.mts +0 -7
  95. package/dist/swagger-jsdoc/organize-swagger-object.d.ts +0 -7
  96. package/dist/swagger-jsdoc/utils.d.cts +0 -22
  97. package/dist/swagger-jsdoc/utils.d.mts +0 -22
  98. package/dist/swagger-jsdoc/utils.d.ts +0 -22
  99. package/dist/util/customizer.d.cts +0 -2
  100. package/dist/util/customizer.d.mts +0 -2
  101. package/dist/util/customizer.d.ts +0 -2
  102. package/dist/util/object-merge.d.cts +0 -2
  103. package/dist/util/object-merge.d.mts +0 -2
  104. package/dist/util/object-merge.d.ts +0 -2
  105. package/dist/util/yaml-loc.d.cts +0 -2
  106. package/dist/util/yaml-loc.d.mts +0 -2
  107. package/dist/util/yaml-loc.d.ts +0 -2
  108. package/dist/validate.d.cts +0 -2
  109. package/dist/validate.d.mts +0 -2
  110. package/dist/validate.d.ts +0 -2
  111. package/dist/webpack/swagger-compiler-plugin.d.cts +0 -15
  112. package/dist/webpack/swagger-compiler-plugin.d.mts +0 -15
  113. package/dist/webpack/swagger-compiler-plugin.d.ts +0 -15
@@ -1,331 +0,0 @@
1
- import { parse } from 'comment-parser';
2
- import mergeWith from 'lodash.mergewith';
3
- import { c as customizer } from './customizer-CoMXMU7a.mjs';
4
-
5
- const ARRAY_SUFFIX_REGEX = /\[\]$/;
6
- const DESCRIPTION_DASH_REGEX = /^- /u;
7
- const PARAM_SUFFIX_REGEX = /Param$/u;
8
- const OPEN_API_REGEX = /^(GET|PUT|POST|DELETE|OPTIONS|HEAD|PATCH|TRACE) \/.*$/;
9
- const fixSecurityObject = (thing) => {
10
- if (thing.security) {
11
- thing.security = Object.keys(thing.security).map((s) => {
12
- return {
13
- [s]: thing.security[s]
14
- };
15
- });
16
- }
17
- };
18
- const primitiveTypes = /* @__PURE__ */ new Set(["array", "boolean", "integer", "number", "object", "string"]);
19
- const formatMap = {
20
- binary: "string",
21
- byte: "string",
22
- date: "string",
23
- "date-time": "string",
24
- double: "number",
25
- float: "number",
26
- int32: "integer",
27
- int64: "integer",
28
- password: "string"
29
- };
30
- const parseDescription = (tag) => {
31
- const rawType = tag.type;
32
- const isArray = rawType.endsWith("[]");
33
- const parsedType = rawType.replace(ARRAY_SUFFIX_REGEX, "");
34
- const isPrimitive = primitiveTypes.has(parsedType);
35
- const isFormat = Object.keys(formatMap).includes(parsedType);
36
- let defaultValue;
37
- if (tag.default) {
38
- switch (parsedType) {
39
- case "double":
40
- case "float":
41
- case "number": {
42
- defaultValue = Number.parseFloat(tag.default);
43
- break;
44
- }
45
- case "int32":
46
- case "int64":
47
- case "integer": {
48
- defaultValue = Number.parseInt(tag.default, 10);
49
- break;
50
- }
51
- default: {
52
- defaultValue = tag.default;
53
- break;
54
- }
55
- }
56
- }
57
- let rootType;
58
- if (isPrimitive) {
59
- rootType = { default: defaultValue, type: parsedType };
60
- } else if (isFormat) {
61
- rootType = {
62
- default: defaultValue,
63
- format: parsedType,
64
- type: formatMap[parsedType]
65
- };
66
- } else {
67
- rootType = { $ref: `#/components/schemas/${parsedType}` };
68
- }
69
- let schema = isArray ? {
70
- items: {
71
- ...rootType
72
- },
73
- type: "array"
74
- } : {
75
- ...rootType
76
- };
77
- if (parsedType === "") {
78
- schema = void 0;
79
- }
80
- let description = tag.description.trim().replace(DESCRIPTION_DASH_REGEX, "");
81
- if (description === "") {
82
- description = void 0;
83
- }
84
- return {
85
- description,
86
- name: tag.name,
87
- rawType,
88
- required: !tag.optional,
89
- schema
90
- };
91
- };
92
- const tagsToObjects = (tags, _verbose) => tags.map((tag) => {
93
- const parsedResponse = parseDescription(tag);
94
- let nameAndDescription = "";
95
- if (parsedResponse.name) {
96
- nameAndDescription += parsedResponse.name;
97
- }
98
- if (parsedResponse.description) {
99
- nameAndDescription += ` ${parsedResponse.description.trim()}`;
100
- }
101
- switch (tag.tag) {
102
- case "bodyComponent": {
103
- return {
104
- requestBody: {
105
- $ref: `#/components/requestBodies/${parsedResponse.rawType}`
106
- }
107
- };
108
- }
109
- case "bodyContent": {
110
- return {
111
- requestBody: {
112
- content: {
113
- [parsedResponse.name.replace(String.raw`*\/*`, "*/*")]: {
114
- schema: parsedResponse.schema
115
- }
116
- }
117
- }
118
- };
119
- }
120
- case "bodyDescription": {
121
- return { requestBody: { description: nameAndDescription } };
122
- }
123
- case "bodyExample": {
124
- const [contentType, example] = parsedResponse.name.split(".");
125
- return {
126
- requestBody: {
127
- content: {
128
- [contentType]: {
129
- examples: {
130
- [example]: {
131
- $ref: `#/components/examples/${parsedResponse.rawType}`
132
- }
133
- }
134
- }
135
- }
136
- }
137
- };
138
- }
139
- case "bodyRequired": {
140
- return { requestBody: { required: true } };
141
- }
142
- case "callback": {
143
- return {
144
- callbacks: {
145
- [parsedResponse.name]: {
146
- $ref: `#/components/callbacks/${parsedResponse.rawType}`
147
- }
148
- }
149
- };
150
- }
151
- case "cookieParam":
152
- case "headerParam":
153
- case "pathParam":
154
- case "queryParam": {
155
- return {
156
- parameters: [
157
- {
158
- description: parsedResponse.description,
159
- in: tag.tag.replace(PARAM_SUFFIX_REGEX, ""),
160
- name: parsedResponse.name,
161
- required: parsedResponse.required,
162
- schema: parsedResponse.schema
163
- }
164
- ]
165
- };
166
- }
167
- case "deprecated": {
168
- return { deprecated: true };
169
- }
170
- case "description":
171
- case "operationId":
172
- case "summary": {
173
- return { [tag.tag]: nameAndDescription };
174
- }
175
- case "externalDocs": {
176
- return {
177
- externalDocs: {
178
- description: parsedResponse.description,
179
- url: parsedResponse.name
180
- }
181
- };
182
- }
183
- case "paramComponent": {
184
- return {
185
- parameters: [{ $ref: `#/components/parameters/${parsedResponse.rawType}` }]
186
- };
187
- }
188
- case "response": {
189
- return {
190
- responses: {
191
- [parsedResponse.name]: {
192
- description: parsedResponse.description
193
- }
194
- }
195
- };
196
- }
197
- case "responseComponent": {
198
- return {
199
- responses: {
200
- [parsedResponse.name]: {
201
- $ref: `#/components/responses/${parsedResponse.rawType}`
202
- }
203
- }
204
- };
205
- }
206
- case "responseContent": {
207
- const [status, contentType] = parsedResponse.name.split(".");
208
- return {
209
- responses: {
210
- [status]: {
211
- content: {
212
- [contentType]: {
213
- schema: parsedResponse.schema
214
- }
215
- }
216
- }
217
- }
218
- };
219
- }
220
- case "responseExample": {
221
- const [status, contentType, example] = parsedResponse.name.split(".");
222
- return {
223
- responses: {
224
- [status]: {
225
- content: {
226
- [contentType]: {
227
- examples: {
228
- [example]: {
229
- $ref: `#/components/examples/${parsedResponse.rawType}`
230
- }
231
- }
232
- }
233
- }
234
- }
235
- }
236
- };
237
- }
238
- case "responseHeader": {
239
- const [status, header] = parsedResponse.name.split(".");
240
- return {
241
- responses: {
242
- [status]: {
243
- headers: {
244
- [header]: {
245
- description: parsedResponse.description,
246
- schema: parsedResponse.schema
247
- }
248
- }
249
- }
250
- }
251
- };
252
- }
253
- case "responseHeaderComponent": {
254
- const [status, header] = parsedResponse.name.split(".");
255
- return {
256
- responses: {
257
- [status]: {
258
- headers: {
259
- [header]: {
260
- $ref: `#/components/headers/${parsedResponse.rawType}`
261
- }
262
- }
263
- }
264
- }
265
- };
266
- }
267
- case "responseLink": {
268
- const [status, link] = parsedResponse.name.split(".");
269
- return {
270
- responses: {
271
- [status]: {
272
- links: {
273
- [link]: {
274
- $ref: `#/components/links/${parsedResponse.rawType}`
275
- }
276
- }
277
- }
278
- }
279
- };
280
- }
281
- case "security": {
282
- const [security, scopeItem] = parsedResponse.name.split(".");
283
- let scope = [];
284
- if (scopeItem) {
285
- scope = [scopeItem];
286
- }
287
- return {
288
- security: { [security]: scope }
289
- };
290
- }
291
- case "server": {
292
- return {
293
- servers: [
294
- {
295
- description: parsedResponse.description,
296
- url: parsedResponse.name
297
- }
298
- ]
299
- };
300
- }
301
- case "tag": {
302
- return { tags: [nameAndDescription] };
303
- }
304
- default: {
305
- return {};
306
- }
307
- }
308
- });
309
- const commentsToOpenApi = (fileContents, verbose) => {
310
- const jsDocumentComments = parse(fileContents, { spacing: "preserve" });
311
- return jsDocumentComments.filter((comment) => OPEN_API_REGEX.test(comment.description.trim())).map((comment) => {
312
- const loc = comment.tags.length + 1;
313
- const result = mergeWith({}, ...tagsToObjects(comment.tags), customizer);
314
- fixSecurityObject(result);
315
- const [method, path] = comment.description.split(" ");
316
- const pathsObject = {
317
- [path.trim()]: {
318
- [method.toLowerCase().trim()]: {
319
- ...result
320
- }
321
- }
322
- };
323
- const spec = JSON.parse(JSON.stringify({ paths: pathsObject }));
324
- return {
325
- loc,
326
- spec
327
- };
328
- });
329
- };
330
-
331
- export { commentsToOpenApi as default };
@@ -1,35 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import path from 'node:path';
3
- import yaml from 'yaml';
4
- import yamlLoc from './yamlLoc-C7iHWvdq.mjs';
5
-
6
- const ALLOWED_KEYS = /* @__PURE__ */ new Set(["components", "externalDocs", "info", "openapi", "paths", "security", "servers", "tags"]);
7
- class ParseError extends Error {
8
- filePath;
9
- }
10
- const parseFile = (file, commentsToOpenApi, verbose) => {
11
- const fileContent = readFileSync(file, { encoding: "utf8" });
12
- const extension = path.extname(file);
13
- if (extension === ".yaml" || extension === ".yml") {
14
- const spec = yaml.parse(fileContent);
15
- const invalidKeys = Object.keys(spec).filter((key) => !ALLOWED_KEYS.has(key));
16
- if (invalidKeys.length > 0) {
17
- const error = new ParseError(`Unexpected keys: ${invalidKeys.join(", ")}`);
18
- error.filePath = file;
19
- throw error;
20
- }
21
- if (Object.keys(spec).some((key) => ALLOWED_KEYS.has(key))) {
22
- const loc = yamlLoc(fileContent);
23
- return [{ loc, spec }];
24
- }
25
- return [];
26
- }
27
- try {
28
- return commentsToOpenApi(fileContent, verbose);
29
- } catch (error) {
30
- error.filePath = file;
31
- throw error;
32
- }
33
- };
34
-
35
- export { parseFile as default };
@@ -1,42 +0,0 @@
1
- 'use strict';
2
-
3
- const node_fs = require('node:fs');
4
- const path = require('node:path');
5
- const yaml = require('yaml');
6
- const yamlLoc = require('./yamlLoc-CEvBiR4K.cjs');
7
-
8
- const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
9
-
10
- const path__default = /*#__PURE__*/_interopDefaultCompat(path);
11
- const yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml);
12
-
13
- const ALLOWED_KEYS = /* @__PURE__ */ new Set(["components", "externalDocs", "info", "openapi", "paths", "security", "servers", "tags"]);
14
- class ParseError extends Error {
15
- filePath;
16
- }
17
- const parseFile = (file, commentsToOpenApi, verbose) => {
18
- const fileContent = node_fs.readFileSync(file, { encoding: "utf8" });
19
- const extension = path__default.extname(file);
20
- if (extension === ".yaml" || extension === ".yml") {
21
- const spec = yaml__default.parse(fileContent);
22
- const invalidKeys = Object.keys(spec).filter((key) => !ALLOWED_KEYS.has(key));
23
- if (invalidKeys.length > 0) {
24
- const error = new ParseError(`Unexpected keys: ${invalidKeys.join(", ")}`);
25
- error.filePath = file;
26
- throw error;
27
- }
28
- if (Object.keys(spec).some((key) => ALLOWED_KEYS.has(key))) {
29
- const loc = yamlLoc(fileContent);
30
- return [{ loc, spec }];
31
- }
32
- return [];
33
- }
34
- try {
35
- return commentsToOpenApi(fileContent, verbose);
36
- } catch (error) {
37
- error.filePath = file;
38
- throw error;
39
- }
40
- };
41
-
42
- module.exports = parseFile;
@@ -1,131 +0,0 @@
1
- import { parse } from 'comment-parser';
2
- import mergeWith from 'lodash.mergewith';
3
- import yaml from 'yaml';
4
- import { c as customizer } from './customizer-CoMXMU7a.mjs';
5
-
6
- const mergeDeep = (first, second) => mergeWith({}, first, second, (a, b) => {
7
- if (b === null) {
8
- return a;
9
- }
10
- return void 0;
11
- });
12
- const hasEmptyProperty = (object) => Object.keys(object).map((key) => object[key]).every((keyObject) => typeof keyObject === "object" && Object.keys(keyObject).every((key) => !(key in keyObject)));
13
- const isTagPresentInTags = (tag, tags) => tags.some((targetTag) => tag.name === targetTag.name);
14
- const getSwaggerVersionFromSpec = (tag) => {
15
- switch (tag.tag) {
16
- case "asyncapi": {
17
- return "v4";
18
- }
19
- case "openapi": {
20
- return "v3";
21
- }
22
- case "swagger": {
23
- return "v2";
24
- }
25
- default: {
26
- return "v2";
27
- }
28
- }
29
- };
30
-
31
- const organizeSwaggerObject = (swaggerObject, annotation, property) => {
32
- if (property === "x-webhooks") {
33
- swaggerObject[property] = annotation[property];
34
- }
35
- if (property.startsWith("x-")) {
36
- return;
37
- }
38
- const commonProperties = [
39
- "components",
40
- "consumes",
41
- "produces",
42
- "paths",
43
- "schemas",
44
- "securityDefinitions",
45
- "responses",
46
- "parameters",
47
- "definitions",
48
- "channels"
49
- ];
50
- if (commonProperties.includes(property)) {
51
- Object.keys(annotation[property]).forEach((definition) => {
52
- swaggerObject[property][definition] = mergeDeep(swaggerObject[property][definition], annotation[property][definition]);
53
- });
54
- } else if (property === "tags") {
55
- const { tags } = annotation;
56
- if (Array.isArray(tags)) {
57
- tags.forEach((tag) => {
58
- if (!isTagPresentInTags(tag, swaggerObject.tags)) {
59
- swaggerObject.tags.push(tag);
60
- }
61
- });
62
- } else if (!isTagPresentInTags(tags, swaggerObject.tags)) {
63
- swaggerObject.tags.push(tags);
64
- }
65
- } else if (property === "security") {
66
- const { security } = annotation;
67
- swaggerObject.security = security;
68
- } else if (property.startsWith("/")) {
69
- swaggerObject.paths[property] = mergeDeep(swaggerObject.paths[property], annotation[property]);
70
- }
71
- };
72
-
73
- const specificationTemplate = {
74
- v2: ["paths", "definitions", "responses", "parameters", "securityDefinitions"],
75
- v3: ["paths", "definitions", "responses", "parameters", "securityDefinitions", "components"],
76
- v4: ["components", "channels"]
77
- };
78
- const tagsToObjects = (specs, verbose) => specs.map((spec) => {
79
- const hasContent = spec.description !== "" || spec.name.startsWith("/") || spec.name.endsWith(":");
80
- if ((spec.tag === "openapi" || spec.tag === "swagger" || spec.tag === "asyncapi") && hasContent) {
81
- let yamlContent = spec.description;
82
- if (spec.name.startsWith("/") || spec.name.endsWith(":")) {
83
- yamlContent = yamlContent.trim() === "" ? spec.name : `${spec.name}
84
- ${yamlContent}`;
85
- }
86
- const parsed = yaml.parseDocument(yamlContent);
87
- if (parsed.errors.length > 0) {
88
- parsed.errors.forEach((error) => {
89
- error.annotation = yamlContent;
90
- });
91
- let errorString = "Error parsing YAML in @openapi spec:";
92
- errorString += verbose ? parsed.errors.map((error) => `${error.toString()}
93
- Imbedded within:
94
- \`\`\`
95
- ${error.annotation?.replaceAll("\n", "\n ")}
96
- \`\`\``).join("\n") : parsed.errors.map((error) => error.toString()).join("\n");
97
- throw new Error(errorString);
98
- }
99
- const parsedDocument = parsed.toJSON();
100
- const specification = {
101
- tags: []
102
- };
103
- specificationTemplate[getSwaggerVersionFromSpec(spec)].forEach((property) => {
104
- specification[property] = specification[property] ?? {};
105
- });
106
- Object.keys(parsedDocument).forEach((property) => {
107
- organizeSwaggerObject(specification, parsedDocument, property);
108
- });
109
- return specification;
110
- }
111
- return {};
112
- });
113
- const commentsToOpenApi = (fileContents, verbose) => {
114
- const jsDocumentComments = parse(fileContents, { spacing: "preserve" });
115
- return jsDocumentComments.map((comment) => {
116
- const loc = comment.tags.length + 1;
117
- const result = mergeWith({}, ...tagsToObjects(comment.tags, verbose), customizer);
118
- ["definitions", "responses", "parameters", "securityDefinitions", "components", "tags"].forEach((property) => {
119
- if (result[property] !== void 0 && hasEmptyProperty(result[property])) {
120
- delete result[property];
121
- }
122
- });
123
- const spec = JSON.parse(JSON.stringify(result));
124
- return {
125
- loc,
126
- spec
127
- };
128
- });
129
- };
130
-
131
- export { commentsToOpenApi as default };
@@ -1,138 +0,0 @@
1
- 'use strict';
2
-
3
- const commentParser = require('comment-parser');
4
- const mergeWith = require('lodash.mergewith');
5
- const yaml = require('yaml');
6
- const customizer = require('./customizer-DDFwFqJl.cjs');
7
-
8
- const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
9
-
10
- const mergeWith__default = /*#__PURE__*/_interopDefaultCompat(mergeWith);
11
- const yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml);
12
-
13
- const mergeDeep = (first, second) => mergeWith__default({}, first, second, (a, b) => {
14
- if (b === null) {
15
- return a;
16
- }
17
- return void 0;
18
- });
19
- const hasEmptyProperty = (object) => Object.keys(object).map((key) => object[key]).every((keyObject) => typeof keyObject === "object" && Object.keys(keyObject).every((key) => !(key in keyObject)));
20
- const isTagPresentInTags = (tag, tags) => tags.some((targetTag) => tag.name === targetTag.name);
21
- const getSwaggerVersionFromSpec = (tag) => {
22
- switch (tag.tag) {
23
- case "asyncapi": {
24
- return "v4";
25
- }
26
- case "openapi": {
27
- return "v3";
28
- }
29
- case "swagger": {
30
- return "v2";
31
- }
32
- default: {
33
- return "v2";
34
- }
35
- }
36
- };
37
-
38
- const organizeSwaggerObject = (swaggerObject, annotation, property) => {
39
- if (property === "x-webhooks") {
40
- swaggerObject[property] = annotation[property];
41
- }
42
- if (property.startsWith("x-")) {
43
- return;
44
- }
45
- const commonProperties = [
46
- "components",
47
- "consumes",
48
- "produces",
49
- "paths",
50
- "schemas",
51
- "securityDefinitions",
52
- "responses",
53
- "parameters",
54
- "definitions",
55
- "channels"
56
- ];
57
- if (commonProperties.includes(property)) {
58
- Object.keys(annotation[property]).forEach((definition) => {
59
- swaggerObject[property][definition] = mergeDeep(swaggerObject[property][definition], annotation[property][definition]);
60
- });
61
- } else if (property === "tags") {
62
- const { tags } = annotation;
63
- if (Array.isArray(tags)) {
64
- tags.forEach((tag) => {
65
- if (!isTagPresentInTags(tag, swaggerObject.tags)) {
66
- swaggerObject.tags.push(tag);
67
- }
68
- });
69
- } else if (!isTagPresentInTags(tags, swaggerObject.tags)) {
70
- swaggerObject.tags.push(tags);
71
- }
72
- } else if (property === "security") {
73
- const { security } = annotation;
74
- swaggerObject.security = security;
75
- } else if (property.startsWith("/")) {
76
- swaggerObject.paths[property] = mergeDeep(swaggerObject.paths[property], annotation[property]);
77
- }
78
- };
79
-
80
- const specificationTemplate = {
81
- v2: ["paths", "definitions", "responses", "parameters", "securityDefinitions"],
82
- v3: ["paths", "definitions", "responses", "parameters", "securityDefinitions", "components"],
83
- v4: ["components", "channels"]
84
- };
85
- const tagsToObjects = (specs, verbose) => specs.map((spec) => {
86
- const hasContent = spec.description !== "" || spec.name.startsWith("/") || spec.name.endsWith(":");
87
- if ((spec.tag === "openapi" || spec.tag === "swagger" || spec.tag === "asyncapi") && hasContent) {
88
- let yamlContent = spec.description;
89
- if (spec.name.startsWith("/") || spec.name.endsWith(":")) {
90
- yamlContent = yamlContent.trim() === "" ? spec.name : `${spec.name}
91
- ${yamlContent}`;
92
- }
93
- const parsed = yaml__default.parseDocument(yamlContent);
94
- if (parsed.errors.length > 0) {
95
- parsed.errors.forEach((error) => {
96
- error.annotation = yamlContent;
97
- });
98
- let errorString = "Error parsing YAML in @openapi spec:";
99
- errorString += verbose ? parsed.errors.map((error) => `${error.toString()}
100
- Imbedded within:
101
- \`\`\`
102
- ${error.annotation?.replaceAll("\n", "\n ")}
103
- \`\`\``).join("\n") : parsed.errors.map((error) => error.toString()).join("\n");
104
- throw new Error(errorString);
105
- }
106
- const parsedDocument = parsed.toJSON();
107
- const specification = {
108
- tags: []
109
- };
110
- specificationTemplate[getSwaggerVersionFromSpec(spec)].forEach((property) => {
111
- specification[property] = specification[property] ?? {};
112
- });
113
- Object.keys(parsedDocument).forEach((property) => {
114
- organizeSwaggerObject(specification, parsedDocument, property);
115
- });
116
- return specification;
117
- }
118
- return {};
119
- });
120
- const commentsToOpenApi = (fileContents, verbose) => {
121
- const jsDocumentComments = commentParser.parse(fileContents, { spacing: "preserve" });
122
- return jsDocumentComments.map((comment) => {
123
- const loc = comment.tags.length + 1;
124
- const result = mergeWith__default({}, ...tagsToObjects(comment.tags, verbose), customizer.customizer);
125
- ["definitions", "responses", "parameters", "securityDefinitions", "components", "tags"].forEach((property) => {
126
- if (result[property] !== void 0 && hasEmptyProperty(result[property])) {
127
- delete result[property];
128
- }
129
- });
130
- const spec = JSON.parse(JSON.stringify(result));
131
- return {
132
- loc,
133
- spec
134
- };
135
- });
136
- };
137
-
138
- module.exports = commentsToOpenApi;