import{a}from"./chunk-GD7WKZ3L.mjs";import{a as f,b as c}from"./chunk-WY5IDFPH.mjs";import l from"debug";var s=l("@tomrpc/core/fn"),h=class extends a{constructor(t){super(t),this.name="Fn",this.prefix===""&&(this.prefix="/api"),this.app.use(this.compose([this.before(),this.mount(),this.default()]))}fn(t,o){this.config.functions||(this.config.functions={});let e=t.split("")[0]=="/"?"":"/";this.config.functions[e+t]=o}add(t){for(let[o,e]of Object.entries(t))f(e)&&console.log(`this.rpcFunctions[${o}] is arrow function, please use ctx as param, not this`),this.config.functions[o]&&(s(`add ${o}: ${e}`),console.log(`this.rpcFunctions[${o}] exisit`)),this.fn(o,e)}default(){return async(t,o)=>{s("default"),t.body="[fn plugin] no fn repsonse, please check your fn if exist",s("default end")}}mount(){return async(t,o)=>{s(this);let e=this.config.functions;s(e);let p=t.path;s("mountMiddleware"+t.path);let r="/"+p.replace("/","").split("/").join(".");if(!["POST","PUT","PATCH"].includes(t.method)&&!t.query.$p)console.log("not match $p param, no process"),t.body="not match $p param, no process";else{let n=t.method==="POST"?t.request.body:JSON.parse(t.query.$p);if(s(r),s(n),s(e[r]),e[r]){let i=[...n,t];console.dir(`call fn ${t.path}, with [${n}]`);let u=e[r].apply(t,i);t.body=u}else{let i=JSON.stringify(t,null,4);t.body=` not match path ${t.path}  
 ctx = ${i}`,await o()}}}}before(){return async(t,o)=>{s("pre");let p=t.path.replace("/","").split("/").join(".").split(".").pop(),r=c(),n=[];r.forEach(function(i){if(p.indexOf(i)!=-1)return n.push(i),i}),n.length===0?(s(t.path+",\u6CA1\u6709\u5339\u914D\u5230\u5305\u542Bget/post\u7B49\u5F00\u5934\u7684\u51FD\u6570"),await o()):t.method===n[0]?(s("\u5339\u914D\u5230\u5305\u542Bget/post\u7B49\u65B9\u6CD5\u7684\u51FD\u6570"),await o()):(s("\u5339\u914D\u5230\u5305\u542Bget/post\u7B49\u65B9\u6CD5\u7684\u51FD\u6570\uFF0C\u4F46method\u4E0D\u5BF9"),t.body="process fn:"+p+" , you need send "+n[0]+" request from client")}}};export{h as a};
//# sourceMappingURL=chunk-42XPMRYT.mjs.map