nestjs-knife4j-plus 1.0.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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -0
  3. package/dist/index.d.ts +47 -0
  4. package/dist/index.js +38 -0
  5. package/package.json +48 -0
  6. package/ui/assets/css/app.12fbd497.css +15 -0
  7. package/ui/assets/css/chunk-1287e0be.d6530adc.css +1 -0
  8. package/ui/assets/css/chunk-f3245bfc.9612b987.css +1 -0
  9. package/ui/assets/css/chunk-vendors.f24a310a.css +1 -0
  10. package/ui/assets/fonts/fontawesome-webfont.706450d7.ttf +0 -0
  11. package/ui/assets/fonts/fontawesome-webfont.97493d3f.woff2 +0 -0
  12. package/ui/assets/fonts/fontawesome-webfont.d9ee23d5.woff +0 -0
  13. package/ui/assets/fonts/fontawesome-webfont.f7c2b4b7.eot +0 -0
  14. package/ui/assets/fonts/iconfont.4ca3d0c0.ttf +0 -0
  15. package/ui/assets/fonts/iconfont.e2d2b98e.eot +0 -0
  16. package/ui/assets/img/editormd-logo.53ea80e2.svg +11 -0
  17. package/ui/assets/img/fontawesome-webfont.29800836.svg +565 -0
  18. package/ui/assets/img/iconfont.1d48c203.svg +56 -0
  19. package/ui/assets/img/loading.c929501e.gif +0 -0
  20. package/ui/assets/img/loading@2x.695405a9.gif +0 -0
  21. package/ui/assets/img/loading@3x.65eacf61.gif +0 -0
  22. package/ui/assets/js/app.f5d7ba62.js +1 -0
  23. package/ui/assets/js/chunk-0d102d5a.52847613.js +1 -0
  24. package/ui/assets/js/chunk-0fd67716.7c05a478.js +1 -0
  25. package/ui/assets/js/chunk-1287e0be.3f3aadeb.js +8 -0
  26. package/ui/assets/js/chunk-214218f0.e57955ea.js +1 -0
  27. package/ui/assets/js/chunk-2d0af44e.986e3858.js +1 -0
  28. package/ui/assets/js/chunk-2d0bd799.9cfd78bf.js +1 -0
  29. package/ui/assets/js/chunk-2d0da532.afc7b81f.js +1 -0
  30. package/ui/assets/js/chunk-3b888a65.4da69ee0.js +1 -0
  31. package/ui/assets/js/chunk-42d52f4e.cb854102.js +9 -0
  32. package/ui/assets/js/chunk-589faee0.76a3ceab.js +8 -0
  33. package/ui/assets/js/chunk-6cbe5a9e.eef687ac.js +1 -0
  34. package/ui/assets/js/chunk-735c675c.fa58a10f.js +1 -0
  35. package/ui/assets/js/chunk-9185dcc8.4d55eb83.js +9 -0
  36. package/ui/assets/js/chunk-9aa77aca.5de9c585.js +47 -0
  37. package/ui/assets/js/chunk-a27fb06c.58b6553d.js +1 -0
  38. package/ui/assets/js/chunk-adb9e944.9d64cfc4.js +8 -0
  39. package/ui/assets/js/chunk-f3245bfc.91b00092.js +1 -0
  40. package/ui/assets/js/chunk-vendors.b5c20e55.js +335 -0
  41. package/ui/doc.html +1 -0
  42. package/ui/favicon.ico +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 jkhuangfu
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,59 @@
1
+ ## Description
2
+
3
+ The provided code sets up Knife4j for enhancing Swagger/OpenAPI documentation in a NestJS application, supporting both Express and Fastify HTTP adapters. Thanks to [@xiaoymin](https://github.com/xiaoymin) for providing the WebUI.
4
+
5
+ [![NPM version](https://img.shields.io/npm/v/nestjs-knife4j-plus?style=for-the-badge)](https://www.npmjs.com/package/nestjs-knife4j-plus) [![License: MIT](https://img.shields.io/badge/License-MIT-green?style=for-the-badge)](LICENSE) [![GitHub issues](https://img.shields.io/github/issues/jkhuangfu/nestjs-knife4j-plus?style=for-the-badge)](https://github.com/jkhuangfu/nestjs-knife4j-plus/issues)
6
+
7
+ ## 📦 Installation
8
+
9
+ ```bash
10
+ # Install the package
11
+ npm install nestjs-knife4j-plus @nestjs/swagger
12
+
13
+ # For Fastify adapter, also install:
14
+ npm install @fastify/static
15
+ ```
16
+
17
+ ## 🔄 Compatibility Matrix
18
+
19
+ | @fastify/static version | Fastify version |
20
+ | ----------------------- | --------------- |
21
+ | `^8.x` | `^5.x` |
22
+ | `^7.x` | `^4.x` |
23
+ | `^5.x` | `^3.x` |
24
+ | `^2.x` | `^2.x` |
25
+ | `^1.x` | `^1.x` |
26
+
27
+ ## 🚀 Usage
28
+
29
+ ```typescript
30
+ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
31
+ import { knife4jSetup } from 'nestjs-knife4j-plus'
32
+
33
+ // To use this with Fastify, first install @fastify/static. Please ensure version compatibility.
34
+
35
+ async function bootstrap() {
36
+ const options = new DocumentBuilder()
37
+ .setTitle('Cats example')
38
+ .setDescription('The cats API description')
39
+ .setVersion('1.0')
40
+ .addTag('cats')
41
+ .build()
42
+ const document = SwaggerModule.createDocument(app, options)
43
+ SwaggerModule.setup('api', app, document)
44
+
45
+ // if you want to use express adapter, you can use the following code
46
+ knife4jSetup(app, [
47
+ {
48
+ name: '2.0 version',
49
+ url: `/api-json`,
50
+ },
51
+ ])
52
+
53
+ await app.listen(3000)
54
+ }
55
+ ```
56
+
57
+ then you can browse on [http://127.0.0.1:3000/doc.html](http://127.0.0.1:3000/doc.html)
58
+
59
+ ## 📈 Changelog
@@ -0,0 +1,47 @@
1
+ import type { INestApplication } from '@nestjs/common';
2
+ /**
3
+ * 服务配置接口
4
+ */
5
+ export interface Service {
6
+ /** 服务名称 */
7
+ name: string;
8
+ /**
9
+ * OpenAPI JSON 地址,通常为 Swagger 配置的路径 + '-json'
10
+ * @example 如果 Swagger 访问路径为 '/api',则 OpenAPI JSON 地址为 '/api-json'
11
+ */
12
+ url: string;
13
+ }
14
+ /**
15
+ * 配置knife4j
16
+ *
17
+ * @param app - NestFactory.create() 创建的应用实例 (支持 Express / Fastify)
18
+ * @param services - Knife4j 多服务配置数组,每项需实现 {@link Service} 接口
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // in main.ts
23
+ *
24
+ * import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
25
+ * import { knife4jSetup, type Service } from 'nestjs-knife4j-plus';
26
+ * // To use this with Fastify, first install `@fastify/static`. Please ensure version compatibility.
27
+ *
28
+ * async function bootstrap() {
29
+ * const app = await NestFactory.create(AppModule);
30
+ * const config = new DocumentBuilder()
31
+ * .setTitle("API")
32
+ * .setDescription("API接口文档")
33
+ * .build();
34
+ * const document = SwaggerModule.createDocument(app, config);
35
+ * SwaggerModule.setup("api", app, document);
36
+ * await knife4jSetup(app, [
37
+ * {
38
+ * name: '用户服务',
39
+ * url: '/api-json'
40
+ * }
41
+ * ]);
42
+ * await app.listen(3000);
43
+ * }
44
+ * bootstrap();
45
+ * ```
46
+ */
47
+ export declare function knife4jSetup(app: INestApplication, services: Service[]): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.knife4jSetup = knife4jSetup;
4
+ const node_path_1 = require("node:path");
5
+ async function knife4jSetup(app, services) {
6
+ const httpAdapter = app.getHttpAdapter().getType();
7
+ const instance = app.getHttpAdapter().getInstance();
8
+ if (!['express', 'fastify'].includes(httpAdapter)) {
9
+ throw new Error('http adapter only supported express and fastify');
10
+ }
11
+ if (httpAdapter === 'express') {
12
+ try {
13
+ const expressStatic = await Promise.resolve().then(() => require('express')).then((mod) => mod.static);
14
+ app.use('/', expressStatic((0, node_path_1.resolve)(__dirname, '../ui/')));
15
+ app.use('/services.json', (_, res) => {
16
+ res.send(services);
17
+ });
18
+ }
19
+ catch (error) {
20
+ throw new Error('Express is not installed');
21
+ }
22
+ return;
23
+ }
24
+ try {
25
+ const fastifyStatic = await Promise.resolve().then(() => require('@fastify/static')).then((mod) => mod.default);
26
+ instance.register(fastifyStatic, {
27
+ root: (0, node_path_1.join)(__dirname, '../ui'),
28
+ prefix: '/',
29
+ decorateReply: false,
30
+ });
31
+ instance.get('/services.json', (_, repl) => {
32
+ repl.send(services);
33
+ });
34
+ }
35
+ catch (error) {
36
+ throw new Error('@fastify/static is not installed please install it first , run `npm install @fastify/static` or `yarn add @fastify/static`');
37
+ }
38
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "nestjs-knife4j-plus",
3
+ "version": "1.0.0",
4
+ "description": "NestJS Knife4j integration for enhanced Swagger/OpenAPI documentation with support for both Express and Fastify adapters. Provides a powerful API documentation UI based on Knife4j.",
5
+ "main": "dist/index.js",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "nestjs",
9
+ "knife4j",
10
+ "swagger",
11
+ "openapi"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/jkhuangfu/nestjs-knife4j-plus.git"
16
+ },
17
+ "publishConfig": {
18
+ "registry": "https://registry.npmjs.org/"
19
+ },
20
+ "scripts": {
21
+ "build-js": "tsc -p tsconfig.js.json",
22
+ "build-dts": "tsc -p tsconfig.dts.json",
23
+ "build": "npm run build-js && npm run build-dts"
24
+ },
25
+ "files": [
26
+ "/ui",
27
+ "/dist"
28
+ ],
29
+ "author": "jkhuangfu",
30
+ "devDependencies": {
31
+ "@types/node": "^20.14.12",
32
+ "typescript": "^5.5.4"
33
+ },
34
+ "peerDependencies": {
35
+ "@nestjs/common": "*",
36
+ "@fastify/static": "*",
37
+ "express": "*",
38
+ "fastify": "*"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "@fastify/static": {
42
+ "optional": true
43
+ },
44
+ "fastify": {
45
+ "optional": true
46
+ }
47
+ }
48
+ }