Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
netweng committed Dec 6, 2023
1 parent b8c2c2c commit 1648069
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cloudtower-api-doc/swagger/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import zh3_4API from './locales/zh/3.4.0.json';
import en3_4API from './locales/en/3.4.0.json';


export const fallbackNS = ['3_4_0','3_3_0', '3_2_0', '3_1_0', '3_0_0', '2_8_0', '2_7_0', '2_6_0', '2_5_0', '2_4_0', '2_3_0', '2_2_0', '2_1_0', '2_0_0', '1_10_0', '1_9_0', '1_8_0'];
export type ApiDoc = {
summary: string;
description: string;
Expand Down Expand Up @@ -113,7 +114,7 @@ i18next.init({
lng: SupportLanguage.zh,
updateMissing: true,
fallbackLng: [SupportLanguage.en, SupportLanguage.zh],
fallbackNS: ['1_8_0','1_9_0','1_10_0', '2_0_0', '2_1_0','2_2_0', '2_3_0', '2_4_0','2_5_0', '2_6_0','2_7_0', '2_8_0', '3_0_0', '3_1_0', '3_2_0', '3_3_0', '3_4_0'],
fallbackNS,
interpolation: {
prefix: "{",
suffix: "}",
Expand Down
4 changes: 2 additions & 2 deletions cloudtower-api-doc/swagger/locales/zh/1.8.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"description": ""
},
"/create-vm-vlan": {
"summary": "创建 VLAN",
"summary": "创建虚拟机网络",
"description": ""
},
"/create-vm-volume": {
Expand Down Expand Up @@ -329,7 +329,7 @@
"description": ""
},
"/delete-vm-vlan": {
"summary": "删除 VLAN",
"summary": "删除虚拟机网络",
"description": ""
},
"/delete-vm-volume": {
Expand Down
3 changes: 3 additions & 0 deletions cloudtower-api-doc/swagger/locales/zh/3.0.0.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"schemas": {
"UserSource": {
"enum": "创建来源。当用户为 tower 平台创建用户时,须指定 source 为 LOCAL\n* LDAP: LDAP (已弃用)\n* LOCAL: tower 平台创建\n* AUTHN: 外部认证源"
},
"HostWhereInput": {
"access_ip": "接入 IP",
"access_ip_contains": "接入 IP包含指定字符",
Expand Down
19 changes: 15 additions & 4 deletions cloudtower-api-doc/swagger/utils/wrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from "lodash";
import { OpenAPIV3 } from "openapi-types";
import i18next, { ApiDoc } from "../i18n";
import i18next, { ApiDoc, fallbackNS } from "../i18n";
import { ISpec } from "./swagger";
import { describeSchema } from "./describe";
import { tagsGroup } from './constant';
Expand All @@ -15,6 +15,14 @@ const replaceTags = (tag:string) => {
return tag;
}

const getNs = (version: string) => {
return version.split('.').join('_');
}

const getFallbackNS = (version: string) => {
return fallbackNS.slice(fallbackNS.indexOf(getNs(version)))
}

export const wrapSpecWithI18n = (
spec: ISpec,
language: string,
Expand All @@ -23,8 +31,11 @@ export const wrapSpecWithI18n = (
const cloneSpec = _.cloneDeep(spec);
const { components, paths } = cloneSpec;
const tags = new Set<string>();
const ns = getNs(version);
const fallbackNS = getFallbackNS(version);
i18next.options.fallbackNS = fallbackNS;
Object.keys(paths).forEach((p) => {
const apiDoc = i18next.t(`${version.split('.').join('_')}.paths.${p}`, {lng: language, returnObjects: true }) as ApiDoc;
const apiDoc = i18next.t(`${ns}.paths.${p}`, {lng: language, returnObjects: true }) as ApiDoc;
const method = Object.keys(paths[p])[0]
const operationObj = paths[p][method] as OpenAPIV3.OperationObject;
const { description, summary } = apiDoc;
Expand Down Expand Up @@ -71,7 +82,7 @@ export const wrapSpecWithI18n = (
});
// handle schemas
Object.keys(components.schemas).forEach((s) => {
const schema = i18next.t(`${version.split('.').join('_')}.schemas.${s}`, {lng: language, returnObjects: true}) as Record<string, string>;
const schema = i18next.t(`${ns}.schemas.${s}`, {lng: language, returnObjects: true}) as Record<string, string>;
describeSchema({
schema: components.schemas[s],
prefix: ["components", "schemas", s],
Expand All @@ -82,7 +93,7 @@ export const wrapSpecWithI18n = (
});
// handle security schemas
Object.keys(components.securitySchemes).forEach((s) => {
const schema = i18next.t(`${version.split('.').join('_')}.schemas.${s}`, {lng: language, returnObjects: true}) as Record<string, string>;
const schema = i18next.t(`${ns}.schemas.${s}`, {lng: language, returnObjects: true}) as Record<string, string>;
_.set(cloneSpec, ["components","securitySchemes", s, "description"], schema['description']);
_.set(cloneSpec, ["components","securitySchemes", s, "x-displayName"], schema['name']);
});
Expand Down

1 comment on commit 1648069

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for cloudtower-api-doc ready!

✅ Preview
https://cloudtower-api-m3ettwwt0-xiaojun.vercel.app

Built with commit 1648069.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.