@visulima/jsdoc-open-api 1.0.2 → 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,16 @@
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
+
1
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)
2
15
 
3
16
 
package/README.md CHANGED
@@ -47,7 +47,7 @@ Choose the syntax you want to use for your OpenAPI definitions:
47
47
 
48
48
  ![choose the syntax](./assets/swagger-difference.png)
49
49
 
50
- CLI:
50
+ ### CLI:
51
51
 
52
52
  ```bash
53
53
  # This generate the .openapirc.js config file, this command is only needed on the first run
@@ -57,6 +57,96 @@ jsdoc-open-api init
57
57
  jsdoc-open-api generate src/
58
58
  ```
59
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
+
60
150
  ## OpenApi yaml syntax
61
151
 
62
152
  The library will take the contents of @openapi (or @swagger):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/jsdoc-open-api",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Generates swagger doc based on JSDoc.",
5
5
  "keywords": [
6
6
  "visulima",
@@ -53,7 +53,8 @@
53
53
  "jsdoc-open-api": "./bin/index.js"
54
54
  },
55
55
  "files": [
56
- "dist/**",
56
+ "src",
57
+ "dist",
57
58
  "README.md",
58
59
  "CHANGELOG.md",
59
60
  "LICENSE.md"
@@ -70,7 +71,7 @@
70
71
  },
71
72
  "dependencies": {
72
73
  "@apidevtools/swagger-parser": "^10.1.0",
73
- "@visulima/readdir": "1.1.0",
74
+ "@visulima/readdir": "1.1.1",
74
75
  "cli-progress": "^3.11.2",
75
76
  "commander": "^9.4.1",
76
77
  "comment-parser": "^1.3.1",
@@ -0,0 +1,266 @@
1
+ declare function openapi(options?: ParserOptions): OpenApiObject;
2
+
3
+ export default openapi;
4
+
5
+ export interface ParserOptions {
6
+ cwd: string;
7
+ extension?: string[];
8
+ include?: string[];
9
+ exclude?: string[];
10
+ excludeNodeModules?: boolean;
11
+ verbose?: boolean;
12
+ }
13
+
14
+ export interface BaseDefinition {
15
+ openapi: string;
16
+ info: InfoObject;
17
+ servers?: ServerObject[];
18
+ paths?: PathsObject;
19
+ components?: ComponentsObject;
20
+ security?: SecurityRequirementObject[];
21
+ tags?: TagObject[];
22
+ externalDocs?: ExternalDocumentationObject;
23
+ }
24
+
25
+ export interface OpenApiObject extends BaseDefinition {
26
+ paths: PathsObject;
27
+ }
28
+
29
+ export interface InfoObject {
30
+ title: string;
31
+ version: string;
32
+ description?: string;
33
+ termsOfService?: string;
34
+ contact?: ContactObject;
35
+ license?: LicenseObject;
36
+ }
37
+
38
+ export interface ContactObject {
39
+ name?: string;
40
+ url?: string;
41
+ email?: string;
42
+ }
43
+
44
+ export interface LicenseObject {
45
+ name: string;
46
+ url?: string;
47
+ }
48
+
49
+ export interface ServerObject {
50
+ url: string;
51
+ description?: string;
52
+ variables?: Map<ServerVariable>;
53
+ }
54
+
55
+ export interface ServerVariable {
56
+ default: string;
57
+ enum?: string[];
58
+ description?: string;
59
+ }
60
+
61
+ export interface ComponentsObject {
62
+ schemas?: Map<SchemaObject | ReferenceObject>;
63
+ responses?: Map<ResponseObject | ReferenceObject>;
64
+ parameters?: Map<ParameterObject | ReferenceObject>;
65
+ examples?: Map<ExampleObject | ReferenceObject>;
66
+ requestBodies?: Map<RequestBodyObject | ReferenceObject>;
67
+ headers?: Map<HeaderObject | ReferenceObject>;
68
+ securitySchemes?: Map<
69
+ ApiKeySecuritySchemeObject | HttpSecuritySchemeObject | Oauth2SecuritySchemeObject | OpenIdConnectSecuritySchemeObject | ReferenceObject
70
+ >;
71
+ links?: Map<LinkObject | ReferenceObject>;
72
+ callbacks?: Map<CallbackObject | ReferenceObject>;
73
+ }
74
+
75
+ export interface PathsObject {
76
+ [path: string]: PathItemObject;
77
+ }
78
+
79
+ export interface PathItemObject {
80
+ $ref?: string;
81
+ summary?: string;
82
+ description?: string;
83
+ get?: OperationObject;
84
+ put?: OperationObject;
85
+ post?: OperationObject;
86
+ delete?: OperationObject;
87
+ options?: OperationObject;
88
+ head?: OperationObject;
89
+ patch?: OperationObject;
90
+ trace?: OperationObject;
91
+ servers?: ServerObject[];
92
+ parameters?: (ParameterObject | ReferenceObject)[];
93
+ }
94
+
95
+ export interface OperationObject {
96
+ responses: ResponsesObject;
97
+ tags?: string[];
98
+ summary?: string;
99
+ description?: string;
100
+ externalDocs?: ExternalDocumentationObject;
101
+ operationId?: string;
102
+ parameters?: (ParameterObject | ReferenceObject)[];
103
+ requestBody?: RequestBodyObject | ReferenceObject;
104
+ callbacks?: Map<CallbackObject | ReferenceObject>;
105
+ deprecated?: boolean;
106
+ security?: SecurityRequirementObject[];
107
+ servers?: ServerObject[];
108
+ }
109
+
110
+ export interface ExternalDocumentationObject {
111
+ url: string;
112
+ description?: string;
113
+ }
114
+
115
+ export interface ParameterObject {
116
+ name: string;
117
+ in: string;
118
+ description?: string;
119
+ required?: boolean;
120
+ deprecated?: boolean;
121
+ allowEmptyValue?: boolean;
122
+ //
123
+ style?: string;
124
+ explode?: string;
125
+ allowReserved?: boolean;
126
+ schema?: SchemaObject | ReferenceObject;
127
+ example?: any;
128
+ examples?: Map<ExampleObject | ReferenceObject>;
129
+ //
130
+ content?: Map<MediaTypeObject>;
131
+ // ignoring stylings: matrix, label, form, simple, spaceDelimited,
132
+ // pipeDelimited and deepObject
133
+ }
134
+
135
+ export interface RequestBodyObject {
136
+ content: Map<MediaTypeObject>;
137
+ description?: string;
138
+ required?: boolean;
139
+ }
140
+
141
+ export interface MediaTypeObject {
142
+ schema?: SchemaObject | ReferenceObject;
143
+ example?: any;
144
+ examples?: Map<ExampleObject | ReferenceObject>;
145
+ encoding?: Map<EncodingObject>;
146
+ }
147
+
148
+ export interface EncodingObject {
149
+ contentType?: string;
150
+ headers?: Map<HeaderObject | ReferenceObject>;
151
+ style?: string;
152
+ explode?: boolean;
153
+ allowReserved?: boolean;
154
+ }
155
+
156
+ export interface ResponsesObject {
157
+ [code: string]: ResponseObject | ReferenceObject;
158
+ }
159
+
160
+ export interface ResponseObject {
161
+ description: string;
162
+ headers?: Map<HeaderObject | ReferenceObject>;
163
+ content?: Map<MediaTypeObject>;
164
+ links?: Map<LinkObject | ReferenceObject>;
165
+ }
166
+
167
+ export interface CallbackObject {
168
+ [expression: string]: PathItemObject;
169
+ }
170
+
171
+ export interface ExampleObject {
172
+ summary?: string;
173
+ description?: string;
174
+ value?: any;
175
+ externalValue?: string;
176
+ }
177
+
178
+ export interface LinkObject {
179
+ operationRef?: string;
180
+ operationId?: string;
181
+ parameters?: Map<any>;
182
+ requestBody?: any;
183
+ description?: string;
184
+ server?: ServerObject;
185
+ }
186
+
187
+ export interface HeaderObject {
188
+ description?: string;
189
+ required?: boolean;
190
+ deprecated?: boolean;
191
+ allowEmptyValue?: boolean;
192
+ //
193
+ style?: string;
194
+ explode?: string;
195
+ allowReserved?: boolean;
196
+ schema?: SchemaObject | ReferenceObject;
197
+ example?: any;
198
+ examples?: Map<ExampleObject | ReferenceObject>;
199
+ //
200
+ content?: Map<MediaTypeObject>;
201
+ // ignoring stylings: matrix, label, form, simple, spaceDelimited,
202
+ // pipeDelimited and deepObject
203
+ }
204
+
205
+ export interface TagObject {
206
+ name: string;
207
+ description?: string;
208
+ externalDocs?: ExternalDocumentationObject;
209
+ }
210
+
211
+ export interface ReferenceObject {
212
+ $ref: string;
213
+ }
214
+
215
+ // TODO: this could be expanded on.
216
+ export interface SchemaObject {
217
+ [key: string]: any;
218
+ }
219
+
220
+ export interface ApiKeySecuritySchemeObject {
221
+ type: string;
222
+ description?: string;
223
+ name: string;
224
+ in: string;
225
+ }
226
+
227
+ export interface HttpSecuritySchemeObject {
228
+ type: string;
229
+ description?: string;
230
+ scheme: string;
231
+ bearerFormat?: string;
232
+ }
233
+
234
+ export interface Oauth2SecuritySchemeObject {
235
+ type: string;
236
+ description?: string;
237
+ flows: OAuthFlowsObject;
238
+ }
239
+
240
+ export interface OpenIdConnectSecuritySchemeObject {
241
+ type: string;
242
+ description?: string;
243
+ openIdConnectUrl: string;
244
+ }
245
+
246
+ export interface OAuthFlowsObject {
247
+ implicit?: OAuthFlowObject;
248
+ password?: OAuthFlowObject;
249
+ clientCredentials?: OAuthFlowObject;
250
+ authorizationCode?: OAuthFlowObject;
251
+ }
252
+
253
+ export interface OAuthFlowObject {
254
+ authorizationUrl?: string; // required for some?
255
+ tokenUrl?: string; // required for some?
256
+ refreshUrl: string;
257
+ scopes: Map<string>;
258
+ }
259
+
260
+ export interface SecurityRequirementObject {
261
+ [name: string]: string[];
262
+ }
263
+
264
+ export interface Map<T> {
265
+ [key: string]: T;
266
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export { default as SpecBuilder } from "./spec-builder";
2
+ export { default as parseFile } from "./parse-file";
3
+ export { default as yamlLoc } from "./util/yaml-loc";
4
+ export type { BaseDefinition, OpenApiObject } from "./exported";
5
+ export { default as SwaggerCompilerPlugin } from "./webpack/swagger-compiler-plugin";
6
+ export { default as jsDocumentCommentsToOpenApi } from "./jsdoc/comments-to-open-api";
7
+ export { default as swaggerJsDocumentCommentsToOpenApi } from "./swagger-jsdoc/comments-to-open-api";