Skip to content

Commit

Permalink
Merge pull request #93 from long-woo/dev
Browse files Browse the repository at this point in the history
release: 2.7.0
  • Loading branch information
long-woo authored Dec 2, 2024
2 parents 75ce468 + c22abdc commit 399a977
Show file tree
Hide file tree
Showing 10 changed files with 1,666 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ App<IAppOption>({
| tag | | number | | Specify the tag from the interface url. By default, the first tag is read for the file name. |
| filter | f | string[] | | Filter interfaces. Interfaces that meet the filter conditions will be generated. Example: `--filter "/pet*"`, generate an interface for `/pet`, and support multiple `--filter`. For more usage information, please refer to [micromatch](https://github.com/micromatch/micromatch) |
| conjunction | c | string | By | The method's connector, the default value is `By`. |
| actionIndex | | number | -1 | The method name index, the default value is `-1`. |
| version | v | boolean | | Output version information. |
| help | h | boolean | | Output help information. |

Expand All @@ -152,8 +153,8 @@ Create a `myPlugin.ts` file:

```ts
// 引用模块
// import { start } from 'https://deno.land/x/stc@2.6.1/mod.ts'
import { start } from 'jsr:@loongwoo/stc@^2.6.1'
// import { start } from 'https://deno.land/x/stc@2.7.0/mod.ts'
import { start } from 'jsr:@loongwoo/stc@^2.7.0'

// Defining plugins
const myPlugin: IPlugin = {
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@loongwoo/stc",
"version": "2.6.1",
"version": "2.7.0",
"exports": "./mod.ts",
"tasks": {
"pack": "deno run -A src/pack.ts",
"dev": "deno task pack && deno run -A --watch=src src/main.ts --url='https://petstore3.swagger.io/api/v3/openapi.json' --lang=ts",
"serve": "deno run -A --watch=src src/service.ts",
"version": "echo '2.6.1' > release/version",
"version": "echo '2.7.0' > release/version",
"build:npm": "deno run -A src/npm/build.ts",
"build:mac": "deno compile -A --target x86_64-apple-darwin --output release/stc src/main.ts",
"build:mac-m": "deno compile -A --target aarch64-apple-darwin --output release/stc-m src/main.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ ${getT("$t(cli.option)")}
-f, --filter ${getT("$t(cli.option_filter)")}
--tag ${getT("$t(cli.option_tag)")}
-c, --conjunction ${getT("$t(cli.option_conjunction)")}
--actionIndex ${getT("$t(cli.option_actionIndex)")}
-v, --version ${getT("$t(cli.option_version)")}
${getT("$t(cli.example)")}
Expand All @@ -184,6 +185,7 @@ export const main = async (): Promise<ISwaggerOptions> => {
"tag",
"filter",
"conjunction",
"actionIndex",
],
alias: {
h: "help",
Expand Down Expand Up @@ -240,5 +242,6 @@ export const main = async (): Promise<ISwaggerOptions> => {
tag: args.tag,
filter: args.filter,
conjunction: args.conjunction,
actionIndex: args.actionIndex,
};
};
23 changes: 20 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,21 @@ export const getDefinition = (
* 从 URL 获取方法名称
* @param url - 接口地址
* @param conjunction - 连接字符
* @param index - 下标, 默认 -1
* @returns
*/
const getMethodName = (url: string, conjunction: string) => {
const getMethodName = (
url: string,
conjunction: string,
index: number = -1,
) => {
const _url = url.split("/");
// 获取URL路径Query方法名称(取第一个Params)
let _query = url.split("?")?.[1]?.split("&")
.shift()?.replace(/[,=]/g, "_");
// 添加_分割标记
_query = _query ? `_${_query}` : "";
let _name = _url.pop()?.split("?")[0] as string;
let _name = _url.slice(index).join("_")?.split("?")[0] as string;

if (!_name) return _name;

Expand Down Expand Up @@ -443,7 +448,7 @@ export const getApiPath = (

// 方法名
let name = currentMethod.operationId ??
getMethodName(url, options!.conjunction!);
getMethodName(url, options!.conjunction!, options?.actionIndex);

if (!name) {
Logs.error(getT("$t(path.notName)", { url, method }));
Expand All @@ -469,6 +474,18 @@ export const getApiPath = (
);

name = `${value.tag}@${name}`;

if (pathMap.has(name)) {
Logs.error(
getT("$t(path.duplicate)", {
url,
method,
name: name.slice(name.indexOf("@") + 1),
}),
);

return;
}
pathMap.set(name, value);
});
});
Expand Down
6 changes: 4 additions & 2 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"parserTypeError": "The {{type}} type could not be resolved."
},
"path": {
"notName": "The {{method}} of {{url}} cannot get the method name, so it is ignored."
"notName": "{{url}} {{method}} cannot get the method name, so it will be ignored.",
"duplicate": "{{url}} {{method}} has the same method name {{name}}, you can set the `--actionIndex` option."
},
"app": {
"apiJsonFileError": "API file parsing failed. Reason: {{error}}",
Expand All @@ -39,7 +40,8 @@
"option_filter": "Filter interfaces, and the interfaces that meet the filter conditions will be generated.",
"option_tag": "Specify tags from the interface url for filenames, the first one of tags is read by default.",
"option_version": "View version information.",
"option_conjunction": "Conjunction of method name. [default: \"By\"]."
"option_conjunction": "Conjunction of method name. [default: \"By\"].",
"option_actionIndex": "Method name index. [default: -1]."
},
"plugin": {
"name": "Load plugin {{name}}",
Expand Down
6 changes: 4 additions & 2 deletions src/i18n/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"parserTypeError": "{{type}} 类型无法解析。"
},
"path": {
"notName": "{{url}} 的 {{method}} 无法获取方法名称,故忽略。"
"notName": "{{url}} {{method}} 无法获取方法名称,故忽略。",
"duplicate": "{{url}} {{method}} 的 {{name}} 方法名重复,可设置 `--actionIndex` 选项。"
},
"app": {
"apiJsonFileError": "API 文件解析失败。原因:{{error}}",
Expand All @@ -39,7 +40,8 @@
"option_filter": "过滤接口,符合过滤条件的接口会被生成。",
"option_tag": "从接口 url 中指定标签,用于文件名,默认读取 tags 的第一个。",
"option_version": "显示版本信息。",
"option_conjunction": "方法名的连接词。 [默认: \"By\"]。"
"option_conjunction": "方法名的连接词。 [默认: \"By\"]。",
"option_actionIndex": "方法名的索引。 [默认: -1]。"
},
"plugin": {
"name": "加载插件 {{name}}",
Expand Down
2 changes: 1 addition & 1 deletion src/npm/pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@loongwoo/stc",
"version": "2.6.1",
"version": "2.7.0",
"description": "A tool for converting OpenApi/Swagger/Apifox into code.",
"type": "module",
"module": "esm/mod.js",
Expand Down
4 changes: 4 additions & 0 deletions src/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ export interface ISwaggerOptions {
* 方法名的连接词,默认值为 By
*/
readonly conjunction?: string;
/**
* 方法名的下标,默认值为 -1
*/
readonly actionIndex?: number;
}

export interface IDefinitionNameMapping {
Expand Down
Loading

0 comments on commit 399a977

Please sign in to comment.