@andrewcaires/express 1.2.8 → 1.2.9

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.2.8
2
+ * @andrewcaires/express v1.2.9
3
3
  * Decorators for express
4
4
  * (c) 2026 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- "use strict";var e=require("@andrewcaires/node"),t=require("@andrewcaires/utils.js"),s=require("compression"),r=require("cookie-parser"),o=require("cors"),i=require("express"),n=require("express-session"),a=require("fs"),p=require("http"),h=require("https"),u=require("net"),l=require("path"),d=require("request-ip"),c=require("@andrewcaires/decorator"),E=require("multer");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 T=_(p),O=_(h);function R(e,t,s,r){return new(s||(s=Promise))(function(o,i){function n(e){try{p(r.next(e))}catch(e){i(e)}}function a(e){try{p(r.throw(e))}catch(e){i(e)}}function p(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(n,a)}p((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class x{}x.CONTINUE=100,x.SWITCHING_PROTOCOLS=101,x.PROCESSING=102,x.OK=200,x.CREATED=201,x.ACCEPTED=202,x.NON_AUTHORITATIVE_INFORMATION=203,x.NO_CONTENT=204,x.RESET_CONTENT=205,x.PARTIAL_CONTENT=206,x.MULTI_STATUS=207,x.ALREADY_REPORTED=208,x.IM_USED=226,x.MULTIPLE_CHOICES=300,x.MOVED_PERMANENTLY=301,x.FOUND=302,x.SEE_OTHER=303,x.NOT_MODIFIED=304,x.USE_PROXY=305,x.TEMPORARY_REDIRECT=307,x.PERMANENT_REDIRECT=308,x.BAD_REQUEST=400,x.UNAUTHORIZED=401,x.PAYMENT_REQUIRED=402,x.FORBIDDEN=403,x.NOT_FOUND=404,x.METHOD_NOT_ALLOWED=405,x.NOT_ACCEPTABLE=406,x.PROXY_AUTHENTICATION_REQUIRED=407,x.REQUEST_TIMEOUT=408,x.CONFLICT=409,x.GONE=410,x.LENGTH_REQUIRED=411,x.PRECONDITION_FAILED=412,x.PAYLOAD_TOO_LARGE=413,x.REQUEST_URI_TOO_LONG=414,x.UNSUPPORTED_MEDIA_TYPE=415,x.REQUESTED_RANGE_NOT_SATISFIABLE=416,x.EXPECTATION_FAILED=417,x.IM_A_TEAPOT=418,x.MISDIRECTED_REQUEST=421,x.UNPROCESSABLE_ENTITY=422,x.LOCKED=423,x.FAILED_DEPENDENCY=424,x.UPGRADE_REQUIRED=426,x.PRECONDITION_REQUIRED=428,x.TOO_MANY_REQUESTS=429,x.REQUEST_HEADER_FIELDS_TOO_LARGE=431,x.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,x.UNAVAILABLE_FOR_LEGAL_REASONS=451,x.CLIENT_CLOSED_REQUEST=499,x.INTERNAL_SERVER_ERROR=500,x.NOT_IMPLEMENTED=501,x.BAD_GATEWAY=502,x.SERVICE_UNAVAILABLE=503,x.GATEWAY_TIMEOUT=504,x.HTTP_VERSION_NOT_SUPPORTED=505,x.VARIANT_ALSO_NEGOCIATES=506,x.INSUFFICIENT_STORAGE=507,x.LOOP_DETECTED=508,x.NOT_EXTENDED=510,x.NETWORK_AUTHENTICATION_REQUIRED=511,x.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class v{constructor(e,t,s,r,o){this.req=e,this.res=t,this.next=s,this.app=r,this.controller=o}cache(e){const s=[];e.private?s.push("private"):e.public&&s.push("public"),e.noStore&&(e.noCache=!0,s.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,s.push("no-cache")),e.noTransform&&s.push("no-transform"),e.proxyRevalidate&&s.push("proxy-revalidate"),e.mustRevalidate?s.push("must-revalidate"):e.noCache||(t.isNumber(e.staleIfError)&&s.push(`stale-if-error=${e.staleIfError}`),t.isNumber(e.staleWhileRevalidate)&&s.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),t.isNumber(e.maxAge)&&s.push(`max-age=${e.maxAge}`),t.isNumber(e.sMaxAge)&&s.push(`s-maxage=${e.sMaxAge}`),s.length&&(this.res.setHeader("Cache-Control",s.join(",")),e.noCache&&t.forEachKey({"Surrogate-Control":"no-store",Pragma:"no-cache",ETag:"false",Expires:"0"},(e,t)=>this.res.setHeader(t,e)))}close(e){this.res.headersSent||this.res.json(e||this.storage)}destroy(e){var t;e&&this.error(e.message),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){this.send(x.INTERNAL_SERVER_ERROR,{message:e})}file(e){this.res.headersSent||(this.status(x.OK),this.res.sendFile(l.resolve(e)))}getIp(){return d.getClientIp(this.req)||"0.0.0.0"}json(e){this.send(x.OK,e)}notFound(e){this.send(x.NOT_FOUND,{message:e})}prepare(e,t){this.res.headersSent||(this.status(e),this.storage=t)}send(e,t){this.res.headersSent||(this.status(e),this.res.json(t))}status(e){return e&&this.res.status(e),this.res.statusCode}success(e){this.send(x.OK,{message:e})}unauthorized(e){this.send(x.UNAUTHORIZED,{message:e})}validation(e){this.send(x.BAD_REQUEST,{message:e})}}class A{static extends(e){return c.metadata(A.EXTENDS,e,[])}static middleware(e){return c.metadata(A.MIDDLEWARE,e,[])}static parameters(e){return c.metadata(A.PARAMETERS,e,[])}static path(e,t){return c.metadata(A.PATH,e,t)}static routers(e){return c.metadata(A.ROUTERS,e,[])}}A.EXTENDS=Symbol("EXTENDS"),A.MIDDLEWARE=Symbol("MIDDLEWARE"),A.PARAMETERS=Symbol("PARAMETERS"),A.PATH=Symbol("PATH"),A.ROUTERS=Symbol("ROUTERS");class N extends t.EventEmitter{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,p){var h,u;super(),this._app=i(),this._options={},this._base=i.Router(),this._router=i.Router(),this._controllers=[],this._controllers.push(...e.map(e=>t.isConstructor(e)?new e:e)),this._options=t.merge({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=p?p:{}),this._app.use(i.json(this._options.json)),this._app.disable("x-powered-by"),this._app.use(i.urlencoded({extended:!0})),this._options.cross&&this._app.use(o(t.isObject(this._options.cross)?this._options.cross:{})),this._app.use(s(this._options.compression)),this._app.use(r(null===(h=this._options.cookie)||void 0===h?void 0:h.secret,this._options.cookie)),this._app.use(n(this._options.session)),this.init(),this._app.use(this._router);const d=(null===(u=this._options.path)||void 0===u?void 0:u.length)?this._options.path:"/";if(this._app.use(d,this._base),this._app.all(d+"/*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(x.NOT_FOUND),t.json({message:"INVALID API"}))}),this._options.public){const e=l.resolve(this._options.public);a.existsSync(e)&&this._app.use(i.static(e)),this._app.get("*path",(t,s)=>{a.existsSync(e)?s.sendFile(e+"/index.html"):this._options.notfound?s.redirect(this._options.notfound):(s.status(x.NOT_FOUND),s.json({message:'FILE "index.html" NOT FOUND'}))})}else this._app.all("*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(x.NOT_FOUND),t.json({message:"INVALID API"}))});this._controllers.forEach(e=>{const s=i.Router(),r=this._middlewares(e),o=A.parameters(e);this._extends(e).forEach(e=>{r.push(...this._middlewares(e)),o.push(...A.parameters(e))}),A.routers(e).forEach(i=>{const n=i.propertyKey.toString();if(t.isFunction(e[n])){if(!t.isFunction(s[i.method]))return this._notFound(i.method);const a=this._handles(e,n,r,o);return s[i.method](i.path,...a,(s,r,i)=>R(this,void 0,void 0,function*(){const a=new v(s,r,i,this,e),p=this._args(a,this._indexes(n,o)),h=yield e[n](...p);a.close(t.isArray(h)||t.isObject(h)?h:void 0)}))}this._notFound(`${e.name}.${n}`)}),this._extends(e).forEach(i=>{A.routers(i).forEach(i=>{const n=i.propertyKey.toString();if(t.isFunction(e[n])){if(!t.isFunction(s[i.method]))return this._notFound(i.method);const a=this._handles(e,n,r,o);return s[i.method](i.path,...a,(s,r,i)=>R(this,void 0,void 0,function*(){const a=new v(s,r,i,this,e),p=this._args(a,this._indexes(n,o)),h=yield e[n](...p);a.close(t.isArray(h)||t.isObject(h)?h:void 0)}))}this._notFound(`${e.constructor.name}.${n}`)})}),this._base.use(A.path(e,"/"),s)})}init(){}error(t,s){e.Log.error(t)}log(t,s){e.Log.info(t)}_args(e,t,s){const r=[],o={ctx:e,key:"",app:this,argument:s};return t.forEach(e=>{r[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),r}_handles(e,s,r,o){return r.filter(e=>e.propertyKey==s).map(s=>{if(s.callback!=t.noop){const r=this._indexes(s.method,o);return(o,i,n)=>R(this,void 0,void 0,function*(){const a=new v(o,i,n,this,e),p=t.isFunction(s.argument)?s.argument(a):s.argument,h=yield s.callback.call(e,...this._args(a,r,p));(t.isArray(h)||t.isObject(h))&&a.close(h)})}this._notFound(s.method)}).filter(t.isFunction)}_extends(e){return A.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[...A.middleware(e)].reverse()}_notFound(e){this.error(new Error(`Router "${e}" not found`),"express")}_port(e){return`Server listening on port ${e}`}listen(){return R(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=T.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=T.createServer((e,t)=>{const s=80==this._options.port?":80":"";t.writeHead(x.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${s}${e.url}`}),t.end()}),t=O.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new u.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(this._port(this._options.port),"express")),this._server=t}this._server||(443==this._options.port&&e.listen(80,()=>this.log(this._port(80),"express")),t.listen(this._options.port,()=>this.log(this._port(this._options.port),"express")),this._server=t)}})}}var m;exports.Methods=void 0,(m=exports.Methods||(exports.Methods={})).ALL="all",m.GET="get",m.POST="post",m.PUT="put",m.HEAD="head",m.DELETE="delete",m.OPTIONS="options",m.PATCH="patch";const I=e=>t=>(s,r)=>{A.routers(s).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:r})},S=I(exports.Methods.ALL),D=I(exports.Methods.DELETE),f=I(exports.Methods.GET),y=I(exports.Methods.HEAD),g=I(exports.Methods.OPTIONS),U=I(exports.Methods.PATCH),P=I(exports.Methods.POST),C=I(exports.Methods.PUT),L=e=>t=>(s,r,o)=>{A.parameters(s).push({callback:e,key:t,propertyKey:r,parameterIndex:o})},b=L(({app:e})=>e),M=L(({argument:e})=>e),F=(e,s)=>t.isUndefined(s)?e:e[s],q=L(({ctx:e,key:t})=>{var s,r;return F(null!==(r=null===(s=e.req)||void 0===s?void 0:s.body)&&void 0!==r?r:{},t)}),k=L(({ctx:e,key:t})=>{var s,r;return F(null!==(r=null===(s=e.req)||void 0===s?void 0:s.cookies)&&void 0!==r?r:{},t)}),w=L(({ctx:e})=>e),H=L(({ctx:e})=>{var s,r;return t.toArray(null!==(r=null===(s=e.req)||void 0===s?void 0:s.files)&&void 0!==r?r:[])}),j=L(({ctx:e,key:t})=>{var s,r;return F(null!==(r=null===(s=e.req)||void 0===s?void 0:s.headers)&&void 0!==r?r:{},t)}),G=L(({ctx:e,key:t})=>{var s,r;return F(null!==(r=null===(s=e.req)||void 0===s?void 0:s.params)&&void 0!==r?r:{},t)}),K=L(({ctx:e,key:t})=>{var s,r,o;return String(null!==(o=F(null!==(r=null===(s=e.req)||void 0===s?void 0:s.query)&&void 0!==r?r:{},t))&&void 0!==o?o:"")}),Q=L(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),V=(e,s)=>(r,o)=>{const i=t.uuidv7();w()(r,i,0),A.middleware(r).push({method:i,argument:s,propertyKey:o,callback:e})};class Y{static file(e,s){const r=t.uuidv7()+"."+s;return a.existsSync(l.join(e,r))?Y.file(e,s):r}static middleware(e,t){const s=Y._cache(t);s.upload||(s.upload=Y.multer(t)),s.upload(e.req,e.res,t=>{if(t)return e.destroy(t);e.next()})}static multer(e){const t=(null==e?void 0:e.path)||"./",s=E.diskStorage({destination:(e,s,r)=>r(null,t),filename:(e,s,r)=>R(this,void 0,void 0,function*(){return r(null,Y.file(t,s.originalname.split(".").pop()||"bin"))})}),r=(null==e?void 0:e.types)||[];return E({storage:s,fileFilter(e,t,s){return R(this,void 0,void 0,function*(){if(r.length){const e=t.originalname.split(".").pop()||"bin";return-1!=r.indexOf(t.mimetype)||-1!=r.indexOf(e)?s(null,!0):s(new Error("File not allowed"))}return s(null,!0)})},limits:{fileSize:1e6*((null==e?void 0:e.size)||5)}}).array((null==e?void 0:e.field)||"field",(null==e?void 0:e.count)||1)}}Y._cache=t.cache(),exports.All=S,exports.App=b,exports.Application=N,exports.Argument=M,exports.Body=q,exports.Cache=e=>(t,s,r)=>{V(t=>{t.cache(Object.assign({},e)),t.next()})(t,s,r)},exports.Context=v,exports.Controller=e=>t=>{A.path(t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=k,exports.Ctx=w,exports.Delete=D,exports.Extends=e=>t=>{A.extends(t).push(e)},exports.Files=H,exports.Get=f,exports.Head=y,exports.Headers=j,exports.Metadata=A,exports.Middleware=(e,s)=>(r,o)=>{let i=t.noop;if(t.isString(o)){const s=r;t.isFunction(s[e])&&(i=s[e])}A.middleware(r).push({method:e,argument:s,propertyKey:o,callback:i})},exports.Options=g,exports.Params=G,exports.Parse=e=>{const s=t.Validation.object(t.isFunction(e)?e(t.Validation):e).parse();return(e,t,r)=>{V(e=>R(void 0,void 0,void 0,function*(){const{error:t,message:r,value:o}=yield s.validateAsync(e.req.body);if(t)return e.validation(null!=r?r:t);e.req.body=o,e.next()}))(e,t,r)}},exports.Patch=U,exports.Post=P,exports.Put=C,exports.Query=K,exports.Session=Q,exports.Status=x,exports.Upload=Y,exports.Use=V,exports.methodFactory=I,exports.parameterFactory=L;
7
+ "use strict";var e=require("@andrewcaires/node"),t=require("@andrewcaires/utils.js"),s=require("compression"),r=require("cookie-parser"),o=require("cors"),i=require("express"),n=require("express-session"),a=require("fs"),p=require("http"),h=require("https"),u=require("net"),l=require("path"),d=require("request-ip"),E=require("@andrewcaires/decorator"),c=require("multer");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 T=_(p),O=_(h);function v(e,t,s,r){return new(s||(s=Promise))(function(o,i){function n(e){try{p(r.next(e))}catch(e){i(e)}}function a(e){try{p(r.throw(e))}catch(e){i(e)}}function p(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(n,a)}p((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class R{}R.CONTINUE=100,R.SWITCHING_PROTOCOLS=101,R.PROCESSING=102,R.OK=200,R.CREATED=201,R.ACCEPTED=202,R.NON_AUTHORITATIVE_INFORMATION=203,R.NO_CONTENT=204,R.RESET_CONTENT=205,R.PARTIAL_CONTENT=206,R.MULTI_STATUS=207,R.ALREADY_REPORTED=208,R.IM_USED=226,R.MULTIPLE_CHOICES=300,R.MOVED_PERMANENTLY=301,R.FOUND=302,R.SEE_OTHER=303,R.NOT_MODIFIED=304,R.USE_PROXY=305,R.TEMPORARY_REDIRECT=307,R.PERMANENT_REDIRECT=308,R.BAD_REQUEST=400,R.UNAUTHORIZED=401,R.PAYMENT_REQUIRED=402,R.FORBIDDEN=403,R.NOT_FOUND=404,R.METHOD_NOT_ALLOWED=405,R.NOT_ACCEPTABLE=406,R.PROXY_AUTHENTICATION_REQUIRED=407,R.REQUEST_TIMEOUT=408,R.CONFLICT=409,R.GONE=410,R.LENGTH_REQUIRED=411,R.PRECONDITION_FAILED=412,R.PAYLOAD_TOO_LARGE=413,R.REQUEST_URI_TOO_LONG=414,R.UNSUPPORTED_MEDIA_TYPE=415,R.REQUESTED_RANGE_NOT_SATISFIABLE=416,R.EXPECTATION_FAILED=417,R.IM_A_TEAPOT=418,R.MISDIRECTED_REQUEST=421,R.UNPROCESSABLE_ENTITY=422,R.LOCKED=423,R.FAILED_DEPENDENCY=424,R.UPGRADE_REQUIRED=426,R.PRECONDITION_REQUIRED=428,R.TOO_MANY_REQUESTS=429,R.REQUEST_HEADER_FIELDS_TOO_LARGE=431,R.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,R.UNAVAILABLE_FOR_LEGAL_REASONS=451,R.CLIENT_CLOSED_REQUEST=499,R.INTERNAL_SERVER_ERROR=500,R.NOT_IMPLEMENTED=501,R.BAD_GATEWAY=502,R.SERVICE_UNAVAILABLE=503,R.GATEWAY_TIMEOUT=504,R.HTTP_VERSION_NOT_SUPPORTED=505,R.VARIANT_ALSO_NEGOCIATES=506,R.INSUFFICIENT_STORAGE=507,R.LOOP_DETECTED=508,R.NOT_EXTENDED=510,R.NETWORK_AUTHENTICATION_REQUIRED=511,R.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class m{constructor(e,t,s,r,o){this.req=e,this.res=t,this.next=s,this.app=r,this.controller=o}cache(e){const s=[];e.private?s.push("private"):e.public&&s.push("public"),e.noStore&&(e.noCache=!0,s.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,s.push("no-cache")),e.noTransform&&s.push("no-transform"),e.proxyRevalidate&&s.push("proxy-revalidate"),e.mustRevalidate?s.push("must-revalidate"):e.noCache||(t.isNumber(e.staleIfError)&&s.push(`stale-if-error=${e.staleIfError}`),t.isNumber(e.staleWhileRevalidate)&&s.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),t.isNumber(e.maxAge)&&s.push(`max-age=${e.maxAge}`),t.isNumber(e.sMaxAge)&&s.push(`s-maxage=${e.sMaxAge}`),s.length&&(this.res.setHeader("Cache-Control",s.join(",")),e.noCache&&t.forEachKey({"Surrogate-Control":"no-store",Pragma:"no-cache",ETag:"false",Expires:"0"},(e,t)=>this.res.setHeader(t,e)))}close(e){this.res.headersSent||this.res.json(e||this.storage)}destroy(e){var t;e&&this.error(e.message),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){this.send(R.INTERNAL_SERVER_ERROR,{message:e})}file(e){this.res.headersSent||(this.status(R.OK),this.res.sendFile(l.resolve(e)))}getIp(){return d.getClientIp(this.req)||"0.0.0.0"}json(e){this.send(R.OK,e)}notFound(e){this.send(R.NOT_FOUND,{message:e})}prepare(e,t){this.res.headersSent||(this.status(e),this.storage=t)}send(e,t){this.res.headersSent||(this.status(e),this.res.json(t))}status(e){return e&&this.res.status(e),this.res.statusCode}success(e){this.send(R.OK,{message:e})}unauthorized(e){this.send(R.UNAUTHORIZED,{message:e})}validation(e){this.send(R.BAD_REQUEST,{message:e})}}class x{static extends(e){return E.metadata(x.EXTENDS,e,[])}static middleware(e){return E.metadata(x.MIDDLEWARE,e,[])}static parameters(e){return E.metadata(x.PARAMETERS,e,[])}static path(e,t){return E.metadata(x.PATH,e,t)}static routers(e){return E.metadata(x.ROUTERS,e,[])}}x.EXTENDS=Symbol("EXTENDS"),x.MIDDLEWARE=Symbol("MIDDLEWARE"),x.PARAMETERS=Symbol("PARAMETERS"),x.PATH=Symbol("PATH"),x.ROUTERS=Symbol("ROUTERS");class A extends t.EventEmitter{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,p){var h,u;super(),this._app=i(),this._options={},this._base=i.Router(),this._router=i.Router(),this._controllers=[],this._controllers.push(...e.map(e=>t.isConstructor(e)?new e:e)),this._options=t.merge({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=p?p:{}),this._app.use(i.json(this._options.json)),this._app.disable("x-powered-by"),this._app.use(i.urlencoded({extended:!0})),this._options.cross&&this._app.use(o(t.isObject(this._options.cross)?this._options.cross:{})),this._app.use(s(this._options.compression)),this._app.use(r(null===(h=this._options.cookie)||void 0===h?void 0:h.secret,this._options.cookie)),this._app.use(n(this._options.session)),this.init(),this._app.use(this._router);const d=(null===(u=this._options.path)||void 0===u?void 0:u.length)?this._options.path:"/";if(this._app.use(d,this._base),this._app.all(d+"/*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(R.NOT_FOUND),t.json({message:"INVALID API"}))}),this._options.public){const e=l.resolve(this._options.public);a.existsSync(e)&&this._app.use(i.static(e)),this._app.get("*path",(t,s)=>{a.existsSync(e)?s.sendFile(e+"/index.html"):this._options.notfound?s.redirect(this._options.notfound):(s.status(R.NOT_FOUND),s.json({message:'FILE "index.html" NOT FOUND'}))})}else this._app.all("*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(R.NOT_FOUND),t.json({message:"INVALID API"}))});this._controllers.forEach(e=>{const s=i.Router(),r=this._middlewares(e),o=x.parameters(e);this._extends(e).forEach(e=>{r.push(...this._middlewares(e)),o.push(...x.parameters(e))}),x.routers(e).forEach(i=>{const n=i.propertyKey.toString();if(t.isFunction(e[n])){if(!t.isFunction(s[i.method]))return this._notFound(i.method);const a=this._handles(e,n,r,o);return s[i.method](i.path,...a,(s,r,i)=>v(this,void 0,void 0,function*(){const a=new m(s,r,i,this,e),p=this._args(a,this._indexes(n,o)),h=yield e[n](...p);a.close(t.isArray(h)||t.isObject(h)?h:void 0)}))}this._notFound(`${e.name}.${n}`)}),this._extends(e).forEach(i=>{x.routers(i).forEach(i=>{const n=i.propertyKey.toString();if(t.isFunction(e[n])){if(!t.isFunction(s[i.method]))return this._notFound(i.method);const a=this._handles(e,n,r,o);return s[i.method](i.path,...a,(s,r,i)=>v(this,void 0,void 0,function*(){const a=new m(s,r,i,this,e),p=this._args(a,this._indexes(n,o)),h=yield e[n](...p);a.close(t.isArray(h)||t.isObject(h)?h:void 0)}))}this._notFound(`${e.constructor.name}.${n}`)})}),this._base.use(x.path(e,"/"),s)})}init(){}error(t,s){e.Log.error(t)}log(t,s){e.Log.info(t)}_args(e,t,s){const r=[],o={ctx:e,key:"",app:this,argument:s};return t.forEach(e=>{r[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),r}_handles(e,s,r,o){return r.filter(e=>e.propertyKey==s).map(s=>{if(s.callback!=t.noop){const r=this._indexes(s.method,o);return(o,i,n)=>v(this,void 0,void 0,function*(){const a=new m(o,i,n,this,e),p=t.isFunction(s.argument)?s.argument(a):s.argument,h=yield s.callback.call(e,...this._args(a,r,p));(t.isArray(h)||t.isObject(h))&&a.close(h)})}this._notFound(s.method)}).filter(t.isFunction)}_extends(e){return x.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[...x.middleware(e)].reverse()}_notFound(e){this.error(new Error(`Router "${e}" not found`),"express")}_port(e){return`Server listening on port ${e}`}listen(){return v(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=T.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=T.createServer((e,t)=>{const s=80==this._options.port?":80":"";t.writeHead(R.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${s}${e.url}`}),t.end()}),t=O.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new u.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(this._port(this._options.port),"express")),this._server=t}this._server||(443==this._options.port&&e.listen(80,()=>this.log(this._port(80),"express")),t.listen(this._options.port,()=>this.log(this._port(this._options.port),"express")),this._server=t)}})}}var N;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";const I=e=>t=>(s,r)=>{x.routers(s).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:r})},S=I(exports.Methods.ALL),D=I(exports.Methods.DELETE),g=I(exports.Methods.GET),y=I(exports.Methods.HEAD),f=I(exports.Methods.OPTIONS),L=I(exports.Methods.PATCH),C=I(exports.Methods.POST),U=I(exports.Methods.PUT),P=e=>t=>(s,r,o)=>{x.parameters(s).push({callback:e,key:t,propertyKey:r,parameterIndex:o})},F=P(({app:e})=>e),M=P(({argument:e})=>e),b=(e,s)=>t.isUndefined(s)?e:e[s],k=P(({ctx:e,key:t})=>{var s,r;return b(null!==(r=null===(s=e.req)||void 0===s?void 0:s.body)&&void 0!==r?r:{},t)}),q=P(({ctx:e,key:t})=>{var s,r;return b(null!==(r=null===(s=e.req)||void 0===s?void 0:s.cookies)&&void 0!==r?r:{},t)}),w=P(({ctx:e})=>e),j=P(({ctx:e})=>{var t,s;return null!==(s=null===(t=e.req)||void 0===t?void 0:t.files)&&void 0!==s?s:{}}),H=P(({ctx:e,key:t})=>{var s,r;return b(null!==(r=null===(s=e.req)||void 0===s?void 0:s.headers)&&void 0!==r?r:{},t)}),G=P(({ctx:e,key:t})=>{var s,r;return b(null!==(r=null===(s=e.req)||void 0===s?void 0:s.params)&&void 0!==r?r:{},t)}),K=P(({ctx:e,key:t})=>{var s,r,o;return String(null!==(o=b(null!==(r=null===(s=e.req)||void 0===s?void 0:s.query)&&void 0!==r?r:{},t))&&void 0!==o?o:"")}),Q=P(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),$=(e,s)=>(r,o)=>{const i=t.uuidv7();w()(r,i,0),x.middleware(r).push({method:i,argument:s,propertyKey:o,callback:e})},Y=e=>{var t;return(null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase())||"bin"},V=(e,t)=>{var s;return null!==(s=e[t])&&void 0!==s?s:null},W=(e,s)=>{const r=t.uuidv7()+"."+s;return a.existsSync(l.join(e,r))?W(e,s):r};exports.All=S,exports.App=F,exports.Application=A,exports.Argument=M,exports.Body=k,exports.Cache=e=>(t,s,r)=>{$(t=>{t.cache(Object.assign({},e)),t.next()})(t,s,r)},exports.Context=m,exports.Controller=e=>t=>{x.path(t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=q,exports.Ctx=w,exports.Delete=D,exports.Extends=e=>t=>{x.extends(t).push(e)},exports.Files=j,exports.Get=g,exports.Head=y,exports.Headers=H,exports.Metadata=x,exports.Middleware=(e,s)=>(r,o)=>{let i=t.noop;if(t.isString(o)){const s=r;t.isFunction(s[e])&&(i=s[e])}x.middleware(r).push({method:e,argument:s,propertyKey:o,callback:i})},exports.Options=f,exports.Params=G,exports.Parse=e=>{const s=t.Validation.object(t.isFunction(e)?e(t.Validation):e).parse();return(e,t,r)=>{$(e=>v(void 0,void 0,void 0,function*(){const{error:t,message:r,value:o}=yield s.validateAsync(e.req.body);if(t)return e.validation(null!=r?r:t);e.req.body=o,e.next()}))(e,t,r)}},exports.Patch=L,exports.Post=C,exports.Put=U,exports.Query=K,exports.Session=Q,exports.Status=R,exports.Upload=e=>{const s=t.merge({},e),r=c.diskStorage({destination(e,t,r){var o;const i=V(s,t.fieldname);r(null,null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./")},filename(e,t,r){var o;const i=V(s,t.fieldname);r(null,W(null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./",Y(t.originalname)))}}),o=Math.max(...Object.values(s).map(e=>{var t;return null!==(t=e.size)&&void 0!==t?t:0})),i=c({storage:r,fileFilter(e,t,r){var o;const i=V(s,t.fieldname);return i?((e,t)=>!t.length||t.includes(Y(e.originalname))||t.includes(e.mimetype.toLowerCase()))(t,null!==(o=null==i?void 0:i.types)&&void 0!==o?o:[])?void r(null,!0):r(new Error(`FILE TYPE NOT ALLOWED FOR "${t.fieldname}"`)):r(new Error(`UPLOAD FIELD "${t.fieldname}" IS NOT CONFIGURED`))},limits:{fileSize:1024*o*1024}}).fields(Object.keys(s).map(e=>{var t,r;return{name:e,maxCount:null!==(r=null===(t=s[e])||void 0===t?void 0:t.count)&&void 0!==r?r:1}}));return(e,t,s)=>{$(e=>{i(e.req,e.res,t=>{if(t)return"LIMIT_UNEXPECTED_FILE"==t.code&&(t.message=`FIELD "${t.field}" EXCEEDS MAX SIZE`),e.destroy(t);e.next()})})(e,t,s)}},exports.Use=$,exports.methodFactory=I,exports.parameterFactory=P;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @andrewcaires/express v1.2.8
2
+ * @andrewcaires/express v1.2.9
3
3
  * Decorators for express
4
4
  * (c) 2026 Andrew Caires
5
5
  * @license: MIT
@@ -135,7 +135,6 @@ interface IStore extends TypeAnyObject {
135
135
 
136
136
  interface IUpload {
137
137
  count?: number;
138
- field?: string;
139
138
  path?: string;
140
139
  size?: number;
141
140
  types?: Array<string>;
@@ -195,6 +194,8 @@ declare const Post: (path?: string) => MethodDecorator;
195
194
 
196
195
  declare const Put: (path?: string) => MethodDecorator;
197
196
 
197
+ declare const Upload: (options: TypeObject<IUpload>) => MethodDecorator;
198
+
198
199
  declare const Cache: (options: ICache) => MethodDecorator;
199
200
 
200
201
  declare const Middleware: (method: string, argument?: any) => MethodDecorator;
@@ -304,12 +305,5 @@ declare class Status {
304
305
  static readonly NETWORK_CONNECTION_TIMEOUT_ERROR = 599;
305
306
  }
306
307
 
307
- declare class Upload {
308
- private static _cache;
309
- private static file;
310
- static middleware(ctx: Context, options: IUpload): void;
311
- private static multer;
312
- }
313
-
314
308
  export { All, App, Application, Argument, Body, Cache, Context, Controller, Cookies, Ctx, Delete, Extends, Files, Get, Head, Headers, Metadata, Methods, Middleware, Options, Params, Parse, Patch, Post, Put, Query, Session, Status, Upload, Use, methodFactory, parameterFactory };
315
309
  export type { IArgs, ICache, ICookie, IFile, IHandler, IMiddlewares, IOptions, IParameter, IRouter, IStore, IUpload, IUse, ParseFunction };
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/express v1.2.8
2
+ * @andrewcaires/express v1.2.9
3
3
  * Decorators for express
4
4
  * (c) 2026 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- import{Log as t}from"@andrewcaires/node";import{isNumber as e,forEachKey as s,EventEmitter as r,isConstructor as i,merge as o,isObject as n,isFunction as a,noop as h,isArray as p,isUndefined as l,toArray as E,uuidv7 as u,isString as d,Validation as _,cache as c}from"@andrewcaires/utils.js";import T from"compression";import O from"cookie-parser";import m from"cors";import R,{Router as N,json as A,urlencoded as v}from"express";import I from"express-session";import{existsSync as f}from"fs";import*as S from"http";import*as D from"https";import{Server as g}from"net";import{resolve as y,join as x}from"path";import{getClientIp as U}from"request-ip";import{metadata as L}from"@andrewcaires/decorator";import P from"multer";function C(t,e,s,r){return new(s||(s=Promise))(function(i,o){function n(t){try{h(r.next(t))}catch(t){o(t)}}function a(t){try{h(r.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s(function(t){t(e)})).then(n,a)}h((r=r.apply(t,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class b{}b.CONTINUE=100,b.SWITCHING_PROTOCOLS=101,b.PROCESSING=102,b.OK=200,b.CREATED=201,b.ACCEPTED=202,b.NON_AUTHORITATIVE_INFORMATION=203,b.NO_CONTENT=204,b.RESET_CONTENT=205,b.PARTIAL_CONTENT=206,b.MULTI_STATUS=207,b.ALREADY_REPORTED=208,b.IM_USED=226,b.MULTIPLE_CHOICES=300,b.MOVED_PERMANENTLY=301,b.FOUND=302,b.SEE_OTHER=303,b.NOT_MODIFIED=304,b.USE_PROXY=305,b.TEMPORARY_REDIRECT=307,b.PERMANENT_REDIRECT=308,b.BAD_REQUEST=400,b.UNAUTHORIZED=401,b.PAYMENT_REQUIRED=402,b.FORBIDDEN=403,b.NOT_FOUND=404,b.METHOD_NOT_ALLOWED=405,b.NOT_ACCEPTABLE=406,b.PROXY_AUTHENTICATION_REQUIRED=407,b.REQUEST_TIMEOUT=408,b.CONFLICT=409,b.GONE=410,b.LENGTH_REQUIRED=411,b.PRECONDITION_FAILED=412,b.PAYLOAD_TOO_LARGE=413,b.REQUEST_URI_TOO_LONG=414,b.UNSUPPORTED_MEDIA_TYPE=415,b.REQUESTED_RANGE_NOT_SATISFIABLE=416,b.EXPECTATION_FAILED=417,b.IM_A_TEAPOT=418,b.MISDIRECTED_REQUEST=421,b.UNPROCESSABLE_ENTITY=422,b.LOCKED=423,b.FAILED_DEPENDENCY=424,b.UPGRADE_REQUIRED=426,b.PRECONDITION_REQUIRED=428,b.TOO_MANY_REQUESTS=429,b.REQUEST_HEADER_FIELDS_TOO_LARGE=431,b.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,b.UNAVAILABLE_FOR_LEGAL_REASONS=451,b.CLIENT_CLOSED_REQUEST=499,b.INTERNAL_SERVER_ERROR=500,b.NOT_IMPLEMENTED=501,b.BAD_GATEWAY=502,b.SERVICE_UNAVAILABLE=503,b.GATEWAY_TIMEOUT=504,b.HTTP_VERSION_NOT_SUPPORTED=505,b.VARIANT_ALSO_NEGOCIATES=506,b.INSUFFICIENT_STORAGE=507,b.LOOP_DETECTED=508,b.NOT_EXTENDED=510,b.NETWORK_AUTHENTICATION_REQUIRED=511,b.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class F{constructor(t,e,s,r,i){this.req=t,this.res=e,this.next=s,this.app=r,this.controller=i}cache(t){const r=[];t.private?r.push("private"):t.public&&r.push("public"),t.noStore&&(t.noCache=!0,r.push("no-store")),t.noCache&&(t.maxAge=0,delete t.sMaxAge,r.push("no-cache")),t.noTransform&&r.push("no-transform"),t.proxyRevalidate&&r.push("proxy-revalidate"),t.mustRevalidate?r.push("must-revalidate"):t.noCache||(e(t.staleIfError)&&r.push(`stale-if-error=${t.staleIfError}`),e(t.staleWhileRevalidate)&&r.push(`stale-while-revalidate=${t.staleWhileRevalidate}`)),e(t.maxAge)&&r.push(`max-age=${t.maxAge}`),e(t.sMaxAge)&&r.push(`s-maxage=${t.sMaxAge}`),r.length&&(this.res.setHeader("Cache-Control",r.join(",")),t.noCache&&s({"Surrogate-Control":"no-store",Pragma:"no-cache",ETag:"false",Expires:"0"},(t,e)=>this.res.setHeader(e,t)))}close(t){this.res.headersSent||this.res.json(t||this.storage)}destroy(t){var e;t&&this.error(t.message),null===(e=this.res.socket)||void 0===e||e.destroy(t)}error(t){this.send(b.INTERNAL_SERVER_ERROR,{message:t})}file(t){this.res.headersSent||(this.status(b.OK),this.res.sendFile(y(t)))}getIp(){return U(this.req)||"0.0.0.0"}json(t){this.send(b.OK,t)}notFound(t){this.send(b.NOT_FOUND,{message:t})}prepare(t,e){this.res.headersSent||(this.status(t),this.storage=e)}send(t,e){this.res.headersSent||(this.status(t),this.res.json(e))}status(t){return t&&this.res.status(t),this.res.statusCode}success(t){this.send(b.OK,{message:t})}unauthorized(t){this.send(b.UNAUTHORIZED,{message:t})}validation(t){this.send(b.BAD_REQUEST,{message:t})}}class M{static extends(t){return L(M.EXTENDS,t,[])}static middleware(t){return L(M.MIDDLEWARE,t,[])}static parameters(t){return L(M.PARAMETERS,t,[])}static path(t,e){return L(M.PATH,t,e)}static routers(t){return L(M.ROUTERS,t,[])}}M.EXTENDS=Symbol("EXTENDS"),M.MIDDLEWARE=Symbol("MIDDLEWARE"),M.PARAMETERS=Symbol("PARAMETERS"),M.PATH=Symbol("PATH"),M.ROUTERS=Symbol("ROUTERS");class k extends r{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,e){var s,r;super(),this._app=R(),this._options={},this._base=N(),this._router=N(),this._controllers=[],this._controllers.push(...t.map(t=>i(t)?new t:t)),this._options=o({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=e?e:{}),this._app.use(A(this._options.json)),this._app.disable("x-powered-by"),this._app.use(v({extended:!0})),this._options.cross&&this._app.use(m(n(this._options.cross)?this._options.cross:{})),this._app.use(T(this._options.compression)),this._app.use(O(null===(s=this._options.cookie)||void 0===s?void 0:s.secret,this._options.cookie)),this._app.use(I(this._options.session)),this.init(),this._app.use(this._router);const h=(null===(r=this._options.path)||void 0===r?void 0:r.length)?this._options.path:"/";if(this._app.use(h,this._base),this._app.all(h+"/*path",(t,e)=>{this._options.redirect?e.redirect(this._options.redirect):(e.status(b.NOT_FOUND),e.json({message:"INVALID API"}))}),this._options.public){const t=y(this._options.public);f(t)&&this._app.use(R.static(t)),this._app.get("*path",(e,s)=>{f(t)?s.sendFile(t+"/index.html"):this._options.notfound?s.redirect(this._options.notfound):(s.status(b.NOT_FOUND),s.json({message:'FILE "index.html" NOT FOUND'}))})}else this._app.all("*path",(t,e)=>{this._options.redirect?e.redirect(this._options.redirect):(e.status(b.NOT_FOUND),e.json({message:"INVALID API"}))});this._controllers.forEach(t=>{const e=N(),s=this._middlewares(t),r=M.parameters(t);this._extends(t).forEach(t=>{s.push(...this._middlewares(t)),r.push(...M.parameters(t))}),M.routers(t).forEach(i=>{const o=i.propertyKey.toString();if(a(t[o])){if(!a(e[i.method]))return this._notFound(i.method);const h=this._handles(t,o,s,r);return e[i.method](i.path,...h,(e,s,i)=>C(this,void 0,void 0,function*(){const a=new F(e,s,i,this,t),h=this._args(a,this._indexes(o,r)),l=yield t[o](...h);a.close(p(l)||n(l)?l:void 0)}))}this._notFound(`${t.name}.${o}`)}),this._extends(t).forEach(i=>{M.routers(i).forEach(i=>{const o=i.propertyKey.toString();if(a(t[o])){if(!a(e[i.method]))return this._notFound(i.method);const h=this._handles(t,o,s,r);return e[i.method](i.path,...h,(e,s,i)=>C(this,void 0,void 0,function*(){const a=new F(e,s,i,this,t),h=this._args(a,this._indexes(o,r)),l=yield t[o](...h);a.close(p(l)||n(l)?l:void 0)}))}this._notFound(`${t.constructor.name}.${o}`)})}),this._base.use(M.path(t,"/"),e)})}init(){}error(e,s){t.error(e)}log(e,s){t.info(e)}_args(t,e,s){const r=[],i={ctx:t,key:"",app:this,argument:s};return e.forEach(t=>{r[t.parameterIndex]=t.callback(Object.assign(Object.assign({},i),{key:t.key}))}),r}_handles(t,e,s,r){return s.filter(t=>t.propertyKey==e).map(e=>{if(e.callback!=h){const s=this._indexes(e.method,r);return(r,i,o)=>C(this,void 0,void 0,function*(){const h=new F(r,i,o,this,t),l=a(e.argument)?e.argument(h):e.argument,E=yield e.callback.call(t,...this._args(h,s,l));(p(E)||n(E))&&h.close(E)})}this._notFound(e.method)}).filter(a)}_extends(t){return M.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[...M.middleware(t)].reverse()}_notFound(t){this.error(new Error(`Router "${t}" not found`),"express")}_port(t){return`Server listening on port ${t}`}listen(){return C(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=S.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const t=S.createServer((t,e)=>{const s=80==this._options.port?":80":"";e.writeHead(b.MOVED_PERMANENTLY,{Location:`https://${t.headers.host}${s}${t.url}`}),e.end()}),e=D.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new g(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(this._port(this._options.port),"express")),this._server=e}this._server||(443==this._options.port&&t.listen(80,()=>this.log(this._port(80),"express")),e.listen(this._options.port,()=>this.log(this._port(this._options.port),"express")),this._server=e)}})}}const w=t=>e=>{M.path(e,(null==t?void 0:t.length)?t:"/")},H=t=>e=>{M.extends(e).push(t)};var G;!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"}(G||(G={}));const K=t=>e=>(s,r)=>{M.routers(s).push({method:t,path:(null==e?void 0:e.length)?e:"/",propertyKey:r})},Q=K(G.ALL),q=K(G.DELETE),Y=K(G.GET),j=K(G.HEAD),$=K(G.OPTIONS),V=K(G.PATCH),W=K(G.POST),B=K(G.PUT),X=t=>e=>(s,r,i)=>{M.parameters(s).push({callback:t,key:e,propertyKey:r,parameterIndex:i})},z=X(({app:t})=>t),Z=X(({argument:t})=>t),J=(t,e)=>l(e)?t:t[e],tt=X(({ctx:t,key:e})=>{var s,r;return J(null!==(r=null===(s=t.req)||void 0===s?void 0:s.body)&&void 0!==r?r:{},e)}),et=X(({ctx:t,key:e})=>{var s,r;return J(null!==(r=null===(s=t.req)||void 0===s?void 0:s.cookies)&&void 0!==r?r:{},e)}),st=X(({ctx:t})=>t),rt=X(({ctx:t})=>{var e,s;return E(null!==(s=null===(e=t.req)||void 0===e?void 0:e.files)&&void 0!==s?s:[])}),it=X(({ctx:t,key:e})=>{var s,r;return J(null!==(r=null===(s=t.req)||void 0===s?void 0:s.headers)&&void 0!==r?r:{},e)}),ot=X(({ctx:t,key:e})=>{var s,r;return J(null!==(r=null===(s=t.req)||void 0===s?void 0:s.params)&&void 0!==r?r:{},e)}),nt=X(({ctx:t,key:e})=>{var s,r,i;return String(null!==(i=J(null!==(r=null===(s=t.req)||void 0===s?void 0:s.query)&&void 0!==r?r:{},e))&&void 0!==i?i:"")}),at=X(({ctx:t})=>{var e;return null===(e=t.req)||void 0===e?void 0:e.session}),ht=(t,e)=>(s,r)=>{const i=u();st()(s,i,0),M.middleware(s).push({method:i,argument:e,propertyKey:r,callback:t})},pt=t=>(e,s,r)=>{ht(e=>{e.cache(Object.assign({},t)),e.next()})(e,s,r)},lt=(t,e)=>(s,r)=>{let i=h;if(d(r)){const e=s;a(e[t])&&(i=e[t])}M.middleware(s).push({method:t,argument:e,propertyKey:r,callback:i})},Et=t=>{const e=_.object(a(t)?t(_):t).parse();return(t,s,r)=>{ht(t=>C(void 0,void 0,void 0,function*(){const{error:s,message:r,value:i}=yield e.validateAsync(t.req.body);if(s)return t.validation(null!=r?r:s);t.req.body=i,t.next()}))(t,s,r)}};class ut{static file(t,e){const s=u()+"."+e;return f(x(t,s))?ut.file(t,e):s}static middleware(t,e){const s=ut._cache(e);s.upload||(s.upload=ut.multer(e)),s.upload(t.req,t.res,e=>{if(e)return t.destroy(e);t.next()})}static multer(t){const e=(null==t?void 0:t.path)||"./",s=P.diskStorage({destination:(t,s,r)=>r(null,e),filename:(t,s,r)=>C(this,void 0,void 0,function*(){return r(null,ut.file(e,s.originalname.split(".").pop()||"bin"))})}),r=(null==t?void 0:t.types)||[];return P({storage:s,fileFilter(t,e,s){return C(this,void 0,void 0,function*(){if(r.length){const t=e.originalname.split(".").pop()||"bin";return-1!=r.indexOf(e.mimetype)||-1!=r.indexOf(t)?s(null,!0):s(new Error("File not allowed"))}return s(null,!0)})},limits:{fileSize:1e6*((null==t?void 0:t.size)||5)}}).array((null==t?void 0:t.field)||"field",(null==t?void 0:t.count)||1)}}ut._cache=c();export{Q as All,z as App,k as Application,Z as Argument,tt as Body,pt as Cache,F as Context,w as Controller,et as Cookies,st as Ctx,q as Delete,H as Extends,rt as Files,Y as Get,j as Head,it as Headers,M as Metadata,G as Methods,lt as Middleware,$ as Options,ot as Params,Et as Parse,V as Patch,W as Post,B as Put,nt as Query,at as Session,b as Status,ut as Upload,ht as Use,K as methodFactory,X as parameterFactory};
7
+ import{Log as e}from"@andrewcaires/node";import{isNumber as t,forEachKey as s,EventEmitter as r,isConstructor as o,merge as i,isObject as n,isFunction as a,noop as h,isArray as l,isUndefined as p,uuidv7 as E,isString as u,Validation as d}from"@andrewcaires/utils.js";import _ from"compression";import c from"cookie-parser";import T from"cors";import O,{Router as m,json as v,urlencoded as R}from"express";import N from"express-session";import{existsSync as A}from"fs";import*as I from"http";import*as D from"https";import{Server as S}from"net";import{resolve as f,join as g}from"path";import{getClientIp as L}from"request-ip";import{metadata as y}from"@andrewcaires/decorator";import x,{diskStorage as U}from"multer";function C(e,t,s,r){return new(s||(s=Promise))(function(o,i){function n(e){try{h(r.next(e))}catch(e){i(e)}}function a(e){try{h(r.throw(e))}catch(e){i(e)}}function h(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(n,a)}h((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class P{}P.CONTINUE=100,P.SWITCHING_PROTOCOLS=101,P.PROCESSING=102,P.OK=200,P.CREATED=201,P.ACCEPTED=202,P.NON_AUTHORITATIVE_INFORMATION=203,P.NO_CONTENT=204,P.RESET_CONTENT=205,P.PARTIAL_CONTENT=206,P.MULTI_STATUS=207,P.ALREADY_REPORTED=208,P.IM_USED=226,P.MULTIPLE_CHOICES=300,P.MOVED_PERMANENTLY=301,P.FOUND=302,P.SEE_OTHER=303,P.NOT_MODIFIED=304,P.USE_PROXY=305,P.TEMPORARY_REDIRECT=307,P.PERMANENT_REDIRECT=308,P.BAD_REQUEST=400,P.UNAUTHORIZED=401,P.PAYMENT_REQUIRED=402,P.FORBIDDEN=403,P.NOT_FOUND=404,P.METHOD_NOT_ALLOWED=405,P.NOT_ACCEPTABLE=406,P.PROXY_AUTHENTICATION_REQUIRED=407,P.REQUEST_TIMEOUT=408,P.CONFLICT=409,P.GONE=410,P.LENGTH_REQUIRED=411,P.PRECONDITION_FAILED=412,P.PAYLOAD_TOO_LARGE=413,P.REQUEST_URI_TOO_LONG=414,P.UNSUPPORTED_MEDIA_TYPE=415,P.REQUESTED_RANGE_NOT_SATISFIABLE=416,P.EXPECTATION_FAILED=417,P.IM_A_TEAPOT=418,P.MISDIRECTED_REQUEST=421,P.UNPROCESSABLE_ENTITY=422,P.LOCKED=423,P.FAILED_DEPENDENCY=424,P.UPGRADE_REQUIRED=426,P.PRECONDITION_REQUIRED=428,P.TOO_MANY_REQUESTS=429,P.REQUEST_HEADER_FIELDS_TOO_LARGE=431,P.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,P.UNAVAILABLE_FOR_LEGAL_REASONS=451,P.CLIENT_CLOSED_REQUEST=499,P.INTERNAL_SERVER_ERROR=500,P.NOT_IMPLEMENTED=501,P.BAD_GATEWAY=502,P.SERVICE_UNAVAILABLE=503,P.GATEWAY_TIMEOUT=504,P.HTTP_VERSION_NOT_SUPPORTED=505,P.VARIANT_ALSO_NEGOCIATES=506,P.INSUFFICIENT_STORAGE=507,P.LOOP_DETECTED=508,P.NOT_EXTENDED=510,P.NETWORK_AUTHENTICATION_REQUIRED=511,P.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class F{constructor(e,t,s,r,o){this.req=e,this.res=t,this.next=s,this.app=r,this.controller=o}cache(e){const r=[];e.private?r.push("private"):e.public&&r.push("public"),e.noStore&&(e.noCache=!0,r.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,r.push("no-cache")),e.noTransform&&r.push("no-transform"),e.proxyRevalidate&&r.push("proxy-revalidate"),e.mustRevalidate?r.push("must-revalidate"):e.noCache||(t(e.staleIfError)&&r.push(`stale-if-error=${e.staleIfError}`),t(e.staleWhileRevalidate)&&r.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),t(e.maxAge)&&r.push(`max-age=${e.maxAge}`),t(e.sMaxAge)&&r.push(`s-maxage=${e.sMaxAge}`),r.length&&(this.res.setHeader("Cache-Control",r.join(",")),e.noCache&&s({"Surrogate-Control":"no-store",Pragma:"no-cache",ETag:"false",Expires:"0"},(e,t)=>this.res.setHeader(t,e)))}close(e){this.res.headersSent||this.res.json(e||this.storage)}destroy(e){var t;e&&this.error(e.message),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){this.send(P.INTERNAL_SERVER_ERROR,{message:e})}file(e){this.res.headersSent||(this.status(P.OK),this.res.sendFile(f(e)))}getIp(){return L(this.req)||"0.0.0.0"}json(e){this.send(P.OK,e)}notFound(e){this.send(P.NOT_FOUND,{message:e})}prepare(e,t){this.res.headersSent||(this.status(e),this.storage=t)}send(e,t){this.res.headersSent||(this.status(e),this.res.json(t))}status(e){return e&&this.res.status(e),this.res.statusCode}success(e){this.send(P.OK,{message:e})}unauthorized(e){this.send(P.UNAUTHORIZED,{message:e})}validation(e){this.send(P.BAD_REQUEST,{message:e})}}class M{static extends(e){return y(M.EXTENDS,e,[])}static middleware(e){return y(M.MIDDLEWARE,e,[])}static parameters(e){return y(M.PARAMETERS,e,[])}static path(e,t){return y(M.PATH,e,t)}static routers(e){return y(M.ROUTERS,e,[])}}M.EXTENDS=Symbol("EXTENDS"),M.MIDDLEWARE=Symbol("MIDDLEWARE"),M.PARAMETERS=Symbol("PARAMETERS"),M.PATH=Symbol("PATH"),M.ROUTERS=Symbol("ROUTERS");class b extends r{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,t){var s,r;super(),this._app=O(),this._options={},this._base=m(),this._router=m(),this._controllers=[],this._controllers.push(...e.map(e=>o(e)?new e:e)),this._options=i({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=t?t:{}),this._app.use(v(this._options.json)),this._app.disable("x-powered-by"),this._app.use(R({extended:!0})),this._options.cross&&this._app.use(T(n(this._options.cross)?this._options.cross:{})),this._app.use(_(this._options.compression)),this._app.use(c(null===(s=this._options.cookie)||void 0===s?void 0:s.secret,this._options.cookie)),this._app.use(N(this._options.session)),this.init(),this._app.use(this._router);const h=(null===(r=this._options.path)||void 0===r?void 0:r.length)?this._options.path:"/";if(this._app.use(h,this._base),this._app.all(h+"/*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(P.NOT_FOUND),t.json({message:"INVALID API"}))}),this._options.public){const e=f(this._options.public);A(e)&&this._app.use(O.static(e)),this._app.get("*path",(t,s)=>{A(e)?s.sendFile(e+"/index.html"):this._options.notfound?s.redirect(this._options.notfound):(s.status(P.NOT_FOUND),s.json({message:'FILE "index.html" NOT FOUND'}))})}else this._app.all("*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(P.NOT_FOUND),t.json({message:"INVALID API"}))});this._controllers.forEach(e=>{const t=m(),s=this._middlewares(e),r=M.parameters(e);this._extends(e).forEach(e=>{s.push(...this._middlewares(e)),r.push(...M.parameters(e))}),M.routers(e).forEach(o=>{const i=o.propertyKey.toString();if(a(e[i])){if(!a(t[o.method]))return this._notFound(o.method);const h=this._handles(e,i,s,r);return t[o.method](o.path,...h,(t,s,o)=>C(this,void 0,void 0,function*(){const a=new F(t,s,o,this,e),h=this._args(a,this._indexes(i,r)),p=yield e[i](...h);a.close(l(p)||n(p)?p:void 0)}))}this._notFound(`${e.name}.${i}`)}),this._extends(e).forEach(o=>{M.routers(o).forEach(o=>{const i=o.propertyKey.toString();if(a(e[i])){if(!a(t[o.method]))return this._notFound(o.method);const h=this._handles(e,i,s,r);return t[o.method](o.path,...h,(t,s,o)=>C(this,void 0,void 0,function*(){const a=new F(t,s,o,this,e),h=this._args(a,this._indexes(i,r)),p=yield e[i](...h);a.close(l(p)||n(p)?p:void 0)}))}this._notFound(`${e.constructor.name}.${i}`)})}),this._base.use(M.path(e,"/"),t)})}init(){}error(t,s){e.error(t)}log(t,s){e.info(t)}_args(e,t,s){const r=[],o={ctx:e,key:"",app:this,argument:s};return t.forEach(e=>{r[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),r}_handles(e,t,s,r){return s.filter(e=>e.propertyKey==t).map(t=>{if(t.callback!=h){const s=this._indexes(t.method,r);return(r,o,i)=>C(this,void 0,void 0,function*(){const h=new F(r,o,i,this,e),p=a(t.argument)?t.argument(h):t.argument,E=yield t.callback.call(e,...this._args(h,s,p));(l(E)||n(E))&&h.close(E)})}this._notFound(t.method)}).filter(a)}_extends(e){return M.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[...M.middleware(e)].reverse()}_notFound(e){this.error(new Error(`Router "${e}" not found`),"express")}_port(e){return`Server listening on port ${e}`}listen(){return C(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=I.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=I.createServer((e,t)=>{const s=80==this._options.port?":80":"";t.writeHead(P.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${s}${e.url}`}),t.end()}),t=D.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new S(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(this._port(this._options.port),"express")),this._server=t}this._server||(443==this._options.port&&e.listen(80,()=>this.log(this._port(80),"express")),t.listen(this._options.port,()=>this.log(this._port(this._options.port),"express")),this._server=t)}})}}const k=e=>t=>{M.path(t,(null==e?void 0:e.length)?e:"/")},w=e=>t=>{M.extends(t).push(e)};var H;!function(e){e.ALL="all",e.GET="get",e.POST="post",e.PUT="put",e.HEAD="head",e.DELETE="delete",e.OPTIONS="options",e.PATCH="patch"}(H||(H={}));const G=e=>t=>(s,r)=>{M.routers(s).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:r})},$=G(H.ALL),j=G(H.DELETE),K=G(H.GET),Q=G(H.HEAD),Y=G(H.OPTIONS),q=G(H.PATCH),W=G(H.POST),V=G(H.PUT),X=e=>t=>(s,r,o)=>{M.parameters(s).push({callback:e,key:t,propertyKey:r,parameterIndex:o})},B=X(({app:e})=>e),z=X(({argument:e})=>e),Z=(e,t)=>p(t)?e:e[t],J=X(({ctx:e,key:t})=>{var s,r;return Z(null!==(r=null===(s=e.req)||void 0===s?void 0:s.body)&&void 0!==r?r:{},t)}),ee=X(({ctx:e,key:t})=>{var s,r;return Z(null!==(r=null===(s=e.req)||void 0===s?void 0:s.cookies)&&void 0!==r?r:{},t)}),te=X(({ctx:e})=>e),se=X(({ctx:e})=>{var t,s;return null!==(s=null===(t=e.req)||void 0===t?void 0:t.files)&&void 0!==s?s:{}}),re=X(({ctx:e,key:t})=>{var s,r;return Z(null!==(r=null===(s=e.req)||void 0===s?void 0:s.headers)&&void 0!==r?r:{},t)}),oe=X(({ctx:e,key:t})=>{var s,r;return Z(null!==(r=null===(s=e.req)||void 0===s?void 0:s.params)&&void 0!==r?r:{},t)}),ie=X(({ctx:e,key:t})=>{var s,r,o;return String(null!==(o=Z(null!==(r=null===(s=e.req)||void 0===s?void 0:s.query)&&void 0!==r?r:{},t))&&void 0!==o?o:"")}),ne=X(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),ae=(e,t)=>(s,r)=>{const o=E();te()(s,o,0),M.middleware(s).push({method:o,argument:t,propertyKey:r,callback:e})},he=e=>{var t;return(null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase())||"bin"},le=(e,t)=>{var s;return null!==(s=e[t])&&void 0!==s?s:null},pe=(e,t)=>{const s=E()+"."+t;return A(g(e,s))?pe(e,t):s},Ee=e=>{const t=i({},e),s=U({destination(e,s,r){var o;const i=le(t,s.fieldname);r(null,null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./")},filename(e,s,r){var o;const i=le(t,s.fieldname);r(null,pe(null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./",he(s.originalname)))}}),r=Math.max(...Object.values(t).map(e=>{var t;return null!==(t=e.size)&&void 0!==t?t:0})),o=x({storage:s,fileFilter(e,s,r){var o;const i=le(t,s.fieldname);return i?((e,t)=>!t.length||t.includes(he(e.originalname))||t.includes(e.mimetype.toLowerCase()))(s,null!==(o=null==i?void 0:i.types)&&void 0!==o?o:[])?void r(null,!0):r(new Error(`FILE TYPE NOT ALLOWED FOR "${s.fieldname}"`)):r(new Error(`UPLOAD FIELD "${s.fieldname}" IS NOT CONFIGURED`))},limits:{fileSize:1024*r*1024}}).fields(Object.keys(t).map(e=>{var s,r;return{name:e,maxCount:null!==(r=null===(s=t[e])||void 0===s?void 0:s.count)&&void 0!==r?r:1}}));return(e,t,s)=>{ae(e=>{o(e.req,e.res,t=>{if(t)return"LIMIT_UNEXPECTED_FILE"==t.code&&(t.message=`FIELD "${t.field}" EXCEEDS MAX SIZE`),e.destroy(t);e.next()})})(e,t,s)}},ue=e=>(t,s,r)=>{ae(t=>{t.cache(Object.assign({},e)),t.next()})(t,s,r)},de=(e,t)=>(s,r)=>{let o=h;if(u(r)){const t=s;a(t[e])&&(o=t[e])}M.middleware(s).push({method:e,argument:t,propertyKey:r,callback:o})},_e=e=>{const t=d.object(a(e)?e(d):e).parse();return(e,s,r)=>{ae(e=>C(void 0,void 0,void 0,function*(){const{error:s,message:r,value:o}=yield t.validateAsync(e.req.body);if(s)return e.validation(null!=r?r:s);e.req.body=o,e.next()}))(e,s,r)}};export{$ as All,B as App,b as Application,z as Argument,J as Body,ue as Cache,F as Context,k as Controller,ee as Cookies,te as Ctx,j as Delete,w as Extends,se as Files,K as Get,Q as Head,re as Headers,M as Metadata,H as Methods,de as Middleware,Y as Options,oe as Params,_e as Parse,q as Patch,W as Post,V as Put,ie as Query,ne as Session,P as Status,Ee as Upload,ae as Use,G as methodFactory,X as parameterFactory};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/express",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "description": "Decorators for express",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs.js",