Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugin-layout/src/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect } from 'react';
// @ts-ignore
import { Link, useModel, history, formatMessage } from 'umi';
import { Link, useModel, history } from 'umi';
import ProLayout, { BasicLayoutProps } from '@ant-design/pro-layout';
import './style.less';
import renderRightContent from './renderRightContent';
Expand Down
19 changes: 19 additions & 0 deletions packages/plugin-openapi/README.md
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
```
35 changes: 35 additions & 0 deletions packages/plugin-openapi/package.json
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",
"swagger-ui-react": "^3.41.1"
}
}
108 changes: 108 additions & 0 deletions packages/plugin-openapi/src/index.ts
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',
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';
import { Card } from 'antd';

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');
},
});
};
Loading