honestjs 0.1.5 → 0.1.6
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/README.md
CHANGED
|
@@ -76,22 +76,16 @@ ultra-fast performance of Hono, giving you the best of both worlds.
|
|
|
76
76
|
|
|
77
77
|
## Quick Start
|
|
78
78
|
|
|
79
|
-
### Using Honest CLI
|
|
79
|
+
### Using Honest CLI
|
|
80
80
|
|
|
81
81
|
The fastest way to create a new Honest application is to use the Honest CLI:
|
|
82
82
|
|
|
83
83
|
```bash
|
|
84
84
|
# Install Honest CLI globally
|
|
85
85
|
bun add -g @honestjs/cli
|
|
86
|
-
# or
|
|
87
|
-
pnpm add -g @honestjs/cli
|
|
88
|
-
# or
|
|
89
|
-
yarn global add @honestjs/cli
|
|
90
|
-
# or
|
|
91
|
-
npm install -g @honestjs/cli
|
|
92
86
|
|
|
93
87
|
# Create a new project
|
|
94
|
-
|
|
88
|
+
honestjs new my-project # alias: honest, hnjs
|
|
95
89
|
cd my-project
|
|
96
90
|
|
|
97
91
|
# Start the development server
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import"reflect-metadata";import{Hono as t}from"hono";class M{instances=new Map;resolve($,_=new Set){if(this.instances.has($))return this.instances.get($);if(_.has($))throw new Error(`Circular dependency detected: ${[..._.keys(),$].map((Q)=>Q.name).join(" -> ")}`);_.add($);let Y=(Reflect.getMetadata("design:paramtypes",$)||[]).map((Q)=>{return this.resolve(Q,new Set(_))}),q=new $(...Y);return this.instances.set($,q),q}register($,_){this.instances.set($,_)}}import{HTTPException as r}from"hono/http-exception";function R($,_,W){let Y=new Date().toISOString(),q=_.get("requestId"),Q=_.req.path;if($ instanceof r)return{response:{status:W?.status||$.status,message:W?.title||$.message,timestamp:Y,path:Q,requestId:q,code:W?.code,details:W?.additionalDetails,...W?.detail&&{detail:W.detail}},status:W?.status||$.status};if($.statusCode||$.status){let z=$.statusCode||$.status,G=W?.status||z;return{response:{status:G,message:W?.title||$.message,timestamp:Y,path:Q,requestId:q,code:W?.code||$.name,details:W?.additionalDetails,...W?.detail&&{detail:W.detail}},status:G}}let J=W?.status||500;return{response:{status:J,message:W?.title||$.message,timestamp:Y,path:Q,requestId:q,code:W?.code||$.name,details:W?.additionalDetails||{stack:$.stack},...W?.detail&&{detail:W.detail}},status:J}}class X{static routes=new Map;static controllers=new Map;static controllerOptions=new Map;static services=new Set;static modules=new Map;static parameters=new Map;static contextIndices=new Map;static global=new Map([["middleware",new Set],["guard",new Set],["pipe",new Set],["filter",new Set]]);static controller=new Map([["middleware",new Map],["guard",new Map],["pipe",new Map],["filter",new Map]]);static handler=new Map([["middleware",new Map],["guard",new Map],["pipe",new Map],["filter",new Map]]);static getRoutes($){return this.routes.get($)||[]}static setRoutes($,_){this.routes.set($,_)}static addRoute($,_){if(!this.routes.has($))this.routes.set($,[]);this.routes.get($).push(_)}static getControllerPath($){return this.controllers.get($)||""}static setControllerPath($,_){this.controllers.set($,_)}static getControllerOptions($){return this.controllerOptions.get($)||{}}static setControllerOptions($,_){this.controllerOptions.set($,_)}static isService($){return this.services.has($)}static addService($){this.services.add($)}static getAllServices(){return this.services}static getModuleOptions($){return this.modules.get($)}static setModuleOptions($,_){this.modules.set($,_)}static getParameters($){return this.parameters.get($)||new Map}static setParameterMap($,_){this.parameters.set($,_)}static getContextIndices($){return this.contextIndices.get($)||new Map}static setContextIndices($,_){this.contextIndices.set($,_)}static registerGlobal($,_){this.global.get($).add(_)}static getGlobal($){return this.global.get($)}static registerController($,_,W){let Y=this.controller.get($);if(!Y.has(_))Y.set(_,[]);Y.get(_).push(W)}static getController($,_){return this.controller.get($).get(_)||[]}static registerHandler($,_,W){let Y=this.handler.get($);if(!Y.has(_))Y.set(_,[]);Y.get(_).push(W)}static getHandler($,_){return this.handler.get($).get(_)||[]}}class T{static routes=[];static registerRoute($){this.routes.push($)}static getRoutes(){return this.routes}static getRoutesByController($){return this.routes.filter((_)=>_.controller===$)}static getRoutesByMethod($){return this.routes.filter((_)=>_.method.toUpperCase()===$.toUpperCase())}static getRoutesByPath($){return this.routes.filter((_)=>$.test(_.fullPath))}static clear(){this.routes.length=0}}function b($){return(_="",W={})=>{return(Y,q,Q)=>{let J=Y.constructor;X.addRoute(J,{path:_,method:$,handlerName:q,parameterMetadata:[],version:W.version,prefix:W.prefix})}}}function k($,_){return(W)=>{return(Y,q,Q)=>{let J=Y.constructor;if(!X.getParameters(J).size)X.setParameterMap(J,new Map);let A=X.getParameters(J);if(!A.has(q))A.set(q,[]);let G=Reflect.getMetadata("design:paramtypes",Y,q)?.[Q];if(A.get(q).push({index:Q,name:$,data:W,factory:_,metatype:G}),$==="context"){if(!X.getContextIndices(J).size)X.setContextIndices(J,new Map);X.getContextIndices(J).set(q,Q)}}}}class x{static handle(){return async($,_)=>{let{response:W,status:Y}=R($,_);return _.json(W,Y)}}}class I{static handle(){return async($)=>{return $.json({message:`Not Found - ${$.req.path}`},404)}}}var K=($)=>$!==null&&typeof $==="object";class Z{static container;static init($){this.container=$}static setupGlobalComponents($){let _=$.components||{};if(_.middleware)this.registerGlobal("middleware",..._.middleware);if(_.guards)this.registerGlobal("guard",..._.guards);if(_.pipes)this.registerGlobal("pipe",..._.pipes);if(_.filters)this.registerGlobal("filter",..._.filters)}static registerGlobal($,..._){_.forEach((W)=>{X.registerGlobal($,W)})}static registerController($,_,...W){W.forEach((Y)=>{X.registerController($,_,Y)})}static registerHandler($,_,W,...Y){let q=`${_.name}:${String(W)}`;Y.forEach((Q)=>{X.registerHandler($,q,Q)})}static getComponents($,_,W){let Y=`${_.name}:${String(W)}`,q=X.getHandler($,Y),Q=X.getController($,_);return[...Array.from(X.getGlobal($)),...Q,...q]}static resolveMiddleware($){return $.map((_)=>{if(K(_)&&"use"in _)return _.use.bind(_);let W=this.container.resolve(_);return W.use.bind(W)})}static getHandlerMiddleware($,_){let W=this.getComponents("middleware",$,_);return this.resolveMiddleware(W)}static getGlobalMiddleware(){let $=Array.from(X.getGlobal("middleware"));return this.resolveMiddleware($)}static resolveGuards($){return $.map((_)=>{if(K(_)&&"canActivate"in _)return _;return this.container.resolve(_)})}static getHandlerGuards($,_){let W=this.getComponents("guard",$,_);return this.resolveGuards(W)}static resolvePipes($){return $.map((_)=>{if(K(_)&&"transform"in _)return _;return this.container.resolve(_)})}static getHandlerPipes($,_){let W=this.getComponents("pipe",$,_);return this.resolvePipes(W)}static async executePipes($,_,W){let Y=$;for(let q of W)Y=await q.transform(Y,_);return Y}static async handleException($,_){let W=_.get("controllerClass"),Y=_.get("handlerName");if(W&&Y){let A=X.getHandler("filter",`${W.name}:${Y}`);if(A.length>0){let z=await this.executeFilters(A,$,_);if(z)return z}}if(W){let A=X.getController("filter",W);if(A.length>0){let z=await this.executeFilters(A,$,_);if(z)return z}}let q=Array.from(X.getGlobal("filter"));if(q.length>0){let A=await this.executeFilters(q,$,_);if(A)return A}console.log("No filter handled the exception, creating default response");let{response:Q,status:J}=R($,_);return _.json(Q,J)}static async executeFilters($,_,W){for(let Y of $){let q;if(K(Y)&&"catch"in Y)q=Y;else q=this.container.resolve(Y);try{let Q=await q.catch(_,W);if(Q!==void 0)return Q}catch(Q){console.error("Error in exception filter:",Q)}}return}static async registerModule($,_){let W=X.getModuleOptions($);if(!W)throw new Error(`Module ${$.name} is not properly decorated with @Module()`);let Y=[];if(W.imports&&W.imports.length>0)for(let q of W.imports){let Q=await this.registerModule(q,_);Y.push(...Q)}if(W.services&&W.services.length>0)for(let q of W.services)_.resolve(q);if(W.controllers&&W.controllers.length>0)Y.push(...W.controllers);return Y}}import{HTTPException as o}from"hono/http-exception";var C=Symbol("VERSION_NEUTRAL");var K$=($)=>typeof $==="undefined",H=($)=>$===null||typeof $==="undefined",v=($)=>$!==null&&typeof $==="object",f$=($)=>{if(!v($))return!1;let _=Object.getPrototypeOf($);if(_===null)return!0;let W=Object.prototype.hasOwnProperty.call(_,"constructor")&&_.constructor;return typeof W==="function"&&W instanceof W&&Function.prototype.toString.call(W)===Function.prototype.toString.call(Object)},c=($)=>typeof $==="function",y=($)=>typeof $==="string",M$=($)=>typeof $==="number",x$=($)=>$.length===0,I$=($)=>typeof $==="symbol",C$=($)=>typeof $==="string"?$.charAt(0)!=="/"?"/"+$:$:"",u=($)=>$?$.startsWith("/")?("/"+$.replace(/\/+$/,"")).replace(/\/+/g,"/"):"/"+$.replace(/\/+$/,""):"/",v$=($)=>$.endsWith("/")?$.slice(0,-1):$,i=($)=>{return c($)&&!H($.prototype)&&!c($.prototype)&&Object.getOwnPropertyNames($.prototype).length>1};class g{hono;container;globalPrefix;globalVersion;constructor($,_,W={}){this.hono=$,this.container=_,this.globalPrefix=W.prefix!==void 0?this.normalizePath(W.prefix):void 0,this.globalVersion=W.version,this.applyGlobalMiddleware()}applyGlobalMiddleware(){let $=Z.getGlobalMiddleware();for(let _ of $)this.hono.use("*",_)}normalizePath($){if(y($))return u($);return $?`/${$}`:""}registerRouteHandler($,_,W,Y){if(W.length>0)this.hono.on($.toUpperCase(),_,...W,Y);else this.hono.on($.toUpperCase(),_,Y)}buildRoutePath($,_,W,Y){return u(`${$}${_}${W}${Y}`)}formatVersionSegment($){if(H($))return"";return $===C?"":`/v${String($)}`}async registerController($){let _=X.getControllerPath($)||"",W=X.getControllerOptions($)||{},Y=X.getRoutes($)||[],q=X.getParameters($)||new Map,Q=X.getContextIndices($)||new Map,J=this.normalizePath(_),A=this.container.resolve($),z=W.prefix!==void 0?W.prefix:this.globalPrefix,G=W.version!==void 0?W.version:this.globalVersion;for(let U of Y){let{path:O,method:E,handlerName:f,version:S,prefix:N}=U,w=N!==void 0?N:z,V=!H(w)?this.normalizePath(w):"",L=S!==void 0?S:G,F=this.normalizePath(O);if(H(L)){this.registerRoute(A,U,q,Q,$,V,"",J,F,E);continue}if(L===C){this.registerRoute(A,U,q,Q,$,V,"",J,F,E),this.registerRoute(A,U,q,Q,$,V,"/:version{v[0-9]+}",J,F,E);continue}if(Array.isArray(L)){for(let D of L){let j=this.formatVersionSegment(D);this.registerRoute(A,U,q,Q,$,V,j,J,F,E)}continue}let P=this.formatVersionSegment(L);this.registerRoute(A,U,q,Q,$,V,P,J,F,E)}}registerRoute($,_,W,Y,q,Q,J,A,z,G){let{handlerName:U}=_,O=this.buildRoutePath(Q,J,A,z),E=$[U].bind($),f=W.get(U)||[],S=Y.get(U),N=Z.getHandlerMiddleware(q,U),w=Z.getHandlerPipes(q,U);T.registerRoute({controller:q.name,handler:U,method:G,prefix:Q,version:J,route:A,path:z,fullPath:O,parameters:f});let V=async(L)=>{try{L.set("controllerClass",q),L.set("handlerName",String(U));let F=Z.getHandlerGuards(q,U);for(let j of F)if(!await j.canActivate(L))throw new o(403,{message:"Forbidden"});let P=new Array(E.length);for(let j of f){let d=j.factory(j.data,L),a=await Z.executePipes(d,{type:j.type,metatype:j.metatype,data:j.data},w);P[j.index]=a}let D=await E(...P);if(S!==void 0)return D;if(H(D))return L.json(null);if(y(D))return L.text(D);return L.json(D)}catch(F){return Z.handleException(F,L)}};this.registerRouteHandler(G,O,N,V)}}class p{hono;container;routeManager;options;constructor($={}){this.options=v($)?$:{},this.hono=new t(this.options.hono),this.container=this.options.container||new M,this.setupComponents(),this.setupErrorHandlers(),this.routeManager=new g(this.hono,this.container,{prefix:this.options.routing?.prefix,version:this.options.routing?.version})}setupComponents(){Z.init(this.container),Z.setupGlobalComponents(this.options)}setupErrorHandlers(){this.hono.notFound(this.options.notFound||I.handle()),this.hono.onError(this.options.onError||x.handle())}resolvePlugin($){if(i($))return new $;return $}async register($){let _=await Z.registerModule($,this.container);for(let W of _)await this.routeManager.registerController(W);return this}static async create($,_={}){let W=new p(_),Y=(_.plugins||[]).map((q)=>W.resolvePlugin(q));for(let q of Y)if(q.beforeModulesRegistered)await q.beforeModulesRegistered(W,W.hono);await W.register($);for(let q of Y)if(q.afterModulesRegistered)await q.afterModulesRegistered(W,W.hono);return{app:W,hono:W.getApp()}}getApp(){return this.hono}getRoutes(){return T.getRoutes()}}import{html as B,raw as h}from"hono/html";var e={type:"website",locale:"en_US"},m=($)=>{if(!$)return"";return Object.entries($).map(([_,W])=>{if(typeof W==="boolean")return W?_:"";let Y=String(W).replace(/"/g,""");return`${_}="${Y}"`}).filter(Boolean).join(" ")},Y_=($)=>{let _={...e,...$};return B`
|
|
1
|
+
import"reflect-metadata";import{Hono as o}from"hono";class M{instances=new Map;resolve($,_=new Set){if(this.instances.has($))return this.instances.get($);if(_.has($))throw Error(`Circular dependency detected: ${[..._.keys(),$].map((Q)=>Q.name).join(" -> ")}`);_.add($);let Y=(Reflect.getMetadata("design:paramtypes",$)||[]).map((Q)=>{return this.resolve(Q,new Set(_))}),q=new $(...Y);return this.instances.set($,q),q}register($,_){this.instances.set($,_)}}import{HTTPException as a}from"hono/http-exception";function K($,_,W){let Y=new Date().toISOString(),q=_.get("requestId"),Q=_.req.path;if($ instanceof a)return{response:{status:W?.status||$.status,message:W?.title||$.message,timestamp:Y,path:Q,requestId:q,code:W?.code,details:W?.additionalDetails,...W?.detail&&{detail:W.detail}},status:W?.status||$.status};if($.statusCode||$.status){let z=$.statusCode||$.status,G=W?.status||z;return{response:{status:G,message:W?.title||$.message,timestamp:Y,path:Q,requestId:q,code:W?.code||$.name,details:W?.additionalDetails,...W?.detail&&{detail:W.detail}},status:G}}let J=W?.status||500;return{response:{status:J,message:W?.title||$.message,timestamp:Y,path:Q,requestId:q,code:W?.code||$.name,details:W?.additionalDetails||{stack:$.stack},...W?.detail&&{detail:W.detail}},status:J}}class X{static routes=new Map;static controllers=new Map;static controllerOptions=new Map;static services=new Set;static modules=new Map;static parameters=new Map;static contextIndices=new Map;static global=new Map([["middleware",new Set],["guard",new Set],["pipe",new Set],["filter",new Set]]);static controller=new Map([["middleware",new Map],["guard",new Map],["pipe",new Map],["filter",new Map]]);static handler=new Map([["middleware",new Map],["guard",new Map],["pipe",new Map],["filter",new Map]]);static getRoutes($){return this.routes.get($)||[]}static setRoutes($,_){this.routes.set($,_)}static addRoute($,_){if(!this.routes.has($))this.routes.set($,[]);this.routes.get($).push(_)}static getControllerPath($){return this.controllers.get($)||""}static setControllerPath($,_){this.controllers.set($,_)}static getControllerOptions($){return this.controllerOptions.get($)||{}}static setControllerOptions($,_){this.controllerOptions.set($,_)}static isService($){return this.services.has($)}static addService($){this.services.add($)}static getAllServices(){return this.services}static getModuleOptions($){return this.modules.get($)}static setModuleOptions($,_){this.modules.set($,_)}static getParameters($){return this.parameters.get($)||new Map}static setParameterMap($,_){this.parameters.set($,_)}static getContextIndices($){return this.contextIndices.get($)||new Map}static setContextIndices($,_){this.contextIndices.set($,_)}static registerGlobal($,_){this.global.get($).add(_)}static getGlobal($){return this.global.get($)}static registerController($,_,W){let Y=this.controller.get($);if(!Y.has(_))Y.set(_,[]);Y.get(_).push(W)}static getController($,_){return this.controller.get($).get(_)||[]}static registerHandler($,_,W){let Y=this.handler.get($);if(!Y.has(_))Y.set(_,[]);Y.get(_).push(W)}static getHandler($,_){return this.handler.get($).get(_)||[]}static clear(){this.routes.clear(),this.controllers.clear(),this.controllerOptions.clear(),this.services.clear(),this.modules.clear(),this.parameters.clear(),this.contextIndices.clear();for(let $ of this.global.values())$.clear();for(let $ of this.controller.values())$.clear();for(let $ of this.handler.values())$.clear()}}class S{static routes=[];static registerRoute($){if(!$)throw Error("Route info is required");if(!$.controller)throw Error("Route controller is required");if(!$.handler)throw Error("Route handler is required");if(!$.method)throw Error("Route method is required");if(!$.fullPath)throw Error("Route fullPath is required");this.routes.push($)}static getRoutes(){return this.routes}static getRoutesByController($){return this.routes.filter((_)=>_.controller===$)}static getRoutesByMethod($){return this.routes.filter((_)=>_.method.toUpperCase()===$.toUpperCase())}static getRoutesByPath($){return this.routes.filter((_)=>$.test(_.fullPath))}static clear(){this.routes.length=0}}function b($){return(_="",W={})=>{return(Y,q,Q)=>{let J=Y.constructor;X.addRoute(J,{path:_,method:$,handlerName:q,parameterMetadata:[],version:W.version,prefix:W.prefix})}}}function k($,_){return(W)=>{return(Y,q,Q)=>{let J=Y.constructor;if(!X.getParameters(J).size)X.setParameterMap(J,new Map);let A=X.getParameters(J);if(!A.has(q))A.set(q,[]);let G=Reflect.getMetadata("design:paramtypes",Y,q)?.[Q];if(A.get(q).push({index:Q,name:$,data:W,factory:_,metatype:G}),$==="context"){if(!X.getContextIndices(J).size)X.setContextIndices(J,new Map);X.getContextIndices(J).set(q,Q)}}}}class x{static handle(){return async($,_)=>{let{response:W,status:Y}=K($,_);return _.json(W,Y)}}}class C{static handle(){return async($)=>{return $.json({message:`Not Found - ${$.req.path}`},404)}}}var O$=($)=>typeof $>"u",O=($)=>$===null||typeof $>"u",V=($)=>$!==null&&typeof $==="object",T$=($)=>{if(!V($))return!1;let _=Object.getPrototypeOf($);if(_===null)return!0;let W=Object.prototype.hasOwnProperty.call(_,"constructor")&&_.constructor;return typeof W==="function"&&W instanceof W&&Function.prototype.toString.call(W)===Function.prototype.toString.call(Object)},d=($)=>typeof $==="function",I=($)=>typeof $==="string",S$=($)=>typeof $==="number",N$=($)=>$.length===0,w$=($)=>typeof $==="symbol",P$=($)=>typeof $==="string"?$.charAt(0)!=="/"?"/"+$:$:"",v=($)=>$?$.startsWith("/")?("/"+$.replace(/\/+$/,"")).replace(/\/+/g,"/"):"/"+$.replace(/\/+$/,""):"/",R$=($)=>$.endsWith("/")?$.slice(0,-1):$,c=($)=>{return d($)&&!O($.prototype)&&!d($.prototype)&&Object.getOwnPropertyNames($.prototype).length>1};class Z{static container;static init($){this.container=$}static setupGlobalComponents($){let _=$.components||{};if(_.middleware)this.registerGlobal("middleware",..._.middleware);if(_.guards)this.registerGlobal("guard",..._.guards);if(_.pipes)this.registerGlobal("pipe",..._.pipes);if(_.filters)this.registerGlobal("filter",..._.filters)}static registerGlobal($,..._){_.forEach((W)=>{X.registerGlobal($,W)})}static registerController($,_,...W){W.forEach((Y)=>{X.registerController($,_,Y)})}static registerHandler($,_,W,...Y){let q=`${_.name}:${String(W)}`;Y.forEach((Q)=>{X.registerHandler($,q,Q)})}static getComponents($,_,W){let Y=`${_.name}:${String(W)}`,q=X.getHandler($,Y),Q=X.getController($,_);return[...Array.from(X.getGlobal($)),...Q,...q]}static resolveMiddleware($){return $.map((_)=>{if(V(_)&&"use"in _)return _.use.bind(_);let W=this.container.resolve(_);return W.use.bind(W)})}static getHandlerMiddleware($,_){let W=this.getComponents("middleware",$,_);return this.resolveMiddleware(W)}static getGlobalMiddleware(){let $=Array.from(X.getGlobal("middleware"));return this.resolveMiddleware($)}static resolveGuards($){return $.map((_)=>{if(V(_)&&"canActivate"in _)return _;return this.container.resolve(_)})}static getHandlerGuards($,_){let W=this.getComponents("guard",$,_);return this.resolveGuards(W)}static resolvePipes($){return $.map((_)=>{if(V(_)&&"transform"in _)return _;return this.container.resolve(_)})}static getHandlerPipes($,_){let W=this.getComponents("pipe",$,_);return this.resolvePipes(W)}static async executePipes($,_,W){let Y=$;for(let q of W)Y=await q.transform(Y,_);return Y}static async handleException($,_){let W=_.get("controllerClass"),Y=_.get("handlerName");if(W&&Y){let A=X.getHandler("filter",`${W.name}:${Y}`);if(A.length>0){let z=await this.executeFilters(A,$,_);if(z)return z}}if(W){let A=X.getController("filter",W);if(A.length>0){let z=await this.executeFilters(A,$,_);if(z)return z}}let q=Array.from(X.getGlobal("filter"));if(q.length>0){let A=await this.executeFilters(q,$,_);if(A)return A}let{response:Q,status:J}=K($,_);return _.json(Q,J)}static async executeFilters($,_,W){for(let Y of $){let q;if(V(Y)&&"catch"in Y)q=Y;else q=this.container.resolve(Y);try{let Q=await q.catch(_,W);if(Q!==void 0)return Q}catch(Q){console.error("Error in exception filter:",Q)}}return}static async registerModule($,_){let W=X.getModuleOptions($);if(!W)throw Error(`Module ${$.name} is not properly decorated with @Module()`);let Y=[];if(W.imports&&W.imports.length>0)for(let q of W.imports){let Q=await this.registerModule(q,_);Y.push(...Q)}if(W.services&&W.services.length>0)for(let q of W.services)_.resolve(q);if(W.controllers&&W.controllers.length>0)Y.push(...W.controllers);return Y}}import{HTTPException as r}from"hono/http-exception";var y=Symbol("VERSION_NEUTRAL");class h{hono;container;globalPrefix;globalVersion;constructor($,_,W={}){this.hono=$,this.container=_,this.globalPrefix=W.prefix!==void 0?this.normalizePath(W.prefix):void 0,this.globalVersion=W.version,this.applyGlobalMiddleware()}applyGlobalMiddleware(){let $=Z.getGlobalMiddleware();for(let _ of $)this.hono.use("*",_)}normalizePath($){if(I($))return v($);return $?`/${$}`:""}registerRouteHandler($,_,W,Y){if(W.length>0)this.hono.on($.toUpperCase(),_,...W,Y);else this.hono.on($.toUpperCase(),_,Y)}buildRoutePath($,_,W,Y){return v(`${$}${_}${W}${Y}`)}formatVersionSegment($){if(O($))return"";return $===y?"":`/v${String($)}`}async registerController($){let _=X.getControllerPath($)||"",W=X.getControllerOptions($)||{},Y=X.getRoutes($)||[],q=X.getParameters($)||new Map,Q=X.getContextIndices($)||new Map,J=this.normalizePath(_),A=this.container.resolve($),z=W.prefix!==void 0?W.prefix:this.globalPrefix,G=W.version!==void 0?W.version:this.globalVersion;for(let U of Y){let{path:T,method:E,handlerName:f,version:N,prefix:w}=U,P=w!==void 0?w:z,D=!O(P)?this.normalizePath(P):"",L=N!==void 0?N:G,F=this.normalizePath(T);if(O(L)){this.registerRoute(A,U,q,Q,$,D,"",J,F,E);continue}if(L===y){this.registerRoute(A,U,q,Q,$,D,"",J,F,E),this.registerRoute(A,U,q,Q,$,D,"/:version{v[0-9]+}",J,F,E);continue}if(Array.isArray(L)){for(let H of L){let j=this.formatVersionSegment(H);this.registerRoute(A,U,q,Q,$,D,j,J,F,E)}continue}let R=this.formatVersionSegment(L);this.registerRoute(A,U,q,Q,$,D,R,J,F,E)}}registerRoute($,_,W,Y,q,Q,J,A,z,G){let{handlerName:U}=_,T=this.buildRoutePath(Q,J,A,z),E=$[U].bind($),f=W.get(U)||[],N=Y.get(U),w=Z.getHandlerMiddleware(q,U),P=Z.getHandlerPipes(q,U);S.registerRoute({controller:q.name,handler:U,method:G,prefix:Q,version:J,route:A,path:z,fullPath:T,parameters:f});let D=async(L)=>{try{L.set("controllerClass",q),L.set("handlerName",String(U));let F=Z.getHandlerGuards(q,U);for(let j of F)if(!await j.canActivate(L))throw new r(403,{message:"Forbidden"});let R=Array(E.length);for(let j of f){let m=j.factory(j.data,L),n=await Z.executePipes(m,{type:j.name,metatype:j.metatype,data:j.data},P);R[j.index]=n}let H=await E(...R);if(N!==void 0)return H;if(O(H))return L.json(null);if(I(H))return L.text(H);return L.json(H)}catch(F){return Z.handleException(F,L)}};this.registerRouteHandler(G,T,w,D)}}class i{hono;container;routeManager;options;constructor($={}){this.options=V($)?$:{},this.hono=new o(this.options.hono),this.container=this.options.container||new M,this.setupComponents(),this.setupErrorHandlers(),this.routeManager=new h(this.hono,this.container,{prefix:this.options.routing?.prefix,version:this.options.routing?.version})}setupComponents(){Z.init(this.container),Z.setupGlobalComponents(this.options)}setupErrorHandlers(){this.hono.notFound(this.options.notFound||C.handle()),this.hono.onError(this.options.onError||x.handle())}resolvePlugin($){if(c($))return new $;return $}async register($){let _=await Z.registerModule($,this.container);for(let W of _)await this.routeManager.registerController(W);return this}static async create($,_={}){let W=new i(_),Y=(_.plugins||[]).map((q)=>W.resolvePlugin(q));for(let q of Y)if(q.beforeModulesRegistered)await q.beforeModulesRegistered(W,W.hono);await W.register($);for(let q of Y)if(q.afterModulesRegistered)await q.afterModulesRegistered(W,W.hono);return{app:W,hono:W.getApp()}}getApp(){return this.hono}getRoutes(){return S.getRoutes()}}import{html as B,raw as g}from"hono/html";var t={type:"website",locale:"en_US"},u=($)=>{if(!$)return"";return Object.entries($).map(([_,W])=>{if(typeof W==="boolean")return W?_:"";let Y=String(W).replace(/"/g,""");return`${_}="${Y}"`}).filter(Boolean).join(" ")},Y_=($)=>{let _={...t,...$};return B`
|
|
2
2
|
<!DOCTYPE html>
|
|
3
|
-
<html lang="${_.locale?.split("_")[0]||"en"}" ${
|
|
4
|
-
<head ${
|
|
3
|
+
<html lang="${_.locale?.split("_")[0]||"en"}" ${g(u(_.htmlAttributes))}>
|
|
4
|
+
<head ${g(u(_.headAttributes))}>
|
|
5
5
|
<meta charset="UTF-8" />
|
|
6
6
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
7
|
${_.csp?B`<meta http-equiv="Content-Security-Policy" content="${_.csp}" />`:""}
|
|
@@ -39,8 +39,8 @@ import"reflect-metadata";import{Hono as t}from"hono";class M{instances=new Map;r
|
|
|
39
39
|
<!-- Scripts -->
|
|
40
40
|
${_.scripts?_.scripts.map((W)=>{if(typeof W==="string")return B`<script src="${W}"></script>`;let{src:Y,async:q,defer:Q}=W;if(q&&Q)return B`<script src="${Y}" async defer></script>`;if(q)return B`<script src="${Y}" async></script>`;if(Q)return B`<script src="${Y}" defer></script>`;return B`<script src="${Y}"></script>`}):""}
|
|
41
41
|
</head>
|
|
42
|
-
<body ${
|
|
42
|
+
<body ${g(u(_.bodyAttributes))}>
|
|
43
43
|
${_.children}
|
|
44
44
|
</body>
|
|
45
45
|
</html>
|
|
46
|
-
`};function
|
|
46
|
+
`};function p($="",_={}){return(W)=>{X.setControllerPath(W,$),X.setControllerOptions(W,_)}}var s=b("get"),B_=b("post"),U_=b("put"),z_=b("delete"),L_=b("patch"),k_=b("options"),j_=b("all");function l($={}){return(_)=>{X.setModuleOptions(_,$)}}function H_($="",_={prefix:null,version:null}){return p($,_)}var O_=s;function T_($={}){return l({imports:$.imports,services:$.services,controllers:($.views||[]).concat($.controllers||[])})}var w_=k("body",async($,_)=>{let W=await _.req.json();return $?W[$]:W}),P_=k("param",($,_)=>{return $?_.req.param($):_.req.param()}),R_=k("query",($,_)=>{return $?_.req.query($):_.req.query()}),K_=k("header",($,_)=>{return $?_.req.header($):_.req.header()}),f_=k("request",($,_)=>_.req),M_=k("request",($,_)=>_.req),x_=k("response",($,_)=>_.res),C_=k("response",($,_)=>_.res),I_=k("context",($,_)=>_),v_=k("context",($,_)=>_),y_=k("variable",($,_)=>_.get($)),h_=k("variable",($,_)=>_.get($));function m_(){return($)=>{X.addService($)}}function i_($,..._){return(W,Y)=>{if(Y){let q=W.constructor;Z.registerHandler($,q,Y,..._)}else Z.registerController($,W,..._)}}function l_(...$){return(_,W)=>{if(W){let Y=_.constructor;Z.registerHandler("filter",Y,W,...$)}else Z.registerController("filter",_,...$)}}function r_(...$){return(_,W)=>{if(W){let Y=_.constructor;Z.registerHandler("guard",Y,W,...$)}else Z.registerController("guard",_,...$)}}function e_(...$){return(_,W)=>{if(W){let Y=_.constructor;Z.registerHandler("middleware",Y,W,...$)}else Z.registerController("middleware",_,...$)}}function WW(...$){return(_,W)=>{if(W){let Y=_.constructor;Z.registerHandler("pipe",Y,W,...$)}else Z.registerController("pipe",_,...$)}}export{R$ as stripEndSlash,v as normalizePath,O$ as isUndefined,w$ as isSymbol,I as isString,T$ as isPlainObject,V as isObject,S$ as isNumber,O as isNil,d as isFunction,N$ as isEmpty,c as isConstructor,k as createParamDecorator,b as createHttpMethodDecorator,K as createErrorResponse,P$ as addLeadingSlash,H_ as View,h_ as Variable,y_ as Var,y as VERSION_NEUTRAL,WW as UsePipes,e_ as UseMiddleware,r_ as UseGuards,l_ as UseFilters,i_ as UseComponent,m_ as Service,S as RouteRegistry,h as RouteManager,C_ as Response,x_ as Res,M_ as Request,f_ as Req,R_ as Query,U_ as Put,B_ as Post,L_ as Patch,P_ as Param,O_ as Page,k_ as Options,C as NotFoundHandler,T_ as MvcModule,l as Module,X as MetadataRegistry,Y_ as Layout,K_ as Header,s as Get,x as ErrorHandler,z_ as Delete,I_ as Ctx,p as Controller,v_ as Context,M as Container,Z as ComponentManager,w_ as Body,i as Application,j_ as All};
|
|
@@ -4,9 +4,9 @@ import type { Constructor } from '../types';
|
|
|
4
4
|
*/
|
|
5
5
|
export interface ArgumentMetadata {
|
|
6
6
|
/**
|
|
7
|
-
* The type of argument (body, query, param, or custom)
|
|
7
|
+
* The type of argument (body, query, param, header, request, response, context, variable, or custom)
|
|
8
8
|
*/
|
|
9
|
-
type: 'body' | 'query' | 'param' | '
|
|
9
|
+
type: 'body' | 'query' | 'param' | 'header' | 'request' | 'response' | 'context' | 'variable' | string;
|
|
10
10
|
/**
|
|
11
11
|
* The class type of the argument
|
|
12
12
|
*/
|
|
@@ -60,51 +60,51 @@ export declare class ComponentManager {
|
|
|
60
60
|
* @param type - The type of component to get
|
|
61
61
|
* @param controller - The controller class
|
|
62
62
|
* @param handlerName - The handler method name
|
|
63
|
-
*
|
|
63
|
+
* @returns An array of component instances
|
|
64
64
|
*/
|
|
65
65
|
static getComponents<T extends ComponentType>(type: T, controller: Constructor, handlerName: string | symbol): ComponentTypeMap[T][];
|
|
66
66
|
/**
|
|
67
67
|
* Resolves middleware classes or instances to middleware functions
|
|
68
68
|
* @param middlewareItems - The middleware classes or instances to resolve
|
|
69
|
-
*
|
|
69
|
+
* @returns An array of middleware functions
|
|
70
70
|
*/
|
|
71
71
|
static resolveMiddleware(middlewareItems: MiddlewareType[]): ((c: Context, next: Next) => Promise<Response | void>)[];
|
|
72
72
|
/**
|
|
73
73
|
* Gets middleware for a specific handler
|
|
74
74
|
* @param controller - The controller class
|
|
75
75
|
* @param handlerName - The handler method name
|
|
76
|
-
*
|
|
76
|
+
* @returns An array of middleware functions
|
|
77
77
|
*/
|
|
78
78
|
static getHandlerMiddleware(controller: Constructor, handlerName: string | symbol): ((c: Context, next: Next) => Promise<Response | void>)[];
|
|
79
79
|
/**
|
|
80
80
|
* Gets global middleware
|
|
81
|
-
*
|
|
81
|
+
* @returns An array of middleware functions
|
|
82
82
|
*/
|
|
83
83
|
static getGlobalMiddleware(): ((c: Context, next: Next) => Promise<Response | void>)[];
|
|
84
84
|
/**
|
|
85
85
|
* Resolves guard classes or instances to guard instances
|
|
86
86
|
* @param guardItems - The guard classes or instances to resolve
|
|
87
|
-
*
|
|
87
|
+
* @returns An array of guard instances
|
|
88
88
|
*/
|
|
89
89
|
static resolveGuards(guardItems: GuardType[]): IGuard[];
|
|
90
90
|
/**
|
|
91
91
|
* Gets guards for a specific handler
|
|
92
92
|
* @param controller - The controller class
|
|
93
93
|
* @param handlerName - The handler method name
|
|
94
|
-
*
|
|
94
|
+
* @returns An array of guard instances
|
|
95
95
|
*/
|
|
96
96
|
static getHandlerGuards(controller: Constructor, handlerName: string | symbol): IGuard[];
|
|
97
97
|
/**
|
|
98
98
|
* Resolves pipe classes or instances to pipe instances
|
|
99
99
|
* @param pipeItems - The pipe classes or instances to resolve
|
|
100
|
-
*
|
|
100
|
+
* @returns An array of pipe instances
|
|
101
101
|
*/
|
|
102
102
|
static resolvePipes(pipeItems: PipeType[]): IPipe[];
|
|
103
103
|
/**
|
|
104
104
|
* Gets pipes for a specific handler
|
|
105
105
|
* @param controller - The controller class
|
|
106
106
|
* @param handlerName - The handler method name
|
|
107
|
-
*
|
|
107
|
+
* @returns An array of pipe instances
|
|
108
108
|
*/
|
|
109
109
|
static getHandlerPipes(controller: Constructor, handlerName: string | symbol): IPipe[];
|
|
110
110
|
/**
|
|
@@ -130,7 +130,7 @@ export declare class ComponentManager {
|
|
|
130
130
|
* @param filterItems - The exception filter classes or instances to execute
|
|
131
131
|
* @param exception - The exception that was thrown
|
|
132
132
|
* @param context - The Hono context object
|
|
133
|
-
*
|
|
133
|
+
* @returns The response from the first filter that handles the exception or undefined if no filter handled it
|
|
134
134
|
*/
|
|
135
135
|
private static executeFilters;
|
|
136
136
|
/**
|
|
@@ -169,4 +169,10 @@ export declare class MetadataRegistry {
|
|
|
169
169
|
* Get all handler-level components of a specific type for a handler
|
|
170
170
|
*/
|
|
171
171
|
static getHandler<T extends ComponentType>(type: T, handlerKey: string): ComponentTypeMap[T][];
|
|
172
|
+
/**
|
|
173
|
+
* Clears all registered metadata from the registry
|
|
174
|
+
* Primarily used for testing and development purposes
|
|
175
|
+
* Use with caution in production environments
|
|
176
|
+
*/
|
|
177
|
+
static clear(): void;
|
|
172
178
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "honestjs",
|
|
3
3
|
"description": "HonestJS - a modern web framework built on top of Hono",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.6",
|
|
5
5
|
"author": "Orkhan Karimov <karimovok1@gmail.com> (https://github.com/kerimovok)",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -9,16 +9,18 @@
|
|
|
9
9
|
},
|
|
10
10
|
"main": "dist/index.js",
|
|
11
11
|
"module": "dist/index.js",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
12
14
|
"devDependencies": {
|
|
13
|
-
"@eslint/js": "^9.
|
|
14
|
-
"@types/bun": "^1.
|
|
15
|
-
"eslint": "^9.
|
|
15
|
+
"@eslint/js": "^9.39.2",
|
|
16
|
+
"@types/bun": "^1.3.5",
|
|
17
|
+
"eslint": "^9.39.2",
|
|
16
18
|
"eslint-config-prettier": "^10.1.8",
|
|
17
|
-
"globals": "^16.
|
|
19
|
+
"globals": "^16.5.0",
|
|
18
20
|
"husky": "^9.1.7",
|
|
19
|
-
"lint-staged": "^16.
|
|
21
|
+
"lint-staged": "^16.2.7",
|
|
20
22
|
"prettier": "3.6.2",
|
|
21
|
-
"typescript-eslint": "^8.
|
|
23
|
+
"typescript-eslint": "^8.52.0"
|
|
22
24
|
},
|
|
23
25
|
"peerDependencies": {
|
|
24
26
|
"hono": "^4.8.5",
|
|
@@ -61,7 +63,5 @@
|
|
|
61
63
|
"eslint --fix",
|
|
62
64
|
"prettier --write"
|
|
63
65
|
]
|
|
64
|
-
}
|
|
65
|
-
"type": "module",
|
|
66
|
-
"types": "dist/index.d.ts"
|
|
66
|
+
}
|
|
67
67
|
}
|