@aomex/redoc-ui 3.30.7 → 3.30.8-alpha.62.sha-92b25bb

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/dist/index.d.ts CHANGED
@@ -1,73 +1,74 @@
1
- import { OpenAPI } from '@aomex/common';
2
- import { WebContext, WebMiddleware } from '@aomex/web';
3
-
4
- interface RedocUIOptions {
1
+ import { OpenAPI } from "@aomex/common";
2
+ import { WebContext, WebMiddleware } from "@aomex/web";
3
+ export interface RedocUIOptions {
4
+ /**
5
+ * 文档内容,有三种方式可以提供openapi文档:
6
+ * 1. 文件路径,支持json和yaml格式
7
+ * 2. 传入函数直接生成
8
+ * 3. 利用`@aomex/openapi`动态生成
9
+ *
10
+ * ```typescript
11
+ * import { generateOpenapi } from '@aomex/openapi';
12
+ *
13
+ * redocUI({ openapi: './openapi.json' });
14
+ * redocUI({ openapi: './openapi.yaml' });
15
+ * redocUI({
16
+ * openapi: () => {
17
+ * return {
18
+ * version: '3.0.0',
19
+ * info: { title: 'aomex', version: '1.0.1' },
20
+ * paths: {}
21
+ * };
22
+ * }
23
+ * });
24
+ * redocUI({
25
+ * openapi: async () => {
26
+ * return generateOpenapi({
27
+ * routers: './src/routers',
28
+ * // 与 WebApp.mount 同源,用于收集全局中间件的 OpenAPI 信息
29
+ * mount: [formatError(...), auth(...)],
30
+ * });
31
+ * }
32
+ * });
33
+ * ```
34
+ */
35
+ openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);
36
+ /**
37
+ * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`
38
+ *
39
+ */
40
+ uriPrefix?: string;
41
+ /**
42
+ * 是否允许访问文档服务,每次请求都会询问
43
+ */
44
+ enable?: (ctx?: WebContext) => boolean | Promise<boolean>;
45
+ /**
46
+ * 放在<head></head>内的标签集合
47
+ *
48
+ * ```javascript
49
+ * [{
50
+ * tag: 'link',
51
+ * props: {
52
+ * rel: 'shortcut icon',
53
+ * type: 'image/x-icon',
54
+ * href: 'http://host/favicon.ico',
55
+ * },
56
+ * }]
57
+ * ```
58
+ */
59
+ headTags?: {
5
60
  /**
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
- * ```
61
+ * 标签名,如:meta, link
31
62
  */
32
- openapi: string | (() => Promise<OpenAPI.Document> | OpenAPI.Document);
63
+ tag: string;
33
64
  /**
34
- * 为了更好地区分常规接口和redoc文档,建议设置一个请求路径前缀。默认值:`'/redoc'`
35
- *
65
+ * 标签内属性
36
66
  */
37
- uriPrefix?: string;
67
+ props: Record<string, any>;
38
68
  /**
39
- * 是否允许访问文档服务,每次请求都会询问
69
+ * 是否自闭合标签,类似 <br />。默认值:`true`
40
70
  */
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
- }[];
71
+ selfClosing?: boolean;
72
+ }[];
70
73
  }
71
- declare const redocUI: (opts: RedocUIOptions) => WebMiddleware;
72
-
73
- export { type RedocUIOptions, redocUI };
74
+ export declare const redocUI: (opts: RedocUIOptions) => WebMiddleware;
package/dist/index.js CHANGED
@@ -1,85 +1,77 @@
1
- // src/index.ts
2
- import { compose, I18n, middleware } from "@aomex/common";
1
+ import { compose, createI18n, middleware } from "@aomex/common";
3
2
  import { serveStatic } from "@aomex/serve-static";
4
- import { readFile } from "fs/promises";
5
- import path from "path";
3
+ import { readFile } from "node:fs/promises";
4
+ import path from "node:path";
6
5
  import YAML from "yaml";
7
- var redocUI = (opts) => {
8
- let { uriPrefix = "/redoc", openapi, enable, headTags = [] } = 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).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
- );
59
- resolve(void 0);
60
- });
61
- return promise;
62
- };
63
- return middleware.web(async (ctx, next) => {
64
- const { pathname, method } = ctx.request;
65
- if (method !== "GET" && method !== "HEAD") return next();
66
- if (pathname !== uriPrefix && !pathname.startsWith(uriPrefix + "/")) return next();
67
- if (enable && !await enable(ctx)) return next();
68
- if (pathname === uriPrefix || pathname === htmlPath) {
69
- await loading();
70
- ctx.send(html);
71
- return;
72
- }
73
- if (pathname === openapiPath) {
74
- await loading();
75
- ctx.send(specs);
76
- ctx.response.contentType = "json";
77
- return;
78
- }
79
- return staticFn(ctx, next);
80
- });
81
- };
82
- export {
83
- redocUI
6
+ // src/index.ts
7
+ const redocUI = (opts) => {
8
+ let { uriPrefix = "/redoc", openapi, enable, headTags = [] } = 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 = createI18n({
16
+ zh: {},
17
+ en: {}
18
+ });
19
+ const staticFn = compose([serveStatic({
20
+ root: publicDir,
21
+ useCompressedFile: false,
22
+ indexFile: false,
23
+ formatPath(pathname) {
24
+ return pathname.slice(uriPrefix.length);
25
+ },
26
+ cacheControl: { maxAge: 36e5 }
27
+ })]);
28
+ let html = "";
29
+ let specs;
30
+ let promise;
31
+ const loading = () => {
32
+ if (promise) return promise;
33
+ promise = new Promise(async (resolve) => {
34
+ let docs;
35
+ if (typeof openapi === "function") {
36
+ docs = await openapi();
37
+ specs = JSON.stringify(docs);
38
+ } else {
39
+ const content = await readFile(path.resolve(openapi), "utf8");
40
+ if (openapi.endsWith(".yaml") || openapi.endsWith(".yml")) {
41
+ docs = YAML.parse(content);
42
+ specs = JSON.stringify(docs);
43
+ } else {
44
+ docs = JSON.parse(content);
45
+ specs = JSON.stringify(docs);
46
+ }
47
+ }
48
+ html = await readFile(htmlFile, "utf8");
49
+ html = html.replaceAll("#title#", docs.info.title).replaceAll("#prefix#", "." + uriPrefix).replaceAll("#lang#", i18n.language).replaceAll("#headTags#", headTags.map(({ tag, props, selfClosing = true }) => {
50
+ return `<${tag} ${Object.entries(props).map(([key, value]) => `${key}="${value}"`).join(" ")} ${selfClosing ? "/>" : `></${tag}>`}`;
51
+ }).join("\n"));
52
+ resolve(void 0);
53
+ });
54
+ return promise;
55
+ };
56
+ return middleware.web(async (ctx, next) => {
57
+ const { pathname, method } = ctx.request;
58
+ if (method !== "GET" && method !== "HEAD") return next();
59
+ if (pathname !== uriPrefix && !pathname.startsWith(uriPrefix + "/")) return next();
60
+ if (enable && !await enable(ctx)) return next();
61
+ if (pathname === uriPrefix || pathname === htmlPath) {
62
+ await loading();
63
+ ctx.send(html);
64
+ return;
65
+ }
66
+ if (pathname === openapiPath) {
67
+ await loading();
68
+ ctx.send(specs);
69
+ ctx.response.contentType = "json";
70
+ return;
71
+ }
72
+ return staticFn(ctx, next);
73
+ });
84
74
  };
75
+ export { redocUI };
76
+
85
77
  //# sourceMappingURL=index.js.map
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 * 放在<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 // 必须精确匹配前缀或前缀+/,避免 /redoc../ 这类路径穿越\n if (pathname !== uriPrefix && !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,mBA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index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { compose, createI18n, 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({\n * routers: './src/routers',\n * // 与 WebApp.mount 同源,用于收集全局中间件的 OpenAPI 信息\n * mount: [formatError(...), auth(...)],\n * });\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 = createI18n({ zh: {}, en: {} });\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 // 必须精确匹配前缀或前缀+/,避免 /redoc../ 这类路径穿越\n if (pathname !== uriPrefix && !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":";;;;;;AA+EA,MAAa,WAAW,SAAwC;CAC9D,IAAI,EAAE,YAAY,UAAU,SAAS,QAAQ,WAAW,CAAC,MAAM;CAC/D,YAAY,UAAU,MAAM,GAAG,CAAC,CAAC,IAAI,kBAAkB,CAAC,CAAC,KAAK,GAAG;CACjE,IAAI,CAAC,UAAU,WAAW,GAAG,GAAG,YAAY,MAAM;CAElD,MAAM,WAAW,KAAK,MAAM,KAAK,WAAW,YAAY;CACxD,MAAM,cAAc,KAAK,MAAM,KAAK,WAAW,cAAc;CAC7D,MAAM,YAAY,KAAK,KAAK,YAAY,SAAS,MAAM,QAAQ;CAC/D,MAAM,WAAW,KAAK,KAAK,WAAW,YAAY;CAElD,MAAM,OAAO,WAAW;EAAE,IAAI,CAAC;EAAG,IAAI,CAAC;CAAE,CAAC;CAE1C,MAAM,WAAW,QAAQ,CACvB,YAAY;EACV,MAAM;EACN,mBAAmB;EACnB,WAAW;EACX,WAAW,UAAU;GACnB,OAAO,SAAS,MAAM,UAAU,MAAM;EACxC;EACA,cAAc,EACZ,QAAQ,KACV;CACF,CAAC,CACH,CAAC;CAED,IAAI,OAAO;CACX,IAAI;CACJ,IAAI;CAEJ,MAAM,gBAAgB;EACpB,IAAI,SAAS,OAAO;EAEpB,UAAU,IAAI,QAAQ,OAAO,YAAY;GACvC,IAAI;GACJ,IAAI,OAAO,YAAY,YAAY;IACjC,OAAO,MAAM,QAAQ;IACrB,QAAQ,KAAK,UAAU,IAAI;GAC7B,OAAO;IACL,MAAM,UAAU,MAAM,SAAS,KAAK,QAAQ,OAAO,GAAG,MAAM;IAC5D,IAAI,QAAQ,SAAS,OAAO,KAAK,QAAQ,SAAS,MAAM,GAAG;KACzD,OAAO,KAAK,MAAM,OAAO;KACzB,QA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Redoc文档展示UI",
5
- "repository": {
6
- "type": "git",
7
- "url": "git@github.com:aomex/aomex.git"
8
- },
5
+ "repository": "git@github.com:aomex/aomex.git",
9
6
  "homepage": "https://aomex.js.org",
10
7
  "license": "MIT",
11
8
  "type": "module",
12
9
  "sideEffects": false,
13
- "types": "dist/index.d.ts",
14
- "exports": {
15
- ".": {
16
- "types": "./dist/index.d.ts",
17
- "import": "./dist/index.js"
18
- },
19
- "./package.json": "./package.json"
10
+ "scripts": {
11
+ "prepublishOnly": "tsdown && sh scripts/copy-ui.sh"
20
12
  },
13
+ "types": "src/index.ts",
14
+ "exports": "./src/index.ts",
21
15
  "files": [
22
16
  "dist",
23
17
  "public",
@@ -27,20 +21,28 @@
27
21
  "CHANGELOG.md"
28
22
  ],
29
23
  "publishConfig": {
24
+ "types": "dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
30
32
  "access": "public"
31
33
  },
32
34
  "peerDependencies": {
33
- "@aomex/common": "^3.0.0",
34
- "@aomex/web": "^3.0.0"
35
+ "@aomex/common": "^4.0.0",
36
+ "@aomex/web": "^4.0.0"
35
37
  },
36
38
  "dependencies": {
37
- "@aomex/serve-static": "3.30.7",
38
- "redoc": "^2.5.0",
39
- "yaml": "^2.8.0"
39
+ "@aomex/serve-static": "3.30.8-alpha.62.sha-92b25bb",
40
+ "yaml": "^2.9.1"
40
41
  },
41
42
  "devDependencies": {
42
- "@aomex/common": "3.30.7",
43
- "@aomex/web": "3.30.7"
43
+ "@aomex/common": "3.30.8-alpha.62.sha-92b25bb",
44
+ "@aomex/web": "3.30.8-alpha.62.sha-92b25bb",
45
+ "redoc": "^3.1.0"
44
46
  },
45
- "scripts": {}
46
- }
47
+ "gitHead": "92b25bbcd6c3ae28160316117a74c88619b5bc8d"
48
+ }
package/public/index.html CHANGED
@@ -16,14 +16,14 @@
16
16
  }
17
17
  </style>
18
18
  #headTags#
19
- <script src="#prefix#/redoc.standalone.js"></script>
20
19
  </head>
21
20
  <body>
22
21
  <div id="redoc-ui"></div>
23
- <script>
24
- Redoc.init(
22
+ <script type="module">
23
+ import { init } from '#prefix#/redoc.standalone.js';
24
+ init(
25
25
  '#prefix#/openapi.json',
26
- { expandResponses: '200' },
26
+ { disableTelemetry: true },
27
27
  document.getElementById('redoc-ui'),
28
28
  );
29
29
  </script>