-
Notifications
You must be signed in to change notification settings - Fork 235
feat(openapi): support openapi plugins #527
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 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7e8f9a6
feat(openapi): support openapi plugins
chenshuai2144 dfffd2e
fix bug
chenshuai2144 850027b
support joi
chenshuai2144 e8d217f
use openAPI
chenshuai2144 01f0b0b
add more options
chenshuai2144 2ead10e
support doc
chenshuai2144 64f8265
fix path
chenshuai2144 ec2fe75
remoe unuse
chenshuai2144 e9dea05
remove unuse code
chenshuai2144 b82a866
use default style
chenshuai2144 eb8b9e6
support windows
chenshuai2144 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
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,19 @@ | ||
| # @umijs/plugin-openapi | ||
|
|
||
| > @umijs/plugin-openapi. | ||
|
|
||
| See our website [@umijs/plugin-openapi](https://umijs.org/plugins/plugin-openapi) for more information. | ||
|
|
||
| ## Install | ||
|
|
||
| Using npm: | ||
|
|
||
| ```bash | ||
| $ npm install --save-dev @umijs/plugin-openapi | ||
| ``` | ||
|
|
||
| or using yarn: | ||
|
|
||
| ```bash | ||
| $ yarn add @umijs/plugin-openapi --dev | ||
| ``` |
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,35 @@ | ||
| { | ||
| "name": "@umijs/plugin-openapi", | ||
| "version": "1.0.4", | ||
| "description": "@umijs/plugin-openapi", | ||
| "main": "lib/index.js", | ||
| "types": "lib/index.d.ts", | ||
| "files": [ | ||
| "lib", | ||
| "src" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/umijs/plugins" | ||
| }, | ||
| "keywords": [ | ||
| "umi" | ||
| ], | ||
| "authors": [ | ||
| "chencheng <[email protected]> (https://github.com/sorrycc)" | ||
| ], | ||
| "license": "MIT", | ||
| "bugs": "http://github.com/umijs/plugins/issues", | ||
| "homepage": "https://github.com/umijs/plugins/tree/master/packages/plugin-openapi#readme", | ||
| "peerDependencies": { | ||
| "umi": "3.x" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| "@umijs/openapi": "^1.1.6", | ||
| "serve-static": "^1.14.1", | ||
chenshuai2144 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "swagger-ui-react": "^3.41.1" | ||
| } | ||
| } | ||
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,108 @@ | ||
| import { join } from 'path'; | ||
| import { IApi } from 'umi'; | ||
| import rimraf from 'rimraf'; | ||
| import serveStatic from 'serve-static'; | ||
| import { generateService, getSchema } from '@umijs/openapi'; | ||
| import { existsSync, mkdirSync, writeFileSync } from 'fs'; | ||
|
|
||
| export default (api: IApi) => { | ||
| api.describe({ | ||
| key: 'openAPI', | ||
chenshuai2144 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| config: { | ||
| schema(joi) { | ||
| return joi.object({ | ||
| requestLibPath: joi.string(), | ||
| schemaPath: joi.string(), | ||
| mock: joi.boolean(), | ||
| projectName: joi.string(), | ||
| }); | ||
| }, | ||
| }, | ||
| enableBy: api.EnableBy.config, | ||
| }); | ||
| const { absNodeModulesPath, absTmpPath } = api.paths; | ||
| const openAPIFilesPath = join(absNodeModulesPath!, 'umi_open_api'); | ||
|
|
||
| try { | ||
| if (existsSync(openAPIFilesPath)) { | ||
| rimraf.sync(openAPIFilesPath); | ||
| } | ||
| mkdirSync(join(openAPIFilesPath)); | ||
| } catch (error) { | ||
chenshuai2144 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // console.log(error); | ||
| } | ||
|
|
||
| // 增加中间件 | ||
| api.addMiddewares(() => { | ||
chenshuai2144 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return serveStatic(openAPIFilesPath); | ||
| }); | ||
|
|
||
| api.onGenerateFiles(() => { | ||
| api.writeTmpFile({ | ||
| path: join('plugin-openapi', 'openapi.tsx'), | ||
| content: ` | ||
| import SwaggerUI from 'swagger-ui-react'; | ||
chenshuai2144 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import 'swagger-ui-react/swagger-ui.css'; | ||
| import { Card } from 'antd'; | ||
chenshuai2144 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const App = () => ( | ||
| <Card> | ||
| <SwaggerUI url="/umi-plugins_openapi.json" /> | ||
| </Card> | ||
| ); | ||
| export default App; | ||
| `, | ||
| }); | ||
| }); | ||
|
|
||
| if (api.env === 'development') { | ||
| api.modifyRoutes((routes) => { | ||
| return [ | ||
| { | ||
| path: '/umi/plugin/openapi', | ||
| component: join(absTmpPath!, 'plugin-openapi', 'openapi.tsx'), | ||
| }, | ||
| ...routes, | ||
| ]; | ||
| }); | ||
| } | ||
|
|
||
| api.onDevCompileDone(async () => { | ||
| try { | ||
| const openAPIConfig = api.config.openAPI; | ||
| const openAPIJson = await getSchema(openAPIConfig.schemaPath); | ||
| writeFileSync( | ||
| join(openAPIFilesPath, 'umi-plugins_openapi.json'), | ||
| JSON.stringify(openAPIJson, null, 2), | ||
| ); | ||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
| }); | ||
|
|
||
| api.registerCommand({ | ||
| name: 'openapi', | ||
| fn: async () => { | ||
| const openAPIConfig = api.config.openAPI; | ||
| const pageConfig = require(join(api.cwd, 'package.json')); | ||
| const mockFolder = openAPIConfig.mock ? join(api.cwd, 'mock') : undefined; | ||
| const serversFolder = join(api.cwd, 'src', 'services'); | ||
| // 如果mock 文件不存在,创建一下 | ||
| if (mockFolder && !existsSync(mockFolder)) { | ||
| mkdirSync(mockFolder); | ||
| } | ||
| // 如果mock 文件不存在,创建一下 | ||
| if (serversFolder && !existsSync(serversFolder)) { | ||
| mkdirSync(serversFolder); | ||
| } | ||
|
|
||
| await generateService({ | ||
| projectName: pageConfig.name.split('/').pop(), | ||
| ...openAPIConfig, | ||
| serversPath: serversFolder, | ||
| mockFolder, | ||
| }); | ||
| api.logger.info('[openAPI]: execution complete'); | ||
| }, | ||
| }); | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.