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