-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openapi): support openapi plugins (#527)
* feat(openapi): support openapi plugins * fix bug * support joi * use openAPI * add more options * support doc * fix path * remoe unuse * remove unuse code * use default style * support windows
- Loading branch information
1 parent
4d1c077
commit 516a266
Showing
5 changed files
with
2,321 additions
and
83 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
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 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 <sorrycc@gmail.com> (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", | ||
"swagger-ui-react": "^3.41.1" | ||
} | ||
} |
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,113 @@ | ||
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', | ||
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) { | ||
// console.log(error); | ||
} | ||
|
||
// 增加中间件 | ||
api.addMiddewares(() => { | ||
return serveStatic(openAPIFilesPath); | ||
}); | ||
|
||
api.onGenerateFiles(() => { | ||
api.writeTmpFile({ | ||
path: join('plugin-openapi', 'openapi.tsx'), | ||
content: ` | ||
import SwaggerUI from "swagger-ui-react"; | ||
import "swagger-ui-react/swagger-ui.css"; | ||
const App = () => ( | ||
<div | ||
style={{ | ||
padding: 24, | ||
}} | ||
> | ||
<SwaggerUI url="/umi-plugins_openapi.json" /> | ||
</div> | ||
); | ||
export default App; | ||
`, | ||
}); | ||
}); | ||
|
||
if (api.env === 'development') { | ||
api.modifyRoutes((routes) => { | ||
return [ | ||
{ | ||
path: '/umi/plugin/openapi', | ||
component: api.utils.winPath( | ||
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.