next-openapi-gen 0.8.2 → 0.8.4
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/README.md +6 -0
- package/dist/commands/generate.js +5 -2
- package/dist/commands/init.js +15 -3
- package/dist/index.js +4 -2
- package/dist/lib/openapi-generator.js +2 -2
- package/dist/lib/utils.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,9 @@ Automatically generate OpenAPI 3.0 documentation from Next.js projects, with sup
|
|
|
20
20
|
- Stoplight Elements
|
|
21
21
|
- RapiDoc
|
|
22
22
|
|
|
23
|
+
> [!TIP]
|
|
24
|
+
> You can use the `--ui none` option during initialization to skip UI setup if you only care about generating the OpenAPI documentation.
|
|
25
|
+
|
|
23
26
|
## Installation
|
|
24
27
|
|
|
25
28
|
```bash
|
|
@@ -36,6 +39,9 @@ npx next-openapi-gen init --ui scalar --docs-url api-docs --schema zod
|
|
|
36
39
|
npx next-openapi-gen generate
|
|
37
40
|
```
|
|
38
41
|
|
|
42
|
+
> [!TIP]
|
|
43
|
+
> Use the `--output` option in the `init` command to specify a custom output file for the template. Then you can use the `--template` option in the `generate` command to point to that file.
|
|
44
|
+
|
|
39
45
|
## Configuration
|
|
40
46
|
|
|
41
47
|
During initialization (`npx next-openapi-gen init`), a configuration file `next.openapi.json` will be created in the project's root directory:
|
|
@@ -3,9 +3,12 @@ import fse from "fs-extra";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import ora from "ora";
|
|
5
5
|
import { OpenApiGenerator } from "../lib/openapi-generator.js";
|
|
6
|
-
export async function generate() {
|
|
6
|
+
export async function generate(options) {
|
|
7
|
+
const { template } = options;
|
|
7
8
|
const spinner = ora("Generating OpenAPI specification...\n").start();
|
|
8
|
-
const generator = new OpenApiGenerator(
|
|
9
|
+
const generator = new OpenApiGenerator({
|
|
10
|
+
templatePath: template,
|
|
11
|
+
});
|
|
9
12
|
const config = generator.getConfig();
|
|
10
13
|
// Create api dir if not exists
|
|
11
14
|
const apiDir = path.resolve(config.apiDir);
|
package/dist/commands/init.js
CHANGED
|
@@ -85,6 +85,9 @@ function getDocsPageDependencies(ui) {
|
|
|
85
85
|
return deps.join(" ");
|
|
86
86
|
}
|
|
87
87
|
async function createDocsPage(ui, outputFile) {
|
|
88
|
+
if (ui === "none") {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
88
91
|
const paths = ["app", "api-docs"];
|
|
89
92
|
const srcPath = path.join(process.cwd(), "src");
|
|
90
93
|
if (fs.existsSync(srcPath)) {
|
|
@@ -98,6 +101,9 @@ async function createDocsPage(ui, outputFile) {
|
|
|
98
101
|
spinner.succeed(`Created ${paths.join("/")}/page.tsx for ${ui}.`);
|
|
99
102
|
}
|
|
100
103
|
async function installDependencies(ui) {
|
|
104
|
+
if (ui === "none") {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
101
107
|
const packageManager = await getPackageManager();
|
|
102
108
|
const installCmd = `${packageManager} ${packageManager === "npm" ? "install" : "add"}`;
|
|
103
109
|
const deps = getDocsPageDependencies(ui);
|
|
@@ -111,15 +117,21 @@ function extendOpenApiTemplate(spec, options) {
|
|
|
111
117
|
spec.docsUrl = options.docsUrl ?? spec.docsUrl;
|
|
112
118
|
spec.schemaType = options.schema ?? spec.schemaType;
|
|
113
119
|
}
|
|
120
|
+
function getOutputPath(output) {
|
|
121
|
+
if (output) {
|
|
122
|
+
return path.isAbsolute(output) ? output : path.join(process.cwd(), output);
|
|
123
|
+
}
|
|
124
|
+
return path.join(process.cwd(), "next.openapi.json");
|
|
125
|
+
}
|
|
114
126
|
export async function init(options) {
|
|
115
|
-
const { ui } = options;
|
|
127
|
+
const { ui, output } = options;
|
|
116
128
|
spinner.start();
|
|
117
129
|
try {
|
|
118
|
-
const outputPath =
|
|
130
|
+
const outputPath = getOutputPath(output);
|
|
119
131
|
const template = { ...openapiTemplate };
|
|
120
132
|
extendOpenApiTemplate(template, options);
|
|
121
133
|
await fse.writeJson(outputPath, template, { spaces: 2 });
|
|
122
|
-
spinner.succeed(`Created OpenAPI template in
|
|
134
|
+
spinner.succeed(`Created OpenAPI template in ${outputPath}`);
|
|
123
135
|
createDocsPage(ui, template.outputFile);
|
|
124
136
|
installDependencies(ui);
|
|
125
137
|
}
|
package/dist/index.js
CHANGED
|
@@ -9,17 +9,19 @@ program
|
|
|
9
9
|
.description("Super fast and easy way to generate OpenAPI documentation for Next.js");
|
|
10
10
|
program
|
|
11
11
|
.command("init")
|
|
12
|
-
.addOption(new Option("-i, --ui <type>", "Specify the UI type, e.g., scalar")
|
|
13
|
-
.choices(["scalar", "swagger", "redoc", "stoplight", "rapidoc"])
|
|
12
|
+
.addOption(new Option("-i, --ui <type>", "Specify the UI type, e.g., scalar. Use \"none\" for no UI")
|
|
13
|
+
.choices(["scalar", "swagger", "redoc", "stoplight", "rapidoc", "none"])
|
|
14
14
|
.default("swagger"))
|
|
15
15
|
.option("-u, --docs-url <url>", "Specify the docs URL", "api-docs")
|
|
16
16
|
.addOption(new Option("-s, --schema <schemaType>", "Specify the schema tool")
|
|
17
17
|
.choices(["zod", "typescript"])
|
|
18
18
|
.default("zod"))
|
|
19
|
+
.option("-o, --output <file>", "Specify the output path for the OpenAPI template.", "next.openapi.json")
|
|
19
20
|
.description("Initialize a openapi specification")
|
|
20
21
|
.action(init);
|
|
21
22
|
program
|
|
22
23
|
.command("generate")
|
|
23
24
|
.description("Generate a specification based on api routes")
|
|
25
|
+
.option("-t, --template <file>", "Specify the OpenAPI template file", "next.openapi.json")
|
|
24
26
|
.action(generate);
|
|
25
27
|
program.parse(process.argv);
|
|
@@ -7,8 +7,8 @@ export class OpenApiGenerator {
|
|
|
7
7
|
config;
|
|
8
8
|
template;
|
|
9
9
|
routeProcessor;
|
|
10
|
-
constructor() {
|
|
11
|
-
const templatePath = path.resolve("./next.openapi.json");
|
|
10
|
+
constructor(opts = {}) {
|
|
11
|
+
const templatePath = opts.templatePath || path.resolve("./next.openapi.json");
|
|
12
12
|
this.template = JSON.parse(fs.readFileSync(templatePath, "utf-8"));
|
|
13
13
|
this.config = this.getConfig();
|
|
14
14
|
this.routeProcessor = new RouteProcessor(this.config);
|
package/dist/lib/utils.js
CHANGED
package/package.json
CHANGED