@aomex/redoc-ui 3.24.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 ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [3.24.0](https://github.com/aomex/aomex/compare/v3.23.3...v3.24.0) (2025-04-20)
7
+
8
+
9
+ ### Features
10
+
11
+ * **redoc-ui:** 增加redoc文档平台 ([b5f4197](https://github.com/aomex/aomex/commit/b5f4197a68cb361d95483888f727d5dbb579350d))
@@ -0,0 +1,45 @@
1
+ import { OpenAPI } from '@aomex/common';
2
+ import { WebContext, WebMiddleware } from '@aomex/web';
3
+
4
+ interface RedocUIOptions {
5
+ /**
6
+ * 文档内容,有三种方式可以提供openapi文档:
7
+ * 1. 文件路径,支持json和yaml格式
8
+ * 2. 传入函数直接生成
9
+ * 3. 利用`@aomex/openapi`动态生成
10
+ *
11
+ * ```typescript
12
+ * import { generateOpenapi } from '@aomex/openapi';
13
+ *
14
+ * redocUI({ openapi: './openapi.json' });
15
+ * redocUI({ openapi: './openapi.yaml' });
16
+ * redocUI({
17
+ * openapi: () => {
18
+ * return {
19
+ * version: '3.0.0',
20
+ * info: { title: 'aomex', version: '1.0.1' },
21
+ * paths: {}
22
+ * };
23
+ * }
24
+ * });
25
+ * redocUI({
26
+ * openapi: async () => {
27
+ * return generateOpenapi({ routers: './src/routers' });
28
+ * }
29
+ * });
30
+ * ```
31
+ */
32
+ openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);
33
+ /**
34
+ * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`
35
+ *
36
+ */
37
+ uriPrefix?: string;
38
+ /**
39
+ * 是否允许访问文档服务,每次请求都会询问
40
+ */
41
+ enable?: (ctx?: WebContext) => boolean | Promise<boolean>;
42
+ }
43
+ declare const redocUI: (opts: RedocUIOptions) => WebMiddleware;
44
+
45
+ export { type RedocUIOptions, redocUI };
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
1
+ // src/index.ts
2
+ import { compose, I18n, middleware } from "@aomex/common";
3
+ import { serveStatic } from "@aomex/serve-static";
4
+ import { readFile } from "node:fs/promises";
5
+ import path from "node:path";
6
+ import YAML from "yaml";
7
+ var redocUI = (opts) => {
8
+ let { uriPrefix = "/redoc", openapi, enable } = opts;
9
+ uriPrefix = uriPrefix.split("/").map(encodeURIComponent).join("/");
10
+ if (!uriPrefix.startsWith("/")) uriPrefix = "/" + uriPrefix;
11
+ const htmlPath = path.posix.join(uriPrefix, "index.html");
12
+ const openapiPath = path.posix.join(uriPrefix, "openapi.json");
13
+ const publicDir = path.join(import.meta.dirname, "..", "public");
14
+ const htmlFile = path.join(publicDir, "index.html");
15
+ const i18n = new I18n({
16
+ resources: { zh_CN: {}, en_US: {} },
17
+ defaultLanguage: "zh_CN"
18
+ });
19
+ const staticFn = compose([
20
+ serveStatic({
21
+ root: publicDir,
22
+ useCompressedFile: false,
23
+ indexFile: false,
24
+ formatPath(pathname) {
25
+ return pathname.slice(uriPrefix.length);
26
+ },
27
+ cacheControl: {
28
+ maxAge: 36e5
29
+ }
30
+ })
31
+ ]);
32
+ let html = "";
33
+ let specs;
34
+ let promise;
35
+ const loading = () => {
36
+ if (promise) return promise;
37
+ promise = new Promise(async (resolve) => {
38
+ let docs;
39
+ if (typeof openapi === "function") {
40
+ docs = await openapi();
41
+ specs = JSON.stringify(docs);
42
+ } else {
43
+ const content = await readFile(path.resolve(openapi), "utf8");
44
+ if (openapi.endsWith(".yaml") || openapi.endsWith(".yml")) {
45
+ docs = YAML.parse(content);
46
+ specs = JSON.stringify(docs);
47
+ } else {
48
+ docs = JSON.parse(content);
49
+ specs = JSON.stringify(docs);
50
+ }
51
+ }
52
+ html = await readFile(htmlFile, "utf8");
53
+ html = html.replaceAll("#title#", docs.info.title).replaceAll("#prefix#", "." + uriPrefix).replaceAll("#lang#", i18n.language);
54
+ resolve(void 0);
55
+ });
56
+ return promise;
57
+ };
58
+ return middleware.web(async (ctx, next) => {
59
+ const { pathname, method } = ctx.request;
60
+ if (method !== "GET" && method !== "HEAD") return next();
61
+ if (!pathname.startsWith(uriPrefix)) return next();
62
+ if (enable && !await enable(ctx)) return next();
63
+ if (pathname === uriPrefix || pathname === htmlPath) {
64
+ await loading();
65
+ ctx.send(html);
66
+ return;
67
+ }
68
+ if (pathname === openapiPath) {
69
+ await loading();
70
+ ctx.send(specs);
71
+ ctx.response.contentType = "json";
72
+ return;
73
+ }
74
+ return staticFn(ctx, next);
75
+ });
76
+ };
77
+ export {
78
+ redocUI
79
+ };
80
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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();\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AM,SAAS,KAAK,QAAQ,OAAO,GAAG,MAAM;AAC5D,YAAI,QAAQ,SAAS,OAAO,KAAK,QAAQ,SAAS,MAAM,GAAG;AACzD,iBAAO,KAAK,MAAM,OAAO;AACzB,kBAAQ,KAAK,UAAU,IAAI;AAAA,QAC7B,OAAO;AACL,iBAAO,KAAK,MAAM,OAAO;AACzB,kBAAQ,KAAK,UAAU,IAAI;AAAA,QAC7B;AAAA,MACF;AAEA,aAAO,MAAM,SAAS,UAAU,MAAM;AACtC,aAAO,KACJ,WAAW,WAAW,KAAK,KAAK,KAAK,EACrC,WAAW,YAAY,MAAM,SAAS,EACtC,WAAW,UAAU,KAAK,QAAQ;AACrC,cAAQ,MAAS;AAAA,IACnB,CAAC;AACD,WAAO;AAAA,EACT;AAEA,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,EAAE,UAAU,OAAO,IAAI,IAAI;AACjC,QAAI,WAAW,SAAS,WAAW,OAAQ,QAAO,KAAK;AACvD,QAAI,CAAC,SAAS,WAAW,SAAS,EAAG,QAAO,KAAK;AACjD,QAAI,UAAU,CAAE,MAAM,OAAO,GAAG,EAAI,QAAO,KAAK;AAEhD,QAAI,aAAa,aAAa,aAAa,UAAU;AACnD,YAAM,QAAQ;AACd,UAAI,KAAK,IAAI;AACb;AAAA,IACF;AAEA,QAAI,aAAa,aAAa;AAC5B,YAAM,QAAQ;AACd,UAAI,KAAK,KAAK;AACd,UAAI,SAAS,cAAc;AAC3B;AAAA,IACF;AAEA,WAAO,SAAS,KAAK,IAAI;AAAA,EAC3B,CAAC;AACH;","names":[]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@aomex/redoc-ui",
3
+ "version": "3.24.0",
4
+ "description": "Redoc文档展示UI",
5
+ "repository": "git@github.com:aomex/aomex.git",
6
+ "homepage": "https://aomex.js.org",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "public",
21
+ "LICENSE",
22
+ "package.json",
23
+ "README.md",
24
+ "CHANGELOG.md"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "peerDependencies": {
30
+ "@aomex/common": "^3.0.0",
31
+ "@aomex/web": "^3.0.0"
32
+ },
33
+ "dependencies": {
34
+ "@aomex/serve-static": "",
35
+ "redoc": "^2.5.0",
36
+ "yaml": "^2.7.1"
37
+ },
38
+ "devDependencies": {
39
+ "@aomex/common": "",
40
+ "@aomex/web": ""
41
+ },
42
+ "scripts": {}
43
+ }
@@ -0,0 +1,30 @@
1
+ <!doctype html>
2
+ <html lang="#lang#">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <title>#title#</title>
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
7
+ <meta name="renderer" content="webkit" />
8
+ <meta
9
+ name="viewport"
10
+ content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
11
+ />
12
+ <style>
13
+ body {
14
+ margin: 0;
15
+ padding: 0;
16
+ }
17
+ </style>
18
+ <script src="#prefix#/redoc.standalone.js"></script>
19
+ </head>
20
+ <body>
21
+ <div id="redoc-ui"></div>
22
+ <script>
23
+ Redoc.init(
24
+ '#prefix#/openapi.json',
25
+ { expandResponses: '200' },
26
+ document.getElementById('redoc-ui'),
27
+ );
28
+ </script>
29
+ </body>
30
+ </html>