sy-api-pro 0.0.7 → 0.0.8
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 +10 -10
- package/package.json +1 -1
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
|
|
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=
|
|
3
|
-
`):[` ${
|
|
4
|
-
`)}return` ${
|
|
5
|
-
`)),console.log(a.greenBright(`\u3010success\
|
|
6
|
-
`)),console.log(a.greenBright(`\u3010success\
|
|
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
7
|
`);g.splice(g.map(j=>j.includes("}")).lastIndexOf(!0),0,u.join(`
|
|
8
|
-
`)),c.writeFileSync(l.join(
|
|
9
|
-
`)),console.log(a.blueBright(`\u3010success\
|
|
10
|
-
`)),console.log(a.blueBright(`\u3010success\
|
|
11
|
-
`);j.push(`export * from './${
|
|
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
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}`))});
|