-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: joi schema generate config types (#4325)
* feat: joi schema generate config types * fix: types * fix: deps * fix: lodash * fix: schema validate * fix: add config types * feat: support types comments * fix: types from @umijs/types * fix: config types * fix: types * feat: routes types * fix: additionalProperties * fix: joi2types version * fix: auto generate types, no need to define * fix: joi spell * fix: types
- Loading branch information
信鑫-King
committed
Apr 7, 2020
1 parent
0bc87c2
commit 443e8c2
Showing
10 changed files
with
889 additions
and
1,138 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
33 changes: 33 additions & 0 deletions
33
packages/preset-built-in/src/plugins/generateFiles/core/configTypes.ts
This file contains 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,33 @@ | ||
import { IApi } from '@umijs/types'; | ||
import joi2Types from 'joi2types'; | ||
import joi from '@hapi/joi'; | ||
|
||
export default (api: IApi) => { | ||
api.onGenerateFiles(async () => { | ||
const { service } = api; | ||
const properties = Object.keys(service.plugins).map(plugin => { | ||
const { config, key } = service.plugins[plugin]; | ||
// recognize as key if have schema config | ||
if (!config?.schema) return; | ||
const schema = config.schema(joi); | ||
if (!joi.isSchema(schema)) { | ||
return; | ||
} | ||
return { | ||
[key]: schema, | ||
}; | ||
}) | ||
.reduce((acc, curr) => ({ | ||
...acc, | ||
...curr, | ||
}), {}); | ||
const content = await joi2Types(joi.object(properties).unknown(), { | ||
interfaceName: 'IConfigFromPlugins', | ||
bannerComment: '/** Created by Umi Plugin **/', | ||
}); | ||
api.writeTmpFile({ | ||
path: 'core/pluginConfig.d.ts', | ||
content, | ||
}) | ||
}) | ||
}; |
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { IConfig } from '@umijs/types'; | ||
// @ts-ignore | ||
import { IConfigFromPlugins } from '@@/core/pluginConfig'; | ||
|
||
export function defineConfig(config: IConfig) { | ||
// IConfig types is prior to IConfigFromPlugins in the same key. | ||
export function defineConfig(config: IConfigFromPlugins | IConfig): IConfigFromPlugins | IConfig { | ||
return config; | ||
} |
Oops, something went wrong.