-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
2,301 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* eslint-disable global-require */ | ||
/* eslint-disable import/no-dynamic-require */ | ||
import http from 'http'; | ||
import https from 'https'; | ||
import fetch from 'node-fetch'; | ||
import converter from 'swagger2openapi'; | ||
import type { OperationObject, SchemaObject } from 'openapi3-ts'; | ||
import type { Options as prettierOptions } from 'prettier'; | ||
|
||
import Log from './log'; | ||
import { mockGenerator } from './mockGenerator'; | ||
import { ServiceGenerator } from './serviceGenerator'; | ||
|
||
const getImportStatement = (requestLibPath: string) => { | ||
if (requestLibPath && requestLibPath.startsWith('import')) { | ||
return requestLibPath; | ||
} | ||
if (requestLibPath) { | ||
return `import request from '${requestLibPath}'`; | ||
} | ||
return `import { request } from "umi"`; | ||
}; | ||
|
||
export type GenerateServiceProps = { | ||
requestLibPath?: string; | ||
requestImportStatement?: string; | ||
/** | ||
* api 的前缀 | ||
*/ | ||
apiPrefix?: | ||
| string | ||
| ((params: { | ||
path: string; | ||
method: string; | ||
namespace: string; | ||
functionName: string; | ||
autoExclude?: boolean; | ||
}) => string); | ||
/** | ||
* 生成的文件夹的路径 | ||
*/ | ||
serversPath?: string; | ||
/** | ||
* Swagger 2.0 或 OpenAPI 3.0 的地址 | ||
*/ | ||
schemaPath?: string; | ||
/** | ||
* 项目名称 | ||
*/ | ||
projectName?: string; | ||
|
||
hook?: { | ||
/** 自定义函数名称 */ | ||
customFunctionName?: (data: OperationObject) => string; | ||
/** 自定义类型名称 */ | ||
customTypeName?: (data: OperationObject) => string; | ||
/** 自定义类名 */ | ||
customClassName?: (tagName: string) => string; | ||
/** 自定义swagger ref类型 */ | ||
customRefType?: (data: { | ||
schemaName: string; | ||
typeName: string; | ||
props: (SchemaObject & { name: string; type: string; desc: string; required: string })[]; | ||
}) => (SchemaObject & { name: string; type: string; desc: string; required: string })[]; | ||
}; | ||
|
||
namespace?: string; | ||
|
||
mockFolder?: string; | ||
/** | ||
* 模板文件的文件路径 | ||
*/ | ||
templatesFolder?: string; | ||
|
||
/** | ||
* 枚举样式 | ||
*/ | ||
enumStyle?: 'string-literal' | 'enum'; | ||
|
||
/** | ||
* 增量修改 | ||
*/ | ||
increment?: { | ||
/** 类型增量 */ | ||
types?: boolean; | ||
|
||
/** controller 白名单增量 */ | ||
includes?: string[]; | ||
|
||
// /** controller 黑名单 */ | ||
// excludes?: string[]; | ||
}; | ||
|
||
/** 自定义 prettier 选项 */ | ||
prettierOptions?: prettierOptions; | ||
}; | ||
|
||
const converterSwaggerToOpenApi = (swagger: any) => { | ||
if (!swagger.swagger) { | ||
return swagger; | ||
} | ||
return new Promise((resolve, reject) => { | ||
converter.convertObj(swagger, {}, (err, options) => { | ||
Log(['💺 将 Swagger 转化为 openAPI']); | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(options.openapi); | ||
}); | ||
}); | ||
}; | ||
|
||
export const getSchema = async (schemaPath: string) => { | ||
if (schemaPath.startsWith('http')) { | ||
const protocol = schemaPath.startsWith('https:') ? https : http; | ||
try { | ||
const agent = new protocol.Agent({ | ||
rejectUnauthorized: false, | ||
}); | ||
const json = await fetch(schemaPath, { agent }).then((rest) => rest.json()); | ||
return json; | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.log('fetch openapi error:', error); | ||
} | ||
return null; | ||
} | ||
const schema = require(schemaPath); | ||
return schema; | ||
}; | ||
|
||
const getOpenAPIConfig = async (schemaPath: string) => { | ||
const schema = await getSchema(schemaPath); | ||
if (!schema) { | ||
return null; | ||
} | ||
const openAPI = await converterSwaggerToOpenApi(schema); | ||
return openAPI; | ||
}; | ||
|
||
// 从 appName 生成 service 数据 | ||
export const generateService = async ({ | ||
requestLibPath, | ||
schemaPath, | ||
mockFolder, | ||
...rest | ||
}: GenerateServiceProps) => { | ||
const openAPI = await getOpenAPIConfig(schemaPath); | ||
const requestImportStatement = getImportStatement(requestLibPath); | ||
const serviceGenerator = new ServiceGenerator( | ||
{ | ||
namespace: 'API', | ||
requestImportStatement, | ||
enumStyle: 'string-literal', | ||
...rest, | ||
}, | ||
openAPI, | ||
); | ||
serviceGenerator.genFile(); | ||
|
||
if (mockFolder) { | ||
await mockGenerator({ | ||
openAPI, | ||
mockFolder: mockFolder || './mocks/', | ||
}); | ||
} | ||
}; |
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,6 @@ | ||
import chalk from 'chalk'; | ||
|
||
// eslint-disable-next-line no-console | ||
const Log = (...rest) => console.log(`${chalk.blue('[openAPI]')}: ${rest.join('\n')}`); | ||
|
||
export default Log; |
Oops, something went wrong.