-
Notifications
You must be signed in to change notification settings - Fork 59
🎯 feat: Add customRenderTemplateData hook for fine-grained file generation control #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
afde678
feat: added customGenFileFromTemplateList hook functionality
zwmmm f113c6f
refactor: customGenFileFromTemplateList -> customRenderTemplateData
zwmmm 505e8bc
refactor: customGenFileFromTemplateList.spec -> customRenderTemplateD…
zwmmm d2493ce
refactor: replace string constants with TypescriptFileType to enhance…
zwmmm ca2448f
chore: update changelog
rookie-luochao 3098497
refactor: remove customGenFileFromTemplateList test
zwmmm 1d1fda8
chore: revert 测试空的 schema 引用.snap
rookie-luochao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openapi-ts-request': minor | ||
| --- | ||
|
|
||
| Added customGenFileFromTemplateList hook functionality | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,6 @@ import { | |
| // resolveFunctionName, | ||
| resolveRefs, | ||
| resolveTypeName, | ||
| // stripDot, | ||
| } from './util'; | ||
|
|
||
| export default class ServiceGenerator { | ||
|
|
@@ -865,6 +864,101 @@ export default class ServiceGenerator { | |
| ): boolean { | ||
| try { | ||
| const template = this.getTemplate(type); | ||
|
|
||
| // 应用 customGenFileFromTemplateList hook (如果存在) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hook命名调整一下:customRenderTemplateData |
||
| let processedParams = { ...params }; | ||
| const customListHooks = this.config.hook?.customGenFileFromTemplateList; | ||
|
|
||
| if (customListHooks && params.list) { | ||
| try { | ||
| const context = { | ||
| fileName, | ||
| params: processedParams, | ||
| }; | ||
|
|
||
| let processedList = params.list; | ||
|
|
||
| // 根据不同的文件类型调用相应的 hook 函数 | ||
| switch (type) { | ||
| case 'serviceController': | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case TypescriptFileType.serviceController |
||
| if (customListHooks.serviceController) { | ||
| processedList = customListHooks.serviceController( | ||
| params.list as APIDataType[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| case 'reactQuery': | ||
| if (customListHooks.reactQuery) { | ||
| processedList = customListHooks.reactQuery( | ||
| params.list as APIDataType[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| case 'interface': | ||
| if (customListHooks.interface) { | ||
| processedList = customListHooks.interface( | ||
| params.list as ITypeItem[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| case 'displayEnumLabel': | ||
| if (customListHooks.displayEnumLabel) { | ||
| processedList = customListHooks.displayEnumLabel( | ||
| params.list as ITypeItem[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| case 'displayTypeLabel': | ||
| if (customListHooks.displayTypeLabel) { | ||
| processedList = customListHooks.displayTypeLabel( | ||
| params.list as ITypeItem[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| case 'schema': | ||
| if (customListHooks.schema) { | ||
| processedList = customListHooks.schema( | ||
| params.list as ISchemaItem[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| case 'serviceIndex': | ||
| if (customListHooks.serviceIndex) { | ||
| processedList = customListHooks.serviceIndex( | ||
| params.list as ControllerType[], | ||
| context | ||
| ); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| if (processedList !== params.list) { | ||
| processedParams = { | ||
| ...processedParams, | ||
| list: processedList, | ||
| }; | ||
| this.log( | ||
| `customGenFileFromTemplateList hook applied for ${type}: ${fileName}` | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| `[GenSDK] customGenFileFromTemplateList hook error for ${type}:`, | ||
| error | ||
| ); | ||
| this.log( | ||
| `customGenFileFromTemplateList hook failed for ${type}, using original list` | ||
| ); | ||
| // 发生错误时使用原始参数继续执行 | ||
| } | ||
| } | ||
|
|
||
| // 设置输出不转义 | ||
| const env = nunjucks.configure({ | ||
| autoescape: false, | ||
|
|
@@ -874,7 +968,7 @@ export default class ServiceGenerator { | |
| const destPath = join(this.config.serversPath, fileName); | ||
| const destCode = nunjucks.renderString(template, { | ||
| disableTypeCheck: false, | ||
| ...params, | ||
| ...processedParams, | ||
| }); | ||
| let mergerProps: MergeOption = {} as MergeOption; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feat: Added customRenderTemplateData hook functionality