@xm-fe/create-api
Advanced tools
Comparing version 0.0.3 to 0.0.4
#!/usr/bin/env node | ||
// // import inquirer from "inquirer"; | ||
// const inquirer = require('inquirer') | ||
// inquirer | ||
// .prompt([ | ||
// { | ||
// type: "rawlist", // 交互类型 -- 有序单选 | ||
// message: "请选择一种水果:", // 引导词 | ||
// name: "projectId", // 自定义的字段名 | ||
// choices: ["潜艇SAAS/", | ||
// "summerfarm-manage(鲜沐后台)", | ||
// "pms-service(采购)/1164725", | ||
// "summerfarm-crm(CRM)/1295278", | ||
// "TMS(配送)/1409074", | ||
// "summerfarm-wms(WMS)/1965722", | ||
// "OFC(履约)/2020406", | ||
// "saas-manage (帆台后台)/2437205", | ||
// "saas-oms (帆台大后台)/2437206", | ||
// "saas-mall (帆台商城)/2437207" | ||
// ], // 选项列表 | ||
// }, | ||
// ]) | ||
// .then((answers) => { | ||
// console.log(answers); | ||
// }); | ||
const main = require('../src/index') | ||
main() |
{ | ||
"name": "@xm-fe/create-api", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "API快速导入", | ||
"main": "index.js", | ||
"scripts": { | ||
"capi": "node bin/capi.js" | ||
"capi": "node bin/capi.js", | ||
"capi2": "node bin/capi2.js" | ||
}, | ||
"bin": { | ||
"capi": "bin/capi.js" | ||
"capi": "bin/capi.js", | ||
"capi2": "bin/capi2.js" | ||
}, | ||
@@ -12,0 +14,0 @@ "repository": { |
145
README.md
## ⚡️ 简介 | ||
鲜沐科技 前端API生成工具 | ||
2022 鲜沐科技 前端通用逻辑包 | ||
## 🚀 使用 | ||
@@ -12,4 +11,12 @@ | ||
> capi.config.js 具体配置及解释看文末 | ||
### 方式一 | ||
``` | ||
capi http://127.0.0.1:4523/export/openapi/5?version=3.0 | ||
``` | ||
直接使用导出的地址,无需再拼写 projectId | ||
> 备注:此方法不适用于 全部导出,如需全部导出请使用 方式二 | ||
### 方式二 | ||
``` | ||
capi http://127.0.0.1:4523/export/openapi/9?version=3.0 954456 | ||
@@ -19,3 +26,4 @@ ``` | ||
### 方式二 | ||
### 方式三 | ||
``` | ||
@@ -26,2 +34,25 @@ capi | ||
## 默认项目配置 | ||
``` | ||
954456: 'SAAS', | ||
1164722: 'summerfarm-manage', | ||
1164725: 'pms-service', | ||
1295278: 'summerfarm-crm', | ||
1409074: 'tms', | ||
1965722: 'summerfarm-wms', | ||
2020406: 'OFC', | ||
2437205: 'saas-manage', | ||
2437206: 'saas-oms', | ||
2437207: 'saas-mall', | ||
2517920: 'summerfarm-wnc', | ||
2546551: 'saas-pms', | ||
2546569: 'SCP', | ||
2546585: 'SRM', | ||
2546600: 'bms-service', | ||
2737704: 'summerfarm-mall' | ||
``` | ||
> 缺少的项目 请联系我加进去 | ||
### 0.0.1 | ||
@@ -64,109 +95,3 @@ | ||
module.exports = { | ||
// 生成目录 | ||
targetPath: `/src/apx`, | ||
truePath: `apis`, | ||
// targetTypePath: `/src/api-typess`, | ||
// 读本地文件生成 | ||
useLocal: false, | ||
// 本地文件地址 | ||
localPath: `./test3.0.1.json`, | ||
remotePath: `http://127.0.0.1:4523/export/openapi/6?projectId=2517920&version=3.0`, | ||
// 暂时搞不定的接口 | ||
ignorePath: [ | ||
// 响应值多层数组?? | ||
// '/crm/detail/panic-buy/{pageIndex}/{pageSize}', | ||
// // body参数没有key?? | ||
], | ||
// 运行时会自动修改 | ||
appName: 'main', | ||
// 从apifox生成的直接写死appName映射 | ||
appNameMap: { | ||
'1164722': 'summerfarm-manage', | ||
'1409074': 'tms', | ||
'954456': 'cosfo', | ||
'2517920': 'rs' | ||
}, | ||
// 路径参数别名 | ||
paramsNumName: { | ||
0: '', | ||
1: 'Query', | ||
2: 'Double', | ||
3: 'Triple', | ||
4: 'Quatra', | ||
5: 'Penta' | ||
}, | ||
listModel: { | ||
type: 'object', | ||
properties: { | ||
code: { | ||
type: 'string' | ||
}, | ||
data: { | ||
type: 'object', | ||
properties: { | ||
endRow: { | ||
type: 'number' | ||
}, | ||
firstPage: { | ||
type: 'number' | ||
}, | ||
hasNextPage: { | ||
type: 'boolean' | ||
}, | ||
hasPreviousPage: { | ||
type: 'boolean' | ||
}, | ||
isFirstPage: { | ||
type: 'boolean' | ||
}, | ||
isLastPage: { | ||
type: 'boolean' | ||
}, | ||
lastPage: { | ||
type: 'number' | ||
}, | ||
list: { | ||
type: 'array', | ||
items: {} | ||
}, | ||
navigatePages: { | ||
type: 'number' | ||
}, | ||
navigatepageNums: { | ||
type: 'array', | ||
items: { | ||
type: 'number' | ||
} | ||
}, | ||
nextPage: { | ||
type: 'number' | ||
}, | ||
pageNum: { | ||
type: 'number' | ||
}, | ||
pageSize: { | ||
type: 'number' | ||
}, | ||
pages: { | ||
type: 'number' | ||
}, | ||
prePage: { | ||
type: 'number' | ||
}, | ||
size: { | ||
type: 'number' | ||
}, | ||
startRow: { | ||
type: 'number' | ||
}, | ||
total: { | ||
type: 'number' | ||
} | ||
} | ||
}, | ||
msg: { | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
remotePath: `http://127.0.0.1:4523/export/openapi/4?version=3.0`, | ||
// 插入的模板代码 | ||
@@ -173,0 +98,0 @@ getApiCodeTpl(api) { |
@@ -105,15 +105,19 @@ const fs = require('fs') | ||
try { | ||
const interfaceItem = Object.entries(obj.type === 'array' ? obj.items.properties : obj.properties) | ||
if (interfaceItem.length === 0) { | ||
return ` | ||
export interface ${interfaceName} { | ||
[key: string]: any | ||
}` | ||
} | ||
return ` | ||
/**${obj.description} */ | ||
export interface ${interfaceName} { | ||
${Object.entries(obj.type === 'array' ? obj.items.properties : obj.properties) | ||
.map(([key, val]) => { | ||
return ` /**${val.description} */ | ||
` + (key.indexOf('.') > -1 || key.indexOf('[') > -1 ? `"${key}"` : key) + `${val._required ? '' : '?'}: ${toFeType(val._type)}` | ||
}) | ||
.join('\n')} | ||
} | ||
` | ||
export interface ${interfaceName} { | ||
${interfaceItem.map(([key, val]) => { | ||
return ` | ||
/**${val.description} */ | ||
` + (key.indexOf('.') > -1 || key.indexOf('[') > -1 ? `"${key}"` : key) + `${val._required ? '' : '?'}: ${toFeType(val._type)}` | ||
}).join('\n')} | ||
}` | ||
} catch (error) { | ||
@@ -123,3 +127,3 @@ console.log('类型创建异常:', interfaceName, '使用any代替') | ||
return ` | ||
export type ${interfaceName} = any` | ||
export type ${interfaceName} = any` | ||
} | ||
@@ -126,0 +130,0 @@ |
const path = require('path') | ||
const { isURL, queryURLparams } = require('./utils') | ||
const processaArguments = process.argv | ||
const defaultConfig = require('./defaultConfig') | ||
let useConfig = {} | ||
const defaultConfig = { | ||
// 生成目录 | ||
targetPath: `/src/apix`, | ||
truePath: `apix`, | ||
// targetTypePath: `/src/api-typess`, | ||
// 读本地文件生成 | ||
useLocal: false, | ||
runtimeProjectId: '', // 当前的工程id | ||
// 本地文件地址 | ||
localPath: `./test3.0.1.json`, | ||
remotePath: `http://127.0.0.1:4523/export/openapi?projectId=1164722&version=3.0`, | ||
language: 'ts', // 生成的项目语言类型 | ||
// 暂时搞不定的接口 | ||
ignorePath: [ | ||
// 响应值多层数组?? | ||
// '/crm/detail/panic-buy/{pageIndex}/{pageSize}', | ||
// // body参数没有key?? | ||
], | ||
// 运行时会自动修改 | ||
appName: 'main', | ||
// 从apifox生成的直接写死appName映射 | ||
appNameMap: { | ||
954456: 'SAAS', | ||
1164722: 'summerfarm-manage', | ||
1164725: 'pms-service', | ||
1295278: 'summerfarm-crm', | ||
1409074: 'tms', | ||
1965722: 'summerfarm-wms', | ||
2020406: 'OFC', | ||
2437205: 'saas-manage', | ||
2437206: 'saas-oms', | ||
2437207: 'saas-mall', | ||
2517920: 'summerfarm-wnc', | ||
2546551: 'saas-pms', | ||
2546569: 'SCP', | ||
2546585: 'SRM', | ||
2546600: 'bms-service' | ||
}, | ||
/** | ||
* 接口项目前缀 | ||
* saas项目中访问 | ||
*/ | ||
apiPrefix: ['/summerfarm-manage', '/summerfarm-wms', '/summerfarm-wnc', '/pms-service', '/xianmu-manage', '/bms-service'], // 目前已经有的前缀 | ||
// 路径参数别名 | ||
paramsNumName: { | ||
0: '', | ||
1: 'Query', | ||
2: 'Double', | ||
3: 'Triple', | ||
4: 'Quatra', | ||
5: 'Penta' | ||
}, | ||
listModel: { | ||
type: 'object', | ||
properties: { | ||
code: { | ||
type: 'string' | ||
}, | ||
data: { | ||
type: 'object', | ||
properties: { | ||
endRow: { | ||
type: 'number' | ||
}, | ||
firstPage: { | ||
type: 'number' | ||
}, | ||
hasNextPage: { | ||
type: 'boolean' | ||
}, | ||
hasPreviousPage: { | ||
type: 'boolean' | ||
}, | ||
isFirstPage: { | ||
type: 'boolean' | ||
}, | ||
isLastPage: { | ||
type: 'boolean' | ||
}, | ||
lastPage: { | ||
type: 'number' | ||
}, | ||
list: { | ||
type: 'array', | ||
items: {} | ||
}, | ||
navigatePages: { | ||
type: 'number' | ||
}, | ||
navigatepageNums: { | ||
type: 'array', | ||
items: { | ||
type: 'number' | ||
} | ||
}, | ||
nextPage: { | ||
type: 'number' | ||
}, | ||
pageNum: { | ||
type: 'number' | ||
}, | ||
pageSize: { | ||
type: 'number' | ||
}, | ||
pages: { | ||
type: 'number' | ||
}, | ||
prePage: { | ||
type: 'number' | ||
}, | ||
size: { | ||
type: 'number' | ||
}, | ||
startRow: { | ||
type: 'number' | ||
}, | ||
total: { | ||
type: 'number' | ||
} | ||
} | ||
}, | ||
msg: { | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
// 插入的模板代码 | ||
getApiCodeTpl(api) { | ||
const pathParams = api.pathParams | ||
.map((param) => `${param.name}${param.required ? '' : '?'}: ${param._type},`) | ||
.join('\r\n') | ||
return ` | ||
import net from '@/utils/net' | ||
import { NetConfig } from '@/interface/IAxiosConfig' | ||
/** | ||
* ${api.summary} | ||
*/ | ||
export function ${api.fileName || api.name}( | ||
${pathParams ? pathParams : ''} | ||
${api.queryParams ? `params?: ${api.queryParams._name},` : ''} | ||
${api.bodyParams ? `data?: ${api.bodyParams._name},` : ''} | ||
config?: NetConfig | ||
): Promise<${api.resType}> { | ||
return net( | ||
{ | ||
url: \`${api.url}\`, | ||
method: '${api.method}', | ||
${api.isDownload ? `_download: true,` : ''} | ||
${api.queryParams ? `params,` : ''} | ||
${api.bodyParams ? ` data,` : ''} | ||
}, | ||
config | ||
) | ||
} | ||
` | ||
}, | ||
getApiCodeTplJs(api) { | ||
const pathParams = api.pathParams | ||
.map((param) => `${param.name}${param.required ? '' : '?'}: ${param._type},`) | ||
.join('\r\n') | ||
return ` | ||
import net from '@/utils/net' | ||
import { NetConfig } from '@/interface/IAxiosConfig' | ||
/** | ||
* ${api.summary} | ||
*/ | ||
export function ${api.fileName || api.name}( | ||
${pathParams ? pathParams : ''} | ||
${api.queryParams ? `params,` : ''} | ||
${api.bodyParams ? `data,` : ''} | ||
config | ||
) { | ||
return net( | ||
{ | ||
url: \`${api.url}\`, | ||
method: '${api.method}', | ||
${api.isDownload ? `_download: true,` : ''} | ||
${api.queryParams ? `params,` : ''} | ||
${api.bodyParams ? ` data,` : ''} | ||
}, | ||
config | ||
) | ||
} | ||
` | ||
} | ||
} | ||
@@ -221,26 +35,2 @@ // 用户配置 | ||
/** | ||
* 解析命令中的参数 | ||
*/ | ||
function argumentsParams() { | ||
if(isURL(processaArguments[2])) { | ||
config.remotePath = `${processaArguments[2]}&projectId=${processaArguments[3]}` | ||
} else { | ||
console.log('process 参数错误') | ||
console.log('capi 三种使用方式:') | ||
console.log('第一种:capi http://127.0.0.1:4523/export/openapi/9?version=3.0 954456') | ||
console.log('说明:capi apifox导出的URL projectId') | ||
console.log('第二种:capi') | ||
console.log('说明:项目根目录配置文件 capi.config 中的 remotePath') | ||
process.exit(); | ||
} | ||
} | ||
if(processaArguments.length === 4) { | ||
argumentsParams() | ||
} | ||
const URLparams = queryURLparams(config.remotePath) | ||
config.runtimeProjectId = URLparams.projectId | ||
module.exports = config |
@@ -9,4 +9,6 @@ const fs = require('fs') | ||
const createType = require('./create-type') | ||
const { queryURLparams } = require('./utils') | ||
const { queryURLparams, isURL } = require('./utils') | ||
const { isNumber } = require('@xm-fe/xm-base') | ||
const processaArguments = process.argv | ||
// require('./log') | ||
@@ -29,5 +31,22 @@ // const log4js = require('log4js') | ||
if (!config.useLocal) { | ||
console.log(`从 ${config.remotePath} 获取数据`) | ||
const re = await axios.get(config.remotePath) | ||
let re = {} | ||
if (processaArguments.length === 3) { | ||
console.log(`从 ${processaArguments[2]} 获取数据`) | ||
re = await axios.get(processaArguments[2]) | ||
// 请求地址 获取对应的工程id | ||
console.log('项目名:', re.data.info.title) | ||
const projectId = Object.keys(config.appNameMap).find(id => re.data.info.title.indexOf(config.appNameMap[id]) > -1) | ||
console.log('自动匹配的projectId:', projectId) | ||
config.remotePath = `${processaArguments[2]}&projectId=${projectId}` | ||
} else if (processaArguments.length === 4) { | ||
config.remotePath = `${processaArguments[2]}&projectId=${processaArguments[3]}` | ||
console.log(`从 ${config.remotePath} 获取数据`) | ||
re = await axios.get(config.remotePath) | ||
} else { | ||
console.log(`从 ${config.remotePath} 获取数据`) | ||
re = await axios.get(config.remotePath) | ||
} | ||
Object.keys(re.data.paths).forEach(api => { | ||
@@ -54,3 +73,5 @@ let newUrl = api | ||
const URLparams = queryURLparams(config.remotePath) | ||
config.runtimeProjectId = URLparams.projectId | ||
const apiFoxAppName = config.appNameMap[URLparams.projectId] | ||
@@ -165,3 +186,3 @@ | ||
createdType.push(type) | ||
if(typeMode[type]) { | ||
if (typeMode[type]) { | ||
typeStr += typeMode[type] | ||
@@ -168,0 +189,0 @@ } |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
34391
15
941
125
2