Skip to content

Commit 8d4a7c7

Browse files
feat: add isSplitTypesByModule func (#602)
* feat: add splitTypesByModule func * chore: clean code * fix: fix splitTypesByModule func * chore: clean code * perf: perf serviceGenarator code * chore: clean code * perf: rename splitTypesByModule => isSplitTypesByModule
1 parent 361cb27 commit 8d4a7c7

16 files changed

Lines changed: 9955 additions & 398 deletions

.changeset/common-bears-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': minor
3+
---
4+
5+
feat: add isSplitTypesByModule func

README-en_US.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ $ openapi --help
187187
-cfn, --configFileName <string> config file name
188188
-cfp, --configFilePath <string> config file path
189189
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch" (default: "axios")
190+
--isSplitTypesByModule <boolean> split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts (default: false)
190191
-f, --full <boolean> full replacement (default: true)
191192
--enableLogging <boolean> open the log (default: false)
192193
--priorityRule <string> priority rule, include/exclude/both (default: "include")
@@ -227,6 +228,7 @@ openapi -i ./spec.json -o ./apis
227228
| schemaPath | yes | string | - | Swagger2/OpenAPI3 URL |
228229
| serversPath | no | string | './src/apis' | the folder path for the run results |
229230
| requestLibPath | no | string | 'axios' | custom request lib path, for example: '@/request', 'node-fetch' |
231+
| isSplitTypesByModule | no | boolean | false | split types by module, generates {module}.type.ts (module types), common.type.ts (common types), enum.ts (enum types), types.ts (unified export) |
230232
| full | no | boolean | true | full replacement |
231233
| describe | no | string | - | description information, which will be used when using cli interactive operation mode |
232234
| enableLogging | no | boolean | false | open the log |

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ $ openapi --help
187187
-cfn, --configFileName <string> config file name
188188
-cfp, --configFilePath <string> config file path
189189
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch" (default: "axios")
190+
--isSplitTypesByModule <boolean> split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts (default: false)
190191
-f, --full <boolean> full replacement (default: true)
191192
--enableLogging <boolean> open the log (default: false)
192193
--priorityRule <string> priority rule, include/exclude/both (default: "include")
@@ -227,6 +228,7 @@ openapi --i ./spec.json --o ./apis
227228
| schemaPath || string | - | Swagger2/OpenAPI3 地址 |
228229
| serversPath || string | './src/apis' | 运行结果文件夹路径 |
229230
| requestLibPath || string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
231+
| isSplitTypesByModule || boolean | false | 按模块拆分类型文件,开启后会生成:{module}.type.ts(各模块类型)、common.type.ts(公共类型)、enum.ts(枚举类型)、types.ts(统一导出) |
230232
| full || boolean | true | 是否全量替换 |
231233
| describe || string | - | 描述信息,在用 cli 可交互运行方式时会用到 |
232234
| enableLogging || boolean | false | 是否开启日志 |

agents.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ else if (isNodeProject) {
7070
| `isCamelCase` | boolean | `true` | Use camelCase naming | Consistent naming |
7171
| `isSupportParseEnumDesc` | boolean | `false` | Parse enum descriptions | Labeled enums |
7272
| `supportParseEnumDescByReg` | `string \| RegExp` | - | Custom regex for parsing enum descriptions. If set, replaces default parseDescriptionEnum method. Example: `/([^\s=<>/&;]+(?:\s+[^\s=<>/&;]+)*)\s*=\s*(\d+)/g` matches "普通 = 0" or "SampleMaker = 1" | Custom enum description formats |
73+
| `isSplitTypesByModule` | boolean | `false` | Split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts | Large projects with many types |
7374

7475
#### Request Customization
7576

@@ -122,6 +123,7 @@ else if (isNodeProject) {
122123
| "自定义枚举解析正则" | `supportParseEnumDescByReg: /pattern/g` | Custom enum description parsing |
123124
| "生成JSON验证" | `isGenJsonSchemas: true` | Schema validation |
124125
| "前缀API路径" | `apiPrefix: "'api'"` | Add prefix to paths |
126+
| "按模块拆分类型" | `isSplitTypesByModule: true` | Large projects, better code organization |
125127

126128
## 📋 Common Configuration Scenarios
127129

@@ -277,6 +279,24 @@ else if (isNodeProject) {
277279
}
278280
```
279281

282+
### Scenario 11: Large Project with Module-Based Type Splitting
283+
284+
```typescript
285+
{
286+
schemaPath: "./openapi.json",
287+
serversPath: "./src/apis",
288+
isSplitTypesByModule: true, // Split types by module
289+
isGenReactQuery: true,
290+
isDisplayTypeLabel: true,
291+
isCamelCase: true,
292+
// Generates:
293+
// - {module}.type.ts (module-specific types)
294+
// - common.type.ts (shared types)
295+
// - enum.ts (all enums)
296+
// - types.ts (unified export)
297+
}
298+
```
299+
280300
## ⚠️ Configuration Conflicts & Rules
281301

282302
### ❌ Incompatible Combinations
@@ -304,6 +324,7 @@ else if (isNodeProject) {
304324
- **Development**: `mockFolder + enableLogging + full: false`
305325
- **International**: `isTranslateToEnglishTag + isDisplayTypeLabel + isSupportParseEnumDesc`
306326
- **Type-only libraries**: `isOnlyGenTypeScriptType + isGenJsonSchemas + namespace`
327+
- **Large projects**: `isSplitTypesByModule: true + isGenReactQuery + isDisplayTypeLabel`
307328

308329
## 🔧 Advanced Customization Hooks
309330

@@ -546,6 +567,8 @@ export const ${api.functionName} = async (${api.params}) => {
546567

547568
## 📁 Generated File Structure
548569

570+
### Default Structure (isSplitTypesByModule: false)
571+
549572
```
550573
src/apis/
551574
├── index.ts # Main entry point with all exports
@@ -560,6 +583,25 @@ src/apis/
560583
└── ...
561584
```
562585

586+
### Module-Based Structure (isSplitTypesByModule: true)
587+
588+
```
589+
src/apis/
590+
├── index.ts # Main entry point with all exports
591+
├── types.ts # Unified type exports
592+
├── enum.ts # All enum type definitions
593+
├── common.type.ts # Shared/common types used by multiple modules
594+
├── [module].type.ts # Module-specific types (e.g., user.type.ts, order.type.ts)
595+
├── [controller].ts # API functions grouped by tags
596+
├── displayTypeLabel.ts # Chinese field labels (if enabled)
597+
├── displayEnumLabel.ts # Enum translations (if enabled)
598+
├── schema.ts # JSON Schemas (if enabled)
599+
├── reactquery.ts # React Query hooks (if enabled)
600+
└── mocks/ # Mock data files (if enabled)
601+
├── [endpoint].mock.ts
602+
└── ...
603+
```
604+
563605
## 🚀 Performance Optimization Tips
564606

565607
### For Large APIs (100+ endpoints)

src/bin/openapi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const params = program
2323
'--requestLibPath <string>',
2424
'custom request lib path, for example: "@/request", "node-fetch" (default: "axios")'
2525
)
26+
.option(
27+
'--isSplitTypesByModule <boolean>',
28+
'split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts',
29+
false
30+
)
2631
.option('-f, --full <boolean>', 'full replacement', true)
2732
.option('--enableLogging <boolean>', 'open the log', false)
2833
.option(
@@ -151,6 +156,8 @@ const baseGenerate = (_params_: OptionValues): GenerateServiceProps => {
151156
supportParseEnumDescByReg: _params_.supportParseEnumDescByReg as
152157
| string
153158
| RegExp,
159+
isSplitTypesByModule:
160+
JSON.parse(_params_.isSplitTypesByModule as string) === true,
154161
};
155162

156163
return options;

src/generator/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const displayTypeLabelFileName = 'displayTypeLabel';
1212

1313
export const schemaFileName = 'schema';
1414

15+
export const enumFileName = 'enum';
16+
17+
export const commonTypeFileName = 'common.type';
18+
1519
export const displayReactQueryFileName = (reactQueryMode: IReactQueryMode) => {
1620
return {
1721
react: 'reactquery',
@@ -27,6 +31,9 @@ export enum TypescriptFileType {
2731
displayTypeLabel = 'displayTypeLabel',
2832
schema = 'schema',
2933
reactQuery = 'reactQuery',
34+
moduleType = 'moduleType',
35+
typeIndex = 'typeIndex',
36+
enum = 'enum',
3037
}
3138

3239
export const DEFAULT_SCHEMA: SchemaObject = {

0 commit comments

Comments
 (0)