koa-ts-core 0.0.3
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/base/exception.d.ts +18 -0
- package/dist/base/index.d.ts +1 -0
- package/dist/constant.d.ts +5 -0
- package/dist/decorate/index.d.ts +1 -0
- package/dist/decorate/router_decorate.d.ts +15 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.esm.js +1 -0
- package/dist/init/address.d.ts +8 -0
- package/dist/init/index.d.ts +16 -0
- package/dist/init/register_controller.d.ts +9 -0
- package/dist/init/register_env.d.ts +5 -0
- package/dist/init/register_route.d.ts +10 -0
- package/dist/init/render_doc.d.ts +2 -0
- package/dist/middleware/context_middleware.d.ts +15 -0
- package/dist/middleware/exception_middleware.d.ts +10 -0
- package/dist/router.d.ts +2 -0
- package/dist/types/route.d.ts +25 -0
- package/dist/utils/context_store.d.ts +8 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/path.d.ts +46 -0
- package/dist/utils/port.d.ts +13 -0
- package/dist/utils/rsp.d.ts +22 -0
- package/package.json +36 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface ExceptionOptions {
|
|
2
|
+
message: string;
|
|
3
|
+
code: number;
|
|
4
|
+
data?: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @class BaseException 定义异常基类
|
|
8
|
+
*/
|
|
9
|
+
declare abstract class BaseException extends Error {
|
|
10
|
+
code: number;
|
|
11
|
+
constructor(options?: ExceptionOptions);
|
|
12
|
+
get toRspOptions(): {
|
|
13
|
+
code: number;
|
|
14
|
+
message: string;
|
|
15
|
+
data: string | undefined;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export default BaseException;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as BaseException } from "./exception";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./router_decorate";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HttpMethod } from '../types/route';
|
|
2
|
+
/**
|
|
3
|
+
* 注册路由
|
|
4
|
+
* @param method 请求路由方式
|
|
5
|
+
* @param path? 路由地址, 可选,默认为方法名字
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare function Router(method?: HttpMethod, path?: string): (target: any, key: string, descriptor: PropertyDescriptor) => any;
|
|
9
|
+
/**
|
|
10
|
+
* 注册权限路由路由
|
|
11
|
+
* @param method 请求路由方式
|
|
12
|
+
* @param path? 路由地址, 可选,默认为方法名字
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export declare function AuthRouter(method?: HttpMethod, path?: string): (target: any, key: string, descriptor: PropertyDescriptor) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("async_hooks"),n=require("fs"),r=require("path"),t=require("koa"),s=require("koa-router"),o=require("net"),a=require("dotenv");function c(e,n,r=!1){return(t,s,o)=>{let a=t.constructor.routesMap;a instanceof Map||(t.constructor.routesMap=new Map,a=t.constructor.routesMap);const c=t.constructor.name,i=n??s;return a.set(s,{handler:function(...e){if("function"!=typeof o.value)throw new Error("Register Router Error");return o.value.apply(t,e)},path:`/${i.replace("/","")}`,method:e,functionName:s,authRequired:r,middlewares:[],className:c}),t}}const i=["get","post","put","delete","patch","options"],l=0,p=new e.AsyncLocalStorage,u=()=>{const e=p.getStore();if(!e)throw new Error("context is not exist");return e},d=e=>{const{success:n=!0,errorCode:r=-1,message:t="success",errorMessage:s="error request",data:o=null,statusCode:a=200}=e??{},c=u(),i={code:n?l:r,message:n?t:s,...o&&{data:o}};c.body=i,c.status=a},h=(e,n)=>{d({success:!1,errorMessage:n?.message,errorCode:n?.code,data:n?.data,statusCode:e})};class f extends Error{constructor(e){super(e?.message||"An unexpected error occurred"),this.name=this.constructor.name,this.code=e?.code??-1,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor),this.stack=this.stack??new Error(this.message).stack}get toRspOptions(){return{code:this.code,message:this.message,data:this.stack}}}const y=process.cwd(),g=()=>{const e=r.resolve(y,"src");return{controllerModule:r.resolve(e,"controller"),validateModule:r.resolve(e,"validate"),viewModule:r.resolve(e,"view"),docModule:r.resolve(e,"doc")}},v=e=>{if(n.existsSync(e)){return require(e).default}};function m(e,t){try{n.readdirSync(e,{withFileTypes:!0}).forEach((n=>{const s=r.resolve(e,n.name);n.isFile()?t({path:e,name:n.name,wholePath:s},n):n.isDirectory()&&m(s,t)}))}catch(e){}}const b=e=>"win32"===process.platform?e.replace(/\\/g,"/"):e,w=(e,n)=>p.run(e,(()=>n())),q=new s,x=e=>new Promise((n=>{const r=o.createServer();r.unref(),r.on("error",(()=>n(!1))),r.listen(e,(()=>{r.close((()=>n(!0)))}))})),E=t.prototype.listen,R=e=>{const n=e.address();if(n){((e,n)=>{const r=null!==n?`http://${e}:${n}`:e;console.log(`app listen ${r}`)})("object"==typeof n?(e=>"::"===e?"0.0.0.0":e)(n.address):n,"object"==typeof n?n.port:null)}},k=async e=>{const n=await(async(e=8e3,n=9999)=>{let r=e;for(;r<=n;){if(await x(r))return r;r+=1}throw new Error("No available port found")})();return e.listen=(...r)=>{const t=r?E.apply(e,[n]):E.apply(e,r);return t.on("listening",(()=>R(t))),t},n},S=(e,n,r,t)=>{const{controllerPrefix:s,moduleName:o,name:a}=(e=>{const n="controller",r=e.path.indexOf(n);if(-1===r)throw new Error(`'${n}' not found in path: ${e.path}`);const t=b(e.path.substring(r+10)),s=e.name?.split(".")?.[0];return{controllerPrefix:t,moduleName:s,name:e.name}})(n),c=`${s}/${o}`;if(!r)return;const i=v(`${e}${`${s}/${a}`}`);r.forEach((e=>{const{method:n,path:r,handler:s,functionName:o,authRequired:a,middlewares:l}=e;var p;i&&"function"==typeof i[o]&&l.push((p=i[o],async(e,n)=>{const r=p(e);r instanceof Promise&&await r,await n()}));const u=(d=a,h=s,async(e,n)=>{let r=t(d,e);return r instanceof Promise&&(r=await r),r?h(e,n):e.throw(403,"无权限")});var d,h;const f=[...l,u],y=A(r,c,o,f,n);e.path=y}))},A=(e,n,r,t,s)=>{const o=r.toLowerCase();let a="get";a="get_"===o?"get":i.includes(o)?o:s??"get";const c=`${n}${"get_"==o?"":e??""}`;return q[a](c,...t),c},O=process.cwd();var M=function(e){var n=Object.prototype.hasOwnProperty;function r(e,t){return Array.isArray(e)?function(e,n){for(var t,s="",o="",c=Array.isArray(n),i=0;i<e.length;i++)(t=r(e[i]))&&(c&&n[i]&&(t=a(t)),s=s+o+t,o=" ");return s}(e,t):e&&"object"==typeof e?function(e){var r="",t="";for(var s in e)s&&e[s]&&n.call(e,s)&&(r=r+t+s,t=" ");return r}(e):e||""}function t(e){if(!e)return"";if("object"==typeof e){var r="";for(var t in e)n.call(e,t)&&(r=r+t+":"+e[t]+";");return r}return e+""}function s(e,n,r,t){return!1!==n&&null!=n&&(n||"class"!==e&&"style"!==e)?!0===n?" "+(t?e:e+'="'+e+'"'):("function"==typeof n.toJSON&&(n=n.toJSON()),"string"==typeof n||(n=JSON.stringify(n),r||-1===n.indexOf('"'))?(r&&(n=a(n))," "+e+'="'+n+'"'):" "+e+"='"+n.replace(/'/g,"'")+"'"):""}e.merge=function e(n,r){if(1===arguments.length){for(var s=n[0],o=1;o<n.length;o++)s=e(s,n[o]);return s}for(var a in r)if("class"===a){var c=n[a]||[];n[a]=(Array.isArray(c)?c:[c]).concat(r[a]||[])}else if("style"===a){c=(c=t(n[a]))&&";"!==c[c.length-1]?c+";":c;var i=t(r[a]);i=i&&";"!==i[i.length-1]?i+";":i,n[a]=c+i}else n[a]=r[a];return n},e.classes=r,e.style=t,e.attr=s,e.attrs=function(e,o){var a="";for(var c in e)if(n.call(e,c)){var i=e[c];if("class"===c){a=s(c,i=r(i),!1,o)+a;continue}"style"===c&&(i=t(i)),a+=s(c,i,!1,o)}return a};var o=/["&<>]/;function a(e){var n=""+e,r=o.exec(n);if(!r)return e;var t,s,a,c="";for(t=r.index,s=0;t<n.length;t++){switch(n.charCodeAt(t)){case 34:a=""";break;case 38:a="&";break;case 60:a="<";break;case 62:a=">";break;default:continue}s!==t&&(c+=n.substring(s,t)),s=t+1,c+=a}return s!==t?c+n.substring(s,t):c}return e.escape=a,e.rethrow=function e(n,r,t,s){if(!(n instanceof Error))throw n;if(!("undefined"==typeof window&&r||s))throw n.message+=" on line "+t,n;try{s=s||require("fs").readFileSync(r,"utf8")}catch(r){e(n,null,t)}var o=3,a=s.split("\n"),c=Math.max(t-o,0),i=Math.min(a.length,t+o);o=a.slice(c,i).map((function(e,n){var r=n+c+1;return(r==t?" > ":" ")+r+"| "+e})).join("\n");throw n.path=r,n.message=(r||"Pug")+":"+t+"\n"+o+"\n\n"+n.message,n},e}({});function N(e){var n,r,t="";try{var s={},o=e||{};(function(e,r){t+="<!DOCTYPE html>",t+="\n<html>",t+="\n <head>",t+="\n <title>",t+="API Documentation</title>",t+="\n <style>\n ",t+="body { font-family: Arial, sans-serif; margin: 20px; }",t+="\n ",t+="",t+="\n ",t+="h1 { color: #333; text-align: center; }",t+="\n ",t+="h2, h3 { cursor: pointer; transition: color 0.3s; }",t+="\n ",t+="h2 { color: #007BFF; }",t+="\n ",t+="h2:hover { color: #0056b3; }",t+="\n ",t+="h3 { color: #0d6efd; }",t+="\n ",t+="h3:hover { color: #0056b3; } ",t+="\n ",t+="",t+="\n ",t+=".api-section { ",t+="\n ",t+=" margin: 20px 0; ",t+="\n ",t+=" border: 1px solid #e0e0e0; ",t+="\n ",t+=" border-radius: 8px; ",t+="\n ",t+=" padding: 10px; ",t+="\n ",t+=" background-color: #f9f9f9; ",t+="\n ",t+=" box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); ",t+="\n ",t+="}",t+="\n ",t+="",t+="\n ",t+=".api-method { font-weight: bold; color: #333; }",t+="\n ",t+=".api-path { font-style: italic; color: #555; }",t+="\n ",t+="",t+="\n ",t+="pre { ",t+="\n ",t+=" background: #f4f4f4; ",t+="\n ",t+=" padding: 10px; ",t+="\n ",t+=" border-radius: 5px; ",t+="\n ",t+=" overflow-x: auto; ",t+="\n ",t+="}",t+="\n ",t+="",t+="\n ",t+=".collapse { display: none; }",t+="\n ",t+="\n </style>\n </head>",t+="\n <body>",t+="\n <h1>",t+="API Documentation</h1>",t+="\n \x3c!-- 添加 JavaScript 用于折叠 Sections--\x3e",t+="\n <script>\n ",t+="document.addEventListener('DOMContentLoaded', function() {",t+="\n ",t+=" // 折叠 API 部分",t+="\n ",t+=" const headings = document.querySelectorAll('h2');",t+="\n ",t+=" headings.forEach(heading => {",t+="\n ",t+=" heading.addEventListener('click', function() {",t+="\n ",t+=" const content = this.parentElement.querySelectorAll('.collapse')[0];",t+="\n ",t+=" if (content) {",t+="\n ",t+=' content.style.display = (content.style.display === "block") ? "none" : "block";',t+="\n ",t+=" }",t+="\n ",t+=" });",t+="\n ",t+=" });",t+="\n ",t+=" ",t+="\n ",t+=" // 折叠 API 方法",t+="\n ",t+=" const methodHeadings = document.querySelectorAll('h3');",t+="\n ",t+=" methodHeadings.forEach(method => {",t+="\n ",t+=" method.addEventListener('click', function() {",t+="\n ",t+=" const details = this.nextElementSibling;",t+="\n ",t+=" if (details) {",t+="\n ",t+=' details.style.display = (details.style.display === "block") ? "none" : "block";',t+="\n ",t+=" }",t+="\n ",t+=" });",t+="\n ",t+=" });",t+="\n ",t+="});",t+="\n ",t+="\n <\/script>",function(){var s=r;if("number"==typeof s.length)for(var o=0,a=s.length;o<a;o++){var c=s[o];t+='\n <div class="api-section">',t=(t+='\n <h2 class="toggle">')+M.escape(null==(n=c.name)?"":n)+"</h2>",c.desc&&(t=(t+="\n <p>")+M.escape(null==(n=c.desc)?"":n)+"</p>"),t+='\n <div class="collapse">',function(){var r=c.configs;if("number"==typeof r.length)for(var s=0,o=r.length;s<o;s++){var a=r[s];t=(t+='\n <h3 class="api-method">')+M.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",t=(t+="\n <h4>")+M.escape(null==(n=a.description)?"":n)+"</h4>",t+="\n <pre>",t+="Request Header:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Query:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Response Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}else{o=0;for(var s in r){o++;a=r[s];t=(t+='\n <h3 class="api-method">')+M.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",t=(t+="\n <h4>")+M.escape(null==(n=a.description)?"":n)+"</h4>",t+="\n <pre>",t+="Request Header:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Query:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Response Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}}}.call(this),t+="\n </div>\n </div>"}else{a=0;for(var o in s){a++;c=s[o];t+='\n <div class="api-section">',t=(t+='\n <h2 class="toggle">')+M.escape(null==(n=c.name)?"":n)+"</h2>",c.desc&&(t=(t+="\n <p>")+M.escape(null==(n=c.desc)?"":n)+"</p>"),t+='\n <div class="collapse">',function(){var r=c.configs;if("number"==typeof r.length)for(var s=0,o=r.length;s<o;s++){var a=r[s];t=(t+='\n <h3 class="api-method">')+M.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",t=(t+="\n <h4>")+M.escape(null==(n=a.description)?"":n)+"</h4>",t+="\n <pre>",t+="Request Header:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Query:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Response Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}else{o=0;for(var s in r){o++;a=r[s];t=(t+='\n <h3 class="api-method">')+M.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",t=(t+="\n <h4>")+M.escape(null==(n=a.description)?"":n)+"</h4>",t+="\n <pre>",t+="Request Header:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Request Query:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",t+="\n <pre>",t+="Response Body:</pre>",t=(t+="\n <pre>")+M.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}}}.call(this),t+="\n </div>\n </div>"}}}.call(this),t+="\n </body>\n</html>"}).call(this,"JSON"in o?o.JSON:"undefined"!=typeof JSON?JSON:void 0,"apiData"in o?o.apiData:"undefined"!=typeof apiData?apiData:void 0)}catch(e){M.rethrow(e,r,undefined,s[void 0])}return t}const C={DOCUMENTATION_DIR:r.join(process.cwd(),"doc")},P=e=>{const n=v(e.wholePath),r=n.prototype,t=Object.getOwnPropertyNames(r).filter((e=>"function"==typeof r[e]&&"constructor"!==e)),s=e.path.substring(e.path.indexOf("doc")+3);return{configs:t.map((e=>{if("function"==typeof r[e]){const n=r[e]();return n.path=s+n.path,n}return null})).filter((e=>!!e)),desc:n.desc,name:n.name}};global.isDev="development"===process.env.NODE_ENV,(()=>{const e=process.env.NODE_ENV||"production",n=r.resolve(O,"env");switch(console.log("load env path: ",n),a.config({path:r.resolve(n,".common.env")}),e){case"development":a.config({path:r.resolve(n,".development.env")});break;case"test":a.config({path:r.resolve(n,".test.env")});break;case"production":a.config({path:r.resolve(n,".production.env")})}})();exports.AuthRouter=function(e,n){return c(e,n,!0)},exports.BaseException=f,exports.Router=function(e,n){return c(e,n)},exports.contextStore=p,exports.errorRsp=h,exports.getCurrentContext=u,exports.getSrcModulePaths=g,exports.initializeKoaApp=async e=>{const{koaInstance:n,authCheckCallback:r,catchErrorCallback:s,registerHighPriorityMiddleware:o}=e;(e=>{const{controllerModule:n,validateModule:r}=g();m(n,(n=>{if(!n.path.includes("controller"))return;const t=v(n.wholePath),s=t?.routesMap;s&&S(r,n,s,((r,t)=>!r||e({filePath:n,ctx:t})))}))})(r);const a=n||new t;var c;if(a.use(w),o?.(a),a.use((c=s,async(e,n)=>{try{await n()}catch(n){c?c(n,e):h(500,{message:n.message})}})),isDev){const e=await(async()=>{const e=[];return m(C.DOCUMENTATION_DIR,(n=>{e.push(P(n))})),N({apiData:e})})();q.get("/doc",(async n=>{n.body=e}))}return a.use(q.routes()),await k(a),[a,q]},exports.successRsp=e=>{const n=Array.isArray(e?.data);d({success:!0,message:e?.message,data:n?{list:e?.data}:e?.data})},exports.unSuccessRsp=e=>{d({success:!1,errorCode:e?.code,errorMessage:e?.message,data:e?.data})};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AsyncLocalStorage as e}from"async_hooks";import{readdirSync as n,existsSync as t}from"fs";import{resolve as r,join as o}from"path";import s from"koa";import a from"koa-router";import c from"net";import i from"dotenv";function l(e,n,t=!1){return(r,o,s)=>{let a=r.constructor.routesMap;a instanceof Map||(r.constructor.routesMap=new Map,a=r.constructor.routesMap);const c=r.constructor.name,i=n??o;return a.set(o,{handler:function(...e){if("function"!=typeof s.value)throw new Error("Register Router Error");return s.value.apply(r,e)},path:`/${i.replace("/","")}`,method:e,functionName:o,authRequired:t,middlewares:[],className:c}),r}}function p(e,n){return l(e,n)}function u(e,n){return l(e,n,!0)}const d=["get","post","put","delete","patch","options"],h=0,f=new e,y=()=>{const e=f.getStore();if(!e)throw new Error("context is not exist");return e},g=e=>{const{success:n=!0,errorCode:t=-1,message:r="success",errorMessage:o="error request",data:s=null,statusCode:a=200}=e??{},c=y(),i={code:n?h:t,message:n?r:o,...s&&{data:s}};c.body=i,c.status=a},m=e=>{const n=Array.isArray(e?.data);g({success:!0,message:e?.message,data:n?{list:e?.data}:e?.data})},v=e=>{g({success:!1,errorCode:e?.code,errorMessage:e?.message,data:e?.data})},b=(e,n)=>{g({success:!1,errorMessage:n?.message,errorCode:n?.code,data:n?.data,statusCode:e})};class w extends Error{constructor(e){super(e?.message||"An unexpected error occurred"),this.name=this.constructor.name,this.code=e?.code??-1,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor),this.stack=this.stack??new Error(this.message).stack}get toRspOptions(){return{code:this.code,message:this.message,data:this.stack}}}const q=process.cwd(),x=()=>{const e=r(q,"src");return{controllerModule:r(e,"controller"),validateModule:r(e,"validate"),viewModule:r(e,"view"),docModule:r(e,"doc")}},k=e=>{if(t(e)){return require(e).default}};function E(e,t){try{n(e,{withFileTypes:!0}).forEach((n=>{const o=r(e,n.name);n.isFile()?t({path:e,name:n.name,wholePath:o},n):n.isDirectory()&&E(o,t)}))}catch(e){}}const O=e=>"win32"===process.platform?e.replace(/\\/g,"/"):e,R=(e,n)=>f.run(e,(()=>n())),N=new a,M=e=>new Promise((n=>{const t=c.createServer();t.unref(),t.on("error",(()=>n(!1))),t.listen(e,(()=>{t.close((()=>n(!0)))}))})),A=s.prototype.listen,C=e=>{const n=e.address();if(n){((e,n)=>{const t=null!==n?`http://${e}:${n}`:e;console.log(`app listen ${t}`)})("object"==typeof n?(e=>"::"===e?"0.0.0.0":e)(n.address):n,"object"==typeof n?n.port:null)}},D=async e=>{const n=await(async(e=8e3,n=9999)=>{let t=e;for(;t<=n;){if(await M(t))return t;t+=1}throw new Error("No available port found")})();return e.listen=(...t)=>{const r=t?A.apply(e,[n]):A.apply(e,t);return r.on("listening",(()=>C(r))),r},n},P=(e,n,t,r)=>{const{controllerPrefix:o,moduleName:s,name:a}=(e=>{const n="controller",t=e.path.indexOf(n);if(-1===t)throw new Error(`'${n}' not found in path: ${e.path}`);const r=O(e.path.substring(t+10)),o=e.name?.split(".")?.[0];return{controllerPrefix:r,moduleName:o,name:e.name}})(n),c=`${o}/${s}`;if(!t)return;const i=k(`${e}${`${o}/${a}`}`);t.forEach((e=>{const{method:n,path:t,handler:o,functionName:s,authRequired:a,middlewares:l}=e;var p;i&&"function"==typeof i[s]&&l.push((p=i[s],async(e,n)=>{const t=p(e);t instanceof Promise&&await t,await n()}));const u=(d=a,h=o,async(e,n)=>{let t=r(d,e);return t instanceof Promise&&(t=await t),t?h(e,n):e.throw(403,"无权限")});var d,h;const f=[...l,u],y=S(t,c,s,f,n);e.path=y}))},S=(e,n,t,r,o)=>{const s=t.toLowerCase();let a="get";a="get_"===s?"get":d.includes(s)?s:o??"get";const c=`${n}${"get_"==s?"":e??""}`;return N[a](c,...r),c},$=process.cwd();var B=function(e){var n=Object.prototype.hasOwnProperty;function t(e,r){return Array.isArray(e)?function(e,n){for(var r,o="",s="",c=Array.isArray(n),i=0;i<e.length;i++)(r=t(e[i]))&&(c&&n[i]&&(r=a(r)),o=o+s+r,s=" ");return o}(e,r):e&&"object"==typeof e?function(e){var t="",r="";for(var o in e)o&&e[o]&&n.call(e,o)&&(t=t+r+o,r=" ");return t}(e):e||""}function r(e){if(!e)return"";if("object"==typeof e){var t="";for(var r in e)n.call(e,r)&&(t=t+r+":"+e[r]+";");return t}return e+""}function o(e,n,t,r){return!1!==n&&null!=n&&(n||"class"!==e&&"style"!==e)?!0===n?" "+(r?e:e+'="'+e+'"'):("function"==typeof n.toJSON&&(n=n.toJSON()),"string"==typeof n||(n=JSON.stringify(n),t||-1===n.indexOf('"'))?(t&&(n=a(n))," "+e+'="'+n+'"'):" "+e+"='"+n.replace(/'/g,"'")+"'"):""}e.merge=function e(n,t){if(1===arguments.length){for(var o=n[0],s=1;s<n.length;s++)o=e(o,n[s]);return o}for(var a in t)if("class"===a){var c=n[a]||[];n[a]=(Array.isArray(c)?c:[c]).concat(t[a]||[])}else if("style"===a){c=(c=r(n[a]))&&";"!==c[c.length-1]?c+";":c;var i=r(t[a]);i=i&&";"!==i[i.length-1]?i+";":i,n[a]=c+i}else n[a]=t[a];return n},e.classes=t,e.style=r,e.attr=o,e.attrs=function(e,s){var a="";for(var c in e)if(n.call(e,c)){var i=e[c];if("class"===c){a=o(c,i=t(i),!1,s)+a;continue}"style"===c&&(i=r(i)),a+=o(c,i,!1,s)}return a};var s=/["&<>]/;function a(e){var n=""+e,t=s.exec(n);if(!t)return e;var r,o,a,c="";for(r=t.index,o=0;r<n.length;r++){switch(n.charCodeAt(r)){case 34:a=""";break;case 38:a="&";break;case 60:a="<";break;case 62:a=">";break;default:continue}o!==r&&(c+=n.substring(o,r)),o=r+1,c+=a}return o!==r?c+n.substring(o,r):c}return e.escape=a,e.rethrow=function e(n,t,r,o){if(!(n instanceof Error))throw n;if(!("undefined"==typeof window&&t||o))throw n.message+=" on line "+r,n;try{o=o||require("fs").readFileSync(t,"utf8")}catch(t){e(n,null,r)}var s=3,a=o.split("\n"),c=Math.max(r-s,0),i=Math.min(a.length,r+s);s=a.slice(c,i).map((function(e,n){var t=n+c+1;return(t==r?" > ":" ")+t+"| "+e})).join("\n");throw n.path=t,n.message=(t||"Pug")+":"+r+"\n"+s+"\n\n"+n.message,n},e}({});function I(e){var n,t,r="";try{var o={},s=e||{};(function(e,t){r+="<!DOCTYPE html>",r+="\n<html>",r+="\n <head>",r+="\n <title>",r+="API Documentation</title>",r+="\n <style>\n ",r+="body { font-family: Arial, sans-serif; margin: 20px; }",r+="\n ",r+="",r+="\n ",r+="h1 { color: #333; text-align: center; }",r+="\n ",r+="h2, h3 { cursor: pointer; transition: color 0.3s; }",r+="\n ",r+="h2 { color: #007BFF; }",r+="\n ",r+="h2:hover { color: #0056b3; }",r+="\n ",r+="h3 { color: #0d6efd; }",r+="\n ",r+="h3:hover { color: #0056b3; } ",r+="\n ",r+="",r+="\n ",r+=".api-section { ",r+="\n ",r+=" margin: 20px 0; ",r+="\n ",r+=" border: 1px solid #e0e0e0; ",r+="\n ",r+=" border-radius: 8px; ",r+="\n ",r+=" padding: 10px; ",r+="\n ",r+=" background-color: #f9f9f9; ",r+="\n ",r+=" box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); ",r+="\n ",r+="}",r+="\n ",r+="",r+="\n ",r+=".api-method { font-weight: bold; color: #333; }",r+="\n ",r+=".api-path { font-style: italic; color: #555; }",r+="\n ",r+="",r+="\n ",r+="pre { ",r+="\n ",r+=" background: #f4f4f4; ",r+="\n ",r+=" padding: 10px; ",r+="\n ",r+=" border-radius: 5px; ",r+="\n ",r+=" overflow-x: auto; ",r+="\n ",r+="}",r+="\n ",r+="",r+="\n ",r+=".collapse { display: none; }",r+="\n ",r+="\n </style>\n </head>",r+="\n <body>",r+="\n <h1>",r+="API Documentation</h1>",r+="\n \x3c!-- 添加 JavaScript 用于折叠 Sections--\x3e",r+="\n <script>\n ",r+="document.addEventListener('DOMContentLoaded', function() {",r+="\n ",r+=" // 折叠 API 部分",r+="\n ",r+=" const headings = document.querySelectorAll('h2');",r+="\n ",r+=" headings.forEach(heading => {",r+="\n ",r+=" heading.addEventListener('click', function() {",r+="\n ",r+=" const content = this.parentElement.querySelectorAll('.collapse')[0];",r+="\n ",r+=" if (content) {",r+="\n ",r+=' content.style.display = (content.style.display === "block") ? "none" : "block";',r+="\n ",r+=" }",r+="\n ",r+=" });",r+="\n ",r+=" });",r+="\n ",r+=" ",r+="\n ",r+=" // 折叠 API 方法",r+="\n ",r+=" const methodHeadings = document.querySelectorAll('h3');",r+="\n ",r+=" methodHeadings.forEach(method => {",r+="\n ",r+=" method.addEventListener('click', function() {",r+="\n ",r+=" const details = this.nextElementSibling;",r+="\n ",r+=" if (details) {",r+="\n ",r+=' details.style.display = (details.style.display === "block") ? "none" : "block";',r+="\n ",r+=" }",r+="\n ",r+=" });",r+="\n ",r+=" });",r+="\n ",r+="});",r+="\n ",r+="\n <\/script>",function(){var o=t;if("number"==typeof o.length)for(var s=0,a=o.length;s<a;s++){var c=o[s];r+='\n <div class="api-section">',r=(r+='\n <h2 class="toggle">')+B.escape(null==(n=c.name)?"":n)+"</h2>",c.desc&&(r=(r+="\n <p>")+B.escape(null==(n=c.desc)?"":n)+"</p>"),r+='\n <div class="collapse">',function(){var t=c.configs;if("number"==typeof t.length)for(var o=0,s=t.length;o<s;o++){var a=t[o];r=(r+='\n <h3 class="api-method">')+B.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",r=(r+="\n <h4>")+B.escape(null==(n=a.description)?"":n)+"</h4>",r+="\n <pre>",r+="Request Header:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Query:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Response Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}else{s=0;for(var o in t){s++;a=t[o];r=(r+='\n <h3 class="api-method">')+B.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",r=(r+="\n <h4>")+B.escape(null==(n=a.description)?"":n)+"</h4>",r+="\n <pre>",r+="Request Header:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Query:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Response Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}}}.call(this),r+="\n </div>\n </div>"}else{a=0;for(var s in o){a++;c=o[s];r+='\n <div class="api-section">',r=(r+='\n <h2 class="toggle">')+B.escape(null==(n=c.name)?"":n)+"</h2>",c.desc&&(r=(r+="\n <p>")+B.escape(null==(n=c.desc)?"":n)+"</p>"),r+='\n <div class="collapse">',function(){var t=c.configs;if("number"==typeof t.length)for(var o=0,s=t.length;o<s;o++){var a=t[o];r=(r+='\n <h3 class="api-method">')+B.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",r=(r+="\n <h4>")+B.escape(null==(n=a.description)?"":n)+"</h4>",r+="\n <pre>",r+="Request Header:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Query:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Response Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}else{s=0;for(var o in t){s++;a=t[o];r=(r+='\n <h3 class="api-method">')+B.escape(null==(n=a.method.toUpperCase()+" "+a.path)?"":n)+"</h3>",r=(r+="\n <h4>")+B.escape(null==(n=a.description)?"":n)+"</h4>",r+="\n <pre>",r+="Request Header:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.header,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.body,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Request Query:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.request.query,null,2))?"":n)+"</pre>",r+="\n <pre>",r+="Response Body:</pre>",r=(r+="\n <pre>")+B.escape(null==(n=e.stringify(a.response.body,null,2))?"":n)+"</pre>"}}}.call(this),r+="\n </div>\n </div>"}}}.call(this),r+="\n </body>\n</html>"}).call(this,"JSON"in s?s.JSON:"undefined"!=typeof JSON?JSON:void 0,"apiData"in s?s.apiData:"undefined"!=typeof apiData?apiData:void 0)}catch(e){B.rethrow(e,t,undefined,o[void 0])}return r}const J={DOCUMENTATION_DIR:o(process.cwd(),"doc")},T=e=>{const n=k(e.wholePath),t=n.prototype,r=Object.getOwnPropertyNames(t).filter((e=>"function"==typeof t[e]&&"constructor"!==e)),o=e.path.substring(e.path.indexOf("doc")+3);return{configs:r.map((e=>{if("function"==typeof t[e]){const n=t[e]();return n.path=o+n.path,n}return null})).filter((e=>!!e)),desc:n.desc,name:n.name}};global.isDev="development"===process.env.NODE_ENV,(()=>{const e=process.env.NODE_ENV||"production",n=r($,"env");switch(console.log("load env path: ",n),i.config({path:r(n,".common.env")}),e){case"development":i.config({path:r(n,".development.env")});break;case"test":i.config({path:r(n,".test.env")});break;case"production":i.config({path:r(n,".production.env")})}})();const j=async e=>{const{koaInstance:n,authCheckCallback:t,catchErrorCallback:r,registerHighPriorityMiddleware:o}=e;(e=>{const{controllerModule:n,validateModule:t}=x();E(n,(n=>{if(!n.path.includes("controller"))return;const r=k(n.wholePath),o=r?.routesMap;o&&P(t,n,o,((t,r)=>!t||e({filePath:n,ctx:r})))}))})(t);const a=n||new s;var c;if(a.use(R),o?.(a),a.use((c=r,async(e,n)=>{try{await n()}catch(n){c?c(n,e):b(500,{message:n.message})}})),isDev){const e=await(async()=>{const e=[];return E(J.DOCUMENTATION_DIR,(n=>{e.push(T(n))})),I({apiData:e})})();N.get("/doc",(async n=>{n.body=e}))}return a.use(N.routes()),await D(a),[a,N]};export{u as AuthRouter,w as BaseException,p as Router,f as contextStore,b as errorRsp,y as getCurrentContext,x as getSrcModulePaths,j as initializeKoaApp,m as successRsp,v as unSuccessRsp};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Koa from "koa";
|
|
2
|
+
import { TErrorCallback } from '../middleware/exception_middleware';
|
|
3
|
+
import { appRouter } from '../router';
|
|
4
|
+
import type { AuthRouterCallback } from '../types/route';
|
|
5
|
+
export type TInitOPtions = Partial<{
|
|
6
|
+
koaInstance: Koa;
|
|
7
|
+
authCheckCallback: AuthRouterCallback;
|
|
8
|
+
catchErrorCallback: TErrorCallback;
|
|
9
|
+
registerHighPriorityMiddleware: (app: Koa) => void;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* 初始化core
|
|
13
|
+
* @param {TInitOPtions} options
|
|
14
|
+
* @returns {[Koa, typeof appRouter]} [app, appRouter]
|
|
15
|
+
*/
|
|
16
|
+
export declare const initializeKoaApp: (options: TInitOPtions) => Promise<[Koa, typeof appRouter]>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AuthRouterCallback } from '../types/route';
|
|
2
|
+
/**
|
|
3
|
+
* 约定式控制器注册路由
|
|
4
|
+
* @param controllerModulePath 控制器模块路径
|
|
5
|
+
* @param validateModulePath 验证模块路径
|
|
6
|
+
* @param authCallback 鉴权回调
|
|
7
|
+
*/
|
|
8
|
+
declare const registerControllers: (authCheckCallback?: AuthRouterCallback) => void;
|
|
9
|
+
export default registerControllers;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AuthRouterHandler, FilePathMeta, RouteConfig } from '../types/route';
|
|
2
|
+
/**
|
|
3
|
+
* 注册路由
|
|
4
|
+
* @param validateModulePath 参数验证器路径
|
|
5
|
+
* @param filePath 控制器文件路径
|
|
6
|
+
* @param routesMap 路由映射
|
|
7
|
+
* @param authCheckResult 鉴权结果处理函数
|
|
8
|
+
*/
|
|
9
|
+
declare const registerRoute: (validateModulePath: string, filePath: FilePathMeta, routesMap: Map<string, RouteConfig>, authCheckResult: AuthRouterHandler) => void;
|
|
10
|
+
export default registerRoute;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Middleware } from "koa";
|
|
2
|
+
/**
|
|
3
|
+
* Koa 上下文存储中间件
|
|
4
|
+
*
|
|
5
|
+
* 功能:
|
|
6
|
+
* 1. 为每个请求创建独立的上下文存储环境
|
|
7
|
+
* 2. 确保后续中间件和业务逻辑可以安全访问当前请求的上下文
|
|
8
|
+
* 3. 基于 AsyncLocalStorage 实现请求级别的数据隔离
|
|
9
|
+
*
|
|
10
|
+
* @param ctx Koa 上下文对象
|
|
11
|
+
* @param next 后续中间件执行函数
|
|
12
|
+
* @returns Promise<void>
|
|
13
|
+
*/
|
|
14
|
+
declare const contextMiddleware: Middleware;
|
|
15
|
+
export default contextMiddleware;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import BaseException from '../base/exception';
|
|
2
|
+
import type Koa from "koa";
|
|
3
|
+
export type TErrorCallback = (error: Error | BaseException, ctx: Koa.Context) => void;
|
|
4
|
+
/**
|
|
5
|
+
* 错误处理中间件
|
|
6
|
+
* @param {TErrorCallback} catchCallback
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
declare const catchErrorMiddleware: (catchCallback?: TErrorCallback) => (ctx: Koa.Context, next: Koa.Next) => Promise<void>;
|
|
10
|
+
export default catchErrorMiddleware;
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Context, Middleware } from "koa";
|
|
2
|
+
import type { Dirent } from "fs";
|
|
3
|
+
export type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options";
|
|
4
|
+
/** 文件路径元数据 */
|
|
5
|
+
export interface FilePathMeta {
|
|
6
|
+
path: string;
|
|
7
|
+
name: string;
|
|
8
|
+
wholePath: string;
|
|
9
|
+
}
|
|
10
|
+
export type AuthRouterHandler = (isAuthRouter: boolean, ctx: Context) => Promise<boolean> | boolean;
|
|
11
|
+
export type AuthRouterCallback = (params: {
|
|
12
|
+
filePath: FilePathMeta;
|
|
13
|
+
ctx: Context;
|
|
14
|
+
}) => Promise<boolean> | boolean;
|
|
15
|
+
export type WalkSyncHandler = (filePath: FilePathMeta, dirent: Dirent) => void;
|
|
16
|
+
export interface RouteConfig {
|
|
17
|
+
path: string;
|
|
18
|
+
method?: HttpMethod;
|
|
19
|
+
/** 控制器函数(使用 Koa 标准 Middleware 类型) */
|
|
20
|
+
handler: Middleware;
|
|
21
|
+
functionName: string;
|
|
22
|
+
authRequired: boolean;
|
|
23
|
+
middlewares: Middleware[];
|
|
24
|
+
className: string;
|
|
25
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { FilePathMeta, WalkSyncHandler } from '../types/route';
|
|
2
|
+
/**
|
|
3
|
+
* 获取 src 模块的路径
|
|
4
|
+
* @returns 模块路径对象
|
|
5
|
+
*/
|
|
6
|
+
export declare const getSrcModulePaths: () => {
|
|
7
|
+
controllerModule: string;
|
|
8
|
+
validateModule: string;
|
|
9
|
+
viewModule: string;
|
|
10
|
+
docModule: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* 获取指定路径的默认导出
|
|
14
|
+
* @param {string} filePath - 文件路径
|
|
15
|
+
* @returns {T | undefined} - 文件的默认导出或 undefined
|
|
16
|
+
*/
|
|
17
|
+
export declare const getDefaultExportFromFile: <T = any>(filePath: string) => T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* 递归同步遍历指定目录下的所有文件
|
|
20
|
+
* @param {string} currentDirPath - 当前目录路径
|
|
21
|
+
* @param {WalkSyncHandler} callback - 处理每个文件的回调函数
|
|
22
|
+
*/
|
|
23
|
+
export declare function walkDirectorySync(currentDirPath: string, callback: WalkSyncHandler): void;
|
|
24
|
+
/**
|
|
25
|
+
* 根据路由器的路径生成控制器前缀和类名
|
|
26
|
+
* @param {FilePathMeta} filePath - 文件路径元数据
|
|
27
|
+
* @returns {Object} 包含控制器前缀、模块名和文件名的对象
|
|
28
|
+
*/
|
|
29
|
+
export declare const generateControllerOptions: (filePath: FilePathMeta) => {
|
|
30
|
+
controllerPrefix: string;
|
|
31
|
+
moduleName: string;
|
|
32
|
+
name: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* 统一处理不同平台的文件路径格式,将路径转换为 '/test/file' 格式
|
|
36
|
+
* @param {string} filePath - 原始文件路径
|
|
37
|
+
* @returns {string} 转换后的文件路径
|
|
38
|
+
*/
|
|
39
|
+
export declare const formatPathByPlatform: (filePath: string) => string;
|
|
40
|
+
/**
|
|
41
|
+
* 确保文件存在,如果不存在则创建目录和文件
|
|
42
|
+
* @param {string} filePath - 要检查的文件路径
|
|
43
|
+
* @param {string} content - 文件内容,如果不指定,则为空文件
|
|
44
|
+
* @returns {boolean} 文件是否存在
|
|
45
|
+
*/
|
|
46
|
+
export declare function ensureFileExists(filePath: string, content?: string): boolean;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检查端口是否可用
|
|
3
|
+
* @param {number} port - 要检查的端口号
|
|
4
|
+
* @returns {Promise<boolean>} - 如果端口可用,则返回 true;否则返回 false
|
|
5
|
+
*/
|
|
6
|
+
declare const isPortAvailable: (port: number) => Promise<boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* 找到可用的端口,默认从指定的起始端口开始检查
|
|
9
|
+
* @param {number} startPort - 起始检查的端口号,默认为 8000
|
|
10
|
+
* @returns {Promise<number>} - 返回第一个可用的端口
|
|
11
|
+
*/
|
|
12
|
+
declare const findAvailablePort: (startPort?: number, maxPort?: number) => Promise<number>;
|
|
13
|
+
export { isPortAvailable, findAvailablePort };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type Options = Partial<{
|
|
2
|
+
data: any;
|
|
3
|
+
message: string;
|
|
4
|
+
code: number;
|
|
5
|
+
}>;
|
|
6
|
+
/**
|
|
7
|
+
* 构建成功的请求响应
|
|
8
|
+
* @param options - 成功响应的选项,如数据和消息
|
|
9
|
+
*/
|
|
10
|
+
export declare const successRsp: (options?: Options) => void;
|
|
11
|
+
/**
|
|
12
|
+
* 构建不成功的请求响应,但 HTTP 状态码仍为 200
|
|
13
|
+
* @param options - 包含可能的错误码、消息和数据
|
|
14
|
+
*/
|
|
15
|
+
export declare const unSuccessRsp: (options?: Options) => void;
|
|
16
|
+
/**
|
|
17
|
+
* 构建异常请求响应,HTTP 状态码不为 200
|
|
18
|
+
* @param statusCode - 要返回的 HTTP 状态码
|
|
19
|
+
* @param options - 包含可能的错误信息、数据等
|
|
20
|
+
*/
|
|
21
|
+
export declare const errorRsp: (statusCode: number, options?: Options) => void;
|
|
22
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koa-ts-core",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "npx rollup -c --bundleConfigAsCjs",
|
|
13
|
+
"build:dev": "NODE_ENV=development npx rollup -c --bundleConfigAsCjs"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"dotenv": "^16.4.7",
|
|
20
|
+
"koa": "^2.15.4",
|
|
21
|
+
"koa-router": "^13.0.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
25
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
26
|
+
"@types/koa": "^2.15.0",
|
|
27
|
+
"@types/koa-router": "^7.4.8",
|
|
28
|
+
"@types/node": "^22.13.4",
|
|
29
|
+
"rollup-plugin-delete": "^2.1.0",
|
|
30
|
+
"typeorm": "^0.3.20",
|
|
31
|
+
"typescript": "^5.7.3"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=14.0.0"
|
|
35
|
+
}
|
|
36
|
+
}
|