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
|
|
2
|
-
`);return
|
|
3
|
-
`):[` ${o.name}${o.required?"":"?"}: {${this._getDesc(o)}`,...
|
|
4
|
-
`)}return` ${o.name}${o.required?"":"?"}: ${
|
|
5
|
-
`)),console.log(
|
|
6
|
-
`)),console.log(
|
|
7
|
-
`);
|
|
8
|
-
`)),c.writeFileSync(l.join(r,`./${
|
|
9
|
-
`)),console.log(
|
|
10
|
-
`)),console.log(
|
|
11
|
-
`);
|
|
12
|
-
`))}}})})}}({api:
|
|
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.
|
|
4
|
-
"main": "./bin/index.
|
|
5
|
-
"types": "./bin/
|
|
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.
|
|
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
|
-
"
|
|
25
|
+
"testDist": "ts-node bin/index.js --config ./src/.api-config.ts --project-id 804"
|
|
26
26
|
},
|
|
27
27
|
"description": "...",
|
|
28
28
|
"dependencies": {
|