@aomex/redoc-ui 3.24.0 → 3.25.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/CHANGELOG.md +11 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.25.0](https://github.com/aomex/aomex/compare/v3.24.0...v3.25.0) (2025-04-22)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **swagger-ui,redoc-ui:** 增加头部标签配置 ([ac30843](https://github.com/aomex/aomex/commit/ac30843bcd61885c0f8ab39d8e6e412f5a867707))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [3.24.0](https://github.com/aomex/aomex/compare/v3.23.3...v3.24.0) (2025-04-20)
|
|
7
18
|
|
|
8
19
|
|
package/dist/index.d.ts
CHANGED
|
@@ -39,6 +39,34 @@ interface RedocUIOptions {
|
|
|
39
39
|
* 是否允许访问文档服务,每次请求都会询问
|
|
40
40
|
*/
|
|
41
41
|
enable?: (ctx?: WebContext) => boolean | Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* 放在<head></head>内的标签集合
|
|
44
|
+
*
|
|
45
|
+
* ```javascript
|
|
46
|
+
* [{
|
|
47
|
+
* tag: 'link',
|
|
48
|
+
* props: {
|
|
49
|
+
* rel: 'shortcut icon',
|
|
50
|
+
* type: 'image/x-icon',
|
|
51
|
+
* href: 'http://host/favicon.ico',
|
|
52
|
+
* },
|
|
53
|
+
* }]
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
headTags?: {
|
|
57
|
+
/**
|
|
58
|
+
* 标签名,如:meta, link
|
|
59
|
+
*/
|
|
60
|
+
tag: string;
|
|
61
|
+
/**
|
|
62
|
+
* 标签内属性
|
|
63
|
+
*/
|
|
64
|
+
props: Record<string, any>;
|
|
65
|
+
/**
|
|
66
|
+
* 是否自闭合标签,类似 <br />。默认值:`true`
|
|
67
|
+
*/
|
|
68
|
+
selfClosing?: boolean;
|
|
69
|
+
}[];
|
|
42
70
|
}
|
|
43
71
|
declare const redocUI: (opts: RedocUIOptions) => WebMiddleware;
|
|
44
72
|
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { readFile } from "node:fs/promises";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import YAML from "yaml";
|
|
7
7
|
var redocUI = (opts) => {
|
|
8
|
-
let { uriPrefix = "/redoc", openapi, enable } = opts;
|
|
8
|
+
let { uriPrefix = "/redoc", openapi, enable, headTags = [] } = opts;
|
|
9
9
|
uriPrefix = uriPrefix.split("/").map(encodeURIComponent).join("/");
|
|
10
10
|
if (!uriPrefix.startsWith("/")) uriPrefix = "/" + uriPrefix;
|
|
11
11
|
const htmlPath = path.posix.join(uriPrefix, "index.html");
|
|
@@ -50,7 +50,12 @@ var redocUI = (opts) => {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
html = await readFile(htmlFile, "utf8");
|
|
53
|
-
html = html.replaceAll("#title#", docs.info.title).replaceAll("#prefix#", "." + uriPrefix).replaceAll("#lang#", i18n.language)
|
|
53
|
+
html = html.replaceAll("#title#", docs.info.title).replaceAll("#prefix#", "." + uriPrefix).replaceAll("#lang#", i18n.language).replaceAll(
|
|
54
|
+
"#headTags#",
|
|
55
|
+
headTags.map(({ tag, props, selfClosing = true }) => {
|
|
56
|
+
return `<${tag} ${Object.entries(props).map(([key, value]) => `${key}="${value}"`).join(" ")} ${selfClosing ? "/>" : `></${tag}>`}`;
|
|
57
|
+
}).join("\n")
|
|
58
|
+
);
|
|
54
59
|
resolve(void 0);
|
|
55
60
|
});
|
|
56
61
|
return promise;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { compose, I18n, middleware, OpenAPI } from '@aomex/common';\nimport { serveStatic } from '@aomex/serve-static';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { readFile } from 'node:fs/promises';\nimport path from 'node:path';\nimport YAML from 'yaml';\n\nexport interface RedocUIOptions {\n /**\n * 文档内容,有三种方式可以提供openapi文档:\n * 1. 文件路径,支持json和yaml格式\n * 2. 传入函数直接生成\n * 3. 利用`@aomex/openapi`动态生成\n *\n * ```typescript\n * import { generateOpenapi } from '@aomex/openapi';\n *\n * redocUI({ openapi: './openapi.json' });\n * redocUI({ openapi: './openapi.yaml' });\n * redocUI({\n * openapi: () => {\n * return {\n * version: '3.0.0',\n * info: { title: 'aomex', version: '1.0.1' },\n * paths: {}\n * };\n * }\n * });\n * redocUI({\n * openapi: async () => {\n * return generateOpenapi({ routers: './src/routers' });\n * }\n * });\n * ```\n */\n openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);\n /**\n * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`\n *\n */\n uriPrefix?: string;\n /**\n * 是否允许访问文档服务,每次请求都会询问\n */\n enable?: (ctx?: WebContext) => boolean | Promise<boolean>;\n}\n\nexport const redocUI = (opts: RedocUIOptions): WebMiddleware => {\n let { uriPrefix = '/redoc', openapi, enable } = opts;\n uriPrefix = uriPrefix.split('/').map(encodeURIComponent).join('/');\n if (!uriPrefix.startsWith('/')) uriPrefix = '/' + uriPrefix;\n\n const htmlPath = path.posix.join(uriPrefix, 'index.html');\n const openapiPath = path.posix.join(uriPrefix, 'openapi.json');\n const publicDir = path.join(import.meta.dirname, '..', 'public');\n const htmlFile = path.join(publicDir, 'index.html');\n\n const i18n = new I18n({\n resources: { zh_CN: {}, en_US: {} },\n defaultLanguage: 'zh_CN',\n });\n\n const staticFn = compose([\n serveStatic({\n root: publicDir,\n useCompressedFile: false,\n indexFile: false,\n formatPath(pathname) {\n return pathname.slice(uriPrefix.length);\n },\n cacheControl: {\n maxAge: 3600_000,\n },\n }),\n ]);\n\n let html = '';\n let specs: string;\n let promise: Promise<any>;\n\n const loading = () => {\n if (promise) return promise;\n\n promise = new Promise(async (resolve) => {\n let docs: OpenAPI.Document;\n if (typeof openapi === 'function') {\n docs = await openapi();\n specs = JSON.stringify(docs);\n } else {\n const content = await readFile(path.resolve(openapi), 'utf8');\n if (openapi.endsWith('.yaml') || openapi.endsWith('.yml')) {\n docs = YAML.parse(content);\n specs = JSON.stringify(docs);\n } else {\n docs = JSON.parse(content);\n specs = JSON.stringify(docs);\n }\n }\n\n html = await readFile(htmlFile, 'utf8');\n html = html\n .replaceAll('#title#', docs.info.title)\n .replaceAll('#prefix#', '.' + uriPrefix)\n .replaceAll('#lang#', i18n.language);\n resolve(undefined);\n });\n return promise;\n };\n\n return middleware.web(async (ctx, next) => {\n const { pathname, method } = ctx.request;\n if (method !== 'GET' && method !== 'HEAD') return next();\n if (!pathname.startsWith(uriPrefix)) return next();\n if (enable && !(await enable(ctx))) return next();\n\n if (pathname === uriPrefix || pathname === htmlPath) {\n await loading();\n ctx.send(html);\n return;\n }\n\n if (pathname === openapiPath) {\n await loading();\n ctx.send(specs);\n ctx.response.contentType = 'json';\n return;\n }\n\n return staticFn(ctx, next);\n });\n};\n"],"mappings":";AAAA,SAAS,SAAS,MAAM,kBAA2B;AACnD,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,OAAO,UAAU;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { compose, I18n, middleware, OpenAPI } from '@aomex/common';\nimport { serveStatic } from '@aomex/serve-static';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { readFile } from 'node:fs/promises';\nimport path from 'node:path';\nimport YAML from 'yaml';\n\nexport interface RedocUIOptions {\n /**\n * 文档内容,有三种方式可以提供openapi文档:\n * 1. 文件路径,支持json和yaml格式\n * 2. 传入函数直接生成\n * 3. 利用`@aomex/openapi`动态生成\n *\n * ```typescript\n * import { generateOpenapi } from '@aomex/openapi';\n *\n * redocUI({ openapi: './openapi.json' });\n * redocUI({ openapi: './openapi.yaml' });\n * redocUI({\n * openapi: () => {\n * return {\n * version: '3.0.0',\n * info: { title: 'aomex', version: '1.0.1' },\n * paths: {}\n * };\n * }\n * });\n * redocUI({\n * openapi: async () => {\n * return generateOpenapi({ routers: './src/routers' });\n * }\n * });\n * ```\n */\n openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);\n /**\n * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`\n *\n */\n uriPrefix?: string;\n /**\n * 是否允许访问文档服务,每次请求都会询问\n */\n enable?: (ctx?: WebContext) => boolean | Promise<boolean>;\n /**\n * 放在<head></head>内的标签集合\n *\n * ```javascript\n * [{\n * tag: 'link',\n * props: {\n * rel: 'shortcut icon',\n * type: 'image/x-icon',\n * href: 'http://host/favicon.ico',\n * },\n * }]\n * ```\n */\n headTags?: {\n /**\n * 标签名,如:meta, link\n */\n tag: string;\n /**\n * 标签内属性\n */\n props: Record<string, any>;\n /**\n * 是否自闭合标签,类似 <br />。默认值:`true`\n */\n selfClosing?: boolean;\n }[];\n}\n\nexport const redocUI = (opts: RedocUIOptions): WebMiddleware => {\n let { uriPrefix = '/redoc', openapi, enable, headTags = [] } = opts;\n uriPrefix = uriPrefix.split('/').map(encodeURIComponent).join('/');\n if (!uriPrefix.startsWith('/')) uriPrefix = '/' + uriPrefix;\n\n const htmlPath = path.posix.join(uriPrefix, 'index.html');\n const openapiPath = path.posix.join(uriPrefix, 'openapi.json');\n const publicDir = path.join(import.meta.dirname, '..', 'public');\n const htmlFile = path.join(publicDir, 'index.html');\n\n const i18n = new I18n({\n resources: { zh_CN: {}, en_US: {} },\n defaultLanguage: 'zh_CN',\n });\n\n const staticFn = compose([\n serveStatic({\n root: publicDir,\n useCompressedFile: false,\n indexFile: false,\n formatPath(pathname) {\n return pathname.slice(uriPrefix.length);\n },\n cacheControl: {\n maxAge: 3600_000,\n },\n }),\n ]);\n\n let html = '';\n let specs: string;\n let promise: Promise<any>;\n\n const loading = () => {\n if (promise) return promise;\n\n promise = new Promise(async (resolve) => {\n let docs: OpenAPI.Document;\n if (typeof openapi === 'function') {\n docs = await openapi();\n specs = JSON.stringify(docs);\n } else {\n const content = await readFile(path.resolve(openapi), 'utf8');\n if (openapi.endsWith('.yaml') || openapi.endsWith('.yml')) {\n docs = YAML.parse(content);\n specs = JSON.stringify(docs);\n } else {\n docs = JSON.parse(content);\n specs = JSON.stringify(docs);\n }\n }\n\n html = await readFile(htmlFile, 'utf8');\n html = html\n .replaceAll('#title#', docs.info.title)\n .replaceAll('#prefix#', '.' + uriPrefix)\n .replaceAll('#lang#', i18n.language)\n .replaceAll(\n '#headTags#',\n headTags\n .map(({ tag, props, selfClosing = true }) => {\n return `<${tag} ${Object.entries(props)\n .map(([key, value]) => `${key}=\"${value}\"`)\n .join(' ')} ${selfClosing ? '/>' : `></${tag}>`}`;\n })\n .join('\\n'),\n );\n resolve(undefined);\n });\n return promise;\n };\n\n return middleware.web(async (ctx, next) => {\n const { pathname, method } = ctx.request;\n if (method !== 'GET' && method !== 'HEAD') return next();\n if (!pathname.startsWith(uriPrefix)) return next();\n if (enable && !(await enable(ctx))) return next();\n\n if (pathname === uriPrefix || pathname === htmlPath) {\n await loading();\n ctx.send(html);\n return;\n }\n\n if (pathname === openapiPath) {\n await loading();\n ctx.send(specs);\n ctx.response.contentType = 'json';\n return;\n }\n\n return staticFn(ctx, next);\n });\n};\n"],"mappings":";AAAA,SAAS,SAAS,MAAM,kBAA2B;AACnD,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,OAAO,UAAU;AAsEV,IAAM,UAAU,CAAC,SAAwC;AA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