@andrewcaires/express 2.0.3 → 2.0.5
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 +2 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.esm.js +2 -2
- package/package.json +7 -7
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v2.0.
|
|
2
|
+
* @andrewcaires/express v2.0.5
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2026 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
"use strict";var e=require("@andrewcaires/decorator"),t=require("@andrewcaires/node"),r=require("@andrewcaires/utils.js"),s=require("compression"),o=require("cookie-parser"),i=require("cors"),n=require("express"),a=require("express-session"),l=require("fs"),u=require("http"),d=require("https"),p=require("net"),h=require("path"),E=require("request-ip"),c=require("multer");function _(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,Object.freeze(t)}var v=_(u),T=_(d);function O(e,t,r,s){return new(r||(r=Promise))(function(o,i){function n(e){try{l(s.next(e))}catch(e){i(e)}}function a(e){try{l(s.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(n,a)}l((s=s.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 x extends Error{constructor(e,t=R.INTERNAL_SERVER_ERROR){super(e),this.status=t}}class N extends x{constructor(e){super(e,R.UNPROCESSABLE_ENTITY)}}class A{constructor(e,t,r,s,o){this.req=e,this.res=t,this.next=r,this.app=s,this.controller=o}cache(e){const t=[];e.private?t.push("private"):e.public&&t.push("public"),e.noStore&&(e.noCache=!0,t.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,t.push("no-cache")),e.noTransform&&t.push("no-transform"),e.proxyRevalidate&&t.push("proxy-revalidate"),e.mustRevalidate?t.push("must-revalidate"):e.noCache||(r.isNumber(e.staleIfError)&&t.push(`stale-if-error=${e.staleIfError}`),r.isNumber(e.staleWhileRevalidate)&&t.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),r.isNumber(e.maxAge)&&t.push(`max-age=${e.maxAge}`),r.isNumber(e.sMaxAge)&&t.push(`s-maxage=${e.sMaxAge}`),t.length&&(this.res.setHeader("Cache-Control",t.join(",")),e.noCache&&r.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)}cookie(e,t,s){r.isUndefined(t)?this.res.clearCookie(e):this.res.cookie(e,t,null!=s?s:{})}destroy(e){var t;e&&this.error(e),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){var t;let r=null!==(t=e.toString())&&void 0!==t?t:"",s=R.INTERNAL_SERVER_ERROR;e instanceof Error&&(r=e.message,e instanceof x&&(s=e.status)),this.send(s,{error:r})}file(e){this.res.headersSent||(this.status(R.OK),this.res.sendFile(h.resolve(e)))}getBody(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.body)&&void 0!==t?t:{}}getBodyValue(e){var t;return null===(t=this.req.body)||void 0===t?void 0:t[e]}getCookie(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.cookies)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getCookies(){var e;return null!==(e=this.req.cookies)&&void 0!==e?e:{}}getHeader(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req)||void 0===t?void 0:t.header(e))||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getHeaders(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.headers)&&void 0!==t?t:{}}getIp(){return E.getClientIp(this.req)||"0.0.0.0"}getQuery(e){var t;return null===(t=this.req.query)||void 0===t?void 0:t[e]}getQueries(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.query)&&void 0!==t?t:{}}getParam(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.params)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getParams(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.params)&&void 0!==t?t:{}}getUrl(){return`${this.req.protocol}://${this.req.get("host")}${this.req.originalUrl}`}json(e){this.send(R.OK,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})}}class m{static extends(t){return e.metadata(m.EXTENDS,t,[])}static middleware(t){return e.metadata(m.MIDDLEWARE,t,[])}static parameters(t){return e.metadata(m.PARAMETERS,t,[])}static path(t,r){return e.metadata(m.PATH,t,r)}static routers(t){return e.metadata(m.ROUTERS,t,[])}}m.EXTENDS=Symbol("EXTENDS"),m.MIDDLEWARE=Symbol("MIDDLEWARE"),m.PARAMETERS=Symbol("PARAMETERS"),m.PATH=Symbol("PATH"),m.ROUTERS=Symbol("ROUTERS");class I extends r.EventEmitter{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,u){var d,p;super(),this._app=n(),this._options={},this._base=n.Router(),this._router=n.Router(),this._controllers=[],this._controllers.push(...t.map(t=>r.isConstructor(t)?new t(...e.getInjectionArgs(t)):t)),this._options=r.merge({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=u?u:{}),this._app.use(n.json(this._options.json)),this._app.disable("x-powered-by"),this._app.use(n.urlencoded({extended:!0})),this._options.cross&&this._app.use(i(r.isObject(this._options.cross)?this._options.cross:{})),this._app.use(s(this._options.compression)),this._app.use(o(null===(d=this._options.cookie)||void 0===d?void 0:d.secret,this._options.cookie)),this._app.use(a(this._options.session)),this.init(),this._app.use(this._router);const E=(null===(p=this._options.path)||void 0===p?void 0:p.length)?this._options.path:"/";if(this._app.use(E,this._base),this._app.all(E+"/*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=h.resolve(this._options.public);l.existsSync(e)&&this._app.use(n.static(e)),this._app.get("*path",(t,r)=>{l.existsSync(e)?r.sendFile(e+"/index.html"):this._options.notfound?r.redirect(this._options.notfound):(r.status(R.NOT_FOUND),r.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 t=n.Router(),s=this._middlewares(e),o=m.parameters(e);this._extends(e).forEach(e=>{s.push(...this._middlewares(e)),o.push(...m.parameters(e))}),m.routers(e).forEach(i=>{const n=i.propertyKey.toString();if(r.isFunction(e[n])){if(!r.isFunction(t[i.method]))return this._notFound(i.method);const a=this._handles(e,n,s,o);return t[i.method](i.path,...a,(t,s,i)=>O(this,void 0,void 0,function*(){const a=new A(t,s,i,this,e),l=this._args(a,this._indexes(n,o)),u=yield e[n](...l);a.close(r.isArray(u)||r.isObject(u)?u:void 0)}))}this._notFound(`${e.name}.${n}`)}),this._extends(e).forEach(i=>{m.routers(i).forEach(i=>{const n=i.propertyKey.toString();if(r.isFunction(e[n])){if(!r.isFunction(t[i.method]))return this._notFound(i.method);const a=this._handles(e,n,s,o);return t[i.method](i.path,...a,(t,s,i)=>O(this,void 0,void 0,function*(){const a=new A(t,s,i,this,e),l=this._args(a,this._indexes(n,o)),u=yield e[n](...l);a.close(r.isArray(u)||r.isObject(u)?u:void 0)}))}this._notFound(`${e.constructor.name}.${n}`)})}),this._base.use(m.path(e,"/"),t)})}init(){}error(e,r){t.Log.error(e)}log(e,r){t.Log.info(e)}_args(e,t,r){const s=[],o={ctx:e,key:"",app:this,argument:r};return t.forEach(e=>{s[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),s}_handles(e,t,s,o){return s.filter(e=>e.propertyKey==t).map(t=>{if(t.callback!=r.noop){const s=this._indexes(t.method,o);return(o,i,n)=>O(this,void 0,void 0,function*(){const a=new A(o,i,n,this,e),l=r.isFunction(t.argument)?t.argument(a):t.argument,u=yield t.callback.call(e,...this._args(a,s,l));(r.isArray(u)||r.isObject(u))&&a.close(u)})}this._notFound(t.method)}).filter(r.isFunction)}_extends(e){return m.extends(e).reduce((e,t)=>(e.push(t,...this._extends(t)),e),[]).filter((e,t,r)=>r.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 O(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=v.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=v.createServer((e,t)=>{const r=80==this._options.port?":80":"";t.writeHead(R.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${r}${e.url}`}),t.end()}),t=T.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new p.Server(r=>{r.once("data",s=>{r.pause(),r.unshift(s),(22===s[0]?t:e).emit("connection",r),process.nextTick(()=>r.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 S=e=>t=>(r,s,o)=>{m.parameters(r).push({callback:e,key:t,propertyKey:s,parameterIndex:o})},g=S(({app:e})=>e),D=S(({argument:e})=>e),y=(e,t)=>r.isUndefined(t)?e:e[t],f=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{},t)}),L=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.cookies)&&void 0!==s?s:{},t)}),C=S(({ctx:e})=>e),U=S(({ctx:e})=>{var t,r;return null!==(r=null===(t=e.req)||void 0===t?void 0:t.files)&&void 0!==r?r:{}}),P=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.headers)&&void 0!==s?s:{},t)}),b=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.params)&&void 0!==s?s:{},t)}),F=S(({ctx:e,key:t})=>{var r,s,o;return String(null!==(o=y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.query)&&void 0!==s?s:{},t))&&void 0!==o?o:"")}),M=S(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),q=(e,t)=>(s,o)=>{const i=r.uuidv7();C()(s,i,0),m.middleware(s).push({method:i,argument:t,propertyKey:o,callback:e})};var k;exports.Methods=void 0,(k=exports.Methods||(exports.Methods={})).ALL="all",k.GET="get",k.POST="post",k.PUT="put",k.HEAD="head",k.DELETE="delete",k.OPTIONS="options",k.PATCH="patch";const w=e=>t=>(r,s)=>{m.routers(r).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:s})},j=w(exports.Methods.ALL),H=w(exports.Methods.DELETE),V=w(exports.Methods.GET),$=w(exports.Methods.HEAD),G=w(exports.Methods.OPTIONS),Q=w(exports.Methods.PATCH),K=w(exports.Methods.POST),Y=w(exports.Methods.PUT),B=e=>{var t;return(null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase())||"bin"},W=(e,t)=>{var r;return null!==(r=e[t])&&void 0!==r?r:null},X=(e,t)=>{const s=r.uuidv7()+"."+t;return l.existsSync(h.join(e,s))?X(e,t):s};exports.All=j,exports.App=g,exports.Application=I,exports.Argument=D,exports.Body=f,exports.Cache=e=>(t,r,s)=>{q(t=>{t.cache(Object.assign({},e)),t.next()})(t,r,s)},exports.Context=A,exports.Controller=e=>t=>{m.path(t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=L,exports.Ctx=C,exports.Delete=H,exports.Extends=e=>t=>{m.extends(t).push(e)},exports.Files=U,exports.ForbiddenError=class extends x{constructor(e="Forbidden"){super(e,R.FORBIDDEN)}},exports.Get=V,exports.Head=$,exports.Headers=P,exports.InternalServerError=class extends x{constructor(e="Internal server error"){super(e,R.INTERNAL_SERVER_ERROR)}},exports.Metadata=m,exports.Middleware=(e,t)=>(s,o)=>{let i=r.noop;if(r.isString(o)){const t=s;r.isFunction(t[e])&&(i=t[e])}m.middleware(s).push({method:e,argument:t,propertyKey:o,callback:i})},exports.NotFoundError=class extends x{constructor(e="Record not found"){super(e,R.NOT_FOUND)}},exports.Options=G,exports.Params=b,exports.Patch=Q,exports.Post=K,exports.Put=Y,exports.Query=F,exports.ResponseError=x,exports.Session=M,exports.Status=R,exports.UnauthorizedError=class extends x{constructor(e="Unauthorized"){super(e,R.UNAUTHORIZED)}},exports.Upload=e=>{const t=r.merge({},e),s=c.diskStorage({destination(e,r,s){var o;const i=W(t,r.fieldname);s(null,null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./")},filename(e,r,s){var o;const i=W(t,r.fieldname);s(null,X(null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./",B(r.originalname)))}}),o=Math.max(...Object.values(t).map(e=>{var t;return null!==(t=e.size)&&void 0!==t?t:0})),i=c({storage:s,fileFilter(e,r,s){var o;const i=W(t,r.fieldname);return i?((e,t)=>!t.length||t.includes(B(e.originalname))||t.includes(e.mimetype.toLowerCase()))(r,null!==(o=null==i?void 0:i.types)&&void 0!==o?o:[])?void s(null,!0):s(new Error(`FILE TYPE NOT ALLOWED FOR "${r.fieldname}"`)):s(new Error(`UPLOAD FIELD "${r.fieldname}" IS NOT CONFIGURED`))},limits:{fileSize:1024*o*1024}}).fields(Object.keys(t).map(e=>{var r,s;return{name:e,maxCount:null!==(s=null===(r=t[e])||void 0===r?void 0:r.count)&&void 0!==s?s:1}}));return(e,t,r)=>{q(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,r)}},exports.Use=q,exports.Validate=e=>{const t=r.Validation.object(r.isFunction(e)?e(r.Validation):e).parse();return(e,r,s)=>{q(e=>O(void 0,void 0,void 0,function*(){var r,s;const{error:o,message:i,value:n}=yield t.validateAsync(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{});if(o)return e.error(new N(null!=i?i:o));e.req.body=n,e.next()}))(e,r,s)}},exports.ValidationError=N,exports.methodFactory=w,exports.parameterFactory=S;
|
|
7
|
+
"use strict";var e=require("@andrewcaires/decorator"),t=require("@andrewcaires/node"),r=require("@andrewcaires/utils.js"),s=require("compression"),o=require("cookie-parser"),i=require("cors"),n=require("express"),a=require("express-session"),l=require("fs"),u=require("http"),d=require("https"),p=require("net"),h=require("path"),E=require("request-ip"),c=require("multer");function _(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,Object.freeze(t)}var v=_(u),T=_(d);function O(e,t,r,s){return new(r||(r=Promise))(function(o,i){function n(e){try{l(s.next(e))}catch(e){i(e)}}function a(e){try{l(s.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(n,a)}l((s=s.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 x extends Error{constructor(e,t=R.INTERNAL_SERVER_ERROR){super(e),this.status=t}}class N extends x{constructor(e){super(e,R.UNPROCESSABLE_ENTITY)}}class A{constructor(e,t,r,s,o){this.req=e,this.res=t,this.next=r,this.app=s,this.controller=o}cache(e){const t=[];e.private?t.push("private"):e.public&&t.push("public"),e.noStore&&(e.noCache=!0,t.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,t.push("no-cache")),e.noTransform&&t.push("no-transform"),e.proxyRevalidate&&t.push("proxy-revalidate"),e.mustRevalidate?t.push("must-revalidate"):e.noCache||(r.isNumber(e.staleIfError)&&t.push(`stale-if-error=${e.staleIfError}`),r.isNumber(e.staleWhileRevalidate)&&t.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),r.isNumber(e.maxAge)&&t.push(`max-age=${e.maxAge}`),r.isNumber(e.sMaxAge)&&t.push(`s-maxage=${e.sMaxAge}`),t.length&&(this.res.setHeader("Cache-Control",t.join(",")),e.noCache&&r.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)}cookie(e,t,s){r.isUndefined(t)?this.res.clearCookie(e):this.res.cookie(e,t,null!=s?s:{})}destroy(e){var t;e&&this.error(e),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){var t;let r=null!==(t=e.toString())&&void 0!==t?t:"",s=R.INTERNAL_SERVER_ERROR;e instanceof Error&&(r=e.message,e instanceof x&&(s=e.status)),this.send(s,{error:r})}file(e){this.res.headersSent||(this.status(R.OK),this.res.sendFile(h.resolve(e)))}getBody(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.body)&&void 0!==t?t:{}}getBodyValue(e){var t;return null===(t=this.req.body)||void 0===t?void 0:t[e]}getCookie(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.cookies)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getCookies(){var e;return null!==(e=this.req.cookies)&&void 0!==e?e:{}}getFiles(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.files)&&void 0!==t?t:{}}getHeader(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req)||void 0===t?void 0:t.header(e))||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getHeaders(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.headers)&&void 0!==t?t:{}}getIp(){return E.getClientIp(this.req)||"0.0.0.0"}getQuery(e){var t;return null===(t=this.req.query)||void 0===t?void 0:t[e]}getQueries(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.query)&&void 0!==t?t:{}}getParam(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.params)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getParams(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.params)&&void 0!==t?t:{}}getUrl(){return`${this.req.protocol}://${this.req.get("host")}${this.req.originalUrl}`}json(e){this.send(R.OK,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})}}class m{static extends(t){return e.metadata(m.EXTENDS,t,[])}static middleware(t){return e.metadata(m.MIDDLEWARE,t,[])}static parameters(t){return e.metadata(m.PARAMETERS,t,[])}static path(t,r){return e.metadata(m.PATH,t,r)}static routers(t){return e.metadata(m.ROUTERS,t,[])}}m.EXTENDS=Symbol("EXTENDS"),m.MIDDLEWARE=Symbol("MIDDLEWARE"),m.PARAMETERS=Symbol("PARAMETERS"),m.PATH=Symbol("PATH"),m.ROUTERS=Symbol("ROUTERS");class I extends r.EventEmitter{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,u){var d,p;super(),this._app=n(),this._options={},this._base=n.Router(),this._router=n.Router(),this._controllers=[],this._controllers.push(...t.map(t=>r.isConstructor(t)?new t(...e.getInjectionArgs(t)):t)),this._options=r.merge({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=u?u:{}),this._app.use(n.json(this._options.json)),this._app.disable("x-powered-by"),this._app.use(n.urlencoded({extended:!0})),this._options.cross&&this._app.use(i(r.isObject(this._options.cross)?this._options.cross:{})),this._app.use(s(this._options.compression)),this._app.use(o(null===(d=this._options.cookie)||void 0===d?void 0:d.secret,this._options.cookie)),this._app.use(a(this._options.session)),this.init(),this._app.use(this._router);const E=(null===(p=this._options.path)||void 0===p?void 0:p.length)?this._options.path:"/";if(this._app.use(E,this._base),this._app.all(E+"/*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=h.resolve(this._options.public);l.existsSync(e)&&this._app.use(n.static(e)),this._app.get("*path",(t,r)=>{l.existsSync(e)?r.sendFile(e+"/index.html"):this._options.notfound?r.redirect(this._options.notfound):(r.status(R.NOT_FOUND),r.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 t=n.Router(),s=this._middlewares(e),o=m.parameters(e);this._extends(e).forEach(e=>{s.push(...this._middlewares(e)),o.push(...m.parameters(e))}),m.routers(e).forEach(i=>{const n=i.propertyKey.toString();if(r.isFunction(e[n])){if(!r.isFunction(t[i.method]))return this._notFound(i.method);const a=this._handles(e,n,s,o);return t[i.method](i.path,...a,(t,s,i)=>O(this,void 0,void 0,function*(){const a=new A(t,s,i,this,e),l=this._args(a,this._indexes(n,o)),u=yield e[n](...l);a.close(r.isArray(u)||r.isObject(u)?u:void 0)}))}this._notFound(`${e.name}.${n}`)}),this._extends(e).forEach(i=>{m.routers(i).forEach(i=>{const n=i.propertyKey.toString();if(r.isFunction(e[n])){if(!r.isFunction(t[i.method]))return this._notFound(i.method);const a=this._handles(e,n,s,o);return t[i.method](i.path,...a,(t,s,i)=>O(this,void 0,void 0,function*(){const a=new A(t,s,i,this,e),l=this._args(a,this._indexes(n,o)),u=yield e[n](...l);a.close(r.isArray(u)||r.isObject(u)?u:void 0)}))}this._notFound(`${e.constructor.name}.${n}`)})}),this._base.use(m.path(e,"/"),t)})}init(){}error(e,r){t.Log.error(e)}log(e,r){t.Log.info(e)}_args(e,t,r){const s=[],o={ctx:e,key:"",app:this,argument:r};return t.forEach(e=>{s[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),s}_handles(e,t,s,o){return s.filter(e=>e.propertyKey==t).map(t=>{if(t.callback!=r.noop){const s=this._indexes(t.method,o);return(o,i,n)=>O(this,void 0,void 0,function*(){const a=new A(o,i,n,this,e),l=r.isFunction(t.argument)?t.argument(a):t.argument,u=yield t.callback.call(e,...this._args(a,s,l));(r.isArray(u)||r.isObject(u))&&a.close(u)})}this._notFound(t.method)}).filter(r.isFunction)}_extends(e){return m.extends(e).reduce((e,t)=>(e.push(t,...this._extends(t)),e),[]).filter((e,t,r)=>r.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 O(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=v.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=v.createServer((e,t)=>{const r=80==this._options.port?":80":"";t.writeHead(R.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${r}${e.url}`}),t.end()}),t=T.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new p.Server(r=>{r.once("data",s=>{r.pause(),r.unshift(s),(22===s[0]?t:e).emit("connection",r),process.nextTick(()=>r.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 S=e=>t=>(r,s,o)=>{m.parameters(r).push({callback:e,key:t,propertyKey:s,parameterIndex:o})},g=S(({app:e})=>e),D=S(({argument:e})=>e),y=(e,t)=>r.isUndefined(t)?e:e[t],f=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{},t)}),L=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.cookies)&&void 0!==s?s:{},t)}),C=S(({ctx:e})=>e),U=S(({ctx:e})=>{var t,r;return null!==(r=null===(t=e.req)||void 0===t?void 0:t.files)&&void 0!==r?r:{}}),P=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.headers)&&void 0!==s?s:{},t)}),F=S(({ctx:e,key:t})=>{var r,s;return y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.params)&&void 0!==s?s:{},t)}),b=S(({ctx:e,key:t})=>{var r,s,o;return String(null!==(o=y(null!==(s=null===(r=e.req)||void 0===r?void 0:r.query)&&void 0!==s?s:{},t))&&void 0!==o?o:"")}),M=S(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),q=(e,t)=>(s,o)=>{const i=r.uuidv7();C()(s,i,0),m.middleware(s).push({method:i,argument:t,propertyKey:o,callback:e})};var k;exports.Methods=void 0,(k=exports.Methods||(exports.Methods={})).ALL="all",k.GET="get",k.POST="post",k.PUT="put",k.HEAD="head",k.DELETE="delete",k.OPTIONS="options",k.PATCH="patch";const w=e=>t=>(r,s)=>{m.routers(r).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:s})},j=w(exports.Methods.ALL),H=w(exports.Methods.DELETE),V=w(exports.Methods.GET),$=w(exports.Methods.HEAD),G=w(exports.Methods.OPTIONS),Q=w(exports.Methods.PATCH),K=w(exports.Methods.POST),Y=w(exports.Methods.PUT),B=e=>{var t;return(null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase())||"bin"},W=(e,t)=>{var r;return null!==(r=e[t])&&void 0!==r?r:null},X=(e,t)=>{const s=r.uuidv7()+"."+t;return l.existsSync(h.join(e,s))?X(e,t):s};exports.All=j,exports.App=g,exports.Application=I,exports.Argument=D,exports.Body=f,exports.Cache=e=>(t,r,s)=>{q(t=>{t.cache(Object.assign({},e)),t.next()})(t,r,s)},exports.Context=A,exports.Controller=e=>t=>{m.path(t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=L,exports.Ctx=C,exports.Delete=H,exports.Extends=e=>t=>{m.extends(t).push(e)},exports.Files=U,exports.ForbiddenError=class extends x{constructor(e="Forbidden"){super(e,R.FORBIDDEN)}},exports.Get=V,exports.Head=$,exports.Headers=P,exports.InternalServerError=class extends x{constructor(e="Internal server error"){super(e,R.INTERNAL_SERVER_ERROR)}},exports.Metadata=m,exports.Middleware=(e,t)=>(s,o)=>{let i=r.noop;if(r.isString(o)){const t=s;r.isFunction(t[e])&&(i=t[e])}m.middleware(s).push({method:e,argument:t,propertyKey:o,callback:i})},exports.NotFoundError=class extends x{constructor(e="Record not found"){super(e,R.NOT_FOUND)}},exports.Options=G,exports.Params=F,exports.Patch=Q,exports.Post=K,exports.Put=Y,exports.Query=b,exports.ResponseError=x,exports.Session=M,exports.Status=R,exports.UnauthorizedError=class extends x{constructor(e="Unauthorized"){super(e,R.UNAUTHORIZED)}},exports.Upload=e=>{const t=r.merge({},e),s=c.diskStorage({destination(e,r,s){var o;const i=W(t,r.fieldname);s(null,null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./")},filename(e,r,s){var o;const i=W(t,r.fieldname);s(null,X(null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./",B(r.originalname)))}}),o=Math.max(...Object.values(t).map(e=>{var t;return null!==(t=e.size)&&void 0!==t?t:0})),i=c({storage:s,fileFilter(e,r,s){var o;const i=W(t,r.fieldname);return i?((e,t)=>!t.length||t.includes(B(e.originalname))||t.includes(e.mimetype.toLowerCase()))(r,null!==(o=null==i?void 0:i.types)&&void 0!==o?o:[])?void s(null,!0):s(new Error(`FILE TYPE NOT ALLOWED FOR "${r.fieldname}"`)):s(new Error(`UPLOAD FIELD "${r.fieldname}" IS NOT CONFIGURED`))},limits:{fileSize:1024*o*1024}}).fields(Object.keys(t).map(e=>{var r,s;return{name:e,maxCount:null!==(s=null===(r=t[e])||void 0===r?void 0:r.count)&&void 0!==s?s:1}}));return(e,t,r)=>{q(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,r)}},exports.Use=q,exports.Validate=e=>{const t=r.Validation.object(r.isFunction(e)?e(r.Validation):e).parse();return(e,r,s)=>{q(e=>O(void 0,void 0,void 0,function*(){var r,s;const{error:o,message:i,value:n}=yield t.validateAsync(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{});if(o)return e.error(new N(null!=i?i:o));e.req.body=n,e.next()}))(e,r,s)}},exports.ValidationError=N,exports.methodFactory=w,exports.parameterFactory=S;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v2.0.
|
|
2
|
+
* @andrewcaires/express v2.0.5
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2026 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
import { TypeAnyObject, TypeAnyFunction, Validation,
|
|
7
|
+
import { TypeAnyObject, TypeObject, TypeAnyFunction, Validation, ValidationBase, EventEmitter, TypeAnyConstructor } from '@andrewcaires/utils.js';
|
|
8
8
|
import { Request, Response, NextFunction, CookieOptions, Express, Router } from 'express';
|
|
9
9
|
import * as http from 'http';
|
|
10
10
|
import * as https from 'https';
|
|
@@ -32,6 +32,7 @@ declare class Context<T = any, A = Application> {
|
|
|
32
32
|
getBodyValue<T = any>(key: string): T | undefined;
|
|
33
33
|
getCookie(key: string): string;
|
|
34
34
|
getCookies<T = TypeAnyObject>(): T;
|
|
35
|
+
getFiles<F = IFile, T = TypeObject<Array<F>>>(): T;
|
|
35
36
|
getHeader(key: string): string;
|
|
36
37
|
getHeaders<T = TypeAnyObject>(): T;
|
|
37
38
|
getIp(): string;
|
|
@@ -401,7 +402,7 @@ interface OpenAPIObject {
|
|
|
401
402
|
|
|
402
403
|
type ParseFunction = (_$: typeof Validation) => TypeObject<ValidationBase>;
|
|
403
404
|
|
|
404
|
-
declare class Application extends EventEmitter {
|
|
405
|
+
declare class Application<T extends TypeAnyObject = TypeAnyObject> extends EventEmitter<T> {
|
|
405
406
|
private readonly _app;
|
|
406
407
|
private _server?;
|
|
407
408
|
private readonly _options;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v2.0.
|
|
2
|
+
* @andrewcaires/express v2.0.5
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2026 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
import{metadata as e,getInjectionArgs as t}from"@andrewcaires/decorator";import{Log as r}from"@andrewcaires/node";import{isNumber as s,forEachKey as o,isUndefined as i,EventEmitter as n,isConstructor as a,merge as l,isObject as h,isFunction as u,noop as d,isArray as E,uuidv7 as p,isString as c,Validation as _}from"@andrewcaires/utils.js";import v from"compression";import T from"cookie-parser";import O from"cors";import R,{Router as m,json as N,urlencoded as A}from"express";import I from"express-session";import{existsSync as S}from"fs";import*as D from"http";import*as f from"https";import{Server as g}from"net";import{resolve as y,join as L}from"path";import{getClientIp as x}from"request-ip";import U,{diskStorage as C}from"multer";function P(e,t,r,s){return new(r||(r=Promise))(function(o,i){function n(e){try{l(s.next(e))}catch(e){i(e)}}function a(e){try{l(s.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(n,a)}l((s=s.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class F{}F.CONTINUE=100,F.SWITCHING_PROTOCOLS=101,F.PROCESSING=102,F.OK=200,F.CREATED=201,F.ACCEPTED=202,F.NON_AUTHORITATIVE_INFORMATION=203,F.NO_CONTENT=204,F.RESET_CONTENT=205,F.PARTIAL_CONTENT=206,F.MULTI_STATUS=207,F.ALREADY_REPORTED=208,F.IM_USED=226,F.MULTIPLE_CHOICES=300,F.MOVED_PERMANENTLY=301,F.FOUND=302,F.SEE_OTHER=303,F.NOT_MODIFIED=304,F.USE_PROXY=305,F.TEMPORARY_REDIRECT=307,F.PERMANENT_REDIRECT=308,F.BAD_REQUEST=400,F.UNAUTHORIZED=401,F.PAYMENT_REQUIRED=402,F.FORBIDDEN=403,F.NOT_FOUND=404,F.METHOD_NOT_ALLOWED=405,F.NOT_ACCEPTABLE=406,F.PROXY_AUTHENTICATION_REQUIRED=407,F.REQUEST_TIMEOUT=408,F.CONFLICT=409,F.GONE=410,F.LENGTH_REQUIRED=411,F.PRECONDITION_FAILED=412,F.PAYLOAD_TOO_LARGE=413,F.REQUEST_URI_TOO_LONG=414,F.UNSUPPORTED_MEDIA_TYPE=415,F.REQUESTED_RANGE_NOT_SATISFIABLE=416,F.EXPECTATION_FAILED=417,F.IM_A_TEAPOT=418,F.MISDIRECTED_REQUEST=421,F.UNPROCESSABLE_ENTITY=422,F.LOCKED=423,F.FAILED_DEPENDENCY=424,F.UPGRADE_REQUIRED=426,F.PRECONDITION_REQUIRED=428,F.TOO_MANY_REQUESTS=429,F.REQUEST_HEADER_FIELDS_TOO_LARGE=431,F.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,F.UNAVAILABLE_FOR_LEGAL_REASONS=451,F.CLIENT_CLOSED_REQUEST=499,F.INTERNAL_SERVER_ERROR=500,F.NOT_IMPLEMENTED=501,F.BAD_GATEWAY=502,F.SERVICE_UNAVAILABLE=503,F.GATEWAY_TIMEOUT=504,F.HTTP_VERSION_NOT_SUPPORTED=505,F.VARIANT_ALSO_NEGOCIATES=506,F.INSUFFICIENT_STORAGE=507,F.LOOP_DETECTED=508,F.NOT_EXTENDED=510,F.NETWORK_AUTHENTICATION_REQUIRED=511,F.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class b extends Error{constructor(e,t=F.INTERNAL_SERVER_ERROR){super(e),this.status=t}}class M extends b{constructor(e="Forbidden"){super(e,F.FORBIDDEN)}}class k extends b{constructor(e="Internal server error"){super(e,F.INTERNAL_SERVER_ERROR)}}class q extends b{constructor(e="Record not found"){super(e,F.NOT_FOUND)}}class w extends b{constructor(e="Unauthorized"){super(e,F.UNAUTHORIZED)}}class H extends b{constructor(e){super(e,F.UNPROCESSABLE_ENTITY)}}class ${constructor(e,t,r,s,o){this.req=e,this.res=t,this.next=r,this.app=s,this.controller=o}cache(e){const t=[];e.private?t.push("private"):e.public&&t.push("public"),e.noStore&&(e.noCache=!0,t.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,t.push("no-cache")),e.noTransform&&t.push("no-transform"),e.proxyRevalidate&&t.push("proxy-revalidate"),e.mustRevalidate?t.push("must-revalidate"):e.noCache||(s(e.staleIfError)&&t.push(`stale-if-error=${e.staleIfError}`),s(e.staleWhileRevalidate)&&t.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),s(e.maxAge)&&t.push(`max-age=${e.maxAge}`),s(e.sMaxAge)&&t.push(`s-maxage=${e.sMaxAge}`),t.length&&(this.res.setHeader("Cache-Control",t.join(",")),e.noCache&&o({"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)}cookie(e,t,r){i(t)?this.res.clearCookie(e):this.res.cookie(e,t,null!=r?r:{})}destroy(e){var t;e&&this.error(e),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){var t;let r=null!==(t=e.toString())&&void 0!==t?t:"",s=F.INTERNAL_SERVER_ERROR;e instanceof Error&&(r=e.message,e instanceof b&&(s=e.status)),this.send(s,{error:r})}file(e){this.res.headersSent||(this.status(F.OK),this.res.sendFile(y(e)))}getBody(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.body)&&void 0!==t?t:{}}getBodyValue(e){var t;return null===(t=this.req.body)||void 0===t?void 0:t[e]}getCookie(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.cookies)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getCookies(){var e;return null!==(e=this.req.cookies)&&void 0!==e?e:{}}getHeader(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req)||void 0===t?void 0:t.header(e))||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getHeaders(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.headers)&&void 0!==t?t:{}}getIp(){return x(this.req)||"0.0.0.0"}getQuery(e){var t;return null===(t=this.req.query)||void 0===t?void 0:t[e]}getQueries(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.query)&&void 0!==t?t:{}}getParam(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.params)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getParams(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.params)&&void 0!==t?t:{}}getUrl(){return`${this.req.protocol}://${this.req.get("host")}${this.req.originalUrl}`}json(e){this.send(F.OK,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(F.OK,{message:e})}}class G{static extends(t){return e(G.EXTENDS,t,[])}static middleware(t){return e(G.MIDDLEWARE,t,[])}static parameters(t){return e(G.PARAMETERS,t,[])}static path(t,r){return e(G.PATH,t,r)}static routers(t){return e(G.ROUTERS,t,[])}}G.EXTENDS=Symbol("EXTENDS"),G.MIDDLEWARE=Symbol("MIDDLEWARE"),G.PARAMETERS=Symbol("PARAMETERS"),G.PATH=Symbol("PATH"),G.ROUTERS=Symbol("ROUTERS");class Q extends n{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,r){var s,o;super(),this._app=R(),this._options={},this._base=m(),this._router=m(),this._controllers=[],this._controllers.push(...e.map(e=>a(e)?new e(...t(e)):e)),this._options=l({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=r?r:{}),this._app.use(N(this._options.json)),this._app.disable("x-powered-by"),this._app.use(A({extended:!0})),this._options.cross&&this._app.use(O(h(this._options.cross)?this._options.cross:{})),this._app.use(v(this._options.compression)),this._app.use(T(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 i=(null===(o=this._options.path)||void 0===o?void 0:o.length)?this._options.path:"/";if(this._app.use(i,this._base),this._app.all(i+"/*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(F.NOT_FOUND),t.json({message:"INVALID API"}))}),this._options.public){const e=y(this._options.public);S(e)&&this._app.use(R.static(e)),this._app.get("*path",(t,r)=>{S(e)?r.sendFile(e+"/index.html"):this._options.notfound?r.redirect(this._options.notfound):(r.status(F.NOT_FOUND),r.json({message:'FILE "index.html" NOT FOUND'}))})}else this._app.all("*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(F.NOT_FOUND),t.json({message:"INVALID API"}))});this._controllers.forEach(e=>{const t=m(),r=this._middlewares(e),s=G.parameters(e);this._extends(e).forEach(e=>{r.push(...this._middlewares(e)),s.push(...G.parameters(e))}),G.routers(e).forEach(o=>{const i=o.propertyKey.toString();if(u(e[i])){if(!u(t[o.method]))return this._notFound(o.method);const n=this._handles(e,i,r,s);return t[o.method](o.path,...n,(t,r,o)=>P(this,void 0,void 0,function*(){const n=new $(t,r,o,this,e),a=this._args(n,this._indexes(i,s)),l=yield e[i](...a);n.close(E(l)||h(l)?l:void 0)}))}this._notFound(`${e.name}.${i}`)}),this._extends(e).forEach(o=>{G.routers(o).forEach(o=>{const i=o.propertyKey.toString();if(u(e[i])){if(!u(t[o.method]))return this._notFound(o.method);const n=this._handles(e,i,r,s);return t[o.method](o.path,...n,(t,r,o)=>P(this,void 0,void 0,function*(){const n=new $(t,r,o,this,e),a=this._args(n,this._indexes(i,s)),l=yield e[i](...a);n.close(E(l)||h(l)?l:void 0)}))}this._notFound(`${e.constructor.name}.${i}`)})}),this._base.use(G.path(e,"/"),t)})}init(){}error(e,t){r.error(e)}log(e,t){r.info(e)}_args(e,t,r){const s=[],o={ctx:e,key:"",app:this,argument:r};return t.forEach(e=>{s[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),s}_handles(e,t,r,s){return r.filter(e=>e.propertyKey==t).map(t=>{if(t.callback!=d){const r=this._indexes(t.method,s);return(s,o,i)=>P(this,void 0,void 0,function*(){const n=new $(s,o,i,this,e),a=u(t.argument)?t.argument(n):t.argument,l=yield t.callback.call(e,...this._args(n,r,a));(E(l)||h(l))&&n.close(l)})}this._notFound(t.method)}).filter(u)}_extends(e){return G.extends(e).reduce((e,t)=>(e.push(t,...this._extends(t)),e),[]).filter((e,t,r)=>r.indexOf(e)===t)}_indexes(e,t){return t.filter(t=>t.propertyKey==e)}_middlewares(e){return[...G.middleware(e)].reverse()}_notFound(e){this.error(new Error(`Router "${e}" not found`),"express")}_port(e){return`Server listening on port ${e}`}listen(){return P(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=D.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=D.createServer((e,t)=>{const r=80==this._options.port?":80":"";t.writeHead(F.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${r}${e.url}`}),t.end()}),t=f.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new g(r=>{r.once("data",s=>{r.pause(),r.unshift(s),(22===s[0]?t:e).emit("connection",r),process.nextTick(()=>r.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 Y=e=>t=>{G.path(t,(null==e?void 0:e.length)?e:"/")},j=e=>t=>{G.extends(t).push(e)},K=e=>t=>(r,s,o)=>{G.parameters(r).push({callback:e,key:t,propertyKey:s,parameterIndex:o})},V=K(({app:e})=>e),W=K(({argument:e})=>e),B=(e,t)=>i(t)?e:e[t],X=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{},t)}),z=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.cookies)&&void 0!==s?s:{},t)}),Z=K(({ctx:e})=>e),J=K(({ctx:e})=>{var t,r;return null!==(r=null===(t=e.req)||void 0===t?void 0:t.files)&&void 0!==r?r:{}}),ee=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.headers)&&void 0!==s?s:{},t)}),te=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.params)&&void 0!==s?s:{},t)}),re=K(({ctx:e,key:t})=>{var r,s,o;return String(null!==(o=B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.query)&&void 0!==s?s:{},t))&&void 0!==o?o:"")}),se=K(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),oe=(e,t)=>(r,s)=>{const o=p();Z()(r,o,0),G.middleware(r).push({method:o,argument:t,propertyKey:s,callback:e})},ie=e=>(t,r,s)=>{oe(t=>{t.cache(Object.assign({},e)),t.next()})(t,r,s)};var ne;!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"}(ne||(ne={}));const ae=e=>t=>(r,s)=>{G.routers(r).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:s})},le=ae(ne.ALL),he=ae(ne.DELETE),ue=ae(ne.GET),de=ae(ne.HEAD),Ee=ae(ne.OPTIONS),pe=ae(ne.PATCH),ce=ae(ne.POST),_e=ae(ne.PUT),ve=(e,t)=>(r,s)=>{let o=d;if(c(s)){const t=r;u(t[e])&&(o=t[e])}G.middleware(r).push({method:e,argument:t,propertyKey:s,callback:o})},Te=e=>{var t;return(null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase())||"bin"},Oe=(e,t)=>{var r;return null!==(r=e[t])&&void 0!==r?r:null},Re=(e,t)=>{const r=p()+"."+t;return S(L(e,r))?Re(e,t):r},me=e=>{const t=l({},e),r=C({destination(e,r,s){var o;const i=Oe(t,r.fieldname);s(null,null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./")},filename(e,r,s){var o;const i=Oe(t,r.fieldname);s(null,Re(null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./",Te(r.originalname)))}}),s=Math.max(...Object.values(t).map(e=>{var t;return null!==(t=e.size)&&void 0!==t?t:0})),o=U({storage:r,fileFilter(e,r,s){var o;const i=Oe(t,r.fieldname);return i?((e,t)=>!t.length||t.includes(Te(e.originalname))||t.includes(e.mimetype.toLowerCase()))(r,null!==(o=null==i?void 0:i.types)&&void 0!==o?o:[])?void s(null,!0):s(new Error(`FILE TYPE NOT ALLOWED FOR "${r.fieldname}"`)):s(new Error(`UPLOAD FIELD "${r.fieldname}" IS NOT CONFIGURED`))},limits:{fileSize:1024*s*1024}}).fields(Object.keys(t).map(e=>{var r,s;return{name:e,maxCount:null!==(s=null===(r=t[e])||void 0===r?void 0:r.count)&&void 0!==s?s:1}}));return(e,t,r)=>{oe(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,r)}},Ne=e=>{const t=_.object(u(e)?e(_):e).parse();return(e,r,s)=>{oe(e=>P(void 0,void 0,void 0,function*(){var r,s;const{error:o,message:i,value:n}=yield t.validateAsync(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{});if(o)return e.error(new H(null!=i?i:o));e.req.body=n,e.next()}))(e,r,s)}};export{le as All,V as App,Q as Application,W as Argument,X as Body,ie as Cache,$ as Context,Y as Controller,z as Cookies,Z as Ctx,he as Delete,j as Extends,J as Files,M as ForbiddenError,ue as Get,de as Head,ee as Headers,k as InternalServerError,G as Metadata,ne as Methods,ve as Middleware,q as NotFoundError,Ee as Options,te as Params,pe as Patch,ce as Post,_e as Put,re as Query,b as ResponseError,se as Session,F as Status,w as UnauthorizedError,me as Upload,oe as Use,Ne as Validate,H as ValidationError,ae as methodFactory,K as parameterFactory};
|
|
7
|
+
import{metadata as e,getInjectionArgs as t}from"@andrewcaires/decorator";import{Log as r}from"@andrewcaires/node";import{isNumber as s,forEachKey as o,isUndefined as i,EventEmitter as n,isConstructor as a,merge as l,isObject as u,isFunction as h,noop as d,isArray as E,uuidv7 as p,isString as c,Validation as _}from"@andrewcaires/utils.js";import v from"compression";import T from"cookie-parser";import O from"cors";import R,{Router as m,json as N,urlencoded as A}from"express";import I from"express-session";import{existsSync as S}from"fs";import*as D from"http";import*as f from"https";import{Server as g}from"net";import{resolve as y,join as L}from"path";import{getClientIp as x}from"request-ip";import U,{diskStorage as C}from"multer";function P(e,t,r,s){return new(r||(r=Promise))(function(o,i){function n(e){try{l(s.next(e))}catch(e){i(e)}}function a(e){try{l(s.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(n,a)}l((s=s.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class F{}F.CONTINUE=100,F.SWITCHING_PROTOCOLS=101,F.PROCESSING=102,F.OK=200,F.CREATED=201,F.ACCEPTED=202,F.NON_AUTHORITATIVE_INFORMATION=203,F.NO_CONTENT=204,F.RESET_CONTENT=205,F.PARTIAL_CONTENT=206,F.MULTI_STATUS=207,F.ALREADY_REPORTED=208,F.IM_USED=226,F.MULTIPLE_CHOICES=300,F.MOVED_PERMANENTLY=301,F.FOUND=302,F.SEE_OTHER=303,F.NOT_MODIFIED=304,F.USE_PROXY=305,F.TEMPORARY_REDIRECT=307,F.PERMANENT_REDIRECT=308,F.BAD_REQUEST=400,F.UNAUTHORIZED=401,F.PAYMENT_REQUIRED=402,F.FORBIDDEN=403,F.NOT_FOUND=404,F.METHOD_NOT_ALLOWED=405,F.NOT_ACCEPTABLE=406,F.PROXY_AUTHENTICATION_REQUIRED=407,F.REQUEST_TIMEOUT=408,F.CONFLICT=409,F.GONE=410,F.LENGTH_REQUIRED=411,F.PRECONDITION_FAILED=412,F.PAYLOAD_TOO_LARGE=413,F.REQUEST_URI_TOO_LONG=414,F.UNSUPPORTED_MEDIA_TYPE=415,F.REQUESTED_RANGE_NOT_SATISFIABLE=416,F.EXPECTATION_FAILED=417,F.IM_A_TEAPOT=418,F.MISDIRECTED_REQUEST=421,F.UNPROCESSABLE_ENTITY=422,F.LOCKED=423,F.FAILED_DEPENDENCY=424,F.UPGRADE_REQUIRED=426,F.PRECONDITION_REQUIRED=428,F.TOO_MANY_REQUESTS=429,F.REQUEST_HEADER_FIELDS_TOO_LARGE=431,F.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,F.UNAVAILABLE_FOR_LEGAL_REASONS=451,F.CLIENT_CLOSED_REQUEST=499,F.INTERNAL_SERVER_ERROR=500,F.NOT_IMPLEMENTED=501,F.BAD_GATEWAY=502,F.SERVICE_UNAVAILABLE=503,F.GATEWAY_TIMEOUT=504,F.HTTP_VERSION_NOT_SUPPORTED=505,F.VARIANT_ALSO_NEGOCIATES=506,F.INSUFFICIENT_STORAGE=507,F.LOOP_DETECTED=508,F.NOT_EXTENDED=510,F.NETWORK_AUTHENTICATION_REQUIRED=511,F.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class b extends Error{constructor(e,t=F.INTERNAL_SERVER_ERROR){super(e),this.status=t}}class M extends b{constructor(e="Forbidden"){super(e,F.FORBIDDEN)}}class k extends b{constructor(e="Internal server error"){super(e,F.INTERNAL_SERVER_ERROR)}}class q extends b{constructor(e="Record not found"){super(e,F.NOT_FOUND)}}class w extends b{constructor(e="Unauthorized"){super(e,F.UNAUTHORIZED)}}class H extends b{constructor(e){super(e,F.UNPROCESSABLE_ENTITY)}}class ${constructor(e,t,r,s,o){this.req=e,this.res=t,this.next=r,this.app=s,this.controller=o}cache(e){const t=[];e.private?t.push("private"):e.public&&t.push("public"),e.noStore&&(e.noCache=!0,t.push("no-store")),e.noCache&&(e.maxAge=0,delete e.sMaxAge,t.push("no-cache")),e.noTransform&&t.push("no-transform"),e.proxyRevalidate&&t.push("proxy-revalidate"),e.mustRevalidate?t.push("must-revalidate"):e.noCache||(s(e.staleIfError)&&t.push(`stale-if-error=${e.staleIfError}`),s(e.staleWhileRevalidate)&&t.push(`stale-while-revalidate=${e.staleWhileRevalidate}`)),s(e.maxAge)&&t.push(`max-age=${e.maxAge}`),s(e.sMaxAge)&&t.push(`s-maxage=${e.sMaxAge}`),t.length&&(this.res.setHeader("Cache-Control",t.join(",")),e.noCache&&o({"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)}cookie(e,t,r){i(t)?this.res.clearCookie(e):this.res.cookie(e,t,null!=r?r:{})}destroy(e){var t;e&&this.error(e),null===(t=this.res.socket)||void 0===t||t.destroy(e)}error(e){var t;let r=null!==(t=e.toString())&&void 0!==t?t:"",s=F.INTERNAL_SERVER_ERROR;e instanceof Error&&(r=e.message,e instanceof b&&(s=e.status)),this.send(s,{error:r})}file(e){this.res.headersSent||(this.status(F.OK),this.res.sendFile(y(e)))}getBody(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.body)&&void 0!==t?t:{}}getBodyValue(e){var t;return null===(t=this.req.body)||void 0===t?void 0:t[e]}getCookie(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.cookies)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getCookies(){var e;return null!==(e=this.req.cookies)&&void 0!==e?e:{}}getFiles(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.files)&&void 0!==t?t:{}}getHeader(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req)||void 0===t?void 0:t.header(e))||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getHeaders(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.headers)&&void 0!==t?t:{}}getIp(){return x(this.req)||"0.0.0.0"}getQuery(e){var t;return null===(t=this.req.query)||void 0===t?void 0:t[e]}getQueries(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.query)&&void 0!==t?t:{}}getParam(e){var t,r,s;return null!==(s=null===(r=null===(t=this.req.params)||void 0===t?void 0:t[e])||void 0===r?void 0:r.toString())&&void 0!==s?s:""}getParams(){var e,t;return null!==(t=null===(e=this.req)||void 0===e?void 0:e.params)&&void 0!==t?t:{}}getUrl(){return`${this.req.protocol}://${this.req.get("host")}${this.req.originalUrl}`}json(e){this.send(F.OK,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(F.OK,{message:e})}}class G{static extends(t){return e(G.EXTENDS,t,[])}static middleware(t){return e(G.MIDDLEWARE,t,[])}static parameters(t){return e(G.PARAMETERS,t,[])}static path(t,r){return e(G.PATH,t,r)}static routers(t){return e(G.ROUTERS,t,[])}}G.EXTENDS=Symbol("EXTENDS"),G.MIDDLEWARE=Symbol("MIDDLEWARE"),G.PARAMETERS=Symbol("PARAMETERS"),G.PATH=Symbol("PATH"),G.ROUTERS=Symbol("ROUTERS");class Q extends n{get controllers(){return this._controllers}get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,r){var s,o;super(),this._app=R(),this._options={},this._base=m(),this._router=m(),this._controllers=[],this._controllers.push(...e.map(e=>a(e)?new e(...t(e)):e)),this._options=l({},{cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},null!=r?r:{}),this._app.use(N(this._options.json)),this._app.disable("x-powered-by"),this._app.use(A({extended:!0})),this._options.cross&&this._app.use(O(u(this._options.cross)?this._options.cross:{})),this._app.use(v(this._options.compression)),this._app.use(T(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 i=(null===(o=this._options.path)||void 0===o?void 0:o.length)?this._options.path:"/";if(this._app.use(i,this._base),this._app.all(i+"/*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(F.NOT_FOUND),t.json({message:"INVALID API"}))}),this._options.public){const e=y(this._options.public);S(e)&&this._app.use(R.static(e)),this._app.get("*path",(t,r)=>{S(e)?r.sendFile(e+"/index.html"):this._options.notfound?r.redirect(this._options.notfound):(r.status(F.NOT_FOUND),r.json({message:'FILE "index.html" NOT FOUND'}))})}else this._app.all("*path",(e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(F.NOT_FOUND),t.json({message:"INVALID API"}))});this._controllers.forEach(e=>{const t=m(),r=this._middlewares(e),s=G.parameters(e);this._extends(e).forEach(e=>{r.push(...this._middlewares(e)),s.push(...G.parameters(e))}),G.routers(e).forEach(o=>{const i=o.propertyKey.toString();if(h(e[i])){if(!h(t[o.method]))return this._notFound(o.method);const n=this._handles(e,i,r,s);return t[o.method](o.path,...n,(t,r,o)=>P(this,void 0,void 0,function*(){const n=new $(t,r,o,this,e),a=this._args(n,this._indexes(i,s)),l=yield e[i](...a);n.close(E(l)||u(l)?l:void 0)}))}this._notFound(`${e.name}.${i}`)}),this._extends(e).forEach(o=>{G.routers(o).forEach(o=>{const i=o.propertyKey.toString();if(h(e[i])){if(!h(t[o.method]))return this._notFound(o.method);const n=this._handles(e,i,r,s);return t[o.method](o.path,...n,(t,r,o)=>P(this,void 0,void 0,function*(){const n=new $(t,r,o,this,e),a=this._args(n,this._indexes(i,s)),l=yield e[i](...a);n.close(E(l)||u(l)?l:void 0)}))}this._notFound(`${e.constructor.name}.${i}`)})}),this._base.use(G.path(e,"/"),t)})}init(){}error(e,t){r.error(e)}log(e,t){r.info(e)}_args(e,t,r){const s=[],o={ctx:e,key:"",app:this,argument:r};return t.forEach(e=>{s[e.parameterIndex]=e.callback(Object.assign(Object.assign({},o),{key:e.key}))}),s}_handles(e,t,r,s){return r.filter(e=>e.propertyKey==t).map(t=>{if(t.callback!=d){const r=this._indexes(t.method,s);return(s,o,i)=>P(this,void 0,void 0,function*(){const n=new $(s,o,i,this,e),a=h(t.argument)?t.argument(n):t.argument,l=yield t.callback.call(e,...this._args(n,r,a));(E(l)||u(l))&&n.close(l)})}this._notFound(t.method)}).filter(h)}_extends(e){return G.extends(e).reduce((e,t)=>(e.push(t,...this._extends(t)),e),[]).filter((e,t,r)=>r.indexOf(e)===t)}_indexes(e,t){return t.filter(t=>t.propertyKey==e)}_middlewares(e){return[...G.middleware(e)].reverse()}_notFound(e){this.error(new Error(`Router "${e}" not found`),"express")}_port(e){return`Server listening on port ${e}`}listen(){return P(this,void 0,void 0,function*(){if(this._options.cert&&this._options.key||(this._server=D.createServer(this._app),this._server.listen(this._options.port,()=>this.log(this._port(this._options.port),"express"))),!this._server){const e=D.createServer((e,t)=>{const r=80==this._options.port?":80":"";t.writeHead(F.MOVED_PERMANENTLY,{Location:`https://${e.headers.host}${r}${e.url}`}),t.end()}),t=f.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new g(r=>{r.once("data",s=>{r.pause(),r.unshift(s),(22===s[0]?t:e).emit("connection",r),process.nextTick(()=>r.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 Y=e=>t=>{G.path(t,(null==e?void 0:e.length)?e:"/")},j=e=>t=>{G.extends(t).push(e)},K=e=>t=>(r,s,o)=>{G.parameters(r).push({callback:e,key:t,propertyKey:s,parameterIndex:o})},V=K(({app:e})=>e),W=K(({argument:e})=>e),B=(e,t)=>i(t)?e:e[t],X=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{},t)}),z=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.cookies)&&void 0!==s?s:{},t)}),Z=K(({ctx:e})=>e),J=K(({ctx:e})=>{var t,r;return null!==(r=null===(t=e.req)||void 0===t?void 0:t.files)&&void 0!==r?r:{}}),ee=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.headers)&&void 0!==s?s:{},t)}),te=K(({ctx:e,key:t})=>{var r,s;return B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.params)&&void 0!==s?s:{},t)}),re=K(({ctx:e,key:t})=>{var r,s,o;return String(null!==(o=B(null!==(s=null===(r=e.req)||void 0===r?void 0:r.query)&&void 0!==s?s:{},t))&&void 0!==o?o:"")}),se=K(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.session}),oe=(e,t)=>(r,s)=>{const o=p();Z()(r,o,0),G.middleware(r).push({method:o,argument:t,propertyKey:s,callback:e})},ie=e=>(t,r,s)=>{oe(t=>{t.cache(Object.assign({},e)),t.next()})(t,r,s)};var ne;!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"}(ne||(ne={}));const ae=e=>t=>(r,s)=>{G.routers(r).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:s})},le=ae(ne.ALL),ue=ae(ne.DELETE),he=ae(ne.GET),de=ae(ne.HEAD),Ee=ae(ne.OPTIONS),pe=ae(ne.PATCH),ce=ae(ne.POST),_e=ae(ne.PUT),ve=(e,t)=>(r,s)=>{let o=d;if(c(s)){const t=r;h(t[e])&&(o=t[e])}G.middleware(r).push({method:e,argument:t,propertyKey:s,callback:o})},Te=e=>{var t;return(null===(t=e.split(".").pop())||void 0===t?void 0:t.toLowerCase())||"bin"},Oe=(e,t)=>{var r;return null!==(r=e[t])&&void 0!==r?r:null},Re=(e,t)=>{const r=p()+"."+t;return S(L(e,r))?Re(e,t):r},me=e=>{const t=l({},e),r=C({destination(e,r,s){var o;const i=Oe(t,r.fieldname);s(null,null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./")},filename(e,r,s){var o;const i=Oe(t,r.fieldname);s(null,Re(null!==(o=null==i?void 0:i.path)&&void 0!==o?o:"./",Te(r.originalname)))}}),s=Math.max(...Object.values(t).map(e=>{var t;return null!==(t=e.size)&&void 0!==t?t:0})),o=U({storage:r,fileFilter(e,r,s){var o;const i=Oe(t,r.fieldname);return i?((e,t)=>!t.length||t.includes(Te(e.originalname))||t.includes(e.mimetype.toLowerCase()))(r,null!==(o=null==i?void 0:i.types)&&void 0!==o?o:[])?void s(null,!0):s(new Error(`FILE TYPE NOT ALLOWED FOR "${r.fieldname}"`)):s(new Error(`UPLOAD FIELD "${r.fieldname}" IS NOT CONFIGURED`))},limits:{fileSize:1024*s*1024}}).fields(Object.keys(t).map(e=>{var r,s;return{name:e,maxCount:null!==(s=null===(r=t[e])||void 0===r?void 0:r.count)&&void 0!==s?s:1}}));return(e,t,r)=>{oe(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,r)}},Ne=e=>{const t=_.object(h(e)?e(_):e).parse();return(e,r,s)=>{oe(e=>P(void 0,void 0,void 0,function*(){var r,s;const{error:o,message:i,value:n}=yield t.validateAsync(null!==(s=null===(r=e.req)||void 0===r?void 0:r.body)&&void 0!==s?s:{});if(o)return e.error(new H(null!=i?i:o));e.req.body=n,e.next()}))(e,r,s)}};export{le as All,V as App,Q as Application,W as Argument,X as Body,ie as Cache,$ as Context,Y as Controller,z as Cookies,Z as Ctx,ue as Delete,j as Extends,J as Files,M as ForbiddenError,he as Get,de as Head,ee as Headers,k as InternalServerError,G as Metadata,ne as Methods,ve as Middleware,q as NotFoundError,Ee as Options,te as Params,pe as Patch,ce as Post,_e as Put,re as Query,b as ResponseError,se as Session,F as Status,w as UnauthorizedError,me as Upload,oe as Use,Ne as Validate,H as ValidationError,ae as methodFactory,K as parameterFactory};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/express",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "Decorators for express",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs.js",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@andrewcaires/decorator": "^1.1.1",
|
|
40
40
|
"@andrewcaires/node": "^1.2.10",
|
|
41
|
-
"@andrewcaires/utils.js": "^0.4.
|
|
41
|
+
"@andrewcaires/utils.js": "^0.4.29",
|
|
42
42
|
"compression": "^1.8.1",
|
|
43
43
|
"cookie-parser": "^1.4.7",
|
|
44
|
-
"cors": "^2.8.
|
|
44
|
+
"cors": "^2.8.6",
|
|
45
45
|
"express": "^5.2.1",
|
|
46
|
-
"express-session": "^1.
|
|
46
|
+
"express-session": "^1.19.0",
|
|
47
47
|
"multer": "^2.0.2",
|
|
48
48
|
"request-ip": "^3.3.0"
|
|
49
49
|
},
|
|
@@ -51,20 +51,20 @@
|
|
|
51
51
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
52
52
|
"@rollup/plugin-terser": "^0.4.4",
|
|
53
53
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
54
|
-
"@rollup/rollup-darwin-arm64": "^4.
|
|
54
|
+
"@rollup/rollup-darwin-arm64": "^4.57.1",
|
|
55
55
|
"@types/compression": "^1.8.1",
|
|
56
56
|
"@types/cookie-parser": "^1.4.10",
|
|
57
57
|
"@types/cors": "^2.8.19",
|
|
58
58
|
"@types/express": "^5.0.6",
|
|
59
59
|
"@types/express-session": "^1.18.2",
|
|
60
60
|
"@types/multer": "^2.0.0",
|
|
61
|
-
"@types/node": "^25.
|
|
61
|
+
"@types/node": "^25.2.3",
|
|
62
62
|
"@types/request-ip": "^0.0.41",
|
|
63
63
|
"@types/swagger-ui-express": "^4.1.8",
|
|
64
64
|
"eslint": "^9.39.2",
|
|
65
65
|
"rollup-plugin-dts": "^6.3.0",
|
|
66
66
|
"swagger-ui-express": "^5.0.1",
|
|
67
67
|
"tslib": "^2.8.1",
|
|
68
|
-
"typescript-eslint": "^8.
|
|
68
|
+
"typescript-eslint": "^8.55.0"
|
|
69
69
|
}
|
|
70
70
|
}
|