Skip to content

Commit

Permalink
Merge pull request #90 from long-woo/dev
Browse files Browse the repository at this point in the history
feat: filter 选项使用 micromatch
  • Loading branch information
long-woo authored Nov 28, 2024
2 parents be1ade5 + 430c73b commit b4a8305
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ App<IAppOption>({
| client | | string | axios | http request client. When `lang` is `ts/js`, the possible values ​​are: `axios`, `wechat`, `fetch`. |
| lang | l | string | ts | Language, used for output file suffix. |
| 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` |
| 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`. |
| version | v | boolean | | Output version information. |
| help | h | boolean | | Output help information. |
Expand Down
29 changes: 8 additions & 21 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import micromatch from "npm:micromatch";

import Logs from "./console.ts";
import type {
IDefaultObject,
Expand Down Expand Up @@ -411,21 +413,6 @@ const getPathVirtualProperty = (
return value;
};

const parserFilter = (filters: string[]) => {
if (!filters.length) return undefined;

const regStr = filters.reduce((prev, current) => {
const _str = current.replace(/\//g, "\\/").replace(/\*/g, ".*").replace(
/\?/g,
"\\?",
);

return `${prev ? `${prev}|` : ""}(${_str})`;
}, "");

return new RegExp(regStr);
};

/**
* 获取接口地址对象
* @param paths - 接口地址
Expand All @@ -439,18 +426,18 @@ export const getApiPath = (

Object.keys(paths).forEach((url) => {
// 过滤接口,符合过滤条件的接口会被生成
if (options?.filter?.length && !parserFilter(options.filter)?.test(url)) {
return;
}
if (
options?.filter?.length && !micromatch.all(url, options.filter, {
bash: true,
})
) return;

// 请求方式
const methods = paths[url];

Object.keys(methods).forEach((method) => {
// url去除 `?` 之后的字符
if (url.includes("?")) {
url = url.slice(0, url.indexOf("?"));
}
if (url.includes("?")) url = url.slice(0, url.indexOf("?"));

const currentMethod = methods[method];
// 方法名
Expand Down
8 changes: 4 additions & 4 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ Deno.test("filter", async () => {
"-A",
"src/main.ts",
"--url=https://petstore3.swagger.io/api/v3/openapi.json",
"--filter=/pet/*",
"-f=/user/*",
"--filter=/user*",
"-f=!/user/createWithList",
],
});
const { code } = await command.output();
const { code, stdout } = await command.output();

// console.log(new TextDecoder().decode(stdout));
console.log(new TextDecoder().decode(stdout));
assertEquals(0, code);
});

Expand Down

0 comments on commit b4a8305

Please sign in to comment.