@visulima/jsdoc-open-api 2.0.76 → 2.0.78
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 +21 -0
- package/README.md +350 -157
- package/dist/{chunk-GOBAMJAM.mjs → chunk-4DV3HJSR.mjs} +6 -6
- package/dist/chunk-4DV3HJSR.mjs.map +1 -0
- package/dist/{chunk-LIBKIIBP.mjs → chunk-D5MYFGTO.mjs} +3 -3
- package/dist/chunk-D5MYFGTO.mjs.map +1 -0
- package/dist/{chunk-KZEWPZ7N.js → chunk-JD7UOSJO.js} +6 -6
- package/dist/chunk-JD7UOSJO.js.map +1 -0
- package/dist/{chunk-TYWUYJHV.js → chunk-SFWB5KDS.js} +3 -3
- package/dist/chunk-SFWB5KDS.js.map +1 -0
- package/dist/cli/commander/index.js +1 -1
- package/dist/cli/commander/index.js.map +1 -1
- package/dist/cli/commander/index.mjs +1 -1
- package/dist/cli/commander/index.mjs.map +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/cli/index.mjs +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/dist/chunk-GOBAMJAM.mjs.map +0 -1
- package/dist/chunk-KZEWPZ7N.js.map +0 -1
- package/dist/chunk-LIBKIIBP.mjs.map +0 -1
- package/dist/chunk-TYWUYJHV.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<h3>Visulima jsdoc-open-api</h3>
|
|
3
3
|
<p>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
Generate OpenAPI (Swagger) specifications automatically from your JSDoc comments.
|
|
5
|
+
<br />
|
|
6
|
+
This package parses your existing code comments to create comprehensive API documentation, saving you time and effort.
|
|
7
|
+
<br />
|
|
8
|
+
It is designed for speed and minimal runtime overhead, building upon the foundations of JSDoc and Swagger standards.
|
|
9
|
+
</p>
|
|
10
|
+
<p>
|
|
11
|
+
<small>
|
|
12
|
+
<i>Note: This package originated as a fork of <a href="https://github.com/bee-travels/openapi-comment-parser" target="_blank" rel="noopener noreferrer">openapi-comment-parser</a> and <a href="https://github.com/Surnet/swagger-jsdoc" target="_blank" rel="noopener noreferrer">swagger-jsdoc</a>.</i>
|
|
13
|
+
</small>
|
|
7
14
|
</p>
|
|
8
15
|
</div>
|
|
9
16
|
|
|
@@ -27,6 +34,15 @@ Visulima jsdoc-open-api parser and generator is a forked version of [openapi-com
|
|
|
27
34
|
|
|
28
35
|
---
|
|
29
36
|
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- **Automatic Generation:** Creates OpenAPI specs from JSDoc comments.
|
|
40
|
+
- **Multiple Syntaxes:** Supports standard OpenAPI YAML/JSON within comments and a concise short syntax.
|
|
41
|
+
- **CLI Tool:** Provides a command-line interface for easy generation.
|
|
42
|
+
- **Programmatic API:** Offers a JavaScript API for integration into build processes.
|
|
43
|
+
- **Framework Integration:** Includes helpers like a Webpack plugin (useful for Next.js).
|
|
44
|
+
- **Performance:** Focused on speed and low overhead.
|
|
45
|
+
|
|
30
46
|
## Installation
|
|
31
47
|
|
|
32
48
|
```sh
|
|
@@ -41,95 +57,202 @@ yarn add @visulima/jsdoc-open-api
|
|
|
41
57
|
pnpm add @visulima/jsdoc-open-api
|
|
42
58
|
```
|
|
43
59
|
|
|
44
|
-
## Usage
|
|
60
|
+
## Usage Overview
|
|
61
|
+
|
|
62
|
+
You can use `@visulima/jsdoc-open-api` in several ways:
|
|
63
|
+
|
|
64
|
+
1. **Via Command Line (CLI):** Quick generation for simple use cases or manual runs.
|
|
65
|
+
2. **Programmatically:** Integrate generation into your custom scripts or build tools.
|
|
66
|
+
3. **With Webpack (e.g., Next.js):** Automate generation during your build process.
|
|
45
67
|
|
|
46
|
-
Choose the syntax you
|
|
68
|
+
Choose the syntax you prefer for defining OpenAPI details within your JSDoc comments:
|
|
47
69
|
|
|
48
70
|

|
|
49
71
|
|
|
50
|
-
|
|
72
|
+
---
|
|
51
73
|
|
|
52
|
-
|
|
74
|
+
## Usage with CLI
|
|
53
75
|
|
|
54
|
-
|
|
55
|
-
npm install cli-progress commander
|
|
56
|
-
```
|
|
76
|
+
The Command Line Interface (CLI) provides a straightforward way to generate your OpenAPI specification.
|
|
57
77
|
|
|
58
|
-
|
|
59
|
-
|
|
78
|
+
### Running via Package Managers
|
|
79
|
+
|
|
80
|
+
Instead of installing the CLI globally or relying on optional dependencies, you can run the installed binary directly using your package manager:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# With npx (comes with npm)
|
|
84
|
+
npx jsdoc-open-api generate src/
|
|
85
|
+
# Or explicitly using the package name:
|
|
86
|
+
npx @visulima/jsdoc-open-api generate src/
|
|
87
|
+
|
|
88
|
+
# With pnpm
|
|
89
|
+
pnpm exec jsdoc-open-api generate src/
|
|
90
|
+
|
|
91
|
+
# With Yarn 1.x
|
|
92
|
+
yarn run jsdoc-open-api generate src/
|
|
93
|
+
|
|
94
|
+
# With Yarn Berry (2+)
|
|
95
|
+
yarn jsdoc-open-api generate src/
|
|
60
96
|
```
|
|
61
97
|
|
|
98
|
+
This is often the recommended way to use package binaries within a project.
|
|
99
|
+
|
|
100
|
+
### Optional Dependencies
|
|
101
|
+
|
|
102
|
+
The CLI relies on `commander` and `cli-progress`. These are listed as `optionalDependencies`. Depending on your package manager (npm/yarn/pnpm) and configuration, you _might_ need to install them manually if they weren't installed automatically:
|
|
103
|
+
|
|
62
104
|
```sh
|
|
63
|
-
|
|
105
|
+
# If needed:
|
|
106
|
+
npm install commander cli-progress
|
|
107
|
+
# or
|
|
108
|
+
yarn add commander cli-progress
|
|
109
|
+
# or
|
|
110
|
+
pnpm add commander cli-progress
|
|
64
111
|
```
|
|
65
112
|
|
|
66
|
-
|
|
113
|
+
### Commands
|
|
114
|
+
|
|
115
|
+
#### `init`
|
|
116
|
+
|
|
117
|
+
Initializes the project by creating a `.openapirc.js` configuration file. This is typically run once per project.
|
|
67
118
|
|
|
68
119
|
```bash
|
|
69
|
-
# This generate the .openapirc.js config file, this command is only needed on the first run
|
|
70
120
|
jsdoc-open-api init
|
|
121
|
+
```
|
|
71
122
|
|
|
72
|
-
|
|
73
|
-
|
|
123
|
+
#### `generate`
|
|
124
|
+
|
|
125
|
+
Parses your source files based on the configuration (or command-line arguments) and generates the OpenAPI specification file.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# Generate using defaults defined in .openapirc.js (if it exists)
|
|
129
|
+
jsdoc-open-api generate
|
|
130
|
+
|
|
131
|
+
# Specify input path(s) directly
|
|
132
|
+
jsdoc-open-api generate src/routes/**/*.js src/controllers/
|
|
133
|
+
|
|
134
|
+
# Specify output file
|
|
135
|
+
jsdoc-open-api generate -o ./public/swagger.json src/
|
|
136
|
+
|
|
137
|
+
# Use verbose output
|
|
138
|
+
jsdoc-open-api generate -v src/
|
|
139
|
+
|
|
140
|
+
# Use very verbose output for debugging
|
|
141
|
+
jsdoc-open-api generate -d src/
|
|
74
142
|
```
|
|
75
143
|
|
|
76
|
-
###
|
|
144
|
+
### `generate` Command Options:
|
|
77
145
|
|
|
78
|
-
|
|
146
|
+
```bash
|
|
147
|
+
jsdoc-open-api generate [options] [path ...]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
- `[path ...]` : Paths to files or directories to parse (optional, uses configuration if not provided).
|
|
151
|
+
- `-c, --config [.openapirc.js]` : Specify the configuration file path. Defaults to `.openapirc.js`.
|
|
152
|
+
- `-o, --output [swaggerSpec.json]` : Specify the output file for the OpenAPI specification. Defaults to `swaggerSpec.json`.
|
|
153
|
+
- `-v, --verbose` : Enable verbose output during generation.
|
|
154
|
+
- `-d, --very-verbose` : Enable _very_ verbose output for detailed debugging.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Programmatic Usage
|
|
159
|
+
|
|
160
|
+
You can integrate the generation process directly into your Node.js scripts.
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
import path from "node:path";
|
|
164
|
+
import { fileURLToPath } from "node:url";
|
|
165
|
+
import jsdocOpenApi from "@visulima/jsdoc-open-api"; // Adjust import based on your module system (require vs import)
|
|
166
|
+
|
|
167
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
168
|
+
const __dirname = path.dirname(__filename);
|
|
169
|
+
|
|
170
|
+
const options = {
|
|
171
|
+
definition: {
|
|
172
|
+
openapi: "3.0.0",
|
|
173
|
+
info: {
|
|
174
|
+
title: "My Programmatic API",
|
|
175
|
+
version: "1.0.0",
|
|
176
|
+
description: "API documentation generated programmatically",
|
|
177
|
+
},
|
|
178
|
+
// Add other base OpenAPI definition properties here
|
|
179
|
+
},
|
|
180
|
+
// Glob patterns pointing to your source files with JSDoc comments
|
|
181
|
+
sources: [path.join(__dirname, "src/routes/**/*.js")],
|
|
182
|
+
// Optional: Specify output path (defaults to 'swaggerSpec.json' in current dir)
|
|
183
|
+
// output: path.join(__dirname, 'public/api-docs.json'),
|
|
184
|
+
// Optional: Enable verbose logging
|
|
185
|
+
// verbose: true,
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
async function generateDocs() {
|
|
189
|
+
try {
|
|
190
|
+
const specification = await jsdocOpenApi(options);
|
|
191
|
+
console.log("OpenAPI specification generated successfully:");
|
|
192
|
+
// The specification object is returned, and also written to the output file if specified.
|
|
193
|
+
// console.log(JSON.stringify(specification, null, 2));
|
|
194
|
+
} catch (error) {
|
|
195
|
+
console.error("Error generating OpenAPI specification:", error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
generateDocs();
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Usage with Next.js (via Webpack Plugin)
|
|
205
|
+
|
|
206
|
+
The package includes a Webpack plugin for seamless integration with frameworks like Next.js.
|
|
207
|
+
|
|
208
|
+
### `with-open-api.js` Helper
|
|
209
|
+
|
|
210
|
+
Create a helper file (e.g., `with-open-api.js`) in your project root:
|
|
79
211
|
|
|
80
212
|
```js
|
|
81
213
|
const path = require("node:path");
|
|
82
214
|
const fs = require("node:fs");
|
|
215
|
+
// Adjust the import path based on your project structure if needed
|
|
83
216
|
const { SwaggerCompilerPlugin } = require("@visulima/jsdoc-open-api");
|
|
84
217
|
|
|
85
218
|
/**
|
|
86
|
-
* @param
|
|
87
|
-
* @param
|
|
88
|
-
* @param
|
|
89
|
-
* @param
|
|
219
|
+
* @param {object} options
|
|
220
|
+
* @param {import('@visulima/jsdoc-open-api').SwaggerDefinition} options.definition - Base OpenAPI definition.
|
|
221
|
+
* @param {string[]} options.sources - Glob patterns for source files relative to project root.
|
|
222
|
+
* @param {boolean} [options.verbose=false] - Enable verbose logging.
|
|
223
|
+
* @param {string} [options.output='swagger/swagger.json'] - Output path relative to project root.
|
|
90
224
|
*
|
|
91
|
-
* @returns {function(
|
|
225
|
+
* @returns {function(import('next').NextConfig): import('next').NextConfig & {webpack: function(import('webpack').Configuration, object): import('webpack').Configuration}}
|
|
92
226
|
*/
|
|
93
227
|
const withOpenApi =
|
|
94
228
|
({ definition, sources, verbose, output = "swagger/swagger.json" }) =>
|
|
95
|
-
(nextConfig) => {
|
|
229
|
+
(nextConfig = {}) => {
|
|
96
230
|
return {
|
|
97
231
|
...nextConfig,
|
|
98
232
|
webpack: (config, options) => {
|
|
233
|
+
// Run generation only on the server build in Next.js
|
|
99
234
|
if (!options.isServer) {
|
|
100
235
|
return config;
|
|
101
236
|
}
|
|
102
237
|
|
|
103
|
-
|
|
104
|
-
|
|
238
|
+
let outputPath = output;
|
|
239
|
+
if (outputPath.startsWith("/")) {
|
|
240
|
+
outputPath = outputPath.slice(1);
|
|
105
241
|
}
|
|
106
242
|
|
|
107
|
-
if (!
|
|
243
|
+
if (!outputPath.endsWith(".json")) {
|
|
244
|
+
// Consider allowing YAML output as well?
|
|
108
245
|
throw new Error("The output path must end with .json");
|
|
109
246
|
}
|
|
110
247
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
...config,
|
|
114
|
-
plugins: [
|
|
115
|
-
// @ts-ignore
|
|
116
|
-
...config.plugins,
|
|
117
|
-
new SwaggerCompilerPlugin(
|
|
118
|
-
`${options.dir}/${output}`,
|
|
119
|
-
sources.map((source) => {
|
|
120
|
-
const combinedPath = path.join(options.dir, source.replace("./", ""));
|
|
121
|
-
|
|
122
|
-
// Check if the path is a directory
|
|
123
|
-
fs.lstatSync(combinedPath).isDirectory();
|
|
124
|
-
|
|
125
|
-
return combinedPath;
|
|
126
|
-
}),
|
|
127
|
-
definition,
|
|
128
|
-
{ verbose },
|
|
129
|
-
),
|
|
130
|
-
],
|
|
131
|
-
};
|
|
248
|
+
const absoluteOutputPath = path.join(options.dir, outputPath);
|
|
249
|
+
const absoluteSourcePaths = sources.map((source) => path.join(options.dir, source.replace(/^\.\//, ""))); // Normalize paths
|
|
132
250
|
|
|
251
|
+
// Add the SwaggerCompilerPlugin to webpack plugins
|
|
252
|
+
config.plugins = config.plugins || [];
|
|
253
|
+
config.plugins.push(new SwaggerCompilerPlugin(absoluteOutputPath, absoluteSourcePaths, definition, { verbose }));
|
|
254
|
+
|
|
255
|
+
// Call the original webpack config function if it exists
|
|
133
256
|
if (typeof nextConfig.webpack === "function") {
|
|
134
257
|
return nextConfig.webpack(config, options);
|
|
135
258
|
}
|
|
@@ -142,204 +265,274 @@ const withOpenApi =
|
|
|
142
265
|
module.exports = withOpenApi;
|
|
143
266
|
```
|
|
144
267
|
|
|
145
|
-
|
|
268
|
+
### `next.config.js`
|
|
269
|
+
|
|
270
|
+
Wrap your Next.js configuration with the helper:
|
|
146
271
|
|
|
147
272
|
```js
|
|
148
|
-
const withOpenApi = require("./with-open-api");
|
|
273
|
+
const withOpenApi = require("./with-open-api"); // Adjust path if necessary
|
|
149
274
|
|
|
150
275
|
/** @type {import('next').NextConfig} */
|
|
151
276
|
const nextConfig = {
|
|
152
277
|
reactStrictMode: true,
|
|
153
278
|
swcMinify: true,
|
|
154
279
|
env: {
|
|
155
|
-
NEXT_PUBLIC_APP_ORIGIN: process.env.VERCEL_URL || "http://localhost:
|
|
280
|
+
NEXT_PUBLIC_APP_ORIGIN: process.env.VERCEL_URL || "http://localhost:3000", // Default to 3000?
|
|
156
281
|
},
|
|
282
|
+
// ... other Next.js config
|
|
157
283
|
};
|
|
158
284
|
|
|
159
285
|
module.exports = withOpenApi({
|
|
160
286
|
definition: {
|
|
161
287
|
openapi: "3.0.0",
|
|
162
288
|
info: {
|
|
163
|
-
title: "My API",
|
|
289
|
+
title: "My Next.js API",
|
|
164
290
|
version: "1.0.0",
|
|
165
291
|
},
|
|
292
|
+
// servers: [{ url: '/api' }], // Optional: Define servers
|
|
166
293
|
},
|
|
167
|
-
|
|
168
|
-
|
|
294
|
+
// Paths relative to your project root
|
|
295
|
+
sources: ["pages/api/**/*.js", "src/controllers/**/*.js"],
|
|
296
|
+
output: "public/swagger.json", // Output to the public folder
|
|
297
|
+
verbose: false,
|
|
169
298
|
})(nextConfig);
|
|
170
299
|
```
|
|
171
300
|
|
|
172
|
-
|
|
301
|
+
Now, the OpenAPI specification (`public/swagger.json`) will be generated automatically during your Next.js build.
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
## Configuration (`.openapirc.js`)
|
|
306
|
+
|
|
307
|
+
When using the CLI `generate` command without specifying paths or using the `init` command, `@visulima/jsdoc-open-api` looks for a `.openapirc.js` file in your project root. This file should export an options object similar to the one used in Programmatic Usage:
|
|
308
|
+
|
|
309
|
+
```javascript
|
|
310
|
+
// .openapirc.js
|
|
311
|
+
module.exports = {
|
|
312
|
+
definition: {
|
|
313
|
+
openapi: "3.0.0",
|
|
314
|
+
info: {
|
|
315
|
+
title: "API from Config",
|
|
316
|
+
version: "2.0.0",
|
|
317
|
+
},
|
|
318
|
+
// ... other base definition properties
|
|
319
|
+
},
|
|
320
|
+
// Array of glob patterns for your source files
|
|
321
|
+
sources: ["src/**/*.js", "routes/**/*.js"],
|
|
322
|
+
output: "docs/swagger.json", // Default output file path
|
|
323
|
+
verbose: false, // Default verbosity
|
|
324
|
+
};
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## Defining OpenAPI Specs in JSDoc
|
|
173
330
|
|
|
174
|
-
|
|
331
|
+
You have two main ways to define your API specifications within JSDoc comments:
|
|
332
|
+
|
|
333
|
+
### 1. Standard OpenAPI (YAML/JSON) Syntax
|
|
334
|
+
|
|
335
|
+
Embed standard OpenAPI 3.0 YAML or JSON directly within `@openapi` or `@swagger` blocks. The library extracts and merges these definitions.
|
|
336
|
+
|
|
337
|
+
```javascript
|
|
338
|
+
/**
|
|
339
|
+
* @openapi
|
|
340
|
+
* components:
|
|
341
|
+
* schemas:
|
|
342
|
+
* User:
|
|
343
|
+
* type: object
|
|
344
|
+
* properties:
|
|
345
|
+
* id:
|
|
346
|
+
* type: integer
|
|
347
|
+
* format: int64
|
|
348
|
+
* example: 10
|
|
349
|
+
* username:
|
|
350
|
+
* type: string
|
|
351
|
+
* example: 'theUser'
|
|
352
|
+
* firstName:
|
|
353
|
+
* type: string
|
|
354
|
+
* example: 'John'
|
|
355
|
+
* lastName:
|
|
356
|
+
* type: string
|
|
357
|
+
* example: 'Doe'
|
|
358
|
+
* required:
|
|
359
|
+
* - id
|
|
360
|
+
* - username
|
|
361
|
+
*/
|
|
175
362
|
|
|
176
|
-
```ts
|
|
177
363
|
/**
|
|
178
364
|
* @openapi
|
|
179
|
-
*
|
|
365
|
+
* /users/{userId}:
|
|
180
366
|
* get:
|
|
181
|
-
*
|
|
367
|
+
* summary: Get user by ID
|
|
368
|
+
* description: Retrieve detailed information about a specific user.
|
|
369
|
+
* tags:
|
|
370
|
+
* - Users
|
|
371
|
+
* parameters:
|
|
372
|
+
* - name: userId
|
|
373
|
+
* in: path
|
|
374
|
+
* required: true
|
|
375
|
+
* description: ID of the user to retrieve.
|
|
376
|
+
* schema:
|
|
377
|
+
* type: integer
|
|
378
|
+
* format: int64
|
|
182
379
|
* responses:
|
|
183
|
-
* 200:
|
|
184
|
-
* description:
|
|
380
|
+
* '200':
|
|
381
|
+
* description: Successful response with user data.
|
|
382
|
+
* content:
|
|
383
|
+
* application/json:
|
|
384
|
+
* schema:
|
|
385
|
+
* $ref: '#/components/schemas/User' # Reference the schema defined above
|
|
386
|
+
* '404':
|
|
387
|
+
* description: User not found.
|
|
185
388
|
*/
|
|
389
|
+
function getUserById(userId) {
|
|
390
|
+
// Implementation...
|
|
391
|
+
}
|
|
186
392
|
```
|
|
187
393
|
|
|
188
|
-
|
|
394
|
+
### 2. OpenApi Short Syntax
|
|
189
395
|
|
|
190
|
-
|
|
396
|
+
Use custom JSDoc tags for a more concise way to define paths, operations, parameters, and responses.
|
|
191
397
|
|
|
192
|
-
|
|
193
|
-
In this guide, we use only JSDoc comments examples. However, YAML files work equally as well.
|
|
398
|
+
#### Basic Structure
|
|
194
399
|
|
|
195
|
-
|
|
196
|
-
For example, `GET /users` can be described as:
|
|
400
|
+
Define the HTTP method and path, followed by tags like `@summary`, `@description`, `@response`, etc.
|
|
197
401
|
|
|
198
|
-
```
|
|
402
|
+
```javascript
|
|
199
403
|
/**
|
|
200
404
|
* GET /users
|
|
201
405
|
* @summary Returns a list of users.
|
|
202
406
|
* @description Optional extended description in CommonMark or HTML.
|
|
407
|
+
* @tags Users
|
|
203
408
|
* @response 200 - A JSON array of user names
|
|
204
409
|
* @responseContent {string[]} 200.application/json
|
|
205
410
|
*/
|
|
411
|
+
function listUsers() {
|
|
412
|
+
// Implementation...
|
|
413
|
+
}
|
|
206
414
|
```
|
|
207
415
|
|
|
208
416
|
#### Parameters
|
|
209
417
|
|
|
210
|
-
|
|
211
|
-
headers (`X-CustomHeader: Value`) or cookies (`Cookie: debug=0`).
|
|
212
|
-
You can define the parameter data types, format, whether they are required or optional, and other details:
|
|
418
|
+
Use `@pathParam`, `@queryParam`, `@headerParam`, `@cookieParam` to define parameters.
|
|
213
419
|
|
|
214
|
-
```
|
|
420
|
+
```javascript
|
|
215
421
|
/**
|
|
216
422
|
* GET /users/{userId}
|
|
217
423
|
* @summary Returns a user by ID.
|
|
218
|
-
* @
|
|
424
|
+
* @tags Users
|
|
425
|
+
* @pathParam {integer | int64} userId - The ID of the user to retrieve. {required}
|
|
426
|
+
* @queryParam {string} [role] - Filter users by role (optional). Possible values: 'admin', 'member'.
|
|
219
427
|
* @response 200 - OK
|
|
428
|
+
* @responseContent {User} 200.application/json - A user object (assuming 'User' schema is defined elsewhere).
|
|
220
429
|
*/
|
|
430
|
+
function getUser(userId, role) {
|
|
431
|
+
// Implementation...
|
|
432
|
+
}
|
|
221
433
|
```
|
|
222
434
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
#### Request body
|
|
435
|
+
#### Request Body
|
|
226
436
|
|
|
227
|
-
|
|
228
|
-
Use `bodyRequired` to indicate that a request body is required.
|
|
437
|
+
Use `@bodyContent` to describe the request body and `@bodyRequired` if it's mandatory.
|
|
229
438
|
|
|
230
|
-
```
|
|
439
|
+
```javascript
|
|
231
440
|
/**
|
|
232
441
|
* POST /users
|
|
233
|
-
* @summary Creates a user.
|
|
234
|
-
* @
|
|
442
|
+
* @summary Creates a new user.
|
|
443
|
+
* @tags Users
|
|
444
|
+
* @bodyContent {User} application/json - User object to create.
|
|
235
445
|
* @bodyRequired
|
|
236
|
-
* @response 201 -
|
|
446
|
+
* @response 201 - User created successfully.
|
|
447
|
+
* @responseContent {User} 201.application/json - The created user object.
|
|
237
448
|
*/
|
|
449
|
+
function createUser(userData) {
|
|
450
|
+
// Implementation...
|
|
451
|
+
}
|
|
238
452
|
```
|
|
239
453
|
|
|
240
|
-
For more information, see [Describing Request Body](/docs/short/describing-request-body.md).
|
|
241
|
-
|
|
242
454
|
#### Responses
|
|
243
455
|
|
|
244
|
-
|
|
245
|
-
You can also provide example responses for different content types:
|
|
456
|
+
Define responses using `@response` for the status code and description, and `@responseContent` for the body schema.
|
|
246
457
|
|
|
247
|
-
```
|
|
458
|
+
```javascript
|
|
248
459
|
/**
|
|
249
|
-
* GET /
|
|
250
|
-
* @summary
|
|
251
|
-
* @
|
|
252
|
-
* @
|
|
253
|
-
* @
|
|
254
|
-
* @
|
|
255
|
-
* @response 404 -
|
|
256
|
-
* @response default - Unexpected error
|
|
460
|
+
* GET /products/{productId}
|
|
461
|
+
* @summary Get product details.
|
|
462
|
+
* @tags Products
|
|
463
|
+
* @pathParam {string} productId - ID of the product.
|
|
464
|
+
* @response 200 - Product details.
|
|
465
|
+
* @responseContent {ProductSchema} 200.application/json
|
|
466
|
+
* @response 404 - Product not found.
|
|
467
|
+
* @response default - Unexpected error.
|
|
257
468
|
*/
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
For more information, see [Describing Responses](/docs/short/describing-responses.md).
|
|
261
|
-
|
|
262
|
-
#### Input and output models
|
|
263
|
-
|
|
264
|
-
You can create global `components/schemas` section lets you define common data structures used in your API.
|
|
265
|
-
They can be referenced by name whenever a schema is required – in parameters, request bodies, and response bodies.
|
|
266
|
-
For example, this JSON object:
|
|
267
|
-
|
|
268
|
-
```json
|
|
269
|
-
{
|
|
270
|
-
"id": 4,
|
|
271
|
-
"name": "Arthur Dent"
|
|
469
|
+
function getProduct(productId) {
|
|
470
|
+
// Implementation...
|
|
272
471
|
}
|
|
273
472
|
```
|
|
274
473
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
```yaml
|
|
278
|
-
components:
|
|
279
|
-
schemas:
|
|
280
|
-
User:
|
|
281
|
-
properties:
|
|
282
|
-
id:
|
|
283
|
-
type: integer
|
|
284
|
-
name:
|
|
285
|
-
type: string
|
|
286
|
-
# Both properties are required
|
|
287
|
-
required:
|
|
288
|
-
- id
|
|
289
|
-
- name
|
|
290
|
-
```
|
|
474
|
+
#### Input and Output Models (Schema References)
|
|
291
475
|
|
|
292
|
-
|
|
476
|
+
Reference schemas defined globally (usually using the standard `@openapi` syntax in a central file or comment block) within your short syntax using type definitions like `{User}` or `{ProductSchema}`.
|
|
293
477
|
|
|
294
|
-
```
|
|
295
|
-
|
|
296
|
-
* GET /users/{userId}
|
|
297
|
-
* @summary Returns a user by ID.
|
|
298
|
-
* @pathParam {integer} userId
|
|
299
|
-
* @response 200 - OK
|
|
300
|
-
* @responseContent {User} 200.application/json
|
|
301
|
-
*/
|
|
478
|
+
```javascript
|
|
479
|
+
// Assuming 'User' schema is defined in components/schemas
|
|
302
480
|
|
|
303
481
|
/**
|
|
304
|
-
*
|
|
305
|
-
* @summary
|
|
306
|
-
* @
|
|
482
|
+
* PUT /users/{userId}
|
|
483
|
+
* @summary Update an existing user.
|
|
484
|
+
* @tags Users
|
|
485
|
+
* @pathParam {integer} userId - ID of the user to update.
|
|
486
|
+
* @bodyContent {User} application/json - Updated user data.
|
|
307
487
|
* @bodyRequired
|
|
308
|
-
* @response
|
|
488
|
+
* @response 200 - User updated successfully.
|
|
489
|
+
* @responseContent {User} 200.application/json
|
|
490
|
+
* @response 404 - User not found.
|
|
309
491
|
*/
|
|
492
|
+
function updateUser(userId, userData) {
|
|
493
|
+
// Implementation...
|
|
494
|
+
}
|
|
310
495
|
```
|
|
311
496
|
|
|
312
497
|
#### Authentication
|
|
313
498
|
|
|
314
|
-
|
|
499
|
+
Reference `securitySchemes` defined globally (using standard `@openapi` syntax) with the `@security` tag.
|
|
315
500
|
|
|
316
|
-
```
|
|
317
|
-
components
|
|
318
|
-
securitySchemes:
|
|
319
|
-
BasicAuth:
|
|
320
|
-
type: http
|
|
321
|
-
scheme: basic
|
|
322
|
-
```
|
|
501
|
+
```javascript
|
|
502
|
+
// Assuming 'BasicAuth' is defined in components/securitySchemes
|
|
323
503
|
|
|
324
|
-
```js
|
|
325
504
|
/**
|
|
326
|
-
* GET /
|
|
505
|
+
* GET /admin/settings
|
|
506
|
+
* @summary Get administrative settings (requires auth).
|
|
507
|
+
* @tags Admin
|
|
327
508
|
* @security BasicAuth
|
|
509
|
+
* @response 200 - Admin settings object.
|
|
328
510
|
*/
|
|
511
|
+
function getAdminSettings() {
|
|
512
|
+
// Implementation...
|
|
513
|
+
}
|
|
329
514
|
```
|
|
330
515
|
|
|
331
|
-
|
|
516
|
+
For detailed information on the short syntax tags and possibilities, please refer to the documentation (link to be added if available).
|
|
517
|
+
|
|
518
|
+
<!-- Add link to short syntax docs here if they exist -->
|
|
332
519
|
|
|
333
|
-
|
|
334
|
-
- API key as a header or query parameter or in cookies
|
|
335
|
-
- OAuth 2
|
|
336
|
-
- OpenID Connect Discovery
|
|
520
|
+
---
|
|
337
521
|
|
|
338
|
-
|
|
522
|
+
## Contributing
|
|
339
523
|
|
|
340
|
-
[
|
|
524
|
+
Contributions are welcome! Please refer to the [Contribution Guidelines](../../../.github/CONTRIBUTING.md) for details on how to submit pull requests, report issues, and suggest improvements.
|
|
341
525
|
|
|
342
|
-
|
|
526
|
+
---
|
|
527
|
+
|
|
528
|
+
## License
|
|
529
|
+
|
|
530
|
+
This project is licensed under the **MIT License**. See the [LICENSE.md](LICENSE.md) file for details.
|
|
531
|
+
|
|
532
|
+
---
|
|
533
|
+
|
|
534
|
+
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
535
|
+
[typescript-url]: https://www.typescriptlang.org/ "TypeScript"
|
|
343
536
|
[license-image]: https://img.shields.io/npm/l/@visulima/jsdoc-open-api?color=blueviolet&style=for-the-badge
|
|
344
537
|
[license-url]: LICENSE.md "license"
|
|
345
538
|
[npm-image]: https://img.shields.io/npm/v/@visulima/jsdoc-open-api/latest.svg?style=for-the-badge&logo=npm
|