sy-api-pro 0.0.8 → 0.0.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.
File without changes
File without changes
package/dist/index.umd.js CHANGED
@@ -1,12 +1,12 @@
1
- var M=(b,$,f)=>new Promise((x,w)=>{var c=h=>{try{a(f.next(h))}catch(I){w(I)}},l=h=>{try{a(f.throw(h))}catch(I){w(I)}},a=h=>h.done?x(h.value):Promise.resolve(h.value).then(c,l);a((f=f.apply(b,$)).next())});(function(b,$){typeof exports=="object"&&typeof module!="undefined"?$(require("axios")):typeof define=="function"&&define.amd?define(["axios"],$):$((b=typeof globalThis!="undefined"?globalThis:b||self).axios)})(this,function(b){"use strict";function $(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}var f=$(b);const x=t=>{var o;const r=(t||"").toLowerCase(),e={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:i}=((o=/^list<(?<listType>.+)>$/.exec(r))==null?void 0:o.groups)||{};return i?`Array<${e[i]||"any"}>`:e[r]||"any"},w=Object.freeze({_getDesc(t){const r=(t.title||"").trim();return r?` // ${r}`:""},getInterface:function({list:t,config:{ignoreParamsDTO:r,deleteParentDTO:e}}){const i=t.filter(o=>!o.id);return t.filter(o=>o.id&&!r.includes(o.type)).map(o=>{if(o.id&&o.json){const s=o.json.split(`
2
- `);return s.forEach((d,n)=>{var y;const u=/(?<space>\s*)"(?<name>[a-zA-Z0-9]+)"\s?:\s*"(?<desc>.*)".*$/;if(u.test(d)){const{space:p,name:g,desc:j}=((y=u.exec(d))==null?void 0:y.groups)||{},S=(g||"").trim(),B=(j||"").trim(),D=i.filter(F=>(F.name||"").trim()+(F.title||"").trim()===S+B);if(D.length>=1){const[{type:F}]=D,L=o.required?"?":"";s[n]=d.replace(u,`${p+g+L}: ${x(F)};${B?` // ${B}`:""}`)}}/\s*"[a-zA-Z0-9]+"\s*:\s*.+$/.test(d)&&(s[n]=s[n].replace(/(\s*)"([a-zA-Z0-9]+)"\s?(:\s*.+)$/,"$1$2$3")),s[n]=s[n].replace(/(.*)\[\s+{$/,"$1Array<{").replace(/(.*)}\s*].*$/,"$1}>")}),e.some(d=>{const n=Object.prototype.toString.call(d);return n==="[object RegExp]"?d.test(o.type):n==="[object String]"?d===o.type:void 0})?s.slice(1,-1).join(`
3
- `):[` ${o.name}${o.required?"":"?"}: {${this._getDesc(o)}`,...s.slice(1,-1).map(d=>` ${d}`)," }"].join(`
4
- `)}return` ${o.name}${o.required?"":"?"}: ${x(o.type)};${this._getDesc(o)}`})}}),c=require("fs"),l=require("path"),a=require("chalk");f.default.interceptors.response.use(t=>{var r,e;return((e=(r=t.data)==null?void 0:r.head)==null?void 0:e.successful)?t:Promise.reject(t)},t=>{throw t});const h=require("path"),{program:I}=require("commander"),P=require("chalk");I.version(require(h.resolve(__dirname,"../package.json")).version).requiredOption("-i, --id <id>","methodId").option("-n, --name <name>","customMethodName").requiredOption("--config <filePath>","configFilePath").requiredOption("--project-id <id>","projectId").parse();const{id:O,name:v,config:R,projectId:A}=I.opts();if(!O||!/^\d+$/.test(O))throw P.red("\u3010error\u3011\u8BF7\u4F20\u5165\u6B63\u786E\u7684 id");const q=require(h.resolve(process.cwd(),R)),m=(q.projects||[]).find(t=>+t.projectId==+A);if(m){if(!m.branchId)throw P.red("\u3010error\u3011\u8BF7\u4F20\u5165\u6B63\u786E\u7684 branchId");if(!q.api)throw P.red("\u3010error\u3011\u8BF7\u4F20\u5165\u6B63\u786E\u7684 api");new class{constructor(t){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,t),this.getApiData()}getApiData(){return M(this,null,function*(){const{api:t,branchId:r,projectId:e,outputDirName:i,outputPath:o,methodId:s,creatBaseFile:d}=this.config;try{const{data:n}=yield f.default.get(`${t}/api/project/getProjectWithBranchById?id=${e}`);this.config.outputPath=l.join(process.cwd(),o,i||n.body.name);const u=n.body.branchList.find(p=>p.id===r);if(!u)throw a.red(`\u3010error\u3011branchId ${r} \u4E0D\u5B58\u5728`);console.log(a.blueBright(`\u3010info\u3011\u6B63\u5728\u4F7F\u7528 branchName: ${u.name} branchId: ${r} methodId: ${s}`));const y=yield f.default.get(`${t}/api/method/getMethodById?id=${s}`);this.apiList=[y.data.body]}catch(n){return console.log(a.red("\u3010error\u3011: \u63A5\u53E3\u83B7\u53D6\u5F02\u5E38")),void console.log(n)}try{yield this.checkApiOutputDir()}catch(n){return console.log(a.red("\u3010error\u3011: \u68C0\u67E5\u8F93\u51FA\u76EE\u5F55\u5F02\u5E38")),void console.log(n)}d&&(yield this.creatBaseFile());try{yield this.createFile()}catch(n){console.log(a.red("\u3010error\u3011: createFile \u5F02\u5E38")),console.log(n)}})}checkApiOutputDir(){const{outputPath:t}=this.config;console.log(a.blueBright(`\u3010info\u3011\u8F93\u51FA\u76EE\u5F55 ${t}`));try{c.statSync(t,{bigint:!0})}catch(r){c.mkdirSync(t,{recursive:!0})}}creatBaseFile(){return M(this,null,function*(){const{outputPath:t}=this.config,r=l.join(__dirname,"../template"),e=c.readdirSync(r),i=c.readdirSync(t);for(const o of e)if(!i.includes(o))try{const s=c.readFileSync(l.join(r,o));c.writeFileSync(l.join(t,o),s)}catch(s){console.log(a.red("\u3010error\u3011: \u57FA\u7840\u6587\u4EF6\u751F\u6210\u5F02\u5E38")),console.log(s)}})}createFile(){return M(this,null,function*(){const{apiBaseName:t,outputPath:r}=this.config;this.apiList.sort((e,i)=>e.path.localeCompare(i.path)),this.apiList.slice(0,1e3).forEach(e=>{var y;const i=e.path.replace(t,"").split("/")[1],o=p=>p.replace(/(-[a-z])|(^[a-z])/g,g=>g.replace("-","").toLocaleUpperCase()),s=o((e.name.startsWith(i)?"":`${i}-`)+e.name);try{c.statSync(l.join(r,i)).isDirectory()}catch(p){c.mkdirSync(l.join(r,i))}const d=(y=e.paramList)==null?void 0:y.length;let n=[];try{n=["/**************************************************/",...d?[`/** ${e.title} RequestModel */`,`export interface ${s}ReqModel {`,...w.getInterface({list:e.paramList||[],config:this.config}),"}",""]:[],`/** ${e.title} ResponseModel */`,`export interface ${s}ResModel {`,...w.getInterface({list:e.returnList||[],config:this.config}),"}",""]}catch(p){throw console.log(a.red(`\u3010error\u3011\u7C7B\u578B\u751F\u6210\u5931\u8D25 projectId: ${this.config.projectId} methodId: ${e.id} path: ${e.path}`)),a.red(p)}try{c.readFileSync(l.join(r,i,"models.ts")).toString().includes(`export interface ${s}ReqModel {`)?console.log(a.yellowBright(`\u3010warning\u3011./${i}/models.ts \u5DF2\u5B58\u5728 ${s}ReqModel`)):(c.appendFileSync(l.join(r,`./${i}/models.ts`),[""].concat(n).join(`
5
- `)),console.log(a.greenBright(`\u3010success\u3011./${i}/models.ts \u6210\u529F\u6DFB\u52A0 ${s}ReqModel`)))}catch(p){c.writeFileSync(l.join(r,`./${i}/models.ts`),n.join(`
6
- `)),console.log(a.greenBright(`\u3010success\u3011./${i}/models.ts \u6210\u529F\u6DFB\u52A0 ${s}ReqModel`))}const u=[` /** ${e.title} */`,` ${e.name}(${d?`params: M.${s}ReqModel`:""}) {`,` return axios.${e.method.toLowerCase()}<ApiResponse<M.${s}ResModel>>(`,` \`\${ApiPrefix}/${e.path}\`,`,d&&` ${["post","put","patch"].includes(e.method.toLowerCase())?"params":"{ params }"},`," );"," },"].filter(Boolean);/^[a-zA-Z]+$/.test((e.method||"").trim())||console.log(a.yellowBright(`\u3010warning\u3011\u63A5\u53E3\u8BF7\u6C42\u7C7B\u578B\u5F02\u5E38\uFF0C\u8BF7\u624B\u52A8\u8865\u5168 method\uFF1A${e.method} path: ${e.path} methodId: ${this.config.methodId} projectId: ${this.config.projectId}`));try{const p=c.readFileSync(l.join(r,i,"api.ts")).toString();if(p.includes(` ${e.name}(`))console.log(a.yellowBright(`\u3010warning\u3011./${i}/api.ts \u5DF2\u5B58\u5728 ${e.name}`));else{const g=p.split(`
7
- `);g.splice(g.map(j=>j.includes("}")).lastIndexOf(!0),0,u.join(`
8
- `)),c.writeFileSync(l.join(r,`./${i}/api.ts`),g.join(`
9
- `)),console.log(a.blueBright(`\u3010success\u3011./${i}/api.ts \u6210\u529F\u6DFB\u52A0 ${e.name}`))}}catch(g){c.writeFileSync(l.join(r,`./${i}/api.ts`),["import axios from 'axios';","","import * as M from './models';","import { ApiResponse } from '../responseModel'","import { ApiPrefix } from '../apiPrefix'","","export * from './models';","",`export const ${o(i)}Api = {`,...u,"};"].join(`
10
- `)),console.log(a.blueBright(`\u3010success\u3011./${i}/api.ts \u6210\u529F\u6DFB\u52A0 ${e.name}`));const p=c.readFileSync(l.join(r,"index.ts")).toString();if(!p.includes(`export * from './${i}/api'`)){const j=p.split(`
11
- `);j.push(`export * from './${i}/api';`);const S=j.slice(1).filter(Boolean);S.sort((B,D)=>B.localeCompare(D)),c.writeFileSync(l.join(r,"index.ts"),[j[0]||"","",...S,""].join(`
12
- `))}}})})}}({api:q.api,creatBaseFile:m.creatBaseFile||!0,methodId:O,customMethodName:v,outputPath:q.outputPath||"./src/apis",projectId:m.projectId,branchId:+m.branchId,outputDirName:m.outputDirName||"",apiBaseName:m.apiBaseName||"",ignoreParamsDTO:m.ignoreParamsDTO||[],deleteParentDTO:m.deleteParentDTO||[]})}else console.log(P.red(`\u3010error\u3011config \u914D\u7F6E\u6587\u4EF6\u4E2D projects \u672A\u5305\u542B\u4F20\u5165\u7684 projectId ${A}`))});
1
+ var M=(b,j,f)=>new Promise((S,x)=>{var c=h=>{try{n(f.next(h))}catch(I){x(I)}},l=h=>{try{n(f.throw(h))}catch(I){x(I)}},n=h=>h.done?S(h.value):Promise.resolve(h.value).then(c,l);n((f=f.apply(b,j)).next())});(function(b,j){typeof exports=="object"&&typeof module!="undefined"?j(require("axios")):typeof define=="function"&&define.amd?define(["axios"],j):j((b=typeof globalThis!="undefined"?globalThis:b||self).axios)})(this,function(b){"use strict";function j(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var f=j(b);const S=e=>{var o;const r=(e||"").toLowerCase(),d={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}=((o=/^list<(?<listType>.+)>$/.exec(r))==null?void 0:o.groups)||{};return s?`Array<${d[s]||"any"}>`:d[r]||"any"},x=Object.freeze({_getDesc(e){const r=(e.title||"").trim();return r?` // ${r}`:""},getInterface:function({list:e,config:{ignoreParamsDTO:r,deleteParentDTO:d}}){const s=e.filter(o=>!o.id);return e.filter(o=>o.id&&!r.includes(o.type)).map(o=>{if(o.id&&o.json){const t=o.json.split(`
2
+ `);return t.forEach((a,i)=>{var $;const u=/(?<space>\s*)"(?<name>[a-zA-Z0-9]+)"\s?:\s*"(?<desc>.*)".*$/;if(u.test(a)){const{space:w,name:P,desc:p}=(($=u.exec(a))==null?void 0:$.groups)||{},y=(P||"").trim(),g=(p||"").trim(),q=s.filter(B=>(B.name||"").trim()+(B.title||"").trim()===y+g);if(q.length>=1){const[{type:B}]=q,A=o.required?"?":"";t[i]=a.replace(u,`${w+P+A}: ${S(B)};${g?` // ${g}`:""}`)}}/\s*"[a-zA-Z0-9]+"\s*:\s*.+$/.test(a)&&(t[i]=t[i].replace(/(\s*)"([a-zA-Z0-9]+)"\s?(:\s*.+)$/,"$1$2$3")),t[i]=t[i].replace(/(.*)\[\s+{$/,"$1Array<{").replace(/(.*)}\s*].*$/,"$1}>")}),d.some(a=>{const i=Object.prototype.toString.call(a);return i==="[object RegExp]"?a.test(o.type):i==="[object String]"?a===o.type:void 0})?t.slice(1,-1).join(`
3
+ `):[` ${o.name}${o.required?"":"?"}: {${this._getDesc(o)}`,...t.slice(1,-1).map(a=>` ${a}`)," }"].join(`
4
+ `)}return` ${o.name}${o.required?"":"?"}: ${S(o.type)};${this._getDesc(o)}`})}}),c=require("fs"),l=require("path"),n=require("chalk");f.default.interceptors.response.use(e=>{var r,d;return((d=(r=e.data)==null?void 0:r.head)==null?void 0:d.successful)?e:Promise.reject(e)},e=>{throw e});const h=require("path"),{program:I}=require("commander"),D=require("chalk");I.version(require(h.resolve(__dirname,"../package.json")).version).requiredOption("-i, --id <id>","methodId").option("-n, --name <name>","customMethodName").requiredOption("--config <filePath>","configFilePath").requiredOption("--project-id <id>","projectId").parse();const{id:O,name:N,config:R,projectId:v}=I.opts();if(!O||!/^\d+$/.test(O))throw D.red("\u3010error\u3011\u8BF7\u4F20\u5165\u6B63\u786E\u7684 id");const F=require(h.resolve(process.cwd(),R)),m=(F.projects||[]).find(e=>+e.projectId==+v);if(m){if(!m.branchId)throw D.red("\u3010error\u3011\u8BF7\u4F20\u5165\u6B63\u786E\u7684 branchId");if(!F.api)throw D.red("\u3010error\u3011\u8BF7\u4F20\u5165\u6B63\u786E\u7684 api");new class{constructor(e){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,e),this.getApiData()}getApiData(){return M(this,null,function*(){const{api:e,branchId:r,projectId:d,outputDirName:s,outputPath:o,methodId:t,creatBaseFile:a}=this.config;try{const{data:i}=yield f.default.get(`${e}/api/project/getProjectWithBranchById?id=${d}`);this.config.outputPath=l.join(process.cwd(),o,s||i.body.name);const u=i.body.branchList.find(w=>w.id===r);if(!u)throw n.red(`\u3010error\u3011branchId ${r} \u4E0D\u5B58\u5728`);console.log(n.blueBright(`\u3010info\u3011\u6B63\u5728\u4F7F\u7528 branchName: ${u.name} branchId: ${r} methodId: ${t}`));const $=yield f.default.get(`${e}/api/method/getMethodById?id=${t}`);this.apiList=[$.data.body]}catch(i){return console.log(n.red("\u3010error\u3011: \u63A5\u53E3\u83B7\u53D6\u5F02\u5E38")),void console.log(i)}try{yield this.checkApiOutputDir()}catch(i){return console.log(n.red("\u3010error\u3011: \u68C0\u67E5\u8F93\u51FA\u76EE\u5F55\u5F02\u5E38")),void console.log(i)}a&&(yield this.creatBaseFile());try{yield this.createFile()}catch(i){console.log(n.red("\u3010error\u3011: createFile \u5F02\u5E38")),console.log(i)}})}checkApiOutputDir(){const{outputPath:e}=this.config;console.log(n.blueBright(`\u3010info\u3011\u8F93\u51FA\u76EE\u5F55 ${e}`));try{c.statSync(e,{bigint:!0})}catch(r){c.mkdirSync(e,{recursive:!0})}}creatBaseFile(){return M(this,null,function*(){const{outputPath:e}=this.config,r=l.join(__dirname,"../template"),d=c.readdirSync(r),s=c.readdirSync(e);for(const o of d)if(!s.includes(o))try{const t=c.readFileSync(l.join(r,o));c.writeFileSync(l.join(e,o),t)}catch(t){console.log(n.red("\u3010error\u3011: \u57FA\u7840\u6587\u4EF6\u751F\u6210\u5F02\u5E38")),console.log(t)}})}createFile(){return M(this,null,function*(){const{apiBaseName:e,outputPath:r,customMethodName:d}=this.config;this.apiList.forEach(s=>{var P;const o=p=>p.replace(/(-[a-z])|(^[a-z])/g,y=>y.replace("-","").toLocaleUpperCase()),t=s.path.replace(e,"").split("/")[1],a=d||s.name,i=o(`${t}-`+a);try{c.statSync(l.join(r,t)).isDirectory()}catch(p){c.mkdirSync(l.join(r,t))}const u=(P=s.paramList)==null?void 0:P.length;let $=[];try{$=["/**************************************************/",...u?[`/** ${s.title} RequestModel */`,`export interface ${i}ReqModel {`,...x.getInterface({list:s.paramList||[],config:this.config}),"}",""]:[],`/** ${s.title} ResponseModel */`,`export interface ${i}ResModel {`,...x.getInterface({list:s.returnList||[],config:this.config}),"}",""]}catch(p){throw console.log(n.red(`\u3010error\u3011\u7C7B\u578B\u751F\u6210\u5931\u8D25 projectId: ${this.config.projectId} methodId: ${s.id} path: ${s.path}`)),n.red(p)}try{c.readFileSync(l.join(r,t,"models.ts")).toString().includes(`export interface ${i}ReqModel {`)?console.log(n.yellowBright(`\u3010warning\u3011./${t}/models.ts \u5DF2\u5B58\u5728 ${i}ReqModel`)):(c.appendFileSync(l.join(r,`./${t}/models.ts`),[""].concat($).join(`
5
+ `)),console.log(n.greenBright(`\u3010success\u3011./${t}/models.ts \u6210\u529F\u6DFB\u52A0 ${i}ReqModel`)))}catch(p){c.writeFileSync(l.join(r,`./${t}/models.ts`),$.join(`
6
+ `)),console.log(n.greenBright(`\u3010success\u3011./${t}/models.ts \u6210\u529F\u6DFB\u52A0 ${i}ReqModel`))}const w=[` /** ${s.title} */`,` ${a}(${u?`params: M.${i}ReqModel`:""}) {`,` return axios.${s.method.toLowerCase()}<ApiResponse<M.${i}ResModel>>(`,` \`\${ApiPrefix}/${s.path}\`,`,u&&` ${["post","put","patch"].includes(s.method.toLowerCase())?"params":"{ params }"},`," );"," },"].filter(Boolean);/^[a-zA-Z]+$/.test((s.method||"").trim())||console.log(n.yellowBright(`\u3010warning\u3011\u63A5\u53E3\u8BF7\u6C42\u7C7B\u578B\u5F02\u5E38\uFF0C\u8BF7\u624B\u52A8\u8865\u5168 method\uFF1A${s.method} path: ${s.path} methodId: ${this.config.methodId} projectId: ${this.config.projectId}`));try{const p=c.readFileSync(l.join(r,t,"api.ts")).toString();if(p.includes(` ${a}(`))console.log(n.yellowBright(`\u3010warning\u3011./${t}/api.ts \u5DF2\u5B58\u5728 ${a}`));else{const y=p.split(`
7
+ `);y.splice(y.map(g=>g.includes("}")).lastIndexOf(!0),0,w.join(`
8
+ `)),c.writeFileSync(l.join(r,`./${t}/api.ts`),y.join(`
9
+ `)),console.log(n.greenBright(`\u3010success\u3011./${t}/api.ts \u6210\u529F\u6DFB\u52A0 ${a}`))}}catch(y){c.writeFileSync(l.join(r,`./${t}/api.ts`),["import axios from 'axios';","","import * as M from './models';","import { ApiResponse } from '../responseModel';","import { ApiPrefix } from '../apiPrefix';","","export * from './models';","",`export const ${o(t)}Api = {`,...w,"};"].join(`
10
+ `)),console.log(n.greenBright(`\u3010success\u3011./${t}/api.ts \u6210\u529F\u6DFB\u52A0 ${a}`));const p=c.readFileSync(l.join(r,"index.ts")).toString();if(!p.includes(`export * from './${t}/api'`)){const g=p.split(`
11
+ `);g.push(`export * from './${t}/api';`);const q=g.slice(1).filter(Boolean);q.sort((B,A)=>B.localeCompare(A)),c.writeFileSync(l.join(r,"index.ts"),[g[0]||"","",...q,""].join(`
12
+ `))}}})})}}({api:F.api,creatBaseFile:m.creatBaseFile||!0,methodId:O,customMethodName:N,outputPath:F.outputPath||"./src/apis",projectId:m.projectId,branchId:+m.branchId,outputDirName:m.outputDirName||"",apiBaseName:m.apiBaseName||"",ignoreParamsDTO:m.ignoreParamsDTO||[],deleteParentDTO:m.deleteParentDTO||[]})}else console.log(D.red(`\u3010error\u3011config \u914D\u7F6E\u6587\u4EF6\u4E2D projects \u672A\u5305\u542B\u4F20\u5165\u7684 projectId ${v}`))});
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "sy-api-pro",
3
- "version": "0.0.8",
4
- "main": "./bin/index.js",
5
- "types": "./bin/index.d.ts",
3
+ "version": "0.0.9",
4
+ "main": "./bin/index.ts",
5
+ "types": "./bin/model.d.ts",
6
6
  "bin": {
7
- "sy-api-pro": "./bin/index.js"
7
+ "sy-api-pro": "./bin/index.ts"
8
8
  },
9
9
  "license": "MIT",
10
10
  "exports": {
@@ -22,7 +22,7 @@
22
22
  "scripts": {
23
23
  "build": "vite build",
24
24
  "api": "ts-node src/index.ts --config ./src/.api-config.ts --project-id 804",
25
- "apiDist": "ts-node bin/index.js --config ./src/.api-config.ts --project-id 804"
25
+ "testDist": "ts-node bin/index.js --config ./src/.api-config.ts --project-id 804"
26
26
  },
27
27
  "description": "...",
28
28
  "dependencies": {
@@ -14,5 +14,3 @@ axios.interceptors.response.use((response: AxiosResponse) => {
14
14
  }, (error) => {
15
15
  console.error(error);
16
16
  });
17
-
18
-