nestjs-knife4j-plus 1.0.4 → 1.0.5

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 CHANGED
@@ -40,6 +40,8 @@ async function bootstrap() {
40
40
  .addTag('cats')
41
41
  .build()
42
42
  const document = SwaggerModule.createDocument(app, options)
43
+ // 如果想自定义访问路径前缀,需要在 Swagger 配置中开启全局前缀(useGlobalPrefix:true)同时 knife4jSetup 也需要配置全局前缀(knife4jSetup第三个参数为前缀)
44
+ // app.setGlobalPrefix('customerPrefix')
43
45
  SwaggerModule.setup('api', app, document)
44
46
 
45
47
  knife4jSetup(app, [
@@ -48,15 +50,26 @@ async function bootstrap() {
48
50
  url: `/api-json`,
49
51
  },
50
52
  ])
51
-
52
53
  await app.listen(3000)
53
54
  }
54
55
  ```
55
56
 
56
57
  then you can browse on [http://127.0.0.1:3000/doc.html](http://127.0.0.1:3000/doc.html)
57
58
 
59
+ if you set customerPrefix, then in Knife4j UI, the path to access will be '/customerPrefix/doc.html'
60
+
58
61
  ## 📈 Changelog
59
62
 
63
+ ### [1.0.5] - 2026-02-09
64
+
65
+ #### ✨ feature [issues/1](https://github.com/jkhuangfu/nestjs-knife4j-plus/issues/1)
66
+
67
+ - **新增自定义路径前缀支持**
68
+ 支持在 Knife4j UI 中配置自定义访问路径前缀,方便在不同路径下部署。
69
+
70
+ - **Add custom path prefix support**
71
+ Allows configuring custom path prefixes for Knife4j UI, enabling deployment under different paths.
72
+
60
73
  ### [1.0.4] - 2026-01-12
61
74
 
62
75
  #### 🐛 Fix
package/dist/index.d.ts CHANGED
@@ -16,7 +16,7 @@ export interface Service {
16
16
  *
17
17
  * @param app - NestFactory.create() 创建的应用实例 (支持 Express / Fastify)
18
18
  * @param services - Knife4j 多服务配置数组,每项需实现 {@link Service} 接口
19
- *
19
+ * @param prefix - Knife4j UI 路径前缀,默认 '/',可自定义路径前缀 feature https://github.com/jkhuangfu/nestjs-knife4j-plus/issues/1
20
20
  * @example
21
21
  * ```typescript
22
22
  * // in main.ts
@@ -32,16 +32,20 @@ export interface Service {
32
32
  * .setDescription("API接口文档")
33
33
  * .build();
34
34
  * const document = SwaggerModule.createDocument(app, config);
35
- * SwaggerModule.setup("api", app, document);
35
+ * // 如果想同步使用全局前缀,需要在 Swagger 配置中开启全局前缀(useGlobalPrefix:true)同时 knife4jSetup 也需要配置全局前缀
36
+ * // 满足issues:https://github.com/jkhuangfu/nestjs-knife4j-plus/issues/1
37
+ * // 服务启动文件中配置app.setGlobalPrefix("customerPrefix");
38
+ *
39
+ * SwaggerModule.setup("api", app, document, { useGlobalPrefix:true });
36
40
  * await knife4jSetup(app, [
37
41
  * {
38
42
  * name: '用户服务',
39
43
  * url: '/api-json'
40
44
  * }
41
- * ]);
45
+ * ],'customPrefix');
42
46
  * await app.listen(3000);
43
47
  * }
44
48
  * bootstrap();
45
49
  * ```
46
50
  */
47
- export declare function knife4jSetup(app: INestApplication, services: Service[]): Promise<void>;
51
+ export declare function knife4jSetup(app: INestApplication, services: Service[], prefix?: string): Promise<void>;
package/dist/index.js CHANGED
@@ -2,7 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.knife4jSetup = knife4jSetup;
4
4
  const node_path_1 = require("node:path");
5
- async function knife4jSetup(app, services) {
5
+ async function knife4jSetup(app, services, prefix = '/') {
6
+ let prefixStr = prefix.trim();
7
+ if (!prefixStr.startsWith('/')) {
8
+ prefixStr = '/' + prefixStr;
9
+ }
6
10
  const httpAdapter = app.getHttpAdapter().getType();
7
11
  const instance = app.getHttpAdapter().getInstance();
8
12
  if (!['express', 'fastify'].includes(httpAdapter)) {
@@ -11,13 +15,13 @@ async function knife4jSetup(app, services) {
11
15
  if (httpAdapter === 'express') {
12
16
  try {
13
17
  const expressStatic = await Promise.resolve().then(() => require('express')).then((mod) => mod.static);
14
- app.use('/', expressStatic((0, node_path_1.resolve)(__dirname, '../ui/')));
18
+ app.use(prefixStr, expressStatic((0, node_path_1.resolve)(__dirname, '../ui/')));
15
19
  app.use('/services.json', (_, res) => {
16
20
  res.send(services);
17
21
  });
18
22
  }
19
23
  catch (error) {
20
- throw new Error('Express is not installed');
24
+ throw new Error('express is not installed please install it first ?, run `npm install express` or `yarn add express` try again');
21
25
  }
22
26
  return;
23
27
  }
@@ -25,7 +29,7 @@ async function knife4jSetup(app, services) {
25
29
  const fastifyStatic = await Promise.resolve().then(() => require('@fastify/static')).then((mod) => mod.default);
26
30
  instance.register(fastifyStatic, {
27
31
  root: (0, node_path_1.join)(__dirname, '../ui'),
28
- prefix: '/',
32
+ prefix: prefixStr,
29
33
  decorateReply: false,
30
34
  });
31
35
  instance.get('/services.json', (_, repl) => {
@@ -33,6 +37,6 @@ async function knife4jSetup(app, services) {
33
37
  });
34
38
  }
35
39
  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`');
40
+ throw new Error('@fastify/static is not installed please install it first? , run `npm install @fastify/static` or `yarn add @fastify/static` try again');
37
41
  }
38
42
  }
package/package.json CHANGED
@@ -1,48 +1,48 @@
1
- {
2
- "name": "nestjs-knife4j-plus",
3
- "version": "1.0.4",
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
- }
1
+ {
2
+ "name": "nestjs-knife4j-plus",
3
+ "version": "1.0.5",
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
+ }