@andrewcaires/express 1.0.10 → 1.1.0

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.cjs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/express v1.0.10
2
+ * @andrewcaires/express v1.1.0
3
3
  * Decorators for express
4
- * (c) 2023 Andrew Caires
4
+ * (c) 2024 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- "use strict";var e=require("@andrewcaires/utils.js"),t=require("cookie-parser"),s=require("cors"),r=require("express"),o=require("express-session"),i=require("fs"),n=require("http"),p=require("https"),a=require("net"),E=require("path");function _(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var h=_(n),u=_(p);function d(e,t,s,r){return new(s||(s=Promise))((function(o,i){function n(e){try{a(r.next(e))}catch(e){i(e)}}function p(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(n,p)}a((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const c=e.cache(),T=(t,s,r)=>{e.isObject(s)&&(s=s.constructor);const o=c(s);return o[t]||(o[t]=r),o[t]};class O{}O.CONTINUE=100,O.SWITCHING_PROTOCOLS=101,O.PROCESSING=102,O.OK=200,O.CREATED=201,O.ACCEPTED=202,O.NON_AUTHORITATIVE_INFORMATION=203,O.NO_CONTENT=204,O.RESET_CONTENT=205,O.PARTIAL_CONTENT=206,O.MULTI_STATUS=207,O.ALREADY_REPORTED=208,O.IM_USED=226,O.MULTIPLE_CHOICES=300,O.MOVED_PERMANENTLY=301,O.FOUND=302,O.SEE_OTHER=303,O.NOT_MODIFIED=304,O.USE_PROXY=305,O.TEMPORARY_REDIRECT=307,O.PERMANENT_REDIRECT=308,O.BAD_REQUEST=400,O.UNAUTHORIZED=401,O.PAYMENT_REQUIRED=402,O.FORBIDDEN=403,O.NOT_FOUND=404,O.METHOD_NOT_ALLOWED=405,O.NOT_ACCEPTABLE=406,O.PROXY_AUTHENTICATION_REQUIRED=407,O.REQUEST_TIMEOUT=408,O.CONFLICT=409,O.GONE=410,O.LENGTH_REQUIRED=411,O.PRECONDITION_FAILED=412,O.PAYLOAD_TOO_LARGE=413,O.REQUEST_URI_TOO_LONG=414,O.UNSUPPORTED_MEDIA_TYPE=415,O.REQUESTED_RANGE_NOT_SATISFIABLE=416,O.EXPECTATION_FAILED=417,O.IM_A_TEAPOT=418,O.MISDIRECTED_REQUEST=421,O.UNPROCESSABLE_ENTITY=422,O.LOCKED=423,O.FAILED_DEPENDENCY=424,O.UPGRADE_REQUIRED=426,O.PRECONDITION_REQUIRED=428,O.TOO_MANY_REQUESTS=429,O.REQUEST_HEADER_FIELDS_TOO_LARGE=431,O.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,O.UNAVAILABLE_FOR_LEGAL_REASONS=451,O.CLIENT_CLOSED_REQUEST=499,O.INTERNAL_SERVER_ERROR=500,O.NOT_IMPLEMENTED=501,O.BAD_GATEWAY=502,O.SERVICE_UNAVAILABLE=503,O.GATEWAY_TIMEOUT=504,O.HTTP_VERSION_NOT_SUPPORTED=505,O.VARIANT_ALSO_NEGOCIATES=506,O.INSUFFICIENT_STORAGE=507,O.LOOP_DETECTED=508,O.NOT_EXTENDED=510,O.NETWORK_AUTHENTICATION_REQUIRED=511,O.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class R{static success(e,t){return e.status(O.OK).json({message:t})}static data(e,t){return e.status(O.OK).json(Object.assign({},t))}static error(e,t){return e.status(O.INTERNAL_SERVER_ERROR).json({message:t})}static list(e,t){return e.status(O.OK).json([...t])}static notfound(e,t){return e.status(O.NOT_FOUND).json({message:t})}static validation(e,t){return e.status(O.BAD_REQUEST).json({message:t})}static unauthorized(e,t){return e.status(O.UNAUTHORIZED).json({message:t})}}var x,l;exports.MetadataKeys=void 0,(x=exports.MetadataKeys||(exports.MetadataKeys={})).EXTENDS="__extends__",x.MIDDLEWARE="__middleware__",x.PARAMETERS="__parameters__",x.PATH="__path__",x.ROUTERS="__routers__",exports.Methods=void 0,(l=exports.Methods||(exports.Methods={})).ALL="all",l.GET="get",l.POST="post",l.PUT="put",l.HEAD="head",l.DELETE="delete",l.OPTIONS="options",l.PATCH="patch";class N extends e.EventEmitter{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(n,p){var a;super(),this._app=r(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=r.Router(),this._controllers=r.Router(),this._options=e.merge(Object.assign({},this._default),Object.assign({},p)),this._app.use(r.json(this._options.json)),this._app.disable("x-powered-by"),this._app.use(r.urlencoded({extended:!0})),this._options.cross&&this._app.use(s(e.isObject(this._options.cross)?this._options.cross:{}));const _=this._options.cookie;this._app.use(t(null==_?void 0:_.secret,null==_?void 0:_.options)),this._app.use(o(this._options.session)),this.init(),this._app.use(this._router);const h=(null===(a=this._options.path)||void 0===a?void 0:a.length)?this._options.path:"/";if(this._app.use(h,this._controllers),this._app.all(h+"/*",((e,t)=>{this._options.redirect?t.redirect(this._options.redirect):R.notfound(t,"Invalid API")})),this._options.public){const e=E.resolve(this._options.public);i.existsSync(e)&&this._app.use(r.static(e)),this._app.get("*",((t,s)=>{i.existsSync(e)?s.sendFile(e+"/index.html"):this._options.redirect?s.redirect(this._options.redirect):R.notfound(s,'File "index.html" not found')}))}n.forEach((t=>{const s=r.Router(),o=this._middlewares(t),i=this._parameters(t);this._extends(t).forEach((e=>{o.push(...this._middlewares(e)),i.push(...this._parameters(e))})),this._routers(t).forEach((r=>{const n=r.propertyKey.toString();if(e.isFunction(t[n])){if(!e.isFunction(s[r.method]))return this.error(new Error('Method "'+r.method+'" Not Found'),"express");const p=this._handles(t,n,o,i);return s[r.method](r.path,...p,((e,s,r)=>t[n](...this._args(e,s,r,this._indexes(n,i)))))}return this.error(new Error('Router "'+t.name+"."+n+'" Not Found'),"express")})),this._extends(t).forEach((r=>{this._routers(r).forEach((r=>{const n=r.propertyKey.toString();if(e.isFunction(t[n])){if(!e.isFunction(s[r.method]))return this.error(new Error('Method "'+r.method+'" Not Found'),"express");const p=this._handles(t,n,o,i);return s[r.method](r.path,...p,((e,s,r)=>t[n](...this._args(e,s,r,this._indexes(n,i)))))}return this.error(new Error('Router "'+t.constructor.name+"."+n+'" Not Found'),"express")}))})),this._controllers.use(this._path(t),s)}))}init(){}error(t,s){e.isUndefined(s)?console.error(t):console.error(s+":",t)}log(t,s){e.isUndefined(s)?console.log(t):console.log(s+":",t)}_args(e,t,s,r,o){const i=[],n={req:e,res:t,next:s,key:"",app:this,argument:o};return r.forEach((e=>{i[e.parameterIndex]=e.callback(Object.assign(Object.assign({},n),{key:e.key}))})),i}_handles(t,s,r,o){return r.filter((e=>e.propertyKey==s)).map((s=>{if(e.isFunction(t[s.method])){const e=this._indexes(s.method,o);return(r,o,i)=>t[s.method](...this._args(r,o,i,e,s.argument))}this.error(new Error('Middleware "'+s.method+'" Not Found'),"express")})).filter(e.isFunction)}_extends(e){return T(exports.MetadataKeys.EXTENDS,e,[]).reduce(((e,t)=>(e.push(t,...this._extends(t)),e)),[]).filter(((e,t,s)=>s.indexOf(e)===t))}_indexes(e,t){return t.filter((t=>t.propertyKey==e))}_middlewares(e){return[...T(exports.MetadataKeys.MIDDLEWARE,e,[])].reverse()}_parameters(e){return T(exports.MetadataKeys.PARAMETERS,e,[])}_path(e){return T(exports.MetadataKeys.PATH,e,"/")}_routers(e){return T(exports.MetadataKeys.ROUTERS,e,[])}listen(){return d(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=h.createServer(this._app),this._server.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express")))),!this._server){const e=h.createServer(((e,t)=>{const s=80==this._options.port?":80":"";t.writeHead(301,{Location:"https://"+e.headers.host+s+e.url}),t.end()})),t=u.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new a.Server((s=>{s.once("data",(r=>{s.pause(),s.unshift(r),(22===r[0]?t:e).emit("connection",s),process.nextTick((()=>s.resume()))}))})).listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=t}this._server||(443==this._options.port&&e.listen(80,(()=>this.log("Server started on port 80","express"))),t.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=t)}}))}}const A=e=>t=>(s,r)=>{T(exports.MetadataKeys.ROUTERS,s,[]).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:r})},I=A(exports.Methods.ALL),S=A(exports.Methods.GET),D=A(exports.Methods.POST),y=A(exports.Methods.PUT),M=A(exports.Methods.DELETE),U=A(exports.Methods.PATCH),P=A(exports.Methods.HEAD),f=A(exports.Methods.OPTIONS),m=e=>t=>(s,r,o)=>{T(exports.MetadataKeys.PARAMETERS,s,[]).push({callback:e,key:t,propertyKey:r,parameterIndex:o})},L=(t,s)=>e.isUndefined(s)?t:t[s],v=m((({app:e})=>e)),C=m((({argument:e})=>e)),g=m((({req:e,key:t})=>L(e.body,t))),F=m((({req:e,key:t})=>L(e.cookies,t))),K=m((({req:e,key:t})=>L(e.headers,t))),H=m((({next:e})=>e)),b=m((({req:e,key:t})=>L(e.params,t))),j=m((({req:e,key:t})=>L(e.query,t))),k=m((({req:e,key:t})=>L(e,t))),q=k,w=m((({res:e})=>e)),G=w,Q=m((({req:e})=>e.session));exports.All=I,exports.App=v,exports.Application=N,exports.Argument=C,exports.Body=g,exports.Controller=e=>t=>{T(exports.MetadataKeys.PATH,t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=F,exports.Delete=M,exports.Extends=e=>t=>{T(exports.MetadataKeys.EXTENDS,t,[]).push(e)},exports.Get=S,exports.Head=P,exports.Headers=K,exports.Metadata=T,exports.Middleware=(e,t)=>(s,r)=>{T(exports.MetadataKeys.MIDDLEWARE,s,[]).push({method:e,argument:t,propertyKey:r})},exports.Next=H,exports.Options=f,exports.Params=b,exports.Patch=U,exports.Post=D,exports.Put=y,exports.Query=j,exports.Req=q,exports.Request=k,exports.Res=G,exports.Response=w,exports.Responses=R,exports.Session=Q,exports.Status=O,exports.methodFactory=A,exports.parameterFactory=m;
7
+ "use strict";var t=require("@andrewcaires/utils.js"),e=require("cookie-parser"),s=require("cors"),r=require("express"),o=require("express-session"),i=require("fs"),n=require("http"),a=require("https"),p=require("net"),E=require("path");function h(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,r.get?r:{enumerable:!0,get:function(){return t[s]}})}})),e.default=t,Object.freeze(e)}var u=h(n),_=h(a);function d(t,e,s,r){return new(s||(s=Promise))((function(o,i){function n(t){try{p(r.next(t))}catch(t){i(t)}}function a(t){try{p(r.throw(t))}catch(t){i(t)}}function p(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,a)}p((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class c{}c.CONTINUE=100,c.SWITCHING_PROTOCOLS=101,c.PROCESSING=102,c.OK=200,c.CREATED=201,c.ACCEPTED=202,c.NON_AUTHORITATIVE_INFORMATION=203,c.NO_CONTENT=204,c.RESET_CONTENT=205,c.PARTIAL_CONTENT=206,c.MULTI_STATUS=207,c.ALREADY_REPORTED=208,c.IM_USED=226,c.MULTIPLE_CHOICES=300,c.MOVED_PERMANENTLY=301,c.FOUND=302,c.SEE_OTHER=303,c.NOT_MODIFIED=304,c.USE_PROXY=305,c.TEMPORARY_REDIRECT=307,c.PERMANENT_REDIRECT=308,c.BAD_REQUEST=400,c.UNAUTHORIZED=401,c.PAYMENT_REQUIRED=402,c.FORBIDDEN=403,c.NOT_FOUND=404,c.METHOD_NOT_ALLOWED=405,c.NOT_ACCEPTABLE=406,c.PROXY_AUTHENTICATION_REQUIRED=407,c.REQUEST_TIMEOUT=408,c.CONFLICT=409,c.GONE=410,c.LENGTH_REQUIRED=411,c.PRECONDITION_FAILED=412,c.PAYLOAD_TOO_LARGE=413,c.REQUEST_URI_TOO_LONG=414,c.UNSUPPORTED_MEDIA_TYPE=415,c.REQUESTED_RANGE_NOT_SATISFIABLE=416,c.EXPECTATION_FAILED=417,c.IM_A_TEAPOT=418,c.MISDIRECTED_REQUEST=421,c.UNPROCESSABLE_ENTITY=422,c.LOCKED=423,c.FAILED_DEPENDENCY=424,c.UPGRADE_REQUIRED=426,c.PRECONDITION_REQUIRED=428,c.TOO_MANY_REQUESTS=429,c.REQUEST_HEADER_FIELDS_TOO_LARGE=431,c.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,c.UNAVAILABLE_FOR_LEGAL_REASONS=451,c.CLIENT_CLOSED_REQUEST=499,c.INTERNAL_SERVER_ERROR=500,c.NOT_IMPLEMENTED=501,c.BAD_GATEWAY=502,c.SERVICE_UNAVAILABLE=503,c.GATEWAY_TIMEOUT=504,c.HTTP_VERSION_NOT_SUPPORTED=505,c.VARIANT_ALSO_NEGOCIATES=506,c.INSUFFICIENT_STORAGE=507,c.LOOP_DETECTED=508,c.NOT_EXTENDED=510,c.NETWORK_AUTHENTICATION_REQUIRED=511,c.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class T{constructor(t,e,s){this.req=t,this.res=e,this.next=s}data(t){return this.result||(this.result=this.res.status(c.OK).json(Object.assign({},t))),this.result}error(t){return this.result||(this.result=this.res.status(c.INTERNAL_SERVER_ERROR).json({message:t})),this.result}list(t){return this.result||(this.result=this.res.status(c.OK).json([...t])),this.result}notFound(t){return this.result||(this.result=this.res.status(c.NOT_FOUND).json({message:t})),this.result}success(t){return this.result||(this.result=this.res.status(c.OK).json({message:t})),this.result}unauthorized(t){return this.result||(this.result=this.res.status(c.UNAUTHORIZED).json({message:t})),this.result}validation(t){return this.result||(this.result=this.res.status(c.BAD_REQUEST).json({message:t})),this.result}}const O=t.cache(),l=(e,s,r)=>{t.isObject(s)&&(s=s.constructor);const o=O(s);return o[e]||(o[e]=r),o[e]};class R{static success(t,e){return t.status(c.OK).json({message:e})}static data(t,e){return t.status(c.OK).json(Object.assign({},e))}static error(t,e){return t.status(c.INTERNAL_SERVER_ERROR).json({message:e})}static list(t,e){return t.status(c.OK).json([...e])}static notfound(t,e){return t.status(c.NOT_FOUND).json({message:e})}static validation(t,e){return t.status(c.BAD_REQUEST).json({message:e})}static unauthorized(t,e){return t.status(c.UNAUTHORIZED).json({message:e})}}var x,N;exports.MetadataKeys=void 0,(x=exports.MetadataKeys||(exports.MetadataKeys={})).EXTENDS="__extends__",x.MIDDLEWARE="__middleware__",x.PARAMETERS="__parameters__",x.PATH="__path__",x.ROUTERS="__routers__",exports.Methods=void 0,(N=exports.Methods||(exports.Methods={})).ALL="all",N.GET="get",N.POST="post",N.PUT="put",N.HEAD="head",N.DELETE="delete",N.OPTIONS="options",N.PATCH="patch";class A extends t.EventEmitter{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(n,a){var p;super(),this._app=r(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=r.Router(),this._controllers=r.Router(),this._options=t.merge(Object.assign({},this._default),Object.assign({},a)),this._app.use(r.json(this._options.json)),this._app.disable("x-powered-by"),this._app.use(r.urlencoded({extended:!0})),this._options.cross&&this._app.use(s(t.isObject(this._options.cross)?this._options.cross:{}));const h=this._options.cookie;this._app.use(e(null==h?void 0:h.secret,null==h?void 0:h.options)),this._app.use(o(this._options.session)),this.init(),this._app.use(this._router);const u=(null===(p=this._options.path)||void 0===p?void 0:p.length)?this._options.path:"/";if(this._app.use(u,this._controllers),this._app.all(u+"/*",((t,e)=>{this._options.redirect?e.redirect(this._options.redirect):R.notfound(e,"Invalid API")})),this._options.public){const t=E.resolve(this._options.public);i.existsSync(t)&&this._app.use(r.static(t)),this._app.get("*",((e,s)=>{i.existsSync(t)?s.sendFile(t+"/index.html"):this._options.redirect?s.redirect(this._options.redirect):R.notfound(s,'File "index.html" not found')}))}n.forEach((e=>{const s=r.Router(),o=this._middlewares(e),i=this._parameters(e);this._extends(e).forEach((t=>{o.push(...this._middlewares(t)),i.push(...this._parameters(t))})),this._routers(e).forEach((r=>{const n=r.propertyKey.toString();if(t.isFunction(e[n])){if(!t.isFunction(s[r.method]))return this.error(new Error('Method "'+r.method+'" Not Found'),"express");const a=this._handles(e,n,o,i);return s[r.method](r.path,...a,((s,r,o)=>d(this,void 0,void 0,(function*(){const a=new T(s,r,o),p=this._args(a,s,r,o,this._indexes(n,i)),E=yield e[n](...p);return E&&!r.headersSent&&(t.isArray(E)||t.isObject(E)?r.status(c.OK).json(E):r.status(c.OK).send(E)),E}))))}return this.error(new Error('Router "'+e.name+"."+n+'" Not Found'),"express")})),this._extends(e).forEach((r=>{this._routers(r).forEach((r=>{const n=r.propertyKey.toString();if(t.isFunction(e[n])){if(!t.isFunction(s[r.method]))return this.error(new Error('Method "'+r.method+'" Not Found'),"express");const a=this._handles(e,n,o,i);return s[r.method](r.path,...a,((s,r,o)=>d(this,void 0,void 0,(function*(){const a=new T(s,r,o),p=this._args(a,s,r,o,this._indexes(n,i)),E=yield e[n](...p);return E&&!r.headersSent&&(t.isArray(E)||t.isObject(E)?r.status(c.OK).json(E):r.status(c.OK).send(E)),E}))))}return this.error(new Error('Router "'+e.constructor.name+"."+n+'" Not Found'),"express")}))})),this._controllers.use(this._path(e),s)}))}init(){}error(e,s){t.isUndefined(s)?console.error(e):console.error(s+":",e)}log(e,s){t.isUndefined(s)?console.log(e):console.log(s+":",e)}_args(t,e,s,r,o,i){const n=[],a={ctx:t,req:e,res:s,next:r,key:"",app:this,argument:i};return o.forEach((t=>{n[t.parameterIndex]=t.callback(Object.assign(Object.assign({},a),{key:t.key}))})),n}_handles(e,s,r,o){return r.filter((t=>t.propertyKey==s)).map((s=>{if(t.isFunction(e[s.method])){const t=this._indexes(s.method,o);return(r,o,i)=>{const n=new T(r,o,i);return e[s.method](...this._args(n,r,o,i,t,s.argument))}}this.error(new Error('Middleware "'+s.method+'" Not Found'),"express")})).filter(t.isFunction)}_extends(t){return l(exports.MetadataKeys.EXTENDS,t,[]).reduce(((t,e)=>(t.push(e,...this._extends(e)),t)),[]).filter(((t,e,s)=>s.indexOf(t)===e))}_indexes(t,e){return e.filter((e=>e.propertyKey==t))}_middlewares(t){return[...l(exports.MetadataKeys.MIDDLEWARE,t,[])].reverse()}_parameters(t){return l(exports.MetadataKeys.PARAMETERS,t,[])}_path(t){return l(exports.MetadataKeys.PATH,t,"/")}_routers(t){return l(exports.MetadataKeys.ROUTERS,t,[])}listen(){return d(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=u.createServer(this._app),this._server.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express")))),!this._server){const t=u.createServer(((t,e)=>{const s=80==this._options.port?":80":"";e.writeHead(301,{Location:"https://"+t.headers.host+s+t.url}),e.end()})),e=_.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new p.Server((s=>{s.once("data",(r=>{s.pause(),s.unshift(r),(22===r[0]?e:t).emit("connection",s),process.nextTick((()=>s.resume()))}))})).listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=e}this._server||(443==this._options.port&&t.listen(80,(()=>this.log("Server started on port 80","express"))),e.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=e)}}))}}const I=t=>e=>(s,r)=>{l(exports.MetadataKeys.ROUTERS,s,[]).push({method:t,path:(null==e?void 0:e.length)?e:"/",propertyKey:r})},S=I(exports.Methods.ALL),D=I(exports.Methods.GET),y=I(exports.Methods.POST),U=I(exports.Methods.PUT),M=I(exports.Methods.DELETE),m=I(exports.Methods.PATCH),P=I(exports.Methods.HEAD),f=I(exports.Methods.OPTIONS),L=t=>e=>(s,r,o)=>{l(exports.MetadataKeys.PARAMETERS,s,[]).push({callback:t,key:e,propertyKey:r,parameterIndex:o})},g=(e,s)=>t.isUndefined(s)?e:e[s],v=L((({app:t})=>t)),C=L((({argument:t})=>t)),j=L((({req:t,key:e})=>g(t.body,e))),K=L((({req:t,key:e})=>g(t.cookies,e))),F=L((({ctx:t})=>t)),b=L((({req:t,key:e})=>g(t.headers,e))),H=L((({next:t})=>t)),k=L((({req:t,key:e})=>g(t.params,e))),w=L((({req:t,key:e})=>g(t.query,e))),q=L((({req:t,key:e})=>g(t,e))),G=L((({res:t})=>t)),Q=L((({req:t})=>t.session));exports.All=S,exports.App=v,exports.Application=A,exports.Argument=C,exports.Body=j,exports.Context=T,exports.Controller=t=>e=>{l(exports.MetadataKeys.PATH,e,(null==t?void 0:t.length)?t:"/")},exports.Cookies=K,exports.Ctx=F,exports.Delete=M,exports.Extends=t=>e=>{l(exports.MetadataKeys.EXTENDS,e,[]).push(t)},exports.Get=D,exports.Head=P,exports.Headers=b,exports.Metadata=l,exports.Middleware=(t,e)=>(s,r)=>{l(exports.MetadataKeys.MIDDLEWARE,s,[]).push({method:t,argument:e,propertyKey:r})},exports.Next=H,exports.Options=f,exports.Params=k,exports.Patch=m,exports.Post=y,exports.Put=U,exports.Query=w,exports.Req=q,exports.Res=G,exports.Responses=R,exports.Session=Q,exports.Status=c,exports.methodFactory=I,exports.parameterFactory=L;
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  /*!
2
- * @andrewcaires/express v1.0.10
2
+ * @andrewcaires/express v1.1.0
3
3
  * Decorators for express
4
- * (c) 2023 Andrew Caires
4
+ * (c) 2024 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
7
  import { TypeCallbackFunction, TypeObjectAny, EventEmitter } from '@andrewcaires/utils.js';
8
- import { NextFunction, Request as Request$1, Response as Response$1, Express, Router } from 'express';
8
+ import { NextFunction, Request, Response, Express, Router } from 'express';
9
9
  import * as http from 'http';
10
10
  import * as https from 'https';
11
11
  import { OptionsJson } from 'body-parser';
@@ -14,12 +14,29 @@ import { SessionOptions } from 'express-session';
14
14
  import { CookieParseOptions } from 'cookie-parser';
15
15
 
16
16
  type INextFunction = NextFunction;
17
- type IRequest = Request$1;
18
- type IResponse = Response$1;
17
+ type IRequest = Request;
18
+ type IResponse = Response;
19
+ type ResponseJson = Response<any, Record<string, any>>;
20
+
21
+ declare class Context {
22
+ readonly req: Request;
23
+ readonly res: Response;
24
+ readonly next: NextFunction;
25
+ private result?;
26
+ constructor(req: Request, res: Response, next: NextFunction);
27
+ data(obj: object): ResponseJson;
28
+ error(message: string): ResponseJson;
29
+ list(arr: Array<any>): ResponseJson;
30
+ notFound(message: string): ResponseJson;
31
+ success(message: string): ResponseJson;
32
+ unauthorized(message: string): ResponseJson;
33
+ validation(message: string): ResponseJson;
34
+ }
19
35
 
20
36
  interface IArgs {
21
- req: Request$1;
22
- res: Response$1;
37
+ ctx: Context;
38
+ req: Request;
39
+ res: Response;
23
40
  next: NextFunction;
24
41
  key: string;
25
42
  app: Application;
@@ -139,27 +156,25 @@ declare const App: (key?: string) => ParameterDecorator;
139
156
  declare const Argument: (key?: string) => ParameterDecorator;
140
157
  declare const Body: (key?: string) => ParameterDecorator;
141
158
  declare const Cookies: (key?: string) => ParameterDecorator;
159
+ declare const Ctx: (key?: string) => ParameterDecorator;
142
160
  declare const Headers: (key?: string) => ParameterDecorator;
143
161
  declare const Next: (key?: string) => ParameterDecorator;
144
162
  declare const Params: (key?: string) => ParameterDecorator;
145
163
  declare const Query: (key?: string) => ParameterDecorator;
146
- declare const Request: (key?: string) => ParameterDecorator;
147
164
  declare const Req: (key?: string) => ParameterDecorator;
148
- declare const Response: (key?: string) => ParameterDecorator;
149
165
  declare const Res: (key?: string) => ParameterDecorator;
150
166
  declare const Session: (key?: string) => ParameterDecorator;
151
167
 
152
168
  declare const Metadata: <T = any>(metadataKey: MetadataKeys, target: any, metadataValue: T) => T;
153
169
 
154
- type ResponseJson = Response$1<any, Record<string, any>>;
155
170
  declare class Responses {
156
- static success(res: Response$1, message: string): ResponseJson;
157
- static data(res: Response$1, obj: object): ResponseJson;
158
- static error(res: Response$1, message: string): ResponseJson;
159
- static list(res: Response$1, arr: Array<any>): ResponseJson;
160
- static notfound(res: Response$1, message: string): ResponseJson;
161
- static validation(res: Response$1, message: string): ResponseJson;
162
- static unauthorized(res: Response$1, message: string): ResponseJson;
171
+ static success(res: Response, message: string): ResponseJson;
172
+ static data(res: Response, obj: object): ResponseJson;
173
+ static error(res: Response, message: string): ResponseJson;
174
+ static list(res: Response, arr: Array<any>): ResponseJson;
175
+ static notfound(res: Response, message: string): ResponseJson;
176
+ static validation(res: Response, message: string): ResponseJson;
177
+ static unauthorized(res: Response, message: string): ResponseJson;
163
178
  }
164
179
 
165
180
  declare class Status {
@@ -228,4 +243,4 @@ declare class Status {
228
243
  static readonly NETWORK_CONNECTION_TIMEOUT_ERROR = 599;
229
244
  }
230
245
 
231
- export { All, App, Application, Argument, Body, Controller, Cookies, Delete, Extends, Get, Head, Headers, type IArgs, type IMiddlewares, type INextFunction, type IOptions, type IParameter, type IRequest, type IResponse, type IRouter, type IStore, Metadata, MetadataKeys, Methods, Middleware, Next, Options, Params, Patch, Post, Put, Query, Req, Request, Res, Response, type ResponseJson, Responses, Session, Status, methodFactory, parameterFactory };
246
+ export { All, App, Application, Argument, Body, Context, Controller, Cookies, Ctx, Delete, Extends, Get, Head, Headers, type IArgs, type IMiddlewares, type INextFunction, type IOptions, type IParameter, type IRequest, type IResponse, type IRouter, type IStore, Metadata, MetadataKeys, Methods, Middleware, Next, Options, Params, Patch, Post, Put, Query, Req, Res, type ResponseJson, Responses, Session, Status, methodFactory, parameterFactory };
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/express v1.0.10
2
+ * @andrewcaires/express v1.1.0
3
3
  * Decorators for express
4
- * (c) 2023 Andrew Caires
4
+ * (c) 2024 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- import{cache as t,isObject as e,EventEmitter as s,merge as r,isFunction as o,isUndefined as i}from"@andrewcaires/utils.js";import n from"cookie-parser";import E from"cors";import _,{Router as p,json as h,urlencoded as a}from"express";import T from"express-session";import{existsSync as u}from"fs";import*as c from"http";import*as O from"https";import{Server as R}from"net";import{resolve as d}from"path";function N(t,e,s,r){return new(s||(s=Promise))((function(o,i){function n(t){try{_(r.next(t))}catch(t){i(t)}}function E(t){try{_(r.throw(t))}catch(t){i(t)}}function _(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,E)}_((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const A=t(),l=(t,s,r)=>{e(s)&&(s=s.constructor);const o=A(s);return o[t]||(o[t]=r),o[t]};class I{}I.CONTINUE=100,I.SWITCHING_PROTOCOLS=101,I.PROCESSING=102,I.OK=200,I.CREATED=201,I.ACCEPTED=202,I.NON_AUTHORITATIVE_INFORMATION=203,I.NO_CONTENT=204,I.RESET_CONTENT=205,I.PARTIAL_CONTENT=206,I.MULTI_STATUS=207,I.ALREADY_REPORTED=208,I.IM_USED=226,I.MULTIPLE_CHOICES=300,I.MOVED_PERMANENTLY=301,I.FOUND=302,I.SEE_OTHER=303,I.NOT_MODIFIED=304,I.USE_PROXY=305,I.TEMPORARY_REDIRECT=307,I.PERMANENT_REDIRECT=308,I.BAD_REQUEST=400,I.UNAUTHORIZED=401,I.PAYMENT_REQUIRED=402,I.FORBIDDEN=403,I.NOT_FOUND=404,I.METHOD_NOT_ALLOWED=405,I.NOT_ACCEPTABLE=406,I.PROXY_AUTHENTICATION_REQUIRED=407,I.REQUEST_TIMEOUT=408,I.CONFLICT=409,I.GONE=410,I.LENGTH_REQUIRED=411,I.PRECONDITION_FAILED=412,I.PAYLOAD_TOO_LARGE=413,I.REQUEST_URI_TOO_LONG=414,I.UNSUPPORTED_MEDIA_TYPE=415,I.REQUESTED_RANGE_NOT_SATISFIABLE=416,I.EXPECTATION_FAILED=417,I.IM_A_TEAPOT=418,I.MISDIRECTED_REQUEST=421,I.UNPROCESSABLE_ENTITY=422,I.LOCKED=423,I.FAILED_DEPENDENCY=424,I.UPGRADE_REQUIRED=426,I.PRECONDITION_REQUIRED=428,I.TOO_MANY_REQUESTS=429,I.REQUEST_HEADER_FIELDS_TOO_LARGE=431,I.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,I.UNAVAILABLE_FOR_LEGAL_REASONS=451,I.CLIENT_CLOSED_REQUEST=499,I.INTERNAL_SERVER_ERROR=500,I.NOT_IMPLEMENTED=501,I.BAD_GATEWAY=502,I.SERVICE_UNAVAILABLE=503,I.GATEWAY_TIMEOUT=504,I.HTTP_VERSION_NOT_SUPPORTED=505,I.VARIANT_ALSO_NEGOCIATES=506,I.INSUFFICIENT_STORAGE=507,I.LOOP_DETECTED=508,I.NOT_EXTENDED=510,I.NETWORK_AUTHENTICATION_REQUIRED=511,I.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class S{static success(t,e){return t.status(I.OK).json({message:e})}static data(t,e){return t.status(I.OK).json(Object.assign({},e))}static error(t,e){return t.status(I.INTERNAL_SERVER_ERROR).json({message:e})}static list(t,e){return t.status(I.OK).json([...e])}static notfound(t,e){return t.status(I.NOT_FOUND).json({message:e})}static validation(t,e){return t.status(I.BAD_REQUEST).json({message:e})}static unauthorized(t,e){return t.status(I.UNAUTHORIZED).json({message:e})}}var D,m;!function(t){t.EXTENDS="__extends__",t.MIDDLEWARE="__middleware__",t.PARAMETERS="__parameters__",t.PATH="__path__",t.ROUTERS="__routers__"}(D||(D={})),function(t){t.ALL="all",t.GET="get",t.POST="post",t.PUT="put",t.HEAD="head",t.DELETE="delete",t.OPTIONS="options",t.PATCH="patch"}(m||(m={}));class U extends s{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,s){var i;super(),this._app=_(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=p(),this._controllers=p(),this._options=r(Object.assign({},this._default),Object.assign({},s)),this._app.use(h(this._options.json)),this._app.disable("x-powered-by"),this._app.use(a({extended:!0})),this._options.cross&&this._app.use(E(e(this._options.cross)?this._options.cross:{}));const c=this._options.cookie;this._app.use(n(null==c?void 0:c.secret,null==c?void 0:c.options)),this._app.use(T(this._options.session)),this.init(),this._app.use(this._router);const O=(null===(i=this._options.path)||void 0===i?void 0:i.length)?this._options.path:"/";if(this._app.use(O,this._controllers),this._app.all(O+"/*",((t,e)=>{this._options.redirect?e.redirect(this._options.redirect):S.notfound(e,"Invalid API")})),this._options.public){const t=d(this._options.public);u(t)&&this._app.use(_.static(t)),this._app.get("*",((e,s)=>{u(t)?s.sendFile(t+"/index.html"):this._options.redirect?s.redirect(this._options.redirect):S.notfound(s,'File "index.html" not found')}))}t.forEach((t=>{const e=p(),s=this._middlewares(t),r=this._parameters(t);this._extends(t).forEach((t=>{s.push(...this._middlewares(t)),r.push(...this._parameters(t))})),this._routers(t).forEach((i=>{const n=i.propertyKey.toString();if(o(t[n])){if(!o(e[i.method]))return this.error(new Error('Method "'+i.method+'" Not Found'),"express");const E=this._handles(t,n,s,r);return e[i.method](i.path,...E,((e,s,o)=>t[n](...this._args(e,s,o,this._indexes(n,r)))))}return this.error(new Error('Router "'+t.name+"."+n+'" Not Found'),"express")})),this._extends(t).forEach((i=>{this._routers(i).forEach((i=>{const n=i.propertyKey.toString();if(o(t[n])){if(!o(e[i.method]))return this.error(new Error('Method "'+i.method+'" Not Found'),"express");const E=this._handles(t,n,s,r);return e[i.method](i.path,...E,((e,s,o)=>t[n](...this._args(e,s,o,this._indexes(n,r)))))}return this.error(new Error('Router "'+t.constructor.name+"."+n+'" Not Found'),"express")}))})),this._controllers.use(this._path(t),e)}))}init(){}error(t,e){i(e)?console.error(t):console.error(e+":",t)}log(t,e){i(e)?console.log(t):console.log(e+":",t)}_args(t,e,s,r,o){const i=[],n={req:t,res:e,next:s,key:"",app:this,argument:o};return r.forEach((t=>{i[t.parameterIndex]=t.callback(Object.assign(Object.assign({},n),{key:t.key}))})),i}_handles(t,e,s,r){return s.filter((t=>t.propertyKey==e)).map((e=>{if(o(t[e.method])){const s=this._indexes(e.method,r);return(r,o,i)=>t[e.method](...this._args(r,o,i,s,e.argument))}this.error(new Error('Middleware "'+e.method+'" Not Found'),"express")})).filter(o)}_extends(t){return l(D.EXTENDS,t,[]).reduce(((t,e)=>(t.push(e,...this._extends(e)),t)),[]).filter(((t,e,s)=>s.indexOf(t)===e))}_indexes(t,e){return e.filter((e=>e.propertyKey==t))}_middlewares(t){return[...l(D.MIDDLEWARE,t,[])].reverse()}_parameters(t){return l(D.PARAMETERS,t,[])}_path(t){return l(D.PATH,t,"/")}_routers(t){return l(D.ROUTERS,t,[])}listen(){return N(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=c.createServer(this._app),this._server.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express")))),!this._server){const t=c.createServer(((t,e)=>{const s=80==this._options.port?":80":"";e.writeHead(301,{Location:"https://"+t.headers.host+s+t.url}),e.end()})),e=O.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new R((s=>{s.once("data",(r=>{s.pause(),s.unshift(r),(22===r[0]?e:t).emit("connection",s),process.nextTick((()=>s.resume()))}))})).listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=e}this._server||(443==this._options.port&&t.listen(80,(()=>this.log("Server started on port 80","express"))),e.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=e)}}))}}const f=t=>e=>{l(D.PATH,e,(null==t?void 0:t.length)?t:"/")},L=t=>e=>{l(D.EXTENDS,e,[]).push(t)},P=t=>e=>(s,r)=>{l(D.ROUTERS,s,[]).push({method:t,path:(null==e?void 0:e.length)?e:"/",propertyKey:r})},C=P(m.ALL),y=P(m.GET),g=P(m.POST),x=P(m.PUT),v=P(m.DELETE),M=P(m.PATCH),F=P(m.HEAD),H=P(m.OPTIONS),k=(t,e)=>(s,r)=>{l(D.MIDDLEWARE,s,[]).push({method:t,argument:e,propertyKey:r})},w=t=>e=>(s,r,o)=>{l(D.PARAMETERS,s,[]).push({callback:t,key:e,propertyKey:r,parameterIndex:o})},G=(t,e)=>i(e)?t:t[e],Q=w((({app:t})=>t)),b=w((({argument:t})=>t)),j=w((({req:t,key:e})=>G(t.body,e))),K=w((({req:t,key:e})=>G(t.cookies,e))),Y=w((({req:t,key:e})=>G(t.headers,e))),W=w((({next:t})=>t)),q=w((({req:t,key:e})=>G(t.params,e))),B=w((({req:t,key:e})=>G(t.query,e))),V=w((({req:t,key:e})=>G(t,e))),X=V,z=w((({res:t})=>t)),Z=z,J=w((({req:t})=>t.session));export{C as All,Q as App,U as Application,b as Argument,j as Body,f as Controller,K as Cookies,v as Delete,L as Extends,y as Get,F as Head,Y as Headers,l as Metadata,D as MetadataKeys,m as Methods,k as Middleware,W as Next,H as Options,q as Params,M as Patch,g as Post,x as Put,B as Query,X as Req,V as Request,Z as Res,z as Response,S as Responses,J as Session,I as Status,P as methodFactory,w as parameterFactory};
7
+ import{cache as t,isObject as s,EventEmitter as e,merge as r,isFunction as o,isUndefined as i,isArray as n}from"@andrewcaires/utils.js";import E from"cookie-parser";import _ from"cors";import h,{Router as a,json as u,urlencoded as p}from"express";import T from"express-session";import{existsSync as c}from"fs";import*as O from"http";import*as d from"https";import{Server as R}from"net";import{resolve as l}from"path";function N(t,s,e,r){return new(e||(e=Promise))((function(o,i){function n(t){try{_(r.next(t))}catch(t){i(t)}}function E(t){try{_(r.throw(t))}catch(t){i(t)}}function _(t){var s;t.done?o(t.value):(s=t.value,s instanceof e?s:new e((function(t){t(s)}))).then(n,E)}_((r=r.apply(t,s||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class A{}A.CONTINUE=100,A.SWITCHING_PROTOCOLS=101,A.PROCESSING=102,A.OK=200,A.CREATED=201,A.ACCEPTED=202,A.NON_AUTHORITATIVE_INFORMATION=203,A.NO_CONTENT=204,A.RESET_CONTENT=205,A.PARTIAL_CONTENT=206,A.MULTI_STATUS=207,A.ALREADY_REPORTED=208,A.IM_USED=226,A.MULTIPLE_CHOICES=300,A.MOVED_PERMANENTLY=301,A.FOUND=302,A.SEE_OTHER=303,A.NOT_MODIFIED=304,A.USE_PROXY=305,A.TEMPORARY_REDIRECT=307,A.PERMANENT_REDIRECT=308,A.BAD_REQUEST=400,A.UNAUTHORIZED=401,A.PAYMENT_REQUIRED=402,A.FORBIDDEN=403,A.NOT_FOUND=404,A.METHOD_NOT_ALLOWED=405,A.NOT_ACCEPTABLE=406,A.PROXY_AUTHENTICATION_REQUIRED=407,A.REQUEST_TIMEOUT=408,A.CONFLICT=409,A.GONE=410,A.LENGTH_REQUIRED=411,A.PRECONDITION_FAILED=412,A.PAYLOAD_TOO_LARGE=413,A.REQUEST_URI_TOO_LONG=414,A.UNSUPPORTED_MEDIA_TYPE=415,A.REQUESTED_RANGE_NOT_SATISFIABLE=416,A.EXPECTATION_FAILED=417,A.IM_A_TEAPOT=418,A.MISDIRECTED_REQUEST=421,A.UNPROCESSABLE_ENTITY=422,A.LOCKED=423,A.FAILED_DEPENDENCY=424,A.UPGRADE_REQUIRED=426,A.PRECONDITION_REQUIRED=428,A.TOO_MANY_REQUESTS=429,A.REQUEST_HEADER_FIELDS_TOO_LARGE=431,A.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,A.UNAVAILABLE_FOR_LEGAL_REASONS=451,A.CLIENT_CLOSED_REQUEST=499,A.INTERNAL_SERVER_ERROR=500,A.NOT_IMPLEMENTED=501,A.BAD_GATEWAY=502,A.SERVICE_UNAVAILABLE=503,A.GATEWAY_TIMEOUT=504,A.HTTP_VERSION_NOT_SUPPORTED=505,A.VARIANT_ALSO_NEGOCIATES=506,A.INSUFFICIENT_STORAGE=507,A.LOOP_DETECTED=508,A.NOT_EXTENDED=510,A.NETWORK_AUTHENTICATION_REQUIRED=511,A.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class I{constructor(t,s,e){this.req=t,this.res=s,this.next=e}data(t){return this.result||(this.result=this.res.status(A.OK).json(Object.assign({},t))),this.result}error(t){return this.result||(this.result=this.res.status(A.INTERNAL_SERVER_ERROR).json({message:t})),this.result}list(t){return this.result||(this.result=this.res.status(A.OK).json([...t])),this.result}notFound(t){return this.result||(this.result=this.res.status(A.NOT_FOUND).json({message:t})),this.result}success(t){return this.result||(this.result=this.res.status(A.OK).json({message:t})),this.result}unauthorized(t){return this.result||(this.result=this.res.status(A.UNAUTHORIZED).json({message:t})),this.result}validation(t){return this.result||(this.result=this.res.status(A.BAD_REQUEST).json({message:t})),this.result}}const S=t(),D=(t,e,r)=>{s(e)&&(e=e.constructor);const o=S(e);return o[t]||(o[t]=r),o[t]};class m{static success(t,s){return t.status(A.OK).json({message:s})}static data(t,s){return t.status(A.OK).json(Object.assign({},s))}static error(t,s){return t.status(A.INTERNAL_SERVER_ERROR).json({message:s})}static list(t,s){return t.status(A.OK).json([...s])}static notfound(t,s){return t.status(A.NOT_FOUND).json({message:s})}static validation(t,s){return t.status(A.BAD_REQUEST).json({message:s})}static unauthorized(t,s){return t.status(A.UNAUTHORIZED).json({message:s})}}var U,f;!function(t){t.EXTENDS="__extends__",t.MIDDLEWARE="__middleware__",t.PARAMETERS="__parameters__",t.PATH="__path__",t.ROUTERS="__routers__"}(U||(U={})),function(t){t.ALL="all",t.GET="get",t.POST="post",t.PUT="put",t.HEAD="head",t.DELETE="delete",t.OPTIONS="options",t.PATCH="patch"}(f||(f={}));class L extends e{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,e){var i;super(),this._app=h(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=a(),this._controllers=a(),this._options=r(Object.assign({},this._default),Object.assign({},e)),this._app.use(u(this._options.json)),this._app.disable("x-powered-by"),this._app.use(p({extended:!0})),this._options.cross&&this._app.use(_(s(this._options.cross)?this._options.cross:{}));const O=this._options.cookie;this._app.use(E(null==O?void 0:O.secret,null==O?void 0:O.options)),this._app.use(T(this._options.session)),this.init(),this._app.use(this._router);const d=(null===(i=this._options.path)||void 0===i?void 0:i.length)?this._options.path:"/";if(this._app.use(d,this._controllers),this._app.all(d+"/*",((t,s)=>{this._options.redirect?s.redirect(this._options.redirect):m.notfound(s,"Invalid API")})),this._options.public){const t=l(this._options.public);c(t)&&this._app.use(h.static(t)),this._app.get("*",((s,e)=>{c(t)?e.sendFile(t+"/index.html"):this._options.redirect?e.redirect(this._options.redirect):m.notfound(e,'File "index.html" not found')}))}t.forEach((t=>{const e=a(),r=this._middlewares(t),i=this._parameters(t);this._extends(t).forEach((t=>{r.push(...this._middlewares(t)),i.push(...this._parameters(t))})),this._routers(t).forEach((E=>{const _=E.propertyKey.toString();if(o(t[_])){if(!o(e[E.method]))return this.error(new Error('Method "'+E.method+'" Not Found'),"express");const h=this._handles(t,_,r,i);return e[E.method](E.path,...h,((e,r,o)=>N(this,void 0,void 0,(function*(){const E=new I(e,r,o),h=this._args(E,e,r,o,this._indexes(_,i)),a=yield t[_](...h);return a&&!r.headersSent&&(n(a)||s(a)?r.status(A.OK).json(a):r.status(A.OK).send(a)),a}))))}return this.error(new Error('Router "'+t.name+"."+_+'" Not Found'),"express")})),this._extends(t).forEach((E=>{this._routers(E).forEach((E=>{const _=E.propertyKey.toString();if(o(t[_])){if(!o(e[E.method]))return this.error(new Error('Method "'+E.method+'" Not Found'),"express");const h=this._handles(t,_,r,i);return e[E.method](E.path,...h,((e,r,o)=>N(this,void 0,void 0,(function*(){const E=new I(e,r,o),h=this._args(E,e,r,o,this._indexes(_,i)),a=yield t[_](...h);return a&&!r.headersSent&&(n(a)||s(a)?r.status(A.OK).json(a):r.status(A.OK).send(a)),a}))))}return this.error(new Error('Router "'+t.constructor.name+"."+_+'" Not Found'),"express")}))})),this._controllers.use(this._path(t),e)}))}init(){}error(t,s){i(s)?console.error(t):console.error(s+":",t)}log(t,s){i(s)?console.log(t):console.log(s+":",t)}_args(t,s,e,r,o,i){const n=[],E={ctx:t,req:s,res:e,next:r,key:"",app:this,argument:i};return o.forEach((t=>{n[t.parameterIndex]=t.callback(Object.assign(Object.assign({},E),{key:t.key}))})),n}_handles(t,s,e,r){return e.filter((t=>t.propertyKey==s)).map((s=>{if(o(t[s.method])){const e=this._indexes(s.method,r);return(r,o,i)=>{const n=new I(r,o,i);return t[s.method](...this._args(n,r,o,i,e,s.argument))}}this.error(new Error('Middleware "'+s.method+'" Not Found'),"express")})).filter(o)}_extends(t){return D(U.EXTENDS,t,[]).reduce(((t,s)=>(t.push(s,...this._extends(s)),t)),[]).filter(((t,s,e)=>e.indexOf(t)===s))}_indexes(t,s){return s.filter((s=>s.propertyKey==t))}_middlewares(t){return[...D(U.MIDDLEWARE,t,[])].reverse()}_parameters(t){return D(U.PARAMETERS,t,[])}_path(t){return D(U.PATH,t,"/")}_routers(t){return D(U.ROUTERS,t,[])}listen(){return N(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=O.createServer(this._app),this._server.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express")))),!this._server){const t=O.createServer(((t,s)=>{const e=80==this._options.port?":80":"";s.writeHead(301,{Location:"https://"+t.headers.host+e+t.url}),s.end()})),s=d.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new R((e=>{e.once("data",(r=>{e.pause(),e.unshift(r),(22===r[0]?s:t).emit("connection",e),process.nextTick((()=>e.resume()))}))})).listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=s}this._server||(443==this._options.port&&t.listen(80,(()=>this.log("Server started on port 80","express"))),s.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port,"express"))),this._server=s)}}))}}const P=t=>s=>{D(U.PATH,s,(null==t?void 0:t.length)?t:"/")},g=t=>s=>{D(U.EXTENDS,s,[]).push(t)},C=t=>s=>(e,r)=>{D(U.ROUTERS,e,[]).push({method:t,path:(null==s?void 0:s.length)?s:"/",propertyKey:r})},y=C(f.ALL),v=C(f.GET),x=C(f.POST),M=C(f.PUT),j=C(f.DELETE),F=C(f.PATCH),H=C(f.HEAD),K=C(f.OPTIONS),k=(t,s)=>(e,r)=>{D(U.MIDDLEWARE,e,[]).push({method:t,argument:s,propertyKey:r})},w=t=>s=>(e,r,o)=>{D(U.PARAMETERS,e,[]).push({callback:t,key:s,propertyKey:r,parameterIndex:o})},G=(t,s)=>i(s)?t:t[s],Q=w((({app:t})=>t)),b=w((({argument:t})=>t)),Y=w((({req:t,key:s})=>G(t.body,s))),q=w((({req:t,key:s})=>G(t.cookies,s))),B=w((({ctx:t})=>t)),V=w((({req:t,key:s})=>G(t.headers,s))),W=w((({next:t})=>t)),X=w((({req:t,key:s})=>G(t.params,s))),z=w((({req:t,key:s})=>G(t.query,s))),Z=w((({req:t,key:s})=>G(t,s))),J=w((({res:t})=>t)),$=w((({req:t})=>t.session));export{y as All,Q as App,L as Application,b as Argument,Y as Body,I as Context,P as Controller,q as Cookies,B as Ctx,j as Delete,g as Extends,v as Get,H as Head,V as Headers,D as Metadata,U as MetadataKeys,f as Methods,k as Middleware,W as Next,K as Options,X as Params,F as Patch,x as Post,M as Put,z as Query,Z as Req,J as Res,m as Responses,$ as Session,A as Status,C as methodFactory,w as parameterFactory};
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@andrewcaires/express",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "Decorators for express",
5
+ "type": "module",
5
6
  "main": "./dist/index.cjs.js",
6
7
  "types": "./dist/index.d.ts",
7
8
  "module": "./dist/index.esm.js",
@@ -35,27 +36,27 @@
35
36
  },
36
37
  "homepage": "https://github.com/andrewcaires/npm/tree/main/api#readme",
37
38
  "dependencies": {
38
- "@andrewcaires/utils.js": "^0.2.22",
39
+ "@andrewcaires/utils.js": "^0.2.23",
39
40
  "cookie-parser": "^1.4.6",
40
41
  "cors": "^2.8.5",
41
- "express": "^4.18.2",
42
- "express-session": "^1.17.3",
42
+ "express": "^4.18.3",
43
+ "express-session": "^1.18.0",
43
44
  "multer": "^1.4.5-lts.1"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@rollup/plugin-commonjs": "^25.0.7",
47
48
  "@rollup/plugin-terser": "^0.4.4",
48
- "@rollup/plugin-typescript": "^11.1.5",
49
- "@types/cookie-parser": "^1.4.6",
49
+ "@rollup/plugin-typescript": "^11.1.6",
50
+ "@types/cookie-parser": "^1.4.7",
50
51
  "@types/cors": "^2.8.17",
51
52
  "@types/express": "^4.17.21",
52
- "@types/express-session": "^1.17.10",
53
+ "@types/express-session": "^1.18.0",
53
54
  "@types/mime-types": "^2.1.4",
54
55
  "@types/multer": "^1.4.11",
55
- "@types/node": "^20.10.5",
56
- "@typescript-eslint/eslint-plugin": "^6.16.0",
57
- "@typescript-eslint/parser": "^6.16.0",
58
- "eslint": "^8.56.0",
56
+ "@types/node": "^20.11.26",
57
+ "@typescript-eslint/eslint-plugin": "^7.2.0",
58
+ "@typescript-eslint/parser": "^7.2.0",
59
+ "eslint": "^8.57.0",
59
60
  "rollup-plugin-dts": "^6.1.0",
60
61
  "ts-node-dev": "^2.0.0",
61
62
  "tslib": "^2.6.2"