sy-api-pro 0.1.7 → 0.1.9
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/dist/index.umd.js +13 -13
- package/package.json +9 -9
package/dist/index.umd.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`);return
|
|
3
|
-
`):[` ${
|
|
4
|
-
`)})}}),
|
|
5
|
-
`)),console.log(
|
|
6
|
-
`)),console.log(
|
|
7
|
-
`);if(
|
|
8
|
-
`))),console.log(
|
|
9
|
-
`)),
|
|
10
|
-
`)),console.log(
|
|
11
|
-
`)),console.log(
|
|
12
|
-
`);
|
|
13
|
-
`))}}})})}},
|
|
1
|
+
(function(y,B){typeof exports=="object"&&typeof module!="undefined"?B(require("axios")):typeof define=="function"&&define.amd?define(["axios"],B):(y=typeof globalThis!="undefined"?globalThis:y||self,B(y.axios))})(this,function(y){"use strict";var N=(y,B,F)=>new Promise((h,m)=>{var l=$=>{try{j(F.next($))}catch(I){m(I)}},O=$=>{try{j(F.throw($))}catch(I){m(I)}},j=$=>$.done?h($.value):Promise.resolve($.value).then(l,O);j((F=F.apply(y,B)).next())});const B=p=>{var e;const n=(p||"").toLowerCase(),o={int:"number",integer:"number",long:"number",bigdecimal:"number",float:"number",double:"number",localdatetime:"string",date:"string",char:"string",byte:"string",short:"string",character:"string",string:"string",multipartfile:"string",boolean:"boolean",void:"void"},{listType:s}=((e=new RegExp("^list<(?<listType>.+)>$").exec(n))==null?void 0:e.groups)||{};return s?`Array<${o[s]||"any"}>`:o[n]||"any"},F=Object.freeze({_getDesc(p){const n=(p.title||"").trim();return n?` // ${n}`:""},getInterface:function({list:p,config:{ignoreParamsDTO:n,deleteParentDTO:o}}){const s=p.map((t,d)=>t.level===0?d:null).filter(t=>t!==null);s.length>1&&s.push(p.length);const e=(s.length>1?s.map((t,d)=>p.slice(t,s[d+1])):[p]).filter(t=>t.length),g=p.filter(t=>!t.json);return e.filter(t=>!n.includes(t[0].type)).filter(t=>t[0].json).map(t=>{const d=t[0],i=(d.json||"").split(`
|
|
2
|
+
`);return i.forEach((c,a)=>{var w;const b=new RegExp('(?<space>\\s*)"(?<name>[a-zA-Z0-9]+)"\\s?:\\s*"(?<desc>.*)".*$');if(b.test(c)){const{space:P,name:T,desc:v}=((w=b.exec(c))==null?void 0:w.groups)||{},L=(T||"").trim(),M=(v||"").trim(),R=g.filter(q=>(q.name||"").trim()+(q.title||"").trim()===L+M);if(R.length>=1){const[{type:q}]=R,u=d.required?"?":"";i[a]=c.replace(b,`${P+T+u}: ${B(q)};${M?` // ${M}`:""}`)}}/\s*"[a-zA-Z0-9]+"\s*:\s*.+$/.test(c)&&(i[a]=i[a].replace(/(\s*)"([a-zA-Z0-9]+)"\s?(:\s*.+)$/,"$1$2$3")),i[a]=i[a].replace(/(.*)\[\s+{$/,"$1Array<{").replace(/(.*)}\s*].*$/,"$1}>")}),o.some(c=>{const a=Object.prototype.toString.call(c);if(a==="[object RegExp]")return c.test(d.type);if(a==="[object String]")return c===d.type})?i.slice(1,-1).join(`
|
|
3
|
+
`):[` ${d.name}${d.required?"":"?"}: {${this._getDesc(d)}`,...i.slice(1,-1).map(c=>` ${c}`)," }"].join(`
|
|
4
|
+
`)})}}),h=require("fs"),m=require("path"),l=require("chalk");y.interceptors.response.use(p=>{var n,o;return(o=(n=p.data)==null?void 0:n.head)!=null&&o.successful?p:Promise.reject(p)},p=>{throw p});const O=class{constructor(n){this.config={api:"",creatBaseFile:!0,customMethodName:"",projectId:void 0,branchId:void 0,methodId:void 0,outputPath:"",outputDirName:"",apiBaseName:"",ignoreParamsDTO:[],deleteParentDTO:[]},this.apiList=[],this.enums=[],Object.assign(this.config,n),this.getApiData()}getApiData(){return N(this,null,function*(){const{api:n,branchId:o,projectId:s,outputDirName:e,outputPath:g,methodId:t,creatBaseFile:d}=this.config;try{const{data:i}=yield y.get(`${n}/api/project/getProjectWithBranchById?id=${s}`);this.config.outputPath=m.join(process.cwd(),g,e||i.body.name);const r=i.body.branchList.find(a=>a.id===o);if(r)console.log(l.blueBright(`【info】正在使用 branchName: ${r.name} branchId: ${o} methodId: ${t}`));else throw l.red(`【error】branchId ${o} 不存在`);const c=yield y.get(`${n}/api/method/getMethodById?id=${t}`);this.apiList=[c.data.body]}catch(i){console.log(l.red("【error】: 接口获取异常")),console.log(i);return}try{yield this.checkApiOutputDir()}catch(i){console.log(l.red("【error】: 检查输出目录异常")),console.log(i);return}d&&(yield this.creatBaseFile());try{yield this.createFile()}catch(i){console.log(l.red("【error】: createFile 异常")),console.log(i)}})}checkApiOutputDir(){const{outputPath:n}=this.config;console.log(l.blueBright(`【info】输出目录 ${n}`));try{h.statSync(n,{bigint:!0})}catch(o){h.mkdirSync(n,{recursive:!0})}}creatBaseFile(){return N(this,null,function*(){const{outputPath:n}=this.config,o=m.join(__dirname,"../template/api"),s=h.readdirSync(o),e=h.readdirSync(n);for(const g of s)if(!e.includes(g))try{const t=h.readFileSync(m.join(o,g));h.writeFileSync(m.join(n,g),t)}catch(t){console.log(l.red("【error】: 基础文件生成异常")),console.log(t)}})}createFile(){return N(this,null,function*(){const{apiBaseName:n,outputPath:o,customMethodName:s}=this.config;this.apiList.forEach(e=>{var M,R,q;const g=u=>u.replace(/(-[a-z])|(^[a-z])/g,f=>f.replace("-","").toLocaleUpperCase()),t=e.path.startsWith("/")?e.path:`/${e.path}`,d=n.startsWith("/")?n:`/${n}`,i=new RegExp(`(${d})?(/)?(?<apiDirName>[a-zA-Z0-9_]+)/.*$`),{apiDirName:r}=((M=i.exec(t))==null?void 0:M.groups)||{},c=s||e.name,a=g(`${r}-`+c);try{h.statSync(m.join(o,r)).isDirectory()}catch(u){h.mkdirSync(m.join(o,r))}let b="";try{b=h.readFileSync(m.join(o,r,"models.ts")).toString()}catch(u){h.writeFileSync(m.join(o,`./${r}/models.ts`),"")}let w=[],P=[];try{const u=F.getInterface({list:e.paramList||[],config:this.config});w=(R=e.paramList)!=null&&R.length&&u.length?[b?"":"/* ************************************************ */",`/** ${e.title} RequestModel */`,`export interface ${a}ReqModel {`,...u,"}",""]:[];const f=F.getInterface({list:e.returnList||[],config:this.config});P=(q=e.returnList)!=null&&q.length&&f.length?[w.length?"":"/* ************************************************ */",`/** ${e.title} ResponseModel */`,`export interface ${a}ResModel {`,...f,"}",""]:[]}catch(u){throw console.log(l.red(`【error】类型生成失败 projectId: ${this.config.projectId} methodId: ${e.id} path: ${t}`)),l.red(u)}b.includes(`export interface ${a}ReqModel {`)?console.log(l.yellowBright(`【warning】./${r}/models.ts 已存在 ${a}ReqModel`)):w.length&&(h.appendFileSync(m.join(o,`./${r}/models.ts`),w.join(`
|
|
5
|
+
`)),console.log(l.greenBright(`【success】./${r}/models.ts 成功添加 ${a}ReqModel`))),b.includes(`export interface ${a}ResModel {`)?console.log(l.yellowBright(`【warning】./${r}/models.ts 已存在 ${a}ResModel`)):P.length&&(h.appendFileSync(m.join(o,`./${r}/models.ts`),P.join(`
|
|
6
|
+
`)),console.log(l.greenBright(`【success】./${r}/models.ts 成功添加 ${a}ResModel`)));const L=[e.title&&` /** ${e.title} */`,` ${c}(${w.length?`params: M.${a}ReqModel`:""}) {`,` return axios.${e.method.toLowerCase()}<ApiResponse<${P.length?`M.${a}ResModel`:"any"}>>(`,` \`\${ApiPrefix}${t}\`,`,w.length&&` ${["post","put","patch"].includes(e.method.toLowerCase())?"params":"{ params }"},`," );"," },"].filter(Boolean);/^[a-zA-Z]+$/.test((e.method||"").trim())||console.log(l.yellowBright(`【warning】接口请求类型异常,请手动补全 method:${e.method} path: ${t} methodId: ${this.config.methodId} projectId: ${this.config.projectId}`));try{const u=h.readFileSync(m.join(o,r,"api.ts")).toString(),f=u.split(`
|
|
7
|
+
`);if(u.includes(` ${c}(`)){const x=f.findIndex(D=>D.includes(` ${c}(`)),A=f.findIndex((D,_)=>_>x&&D===" },");console.log(l.yellowBright([`【warning】./${r}/api.ts 已存在api`,...f.slice(x,A+1).map(D=>` ${D}`)].join(`
|
|
8
|
+
`))),console.log(l.yellowBright(`【warning】当前需要生成的 path 为 ${t}, methodName 为 ${c}`))}else f.splice(f.map(x=>x.includes("}")).lastIndexOf(!0),0,L.join(`
|
|
9
|
+
`)),h.writeFileSync(m.join(o,`./${r}/api.ts`),f.join(`
|
|
10
|
+
`)),console.log(l.greenBright(`【success】./${r}/api.ts 成功添加 ${c}`))}catch(u){h.writeFileSync(m.join(o,`./${r}/api.ts`),["import axios from 'axios';","","import * as M from './models';","import { ApiResponse } from '../responseModel';","import { ApiPrefix } from '../apiPrefix';","","export * from './models';","",`export const ${g(r)}Api = {`,...L,"};",""].join(`
|
|
11
|
+
`)),console.log(l.greenBright(`【success】./${r}/api.ts 成功添加 ${c}`));const f=h.readFileSync(m.join(o,"index.ts")).toString();if(!f.includes(`export * from './${r}/api'`)){const x=f.split(`
|
|
12
|
+
`);x.push(`export * from './${r}/api';`);const A=x.slice(1).filter(Boolean);A.sort((D,_)=>D.localeCompare(_)),h.writeFileSync(m.join(o,"index.ts"),[x[0]||"","",...A,""].join(`
|
|
13
|
+
`))}}})})}},j=require("fs"),$=require("path"),{program:I}=require("commander"),S=require("chalk");I.name("sy-api-pro").version(require($.resolve(__dirname,"../package.json")).version),I.command("init").description("初始化配置文件").option("-p, --path <char>","","./src/apis").action((p,n,o)=>{const s=$.join(process.cwd(),p.path),e=$.join(s,".api-config.ts");try{if(j.existsSync(s)||j.mkdirSync(s),j.existsSync(e))return console.log(S.yellowBright(`【warning】: ${e} 配置文件已存在`));const g=j.readFileSync($.join(__dirname,"../template/config/.api-config.ts"));j.writeFileSync(e,g),console.log(S.greenBright(`【success】: 配置文件已生成, 路径:${e}`))}catch(g){console.log(S.red("【error】: 配置文件生成异常")),console.log(g)}}),I.command("create").description("生成api文件及类型定义").requiredOption("-i, --id <id>","methodId").option("-n, --name <name>","customMethodName").requiredOption("--config <filePath>","configFilePath").requiredOption("--project-id <id>","projectId").action((p,n,o)=>{var r;const{id:s,name:e,config:g,projectId:t}=p;if(!s||!/^\d+$/.test(s))throw S.red("【error】请传入正确的 id");const d=require($.resolve(process.cwd(),g)),i=(d.projects||[]).find(c=>+c.projectId==+t);if(i){if(!i.branchId)throw S.red("【error】请传入正确的 branchId");if(!d.api)throw S.red("【error】请传入正确的 api");new O({api:d.api,creatBaseFile:(r=i.creatBaseFile)!=null?r:!0,methodId:s,customMethodName:e,outputPath:d.outputPath||"./src/apis",projectId:i.projectId,branchId:+i.branchId,outputDirName:i.outputDirName||"",apiBaseName:i.apiBaseName||"",ignoreParamsDTO:i.ignoreParamsDTO||[],deleteParentDTO:i.deleteParentDTO||[]})}else console.log(S.red(`【error】config 配置文件中 projects 未包含传入的 projectId ${t}`))}),I.parse()});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sy-api-pro",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"main": "./bin/index.ts",
|
|
5
5
|
"types": "./bin/model.d.ts",
|
|
6
6
|
"bin": {
|
|
@@ -22,22 +22,22 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "vite build",
|
|
24
24
|
"api": "ts-node src/index.ts create --config ./src/.api-config.ts --project-id 804",
|
|
25
|
-
"api2": "ts-node src/index.ts create --config ./src/.api-config.ts --project-id 801 -i
|
|
25
|
+
"api2": "ts-node src/index.ts create --config ./src/.api-config.ts --project-id 801 -i 1758417",
|
|
26
26
|
"test:dist-init": "ts-node bin/index.ts init",
|
|
27
27
|
"test:dist-create": "ts-node bin/index.ts create --config ./src/.api-config.ts --project-id 804",
|
|
28
28
|
"test:dist-create2": "ts-node bin/index.ts create --config ./src/.api-config.ts --project-id 801"
|
|
29
29
|
},
|
|
30
30
|
"description": "...",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
32
|
+
"@types/node": "^18.15.11",
|
|
33
|
+
"axios": "^1.3.5",
|
|
34
|
+
"chalk": "^4.1.2",
|
|
35
|
+
"commander": "^10.0.0",
|
|
36
|
+
"ts-node": "^10.9.1",
|
|
37
|
+
"typescript": "^5.0.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"rollup-plugin-terser": "^7.0.2",
|
|
41
|
-
"vite": "^2.
|
|
41
|
+
"vite": "^4.2.1"
|
|
42
42
|
}
|
|
43
43
|
}
|