nestjs-openapi 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Eliya Cohen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # nestjs-openapi
2
+
3
+ Static OpenAPI generation for NestJS. Analyzes TypeScript source directly—no build step, no app bootstrap.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/nestjs-openapi)](https://www.npmjs.com/package/nestjs-openapi)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
7
+
8
+ ## Why static analysis?
9
+
10
+ | | Runtime (`@nestjs/swagger`) | Static (`nestjs-openapi`) |
11
+ |---|---|---|
12
+ | Requires runtime execution | Yes | No |
13
+ | Requires app bootstrap | Yes | No |
14
+ | Preserves generics/unions | No | Yes |
15
+
16
+ ## Who is this for?
17
+
18
+ - Teams who want accurate OpenAPI from TypeScript types without runtime metadata
19
+ - CI/CD pipelines that should not boot the app or require infrastructure
20
+ - Projects that want OpenAPI as a build artifact or committed output
21
+
22
+ ## Compatibility
23
+
24
+ - NestJS 10 or 11 (decorator-based controllers)
25
+ - TypeScript 5+
26
+ - Node 20+
27
+
28
+ ## Limitations
29
+
30
+ - Dynamic route registration at runtime is not supported
31
+ - Response shortcut decorators like `@ApiOkResponse()` are not read
32
+ - Controller versioning via `@Controller({ path, version })` is not supported
33
+
34
+ ## Quick start
35
+
36
+ ```bash
37
+ pnpm add -D nestjs-openapi
38
+ ```
39
+
40
+ Create `openapi.config.ts`:
41
+
42
+ ```typescript
43
+ import { defineConfig } from 'nestjs-openapi';
44
+
45
+ export default defineConfig({
46
+ output: 'openapi.json',
47
+ files: {
48
+ entry: 'src/app.module.ts',
49
+ },
50
+ openapi: {
51
+ info: { title: 'My API', version: '1.0.0' },
52
+ },
53
+ });
54
+ ```
55
+
56
+ Generate:
57
+
58
+ ```bash
59
+ npx nestjs-openapi generate
60
+ ```
61
+
62
+ ## Migration from @nestjs/swagger
63
+
64
+ 1. Keep your controllers and route decorators as-is.
65
+ 2. Move `DocumentBuilder` config into `openapi.config.ts`.
66
+ 3. Replace response shortcuts with `@ApiResponse({ status: ... })`.
67
+ 4. (Optional) Use `OpenApiModule` to serve the generated spec at runtime.
68
+
69
+ See the full guide in the docs.
70
+
71
+ ## Documentation
72
+
73
+ Full documentation: **[nestjs-openapi.dev](https://nestjs-openapi.dev)**
74
+
75
+ - [Configuration](https://nestjs-openapi.dev/docs/guides/configuration)
76
+ - [Security schemes](https://nestjs-openapi.dev/docs/guides/security)
77
+ - [Validation extraction](https://nestjs-openapi.dev/docs/guides/validation)
78
+ - [Serving specs at runtime](https://nestjs-openapi.dev/docs/guides/serving)
79
+ - [Migration guide](https://nestjs-openapi.dev/docs/recipes/migration)
80
+ - [CI/CD recipe](https://nestjs-openapi.dev/docs/recipes/ci-cd)
81
+ - [FAQ](https://nestjs-openapi.dev/docs/faq)
82
+ - [Decorator support](https://nestjs-openapi.dev/docs/guides/decorators)
83
+
84
+ ## Contributing
85
+
86
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
87
+
88
+ ## Sponsors
89
+
90
+ <a href="https://github.com/sponsors/Newbie012">
91
+ <img src="https://cdn.jsdelivr.net/gh/newbie012/sponsors/sponsors.svg">
92
+ </a>
93
+
94
+ ## License
95
+
96
+ [MIT](LICENSE)
package/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+
package/dist/cli.mjs ADDED
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env node
2
+ import 'tsx';
3
+ import { g as generate, e as formatValidationResult } from './shared/nestjs-openapi.DlNMM8Zq.mjs';
4
+ import minimist from 'minimist';
5
+ import { relative } from 'node:path';
6
+ import { createRequire } from 'node:module';
7
+ import 'effect';
8
+ import 'node:fs';
9
+ import 'ts-morph';
10
+ import 'glob';
11
+ import 'js-yaml';
12
+ import './shared/nestjs-openapi.B1bBy_tG.mjs';
13
+ import 'ts-json-schema-generator';
14
+ import 'node:crypto';
15
+ import 'node:url';
16
+ import 'child_process';
17
+
18
+ const require = createRequire(import.meta.url);
19
+ const pkg = require("../package.json");
20
+ const VERSION = pkg.version;
21
+ const HELP_TEXT = `
22
+ nestjs-openapi - Generate OpenAPI specs from NestJS applications
23
+
24
+ Usage:
25
+ nestjs-openapi generate -c <config-path> Generate OpenAPI specification
26
+ nestjs-openapi --help Show this help message
27
+ nestjs-openapi --version Show version
28
+
29
+ Options:
30
+ -c, --config <path> Path to configuration file (required)
31
+ -f, --format <format> Output format: json or yaml (overrides config)
32
+ -q, --quiet Suppress output (only show errors)
33
+ -d, --debug Enable debug output (verbose logging, full stack traces)
34
+ -h, --help Show this help message
35
+ -v, --version Show version
36
+
37
+ Examples:
38
+ nestjs-openapi generate -c openapi.config.ts
39
+ nestjs-openapi generate -c openapi.config.ts --format yaml
40
+ nestjs-openapi generate -c openapi.config.ts --debug
41
+ `.trim();
42
+ const formatDuration = (ms) => {
43
+ if (ms < 1e3) return `${ms}ms`;
44
+ return `${(ms / 1e3).toFixed(1)}s`;
45
+ };
46
+ const success = (message) => {
47
+ console.log(`\x1B[32m\u2713\x1B[0m ${message}`);
48
+ };
49
+ const warning = (message) => {
50
+ console.log(`\x1B[33m\u26A0\x1B[0m ${message}`);
51
+ };
52
+ const error = (message) => {
53
+ console.error(`\x1B[31m\u2717\x1B[0m ${message}`);
54
+ };
55
+ const main = async () => {
56
+ const args = minimist(process.argv.slice(2), {
57
+ string: ["c", "config", "f", "format"],
58
+ boolean: ["quiet", "q", "debug", "d", "help", "h", "version", "v"],
59
+ alias: {
60
+ c: "config",
61
+ f: "format",
62
+ q: "quiet",
63
+ d: "debug",
64
+ h: "help",
65
+ v: "version"
66
+ }
67
+ });
68
+ if (args.help || args.h) {
69
+ console.log(HELP_TEXT);
70
+ process.exit(0);
71
+ }
72
+ if (args.version || args.v) {
73
+ console.log(VERSION);
74
+ process.exit(0);
75
+ }
76
+ const command = args._[0];
77
+ if (!command) {
78
+ console.log(HELP_TEXT);
79
+ process.exit(0);
80
+ }
81
+ if (command !== "generate") {
82
+ error(`Unknown command: ${command}`);
83
+ console.log('\nRun "nestjs-openapi --help" for usage information.');
84
+ process.exit(1);
85
+ }
86
+ const configPath = args.config || args.c;
87
+ const format = args.format || args.f;
88
+ const quiet = args.quiet || args.q;
89
+ const debug = args.debug || args.d;
90
+ if (!configPath) {
91
+ error("Config path is required. Use -c or --config to specify the path.");
92
+ console.log("\nExample: nestjs-openapi generate -c openapi.config.ts");
93
+ process.exit(1);
94
+ }
95
+ if (format && format !== "json" && format !== "yaml") {
96
+ error(`Invalid format: ${format}. Must be 'json' or 'yaml'.`);
97
+ process.exit(1);
98
+ }
99
+ const startTime = performance.now();
100
+ try {
101
+ const result = await generate(configPath, {
102
+ format,
103
+ debug
104
+ });
105
+ const duration = performance.now() - startTime;
106
+ if (!quiet) {
107
+ const relativePath = relative(process.cwd(), result.outputPath);
108
+ success(
109
+ `Generated ${relativePath} (${formatDuration(Math.round(duration))})`
110
+ );
111
+ console.log(
112
+ ` ${result.pathCount} paths, ${result.operationCount} operations`
113
+ );
114
+ if (!result.validation.valid) {
115
+ console.log("");
116
+ warning(`Spec has broken references`);
117
+ console.log(` ${formatValidationResult(result.validation)}`);
118
+ }
119
+ }
120
+ process.exit(0);
121
+ } catch (err) {
122
+ const duration = performance.now() - startTime;
123
+ const message = err instanceof Error ? err.message : String(err);
124
+ error(`Generation failed (${formatDuration(Math.round(duration))})`);
125
+ console.error(` ${message}`);
126
+ if (debug && err instanceof Error && err.stack) {
127
+ console.error("\nStack trace:");
128
+ console.error(err.stack);
129
+ if (err.cause) {
130
+ console.error("\nCause:", err.cause);
131
+ }
132
+ }
133
+ process.exit(1);
134
+ }
135
+ };
136
+ main().catch((err) => {
137
+ console.error("Unexpected error:", err);
138
+ process.exit(1);
139
+ });