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