@visulima/jsdoc-open-api 1.0.1 → 1.0.3

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## @visulima/jsdoc-open-api [1.0.3](https://github.com/visulima/visulima/compare/@visulima/jsdoc-open-api@1.0.2...@visulima/jsdoc-open-api@1.0.3) (2022-10-27)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * fixed package.json files paths ([0d21e94](https://github.com/visulima/visulima/commit/0d21e94a75e9518f7b87293706615d8fb280095c))
7
+
8
+
9
+
10
+ ### Dependencies
11
+
12
+ * **@visulima/readdir:** upgraded to 1.1.1
13
+
14
+ ## @visulima/jsdoc-open-api [1.0.2](https://github.com/visulima/visulima/compare/@visulima/jsdoc-open-api@1.0.1...@visulima/jsdoc-open-api@1.0.2) (2022-10-27)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * **jsdoc-open-api:** fixed found errors with empty data, writing swagger file to a not existing folder, added more error returns ([8cef566](https://github.com/visulima/visulima/commit/8cef566b998d37a5c0f5c32acabf8e336510ec81))
20
+ * **jsdoc-open-api:** fixed wrong options for minimatchOptions and added default exclude for the webpack plugin ([e6cba73](https://github.com/visulima/visulima/commit/e6cba73a5add43042249e0caff0d6d605f694274))
21
+
1
22
  ## @visulima/jsdoc-open-api [1.0.1](https://github.com/visulima/visulima/compare/@visulima/jsdoc-open-api@1.0.0...@visulima/jsdoc-open-api@1.0.1) (2022-10-26)
2
23
 
3
24
 
package/README.md CHANGED
@@ -11,14 +11,22 @@
11
11
 
12
12
  <div align="center">
13
13
 
14
- [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url] [![synk-image]][synk-url]
14
+ [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
15
15
 
16
16
  </div>
17
17
 
18
+ ---
19
+
18
20
  <div align="center">
19
- <sub>Built with ❤︎ by <a href="https://twitter.com/_prisis_">Daniel Bannert</a></sub>
21
+ <p>
22
+ <sup>
23
+ Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
24
+ </sup>
25
+ </p>
20
26
  </div>
21
27
 
28
+ ---
29
+
22
30
  ## Installation
23
31
 
24
32
  ```sh
@@ -39,7 +47,7 @@ Choose the syntax you want to use for your OpenAPI definitions:
39
47
 
40
48
  ![choose the syntax](./assets/swagger-difference.png)
41
49
 
42
- CLI:
50
+ ### CLI:
43
51
 
44
52
  ```bash
45
53
  # This generate the .openapirc.js config file, this command is only needed on the first run
@@ -49,6 +57,96 @@ jsdoc-open-api init
49
57
  jsdoc-open-api generate src/
50
58
  ```
51
59
 
60
+ ### As Next.js webpack plugin:
61
+
62
+ #### with-open-api.js
63
+ ```js
64
+
65
+ const path = require("node:path");
66
+ const fs = require("node:fs");
67
+ const { SwaggerCompilerPlugin } = require("@visulima/jsdoc-open-api");
68
+
69
+ /**
70
+ * @param definition {import('@visulima/jsdoc-open-api').SwaggerDefinition}
71
+ * @param sources {string[]}
72
+ * @param verbose {boolean}
73
+ * @param output {string}
74
+ *
75
+ * @returns {function(*): *&{webpack: function(Configuration, *): (Configuration)}}
76
+ */
77
+ const withOpenApi = ({ definition, sources, verbose, output = "swagger/swagger.json" }) => (nextConfig) => {
78
+ return {
79
+ ...nextConfig,
80
+ webpack: (config, options) => {
81
+ if (!options.isServer) {
82
+ return config;
83
+ }
84
+
85
+ if (output.startsWith("/")) {
86
+ output = output.slice(1);
87
+ }
88
+
89
+ if (!output.endsWith(".json")) {
90
+ throw new Error("The output path must end with .json");
91
+ }
92
+
93
+ // eslint-disable-next-line no-param-reassign
94
+ config = {
95
+ ...config,
96
+ plugins: [
97
+ // @ts-ignore
98
+ ...config.plugins,
99
+ new SwaggerCompilerPlugin(`${options.dir}/${output}`, sources.map((source) => {
100
+ const combinedPath = path.join(options.dir, source.replace("./", ""));
101
+
102
+ // Check if the path is a directory
103
+ fs.lstatSync(combinedPath).isDirectory();
104
+
105
+ return combinedPath;
106
+ }), definition, { verbose }),
107
+ ],
108
+ };
109
+
110
+ if (typeof nextConfig.webpack === "function") {
111
+ return nextConfig.webpack(config, options);
112
+ }
113
+
114
+ return config;
115
+ },
116
+ };
117
+ };
118
+
119
+ module.exports = withOpenApi;
120
+ ```
121
+
122
+ #### next.config.js
123
+ ```js
124
+ const withOpenApi = require("./with-open-api");
125
+
126
+ /** @type {import('next').NextConfig} */
127
+ const nextConfig = {
128
+ reactStrictMode: true,
129
+ swcMinify: true,
130
+ env: {
131
+ NEXT_PUBLIC_APP_ORIGIN: process.env.VERCEL_URL || "http://localhost:3001",
132
+ }
133
+ };
134
+
135
+ module.exports = withOpenApi({
136
+ definition: {
137
+ openapi: "3.0.0",
138
+ info: {
139
+ title: "My API",
140
+ version: "1.0.0",
141
+ },
142
+ },
143
+ sources: [
144
+ "pages/api",
145
+ ],
146
+ verbose: false, // default is false
147
+ })(nextConfig);
148
+ ```
149
+
52
150
  ## OpenApi yaml syntax
53
151
 
54
152
  The library will take the contents of @openapi (or @swagger):
@@ -214,7 +312,5 @@ For more information, see [Authentication](/docs/short/authentication.md).
214
312
  [typescript-url]: "typescript"
215
313
  [license-image]: https://img.shields.io/npm/l/@visulima/jsdoc-open-api?color=blueviolet&style=for-the-badge
216
314
  [license-url]: LICENSE.md "license"
217
- [npm-image]: https://img.shields.io/npm/v/@visulima/jsdoc-open-api/alpha.svg?style=for-the-badge&logo=npm
218
- [npm-url]: https://www.npmjs.com/package/@visulima/jsdoc-open-api/v/alpha "npm"
219
- [synk-image]: https://img.shields.io/snyk/vulnerabilities/github/visulima/jsdoc-open-api?label=Synk%20Vulnerabilities&style=for-the-badge
220
- [synk-url]: https://snyk.io/test/github/visulima/jsdoc-open-api?targetFile=package.json "synk"
315
+ [npm-image]: https://img.shields.io/npm/v/@visulima/jsdoc-open-api/latest.svg?style=for-the-badge&logo=npm
316
+ [npm-url]: https://www.npmjs.com/package/@visulima/jsdoc-open-api/v/latest "npm"
package/bin/index.js CHANGED
@@ -4,6 +4,8 @@ const path = require("node:path");
4
4
  // eslint-disable-next-line unicorn/prefer-module
5
5
  const fs = require("node:fs");
6
6
  // eslint-disable-next-line unicorn/prefer-module
7
+ const { exit } = require("node:process");
8
+ // eslint-disable-next-line unicorn/prefer-module
7
9
  const { Command } = require("commander");
8
10
  // eslint-disable-next-line unicorn/prefer-module
9
11
  const SwaggerParser = require("@apidevtools/swagger-parser");
@@ -13,8 +15,11 @@ const { collect } = require("@visulima/readdir");
13
15
  const cliProgress = require("cli-progress");
14
16
 
15
17
  const {
16
- jsDocumentCommentsToOpenApi, parseFile, SpecBuilder, swaggerJsDocumentCommentsToOpenApi,
17
- // eslint-disable-next-line unicorn/prefer-module
18
+ jsDocumentCommentsToOpenApi,
19
+ parseFile,
20
+ SpecBuilder,
21
+ swaggerJsDocumentCommentsToOpenApi,
22
+ // eslint-disable-next-line unicorn/prefer-module
18
23
  } = require("../dist/index.js");
19
24
 
20
25
  // eslint-disable-next-line unicorn/prefer-module,no-underscore-dangle
@@ -32,8 +37,7 @@ program
32
37
  if (fs.existsSync(defaultConfigName)) {
33
38
  // eslint-disable-next-line no-console
34
39
  console.error("Config file already exists");
35
- // eslint-disable-next-line no-undef
36
- process.exit(1);
40
+ exit(1);
37
41
  }
38
42
 
39
43
  fs.writeFileSync(
@@ -58,9 +62,8 @@ program
58
62
  '**/node_modules/**',
59
63
  '**/pnpm-lock.yaml',
60
64
  '**/pnpm-workspace.yaml',
61
- '**/package-lock.json',
65
+ '**/{package,package-lock}.json',
62
66
  '**/yarn.lock',
63
- '**/package.json',
64
67
  '**/package.json5',
65
68
  '**/.next/**',
66
69
  ],
@@ -101,8 +104,7 @@ program
101
104
  console.log("No config file found, on:", options.config || ".openapirc.js\n");
102
105
  // eslint-disable-next-line no-console
103
106
  console.error(error);
104
- // eslint-disable-next-line no-undef
105
- process.exit(1);
107
+ exit(1);
106
108
  }
107
109
 
108
110
  const multibar = new cliProgress.MultiBar(
@@ -117,6 +119,9 @@ program
117
119
 
118
120
  // eslint-disable-next-line no-restricted-syntax,unicorn/prevent-abbreviations
119
121
  for await (const dir of paths) {
122
+ // Check if the path is a directory
123
+ fs.lstatSync(dir).isDirectory();
124
+
120
125
  const files = await collect(dir, {
121
126
  // eslint-disable-next-line @rushstack/security/no-unsafe-regexp
122
127
  skip: [...openapiConfig.exclude, "node_modules/**"],
@@ -124,8 +129,14 @@ program
124
129
  followSymlinks: openapiConfig.followSymlinks || false,
125
130
  match: openapiConfig.include,
126
131
  minimatchOptions: {
127
- debug: options.verbose,
128
- matchBase: true,
132
+ match: {
133
+ debug: options.verbose,
134
+ matchBase: true,
135
+ },
136
+ skip: {
137
+ debug: options.verbose,
138
+ matchBase: true,
139
+ },
129
140
  },
130
141
  });
131
142
 
@@ -149,30 +160,65 @@ program
149
160
 
150
161
  bar.increment(1, { filename: dir });
151
162
 
152
- const parsedJsDocumentFile = parseFile(file, jsDocumentCommentsToOpenApi, this.verbose);
163
+ try {
164
+ const parsedJsDocumentFile = parseFile(file, jsDocumentCommentsToOpenApi, this.verbose);
153
165
 
154
- spec.addData(parsedJsDocumentFile.map((item) => item.spec));
166
+ spec.addData(parsedJsDocumentFile.map((item) => item.spec));
155
167
 
156
- const parsedSwaggerJsDocumentFile = parseFile(file, swaggerJsDocumentCommentsToOpenApi, this.verbose);
168
+ const parsedSwaggerJsDocumentFile = parseFile(file, swaggerJsDocumentCommentsToOpenApi, this.verbose);
157
169
 
158
- spec.addData(parsedSwaggerJsDocumentFile.map((item) => item.spec));
170
+ spec.addData(parsedSwaggerJsDocumentFile.map((item) => item.spec));
171
+ } catch (error) {
172
+ // eslint-disable-next-line no-console
173
+ console.error(error);
174
+ exit(1);
175
+ }
159
176
  });
160
177
  }
161
178
 
162
179
  try {
180
+ if (options.verbose) {
181
+ // eslint-disable-next-line no-console
182
+ console.log("Validating swagger spec");
183
+ }
184
+
185
+ if (options.veryVerbose) {
186
+ // eslint-disable-next-line no-console
187
+ console.log(JSON.stringify(spec, null, 2));
188
+ }
189
+
163
190
  await SwaggerParser.validate(JSON.parse(JSON.stringify(spec)));
164
191
  } catch (error) {
165
192
  // eslint-disable-next-line no-console
166
193
  console.error(error.toJSON());
167
- // eslint-disable-next-line no-undef
168
- process.exit();
194
+ exit(1);
169
195
  }
170
196
 
171
197
  const output = options.output || "swagger.json";
172
198
 
173
199
  multibar.stop();
174
200
 
175
- fs.writeFileSync(output, JSON.stringify(spec, null, 2));
201
+ if (options.verbose) {
202
+ // eslint-disable-next-line no-console
203
+ console.log(`Written swagger spec to "${output}" file`);
204
+ }
205
+
206
+ const errorHandler = (error) => {
207
+ if (error) {
208
+ // eslint-disable-next-line no-console
209
+ console.error(error);
210
+ exit(1);
211
+ }
212
+ };
213
+
214
+ // eslint-disable-next-line consistent-return
215
+ fs.mkdir(path.dirname(output), { recursive: true }, (error) => {
216
+ if (error) {
217
+ errorHandler(error);
218
+ }
219
+
220
+ fs.writeFile(output, JSON.stringify(spec, null, 2), errorHandler);
221
+ });
176
222
 
177
223
  // eslint-disable-next-line no-console
178
224
  console.log(`\nSwagger specification is ready, check the${output}file.`);
@@ -183,6 +229,5 @@ program.parse(process.argv);
183
229
  // eslint-disable-next-line no-undef
184
230
  if (process.argv.slice(2).length === 0) {
185
231
  program.help();
186
- // eslint-disable-next-line no-undef
187
- process.exit();
232
+ exit(1);
188
233
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Configuration } from 'webpack';
1
+ import { Compiler } from 'webpack';
2
2
 
3
3
  interface BaseDefinition {
4
4
  openapi: string;
@@ -281,13 +281,13 @@ declare class SwaggerCompilerPlugin {
281
281
  private readonly swaggerDefinition;
282
282
  private readonly sources;
283
283
  private readonly verbose;
284
- private ignore;
284
+ private readonly ignore;
285
285
  assetsPath: string;
286
286
  constructor(assetsPath: string, sources: string[], swaggerDefinition: BaseDefinition, options: {
287
287
  verbose?: boolean;
288
288
  ignore?: string | ReadonlyArray<string>;
289
289
  });
290
- apply(compiler: Configuration): void;
290
+ apply(compiler: Compiler): void;
291
291
  }
292
292
 
293
293
  declare const commentsToOpenApi$1: (fileContents: string, verbose?: boolean) => {
package/dist/index.js CHANGED
@@ -33,8 +33,8 @@ var SpecBuilder = class {
33
33
  parsedFile.forEach((file) => {
34
34
  const { paths, components, ...rest } = file;
35
35
  object_merge_default(this, {
36
- paths,
37
- components
36
+ paths: paths || {},
37
+ components: components || {}
38
38
  });
39
39
  Object.entries(rest).forEach(([key, value]) => {
40
40
  this[key] = value;
@@ -95,7 +95,8 @@ var parse_file_default = parseFile;
95
95
  // src/webpack/swagger-compiler-plugin.ts
96
96
  var _swaggerparser = require('@apidevtools/swagger-parser'); var _swaggerparser2 = _interopRequireDefault(_swaggerparser);
97
97
  var _readdir = require('@visulima/readdir');
98
- var _debug2 = require('debug'); var _debug3 = _interopRequireDefault(_debug2);
98
+
99
+
99
100
  var _process = require('process');
100
101
 
101
102
  // src/jsdoc/comments-to-open-api.ts
@@ -572,7 +573,36 @@ var commentsToOpenApi2 = (fileContents, verbose) => {
572
573
  var comments_to_open_api_default2 = commentsToOpenApi2;
573
574
 
574
575
  // src/webpack/swagger-compiler-plugin.ts
575
- var debug = _debug3.default.call(void 0, "visulima:jsdoc-open-api:swagger-compiler-plugin");
576
+ var exclude = [
577
+ "coverage/**",
578
+ ".github/**",
579
+ "packages/*/test{,s}/**",
580
+ "**/*.d.ts",
581
+ "test{,s}/**",
582
+ "test{,-*}.{js,cjs,mjs,ts,tsx,jsx,yaml,yml}",
583
+ "**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx,yaml,yml}",
584
+ "**/__tests__/**",
585
+ "**/{ava,babel,nyc}.config.{js,cjs,mjs}",
586
+ "**/jest.config.{js,cjs,mjs,ts}",
587
+ "**/{karma,rollup,webpack}.config.js",
588
+ "**/.{eslint,mocha}rc.{js,cjs}",
589
+ "**/.{travis,yarnrc}.yml",
590
+ "**/{docker-compose,docker}.yml",
591
+ "**/.yamllint.{yaml,yml}",
592
+ "**/node_modules/**",
593
+ "**/pnpm-lock.yaml",
594
+ "**/pnpm-workspace.yaml",
595
+ "**/{package,package-lock}.json",
596
+ "**/yarn.lock",
597
+ "**/package.json5",
598
+ "**/.next/**"
599
+ ];
600
+ var errorHandler = (error) => {
601
+ if (error) {
602
+ console.error(error);
603
+ _process.exit.call(void 0, 1);
604
+ }
605
+ };
576
606
  var SwaggerCompilerPlugin = class {
577
607
  constructor(assetsPath, sources, swaggerDefinition, options) {
578
608
  this.assetsPath = assetsPath;
@@ -582,40 +612,63 @@ var SwaggerCompilerPlugin = class {
582
612
  this.ignore = options.ignore || [];
583
613
  }
584
614
  apply(compiler) {
585
- compiler.hooks.make.tapAsync("SwaggerCompilerPlugin", async (compilation, callback) => {
586
- console.log("Build paused");
587
- console.log("switching to swagger build");
615
+ compiler.hooks.make.tapAsync("SwaggerCompilerPlugin", async (_, callback) => {
616
+ console.log("Build paused, switching to swagger build");
588
617
  const spec = new spec_builder_default(this.swaggerDefinition);
589
618
  for await (const dir of this.sources) {
590
619
  const files = await _readdir.collect.call(void 0, dir, {
591
- skip: [...this.ignore, "node_modules/**"]
620
+ skip: [...this.ignore, ...exclude],
621
+ extensions: [".js", ".cjs", ".mjs", ".ts", ".tsx", ".jsx", ".yaml", ".yml"],
622
+ minimatchOptions: {
623
+ match: {
624
+ debug: this.verbose,
625
+ matchBase: true
626
+ },
627
+ skip: {
628
+ debug: this.verbose,
629
+ matchBase: true
630
+ }
631
+ }
592
632
  });
593
633
  if (this.verbose) {
594
634
  console.log(`Found ${files.length} files in ${dir}`);
595
635
  console.log(files);
596
636
  }
597
637
  files.forEach((file) => {
598
- debug(`Parsing file ${file}`);
599
- const parsedJsDocumentFile = parse_file_default(file, comments_to_open_api_default, this.verbose);
600
- spec.addData(parsedJsDocumentFile.map((item) => item.spec));
601
- const parsedSwaggerJsDocumentFile = parse_file_default(file, comments_to_open_api_default2, this.verbose);
602
- spec.addData(parsedSwaggerJsDocumentFile.map((item) => item.spec));
638
+ if (this.verbose) {
639
+ console.log(`Parsing file ${file}`);
640
+ }
641
+ try {
642
+ const parsedJsDocumentFile = parse_file_default(file, comments_to_open_api_default, this.verbose);
643
+ spec.addData(parsedJsDocumentFile.map((item) => item.spec));
644
+ const parsedSwaggerJsDocumentFile = parse_file_default(file, comments_to_open_api_default2, this.verbose);
645
+ spec.addData(parsedSwaggerJsDocumentFile.map((item) => item.spec));
646
+ } catch (error) {
647
+ console.error(error);
648
+ _process.exit.call(void 0, 1);
649
+ }
603
650
  });
604
651
  }
605
652
  try {
653
+ if (this.verbose) {
654
+ console.log("Validating swagger spec");
655
+ console.log(JSON.stringify(spec, null, 2));
656
+ }
606
657
  await _swaggerparser2.default.validate(JSON.parse(JSON.stringify(spec)));
607
658
  } catch (error) {
608
659
  console.error(error.toJSON());
609
660
  _process.exit.call(void 0, 1);
610
661
  }
611
- compilation.assets[this.assetsPath] = {
612
- source() {
613
- return JSON.stringify(spec, null, 2);
614
- },
615
- size() {
616
- return Object.keys(spec).length;
662
+ const { assetsPath } = this;
663
+ _fs2.default.mkdir(_path.dirname.call(void 0, assetsPath), { recursive: true }, (error) => {
664
+ if (error) {
665
+ errorHandler(error);
617
666
  }
618
- };
667
+ _fs2.default.writeFile(assetsPath, JSON.stringify(spec, null, 2), errorHandler);
668
+ });
669
+ if (this.verbose) {
670
+ console.log(`Written swagger spec to "${this.assetsPath}" file`);
671
+ }
619
672
  console.log("switching back to normal build");
620
673
  callback();
621
674
  });