moyan-api 1.0.85 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -1
- package/.eslintrc.js +0 -28
- package/.gitee/ISSUE_TEMPLATE.zh-CN.md +0 -13
- package/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +0 -11
- package/.prettierrc +0 -5
- package/CHANGELOG.md +0 -209
- package/src/creator.ts +0 -41
- package/src/index.ts +0 -11
- package/src/lib/base.ts +0 -260
- package/src/main.ts +0 -110
- package/src/program.ts +0 -17
- package/src/template/api.ts +0 -123
- package/src/template/schemas.ts +0 -95
- package/src/utils/mo.ts +0 -402
- package/tsconfig.json +0 -21
- package/view/api.ejs +0 -35
- package/view/schemas.ejs +0 -19
package/src/main.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import * as path from "path"
|
|
3
|
-
import MFManage, { mFconfig, smfType, SchemaManager } from "moyan-file-model"
|
|
4
|
-
import axios from 'axios'
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { ApiTemplate } from "./template/api"
|
|
8
|
-
import { SchemasTemplate } from "./template/schemas"
|
|
9
|
-
import { Command } from "commander"
|
|
10
|
-
import { Program } from "./program"
|
|
11
|
-
|
|
12
|
-
export class ApisdkCreator {
|
|
13
|
-
mFManage: MFManage
|
|
14
|
-
error: Array<any> = []
|
|
15
|
-
program:Command|Program
|
|
16
|
-
|
|
17
|
-
get runPath() {
|
|
18
|
-
const g = process.env.INIT_CWD
|
|
19
|
-
return g
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get packageData(){
|
|
23
|
-
return require(path.resolve('package.json'))
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get outputPath(){
|
|
27
|
-
return this.program.getOptionValue('output')|| path.resolve(this.packageData['moyan-api'] && this.packageData['moyan-api']['output']? this.packageData['moyan-api']['output']:"./src")
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get openAPIObject(){
|
|
31
|
-
const file = this.program.getOptionValue('apijson')|| (this.packageData['moyan-api'] && this.packageData['moyan-api']['apijson']?this.packageData['moyan-api']['apijson']:'moyan.api.json')
|
|
32
|
-
console.info('获取本地数据:'+file)
|
|
33
|
-
const data = require(path.resolve(file))
|
|
34
|
-
return data
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get prefix(){
|
|
38
|
-
return this.program.getOptionValue('apiprefix') || (this.packageData['moyan-api'] && this.packageData['moyan-api']['apiprefix'] ?this.packageData['moyan-api']['apiprefix']:'Api')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get pathPrefix(){
|
|
42
|
-
return this.program.getOptionValue('pathprefix') || (this.packageData['moyan-api'] && this.packageData['moyan-api']['pathprefix'] ?this.packageData['moyan-api']['pathprefix']:'')
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get dirname(){
|
|
46
|
-
return this.program.getOptionValue('dirname') || (this.packageData['moyan-api'] && this.packageData['moyan-api']['dirname'] ?this.packageData['moyan-api']['dirname']:'api')
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get jsonurl(){
|
|
50
|
-
return this.program.getOptionValue('jsonurl') || (this.packageData['moyan-api'] && this.packageData['moyan-api']['jsonurl'] ?this.packageData['moyan-api']['jsonurl']:'')
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
constructor(program:Command|Program) {
|
|
54
|
-
this.program = program
|
|
55
|
-
mFconfig.modulesPath = this.outputPath
|
|
56
|
-
mFconfig.modulesTemplatePath = path.resolve(__dirname, './')
|
|
57
|
-
mFconfig.modulesTemplateViewsName = path.resolve(__dirname,"../view")
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
schemaManagerData = {}
|
|
61
|
-
|
|
62
|
-
schemaManager: SchemaManager
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
async getRemoteData(){
|
|
66
|
-
const result = await axios.get( this.jsonurl)
|
|
67
|
-
return result.data
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async init() {
|
|
71
|
-
let openAPIObject :any= {}
|
|
72
|
-
if(this.jsonurl){
|
|
73
|
-
console.log('获取远程数据:'+this.jsonurl)
|
|
74
|
-
openAPIObject = await this.getRemoteData()
|
|
75
|
-
}else{
|
|
76
|
-
openAPIObject = this.openAPIObject
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if(!openAPIObject.openapi){
|
|
80
|
-
throw new Error('api 数据错误')
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
this.schemaManagerData = {
|
|
84
|
-
type: smfType.dir,
|
|
85
|
-
name: this.dirname,
|
|
86
|
-
children: [
|
|
87
|
-
{
|
|
88
|
-
type: smfType.ts,
|
|
89
|
-
name: "index",
|
|
90
|
-
template: new ApiTemplate(openAPIObject,this.prefix,this.pathPrefix)
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
type: smfType.ts,
|
|
94
|
-
name: "schemas",
|
|
95
|
-
template: new SchemasTemplate(openAPIObject)
|
|
96
|
-
}
|
|
97
|
-
]
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async create() {
|
|
102
|
-
await this.init()
|
|
103
|
-
this.mFManage = new MFManage()
|
|
104
|
-
this.schemaManager = new SchemaManager(this.schemaManagerData)
|
|
105
|
-
this.mFManage.run(this.schemaManager)
|
|
106
|
-
return this.error
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
package/src/program.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export class Program {
|
|
2
|
-
options:{
|
|
3
|
-
apijson?:string
|
|
4
|
-
output?:string
|
|
5
|
-
jsonurl?:string
|
|
6
|
-
apiprefix?:string
|
|
7
|
-
pathprefix?:string
|
|
8
|
-
dirname?:string
|
|
9
|
-
}
|
|
10
|
-
constructor(options:Program['options']){
|
|
11
|
-
this.options=options
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
getOptionValue(key: string):any{
|
|
15
|
-
return (this.options as any)[key]
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/template/api.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { OpenAPIObject } from '@nestjs/swagger'
|
|
2
|
-
import { Temp, Template, smfType } from 'moyan-file-model'
|
|
3
|
-
|
|
4
|
-
export class ApiTemplate extends Temp implements Template {
|
|
5
|
-
openAPIObject: OpenAPIObject
|
|
6
|
-
|
|
7
|
-
list: Array<any> = []
|
|
8
|
-
|
|
9
|
-
schemaKeys = []
|
|
10
|
-
|
|
11
|
-
prefix = ''
|
|
12
|
-
|
|
13
|
-
pathPrefix = ''
|
|
14
|
-
|
|
15
|
-
constructor(openAPIObject: OpenAPIObject, prefix: string = '', pathPrefix = '') {
|
|
16
|
-
super(smfType.ts)
|
|
17
|
-
this.prefix = prefix
|
|
18
|
-
this.pathPrefix = pathPrefix
|
|
19
|
-
this.openAPIObject = openAPIObject
|
|
20
|
-
this.init()
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
init() {
|
|
24
|
-
const paths = new Map()
|
|
25
|
-
for (const key in this.openAPIObject.paths) {
|
|
26
|
-
for (const key1 in this.openAPIObject.paths[key]) {
|
|
27
|
-
const item = this.openAPIObject.paths[key][key1]
|
|
28
|
-
item.path = this.pathPrefix + key
|
|
29
|
-
item.method = key1.toUpperCase()
|
|
30
|
-
item.auth = !!item.security
|
|
31
|
-
paths.set(`${item.operationId}`, {
|
|
32
|
-
...item,
|
|
33
|
-
operationId: this.prefix + item.operationId,
|
|
34
|
-
reqType: this._getReqType(item),
|
|
35
|
-
resType: this._getResType(item),
|
|
36
|
-
reqDescription: this._getReqDescription(item),
|
|
37
|
-
resDescription: this._getResDescription(item),
|
|
38
|
-
tag: Array.isArray(item.tags) ? item.tags.join('|') : '------'
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
this.list = [...paths.values()]
|
|
43
|
-
this.schemaKeys = Object.keys(this.openAPIObject.components.schemas)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private _getReqType(item: any) {
|
|
47
|
-
try {
|
|
48
|
-
const key = Object.keys(item.requestBody.content)[0]
|
|
49
|
-
return this.getSchemaEntity(item.requestBody.content[key].schema.$ref)
|
|
50
|
-
} catch {
|
|
51
|
-
return this.parseSchema(item)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
parseSchema(schema: any) {
|
|
56
|
-
const parameters: Map<string, any> = new Map();
|
|
57
|
-
(schema?.parameters || [] as Array<any>).forEach((item: any) => {
|
|
58
|
-
item.in !== 'header' && parameters.set(item.name, item)
|
|
59
|
-
})
|
|
60
|
-
const arr = [...parameters.values()]
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
let str = '{'
|
|
64
|
-
arr.forEach((item1: any) => {
|
|
65
|
-
const fieldType = item1.schema.$ref ? this.getSchemaEntity(item1.schema.$ref) : item1.schema.type
|
|
66
|
-
const fieldName = item1.name.includes('.') ? `'${item1.name}'` : item1.name
|
|
67
|
-
str += (` ${fieldName}${item1.required ? '' : '?'}:${fieldType},${item1.description ? ' //' + item1.description : ''}`).replace(/\"|\n|\\/g, '') + '\n'
|
|
68
|
-
})
|
|
69
|
-
str += '}'
|
|
70
|
-
return str
|
|
71
|
-
} catch (e) {
|
|
72
|
-
return `any`
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
getSchemaEntity(str: string): string {
|
|
77
|
-
const arr = str.split('/')
|
|
78
|
-
return arr[3] || `any`
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
_getResType(item: any) {
|
|
82
|
-
try {
|
|
83
|
-
const key = Object.keys(item.responses[200].content)[0]
|
|
84
|
-
const schema = item.responses[200].content[key].schema
|
|
85
|
-
if (schema.type === 'array') {
|
|
86
|
-
if (schema.items && schema.items.$ref) {
|
|
87
|
-
return `Array<${this.getSchemaEntity(schema.items.$ref)}>`
|
|
88
|
-
} else {
|
|
89
|
-
return `Array<any>`
|
|
90
|
-
}
|
|
91
|
-
} else if (schema.$ref) {
|
|
92
|
-
return this.getSchemaEntity(schema.$ref)
|
|
93
|
-
}
|
|
94
|
-
return `any`
|
|
95
|
-
} catch {
|
|
96
|
-
return `any`
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
_getReqDescription(item: any) {
|
|
101
|
-
try {
|
|
102
|
-
return (item.requestBody.description || '').trim().replace(/\"|\n|\\/g, '')
|
|
103
|
-
} catch {
|
|
104
|
-
return ''
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
_getResDescription(item: any) {
|
|
109
|
-
try {
|
|
110
|
-
return (item.responses[200].description || '').trim().replace(/\"|\n|\\/g, '')
|
|
111
|
-
} catch {
|
|
112
|
-
return ''
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public async run() {
|
|
117
|
-
try {
|
|
118
|
-
return await this.render('api')
|
|
119
|
-
} catch (err) {
|
|
120
|
-
console.info('===========ApiTemplate=err==========', err)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
package/src/template/schemas.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { OpenAPIObject } from '@nestjs/swagger'
|
|
2
|
-
import { Temp, Template, smfType } from 'moyan-file-model'
|
|
3
|
-
|
|
4
|
-
export class SchemasTemplate extends Temp implements Template {
|
|
5
|
-
openAPIObject: OpenAPIObject
|
|
6
|
-
|
|
7
|
-
list: Array<any> = []
|
|
8
|
-
|
|
9
|
-
constructor(openAPIObject:OpenAPIObject) {
|
|
10
|
-
super(smfType.ts)
|
|
11
|
-
this.openAPIObject = openAPIObject
|
|
12
|
-
this.init()
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
init() {
|
|
16
|
-
const schemas = new Map()
|
|
17
|
-
for (const key in this.openAPIObject.components.schemas) {
|
|
18
|
-
const arr = this.parseSchema(this.openAPIObject.components.schemas[key])
|
|
19
|
-
schemas.set(key, {
|
|
20
|
-
key,
|
|
21
|
-
arr
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
this.list = [...schemas.values()]
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
parseSchema(schema: any) {
|
|
28
|
-
const parameters = new Map()
|
|
29
|
-
for (const key in schema.properties) {
|
|
30
|
-
const item = schema.properties[key]
|
|
31
|
-
|
|
32
|
-
if (item.type && item.type === 'array') {
|
|
33
|
-
if (item.items && item.items.$ref) {
|
|
34
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:Array<${this.getSchemaEntity(item.items.$ref)}>${this.getDescription(item)}`)
|
|
35
|
-
continue
|
|
36
|
-
} else if (item.items && item.items.oneOf && Array.isArray(item.items.oneOf)) {
|
|
37
|
-
const schemaEntityArr = item.items.oneOf.map((item1: any) => {
|
|
38
|
-
return this.getSchemaEntity(item1.$ref)
|
|
39
|
-
})
|
|
40
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:Array<${schemaEntityArr.join('|')}>${this.getDescription(item)}`)
|
|
41
|
-
continue
|
|
42
|
-
}else if(item.items && item.items.type && item.type==='array'){
|
|
43
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:Array<${item.items.type}>${this.getDescription(item)}`)
|
|
44
|
-
continue
|
|
45
|
-
} else {
|
|
46
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:Array<any>${this.getDescription(item)}`)
|
|
47
|
-
continue
|
|
48
|
-
}
|
|
49
|
-
} else if (Array.isArray(item.allOf)) {
|
|
50
|
-
const schemaEntityArr = item.allOf.map((item1: any) => {
|
|
51
|
-
return this.getSchemaEntity(item1.$ref)
|
|
52
|
-
})
|
|
53
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:${schemaEntityArr.join('|')}${this.getDescription(item)}`)
|
|
54
|
-
continue
|
|
55
|
-
} else if (Array.isArray(item.oneOf)) {
|
|
56
|
-
const schemaEntityArr = item.oneOf.map((item1: any) => {
|
|
57
|
-
return this.getSchemaEntity(item1.$ref)
|
|
58
|
-
})
|
|
59
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:${schemaEntityArr.join('|')}${this.getDescription(item)}`)
|
|
60
|
-
continue
|
|
61
|
-
} else if (item.type) {
|
|
62
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:${item.type}${this.getDescription(item)}`)
|
|
63
|
-
continue
|
|
64
|
-
} else if (item.$ref) {
|
|
65
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:${this.getSchemaEntity(item.$ref)}${this.getDescription(item)}`)
|
|
66
|
-
continue
|
|
67
|
-
} else {
|
|
68
|
-
parameters.set(key, `${key}${this.isRequired(schema, key) ? '?' : ''}:any${this.getDescription(item)}`)
|
|
69
|
-
continue
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return [...parameters.values()].map((item) => item + '\n')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
isRequired(schema: any, key: string) {
|
|
76
|
-
return !(schema.required && schema.required.includes(key))
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
getSchemaEntity(str: string): string {
|
|
80
|
-
const arr = str.split('/')
|
|
81
|
-
return arr[3] || `any`
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
getDescription(item: any) {
|
|
85
|
-
return item.description ? ' // ' + item.description.trim().replace(/\"|\n|\\/g,'') : ''
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public async run() {
|
|
89
|
-
try {
|
|
90
|
-
return await this.render('schemas')
|
|
91
|
-
} catch (err) {
|
|
92
|
-
console.info('===========SchemasTemplate=err==========', err)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|