@andrewcaires/express 1.2.0 → 1.2.2
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 +1 -1
- package/dist/index.esm.js +2 -2
- package/package.json +3 -2
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v1.2.
|
|
2
|
+
* @andrewcaires/express v1.2.2
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2024 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"),E=require("net"),u=require("path"),d=require("request-ip"),_=require("multer");function c(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 l=c(p),T=c(h);function O(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 N{constructor(e,t,s,r){this.req=e,this.res=t,this.next=s,this.controller=r}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(u.resolve(e)))}ip(){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 A{static extends(t){return e.Metadata(A.EXTENDS,t,[])}static middleware(t){return e.Metadata(A.MIDDLEWARE,t,[])}static parameters(t){return e.Metadata(A.PARAMETERS,t,[])}static path(t,s){return e.Metadata(A.PATH,t,s)}static routers(t){return e.Metadata(A.ROUTERS,t,[])}}A.EXTENDS=Symbol(),A.MIDDLEWARE=Symbol(),A.PARAMETERS=Symbol(),A.PATH=Symbol(),A.ROUTERS=Symbol();class x extends t.EventEmitter{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,p){var h;super(),this._app=i(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=i.Router(),this._controllers=i.Router(),this._options=t.merge(Object.assign({},this._default),Object.assign({},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:{}));const E=this._options.cookie;this._app.use(s()),this._app.use(r(null==E?void 0:E.secret,null==E?void 0:E.options)),this._app.use(n(this._options.session)),this.init(),this._app.use(this._router);const d=(null===(h=this._options.path)||void 0===h?void 0:h.length)?this._options.path:"/";if(this._app.use(d,this._controllers),this._app.all(d+"/*",((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=u.resolve(this._options.public);a.existsSync(e)&&this._app.use(i.static(e)),this._app.get("*",((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("*",((e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(R.NOT_FOUND),t.json({message:"Invalid API"}))}));e.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)=>O(this,void 0,void 0,(function*(){const a=new N(s,r,i,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)=>O(this,void 0,void 0,(function*(){const a=new N(s,r,i,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._controllers.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(t.isFunction(e[s.method])){const r=this._indexes(s.method,o);return(o,i,n)=>O(this,void 0,void 0,(function*(){const a=new N(o,i,n,e),p=t.isFunction(s.argument)?s.argument(a):s.argument,h=yield e[s.method](...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 O(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=l.createServer(this._app),this._server.listen(this._options.port,(()=>this.log(this._port(this._options.port),"express")))),!this._server){const e=l.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=T.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new E.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 I;exports.Methods=void 0,(I=exports.Methods||(exports.Methods={})).ALL="all",I.GET="get",I.POST="post",I.PUT="put",I.HEAD="head",I.DELETE="delete",I.OPTIONS="options",I.PATCH="patch";const S=e=>t=>(s,r)=>{A.routers(s).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:r})},m=S(exports.Methods.ALL),D=S(exports.Methods.DELETE),f=S(exports.Methods.GET),y=S(exports.Methods.HEAD),v=S(exports.Methods.OPTIONS),g=S(exports.Methods.PATCH),U=S(exports.Methods.POST),P=S(exports.Methods.PUT),L=e=>t=>(s,r,o)=>{A.parameters(s).push({callback:e,key:t,propertyKey:r,parameterIndex:o})},C=L((({app:e})=>e)),M=L((({argument:e})=>e)),F=(e,s)=>t.isUndefined(s)?e:e[s],b=L((({ctx:e,key:t})=>F(e.req.body,t))),q=L((({ctx:e,key:t})=>F(e.req.cookies,t))),j=L((({ctx:e})=>e)),k=L((({ctx:e})=>t.toArray(e.req.files))),w=L((({ctx:e,key:t})=>F(e.req.headers,t))),H=L((({ctx:e,key:t})=>F(e.req.params,t))),G=L((({ctx:e,key:t})=>F(e.req.query,t))),Q=L((({ctx:e})=>e.req.session));class K{static file(e,s){const r=t.uuidv4()+"."+s;return a.existsSync(u.join(e,r))?K.file(e,s):r}static middleware(e,t){const s=K._cache(t);s.upload||(s.upload=K.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=_.diskStorage({destination:(e,s,r)=>r(null,t),filename:(e,s,r)=>O(this,void 0,void 0,(function*(){return r(null,K.file(t,s.originalname.split(".").pop()||"bin"))}))}),r=(null==e?void 0:e.types)||[];return _({storage:s,fileFilter(e,t,s){return O(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)}}K._cache=t.cache(),exports.All=m,exports.App=C,exports.Application=x,exports.Argument=M,exports.Body=b,exports.Context=N,exports.Controller=e=>t=>{A.path(t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=q,exports.Ctx=j,exports.Delete=D,exports.Extends=e=>t=>{A.extends(t).push(e)},exports.Files=k,exports.Get=f,exports.Head=y,exports.Headers=w,exports.Metadata=A,exports.Middleware=(e,t)=>(s,r)=>{A.middleware(s).push({method:e,argument:t,propertyKey:r})},exports.Options=v,exports.Params=H,exports.Patch=g,exports.Post=U,exports.Put=P,exports.Query=G,exports.Session=Q,exports.Status=R,exports.Upload=K,exports.methodFactory=S,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"),E=require("net"),u=require("path"),_=require("request-ip"),d=require("multer");function c(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 l=c(p),T=c(h);function O(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 N{constructor(e,t,s,r){this.req=e,this.res=t,this.next=s,this.controller=r}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(u.resolve(e)))}ip(){return _.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 A{static extends(t){return e.Metadata(A.EXTENDS,t,[])}static middleware(t){return e.Metadata(A.MIDDLEWARE,t,[])}static parameters(t){return e.Metadata(A.PARAMETERS,t,[])}static path(t,s){return e.Metadata(A.PATH,t,s)}static routers(t){return e.Metadata(A.ROUTERS,t,[])}}A.EXTENDS=Symbol(),A.MIDDLEWARE=Symbol(),A.PARAMETERS=Symbol(),A.PATH=Symbol(),A.ROUTERS=Symbol();class x extends t.EventEmitter{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(e,p){var h;super(),this._app=i(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=i.Router(),this._controllers=i.Router(),this._options=t.merge(Object.assign({},this._default),Object.assign({},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:{}));const E=this._options.cookie;this._app.use(s()),this._app.use(r(null==E?void 0:E.secret,null==E?void 0:E.options)),this._app.use(n(this._options.session)),this.init(),this._app.use(this._router);const _=(null===(h=this._options.path)||void 0===h?void 0:h.length)?this._options.path:"/";if(this._app.use(_,this._controllers),this._app.all(_+"/*",((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=u.resolve(this._options.public);a.existsSync(e)&&this._app.use(i.static(e)),this._app.get("*",((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("*",((e,t)=>{this._options.redirect?t.redirect(this._options.redirect):(t.status(R.NOT_FOUND),t.json({message:"INVALID API"}))}));e.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)=>O(this,void 0,void 0,(function*(){const a=new N(s,r,i,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)=>O(this,void 0,void 0,(function*(){const a=new N(s,r,i,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._controllers.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(t.isFunction(e[s.method])){const r=this._indexes(s.method,o);return(o,i,n)=>O(this,void 0,void 0,(function*(){const a=new N(o,i,n,e),p=t.isFunction(s.argument)?s.argument(a):s.argument,h=yield e[s.method](...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 O(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=l.createServer(this._app),this._server.listen(this._options.port,(()=>this.log(this._port(this._options.port),"express")))),!this._server){const e=l.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=T.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new E.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 I;exports.Methods=void 0,(I=exports.Methods||(exports.Methods={})).ALL="all",I.GET="get",I.POST="post",I.PUT="put",I.HEAD="head",I.DELETE="delete",I.OPTIONS="options",I.PATCH="patch";const S=e=>t=>(s,r)=>{A.routers(s).push({method:e,path:(null==t?void 0:t.length)?t:"/",propertyKey:r})},m=S(exports.Methods.ALL),D=S(exports.Methods.DELETE),f=S(exports.Methods.GET),y=S(exports.Methods.HEAD),v=S(exports.Methods.OPTIONS),g=S(exports.Methods.PATCH),U=S(exports.Methods.POST),L=S(exports.Methods.PUT),P=e=>t=>(s,r,o)=>{A.parameters(s).push({callback:e,key:t,propertyKey:r,parameterIndex:o})},C=P((({app:e})=>e)),M=P((({argument:e})=>e)),F=(e,s)=>t.isUndefined(s)?e:e[s],b=P((({ctx:e,key:t})=>F(e.req.body,t))),q=P((({ctx:e,key:t})=>F(e.req.cookies,t))),j=P((({ctx:e})=>e)),k=P((({ctx:e})=>t.toArray(e.req.files))),w=P((({ctx:e,key:t})=>F(e.req.headers,t))),H=P((({ctx:e,key:t})=>F(e.req.params,t))),G=P((({ctx:e,key:t})=>F(e.req.query,t))),Q=P((({ctx:e})=>e.req.session));class K{static file(e,s){const r=t.uuidv4()+"."+s;return a.existsSync(u.join(e,r))?K.file(e,s):r}static middleware(e,t){const s=K._cache(t);s.upload||(s.upload=K.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=d.diskStorage({destination:(e,s,r)=>r(null,t),filename:(e,s,r)=>O(this,void 0,void 0,(function*(){return r(null,K.file(t,s.originalname.split(".").pop()||"bin"))}))}),r=(null==e?void 0:e.types)||[];return d({storage:s,fileFilter(e,t,s){return O(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)}}K._cache=t.cache(),exports.All=m,exports.App=C,exports.Application=x,exports.Argument=M,exports.Body=b,exports.Context=N,exports.Controller=e=>t=>{A.path(t,(null==e?void 0:e.length)?e:"/")},exports.Cookies=q,exports.Ctx=j,exports.Delete=D,exports.Extends=e=>t=>{A.extends(t).push(e)},exports.Files=k,exports.Get=f,exports.Head=y,exports.Headers=w,exports.Metadata=A,exports.Middleware=(e,t)=>(s,r)=>{A.middleware(s).push({method:e,argument:t,propertyKey:r})},exports.Options=v,exports.Params=H,exports.Patch=g,exports.Post=U,exports.Put=L,exports.Query=G,exports.Session=Q,exports.Status=R,exports.Upload=K,exports.methodFactory=S,exports.parameterFactory=P;
|
package/dist/index.d.ts
CHANGED
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v1.2.
|
|
2
|
+
* @andrewcaires/express v1.2.2
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
import{Metadata as t,Log as e}from"@andrewcaires/node";import{EventEmitter as s,merge as r,isObject as i,isFunction as o,isArray as n,isUndefined as h,toArray as a,cache as E,uuidv4 as p}from"@andrewcaires/utils.js";import _ from"compression";import d from"cookie-parser";import u from"cors";import l,{Router as c,json as T,urlencoded as O}from"express";import m from"express-session";import{existsSync as R}from"fs";import*as N from"http";import*as A from"https";import{Server as I}from"net";import{resolve as f,join as S}from"path";import{getClientIp as D}from"request-ip";import U from"multer";function v(t,e,s,r){return new(s||(s=Promise))((function(i,o){function n(t){try{a(r.next(t))}catch(t){o(t)}}function h(t){try{a(r.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,h)}a((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class y{}y.CONTINUE=100,y.SWITCHING_PROTOCOLS=101,y.PROCESSING=102,y.OK=200,y.CREATED=201,y.ACCEPTED=202,y.NON_AUTHORITATIVE_INFORMATION=203,y.NO_CONTENT=204,y.RESET_CONTENT=205,y.PARTIAL_CONTENT=206,y.MULTI_STATUS=207,y.ALREADY_REPORTED=208,y.IM_USED=226,y.MULTIPLE_CHOICES=300,y.MOVED_PERMANENTLY=301,y.FOUND=302,y.SEE_OTHER=303,y.NOT_MODIFIED=304,y.USE_PROXY=305,y.TEMPORARY_REDIRECT=307,y.PERMANENT_REDIRECT=308,y.BAD_REQUEST=400,y.UNAUTHORIZED=401,y.PAYMENT_REQUIRED=402,y.FORBIDDEN=403,y.NOT_FOUND=404,y.METHOD_NOT_ALLOWED=405,y.NOT_ACCEPTABLE=406,y.PROXY_AUTHENTICATION_REQUIRED=407,y.REQUEST_TIMEOUT=408,y.CONFLICT=409,y.GONE=410,y.LENGTH_REQUIRED=411,y.PRECONDITION_FAILED=412,y.PAYLOAD_TOO_LARGE=413,y.REQUEST_URI_TOO_LONG=414,y.UNSUPPORTED_MEDIA_TYPE=415,y.REQUESTED_RANGE_NOT_SATISFIABLE=416,y.EXPECTATION_FAILED=417,y.IM_A_TEAPOT=418,y.MISDIRECTED_REQUEST=421,y.UNPROCESSABLE_ENTITY=422,y.LOCKED=423,y.FAILED_DEPENDENCY=424,y.UPGRADE_REQUIRED=426,y.PRECONDITION_REQUIRED=428,y.TOO_MANY_REQUESTS=429,y.REQUEST_HEADER_FIELDS_TOO_LARGE=431,y.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,y.UNAVAILABLE_FOR_LEGAL_REASONS=451,y.CLIENT_CLOSED_REQUEST=499,y.INTERNAL_SERVER_ERROR=500,y.NOT_IMPLEMENTED=501,y.BAD_GATEWAY=502,y.SERVICE_UNAVAILABLE=503,y.GATEWAY_TIMEOUT=504,y.HTTP_VERSION_NOT_SUPPORTED=505,y.VARIANT_ALSO_NEGOCIATES=506,y.INSUFFICIENT_STORAGE=507,y.LOOP_DETECTED=508,y.NOT_EXTENDED=510,y.NETWORK_AUTHENTICATION_REQUIRED=511,y.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class g{constructor(t,e,s,r){this.req=t,this.res=e,this.next=s,this.controller=r}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(y.INTERNAL_SERVER_ERROR,{message:t})}file(t){this.res.headersSent||(this.status(y.OK),this.res.sendFile(f(t)))}ip(){return D(this.req)||"0.0.0.0"}json(t){this.send(y.OK,t)}notFound(t){this.send(y.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(y.OK,{message:t})}unauthorized(t){this.send(y.UNAUTHORIZED,{message:t})}validation(t){this.send(y.BAD_REQUEST,{message:t})}}class L{static extends(e){return t(L.EXTENDS,e,[])}static middleware(e){return t(L.MIDDLEWARE,e,[])}static parameters(e){return t(L.PARAMETERS,e,[])}static path(e,s){return t(L.PATH,e,s)}static routers(e){return t(L.ROUTERS,e,[])}}L.EXTENDS=Symbol(),L.MIDDLEWARE=Symbol(),L.PARAMETERS=Symbol(),L.PATH=Symbol(),L.ROUTERS=Symbol();class P extends s{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,e){var s;super(),this._app=l(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=c(),this._controllers=c(),this._options=r(Object.assign({},this._default),Object.assign({},e)),this._app.use(T(this._options.json)),this._app.disable("x-powered-by"),this._app.use(O({extended:!0})),this._options.cross&&this._app.use(u(i(this._options.cross)?this._options.cross:{}));const h=this._options.cookie;this._app.use(_()),this._app.use(d(null==h?void 0:h.secret,null==h?void 0:h.options)),this._app.use(m(this._options.session)),this.init(),this._app.use(this._router);const a=(null===(s=this._options.path)||void 0===s?void 0:s.length)?this._options.path:"/";if(this._app.use(a,this._controllers),this._app.all(a+"/*",((t,e)=>{this._options.redirect?e.redirect(this._options.redirect):(e.status(y.NOT_FOUND),e.json({message:"Invalid API"}))})),this._options.public){const t=f(this._options.public);R(t)&&this._app.use(l.static(t)),this._app.get("*",((e,s)=>{R(t)?s.sendFile(t+"/index.html"):this._options.notfound?s.redirect(this._options.notfound):(s.status(y.NOT_FOUND),s.json({message:'File "index.html" not found'}))}))}else this._app.all("*",((t,e)=>{this._options.redirect?e.redirect(this._options.redirect):(e.status(y.NOT_FOUND),e.json({message:"Invalid API"}))}));t.forEach((t=>{const e=c(),s=this._middlewares(t),r=L.parameters(t);this._extends(t).forEach((t=>{s.push(...this._middlewares(t)),r.push(...L.parameters(t))})),L.routers(t).forEach((h=>{const a=h.propertyKey.toString();if(o(t[a])){if(!o(e[h.method]))return this._notFound(h.method);const E=this._handles(t,a,s,r);return e[h.method](h.path,...E,((e,s,o)=>v(this,void 0,void 0,(function*(){const h=new g(e,s,o,t),E=this._args(h,this._indexes(a,r)),p=yield t[a](...E);h.close(n(p)||i(p)?p:void 0)}))))}this._notFound(`${t.name}.${a}`)})),this._extends(t).forEach((h=>{L.routers(h).forEach((h=>{const a=h.propertyKey.toString();if(o(t[a])){if(!o(e[h.method]))return this._notFound(h.method);const E=this._handles(t,a,s,r);return e[h.method](h.path,...E,((e,s,o)=>v(this,void 0,void 0,(function*(){const h=new g(e,s,o,t),E=this._args(h,this._indexes(a,r)),p=yield t[a](...E);h.close(n(p)||i(p)?p:void 0)}))))}this._notFound(`${t.constructor.name}.${a}`)}))})),this._controllers.use(L.path(t,"/"),e)}))}init(){}error(t,s){e.error(t)}log(t,s){e.info(t)}_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(o(t[e.method])){const s=this._indexes(e.method,r);return(r,h,a)=>v(this,void 0,void 0,(function*(){const E=new g(r,h,a,t),p=o(e.argument)?e.argument(E):e.argument,_=yield t[e.method](...this._args(E,s,p));(n(_)||i(_))&&E.close(_)}))}this._notFound(e.method)})).filter(o)}_extends(t){return L.extends(t).reduce(((t,e)=>(t.push(e,...this._extends(e)),t)),[]).filter(((t,e,s)=>s.indexOf(t)===e))}_indexes(t,e){return e.filter((e=>e.propertyKey==t))}_middlewares(t){return[...L.middleware(t)].reverse()}_notFound(t){this.error(new Error(`Router "${t}" not found`),"express")}_port(t){return`Server listening on port ${t}`}listen(){return v(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=N.createServer(this._app),this._server.listen(this._options.port,(()=>this.log(this._port(this._options.port),"express")))),!this._server){const t=N.createServer(((t,e)=>{const s=80==this._options.port?":80":"";e.writeHead(y.MOVED_PERMANENTLY,{Location:`https://${t.headers.host}${s}${t.url}`}),e.end()})),e=A.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new I((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 x=t=>e=>{L.path(e,(null==t?void 0:t.length)?t:"/")},C=t=>e=>{L.extends(e).push(t)};var F;!function(t){t.ALL="all",t.GET="get",t.POST="post",t.PUT="put",t.HEAD="head",t.DELETE="delete",t.OPTIONS="options",t.PATCH="patch"}(F||(F={}));const M=t=>e=>(s,r)=>{L.routers(s).push({method:t,path:(null==e?void 0:e.length)?e:"/",propertyKey:r})},k=M(F.ALL),w=M(F.DELETE),b=M(F.GET),H=M(F.HEAD),G=(t,e)=>(s,r)=>{L.middleware(s).push({method:t,argument:e,propertyKey:r})},Q=M(F.OPTIONS),K=M(F.PATCH),Y=M(F.POST),j=M(F.PUT),q=t=>e=>(s,r,i)=>{L.parameters(s).push({callback:t,key:e,propertyKey:r,parameterIndex:i})},V=q((({app:t})=>t)),B=q((({argument:t})=>t)),W=(t,e)=>h(e)?t:t[e],$=q((({ctx:t,key:e})=>W(t.req.body,e))),X=q((({ctx:t,key:e})=>W(t.req.cookies,e))),z=q((({ctx:t})=>t)),Z=q((({ctx:t})=>a(t.req.files))),J=q((({ctx:t,key:e})=>W(t.req.headers,e))),tt=q((({ctx:t,key:e})=>W(t.req.params,e))),et=q((({ctx:t,key:e})=>W(t.req.query,e))),st=q((({ctx:t})=>t.req.session));class rt{static file(t,e){const s=p()+"."+e;return R(S(t,s))?rt.file(t,e):s}static middleware(t,e){const s=rt._cache(e);s.upload||(s.upload=rt.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=U.diskStorage({destination:(t,s,r)=>r(null,e),filename:(t,s,r)=>v(this,void 0,void 0,(function*(){return r(null,rt.file(e,s.originalname.split(".").pop()||"bin"))}))}),r=(null==t?void 0:t.types)||[];return U({storage:s,fileFilter(t,e,s){return v(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)}}rt._cache=E();export{k as All,V as App,P as Application,B as Argument,$ as Body,g as Context,x as Controller,X as Cookies,z as Ctx,w as Delete,C as Extends,Z as Files,b as Get,H as Head,J as Headers,L as Metadata,F as Methods,G as Middleware,Q as Options,tt as Params,K as Patch,Y as Post,j as Put,et as Query,st as Session,y as Status,rt as Upload,M as methodFactory,q as parameterFactory};
|
|
7
|
+
import{Metadata as t,Log as e}from"@andrewcaires/node";import{EventEmitter as s,merge as r,isObject as i,isFunction as o,isArray as n,isUndefined as h,toArray as E,cache as p,uuidv4 as a}from"@andrewcaires/utils.js";import _ from"compression";import d from"cookie-parser";import u from"cors";import c,{Router as l,json as T,urlencoded as O}from"express";import N from"express-session";import{existsSync as m}from"fs";import*as R from"http";import*as A from"https";import{Server as I}from"net";import{resolve as S,join as f}from"path";import{getClientIp as D}from"request-ip";import U from"multer";function y(t,e,s,r){return new(s||(s=Promise))((function(i,o){function n(t){try{E(r.next(t))}catch(t){o(t)}}function h(t){try{E(r.throw(t))}catch(t){o(t)}}function E(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,h)}E((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class L{}L.CONTINUE=100,L.SWITCHING_PROTOCOLS=101,L.PROCESSING=102,L.OK=200,L.CREATED=201,L.ACCEPTED=202,L.NON_AUTHORITATIVE_INFORMATION=203,L.NO_CONTENT=204,L.RESET_CONTENT=205,L.PARTIAL_CONTENT=206,L.MULTI_STATUS=207,L.ALREADY_REPORTED=208,L.IM_USED=226,L.MULTIPLE_CHOICES=300,L.MOVED_PERMANENTLY=301,L.FOUND=302,L.SEE_OTHER=303,L.NOT_MODIFIED=304,L.USE_PROXY=305,L.TEMPORARY_REDIRECT=307,L.PERMANENT_REDIRECT=308,L.BAD_REQUEST=400,L.UNAUTHORIZED=401,L.PAYMENT_REQUIRED=402,L.FORBIDDEN=403,L.NOT_FOUND=404,L.METHOD_NOT_ALLOWED=405,L.NOT_ACCEPTABLE=406,L.PROXY_AUTHENTICATION_REQUIRED=407,L.REQUEST_TIMEOUT=408,L.CONFLICT=409,L.GONE=410,L.LENGTH_REQUIRED=411,L.PRECONDITION_FAILED=412,L.PAYLOAD_TOO_LARGE=413,L.REQUEST_URI_TOO_LONG=414,L.UNSUPPORTED_MEDIA_TYPE=415,L.REQUESTED_RANGE_NOT_SATISFIABLE=416,L.EXPECTATION_FAILED=417,L.IM_A_TEAPOT=418,L.MISDIRECTED_REQUEST=421,L.UNPROCESSABLE_ENTITY=422,L.LOCKED=423,L.FAILED_DEPENDENCY=424,L.UPGRADE_REQUIRED=426,L.PRECONDITION_REQUIRED=428,L.TOO_MANY_REQUESTS=429,L.REQUEST_HEADER_FIELDS_TOO_LARGE=431,L.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,L.UNAVAILABLE_FOR_LEGAL_REASONS=451,L.CLIENT_CLOSED_REQUEST=499,L.INTERNAL_SERVER_ERROR=500,L.NOT_IMPLEMENTED=501,L.BAD_GATEWAY=502,L.SERVICE_UNAVAILABLE=503,L.GATEWAY_TIMEOUT=504,L.HTTP_VERSION_NOT_SUPPORTED=505,L.VARIANT_ALSO_NEGOCIATES=506,L.INSUFFICIENT_STORAGE=507,L.LOOP_DETECTED=508,L.NOT_EXTENDED=510,L.NETWORK_AUTHENTICATION_REQUIRED=511,L.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class v{constructor(t,e,s,r){this.req=t,this.res=e,this.next=s,this.controller=r}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(L.INTERNAL_SERVER_ERROR,{message:t})}file(t){this.res.headersSent||(this.status(L.OK),this.res.sendFile(S(t)))}ip(){return D(this.req)||"0.0.0.0"}json(t){this.send(L.OK,t)}notFound(t){this.send(L.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(L.OK,{message:t})}unauthorized(t){this.send(L.UNAUTHORIZED,{message:t})}validation(t){this.send(L.BAD_REQUEST,{message:t})}}class g{static extends(e){return t(g.EXTENDS,e,[])}static middleware(e){return t(g.MIDDLEWARE,e,[])}static parameters(e){return t(g.PARAMETERS,e,[])}static path(e,s){return t(g.PATH,e,s)}static routers(e){return t(g.ROUTERS,e,[])}}g.EXTENDS=Symbol(),g.MIDDLEWARE=Symbol(),g.PARAMETERS=Symbol(),g.PATH=Symbol(),g.ROUTERS=Symbol();class P extends s{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,e){var s;super(),this._app=c(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3,session:{secret:"yoursecret",resave:!0,saveUninitialized:!1,cookie:{maxAge:864e5}}},this._router=l(),this._controllers=l(),this._options=r(Object.assign({},this._default),Object.assign({},e)),this._app.use(T(this._options.json)),this._app.disable("x-powered-by"),this._app.use(O({extended:!0})),this._options.cross&&this._app.use(u(i(this._options.cross)?this._options.cross:{}));const h=this._options.cookie;this._app.use(_()),this._app.use(d(null==h?void 0:h.secret,null==h?void 0:h.options)),this._app.use(N(this._options.session)),this.init(),this._app.use(this._router);const E=(null===(s=this._options.path)||void 0===s?void 0:s.length)?this._options.path:"/";if(this._app.use(E,this._controllers),this._app.all(E+"/*",((t,e)=>{this._options.redirect?e.redirect(this._options.redirect):(e.status(L.NOT_FOUND),e.json({message:"INVALID API"}))})),this._options.public){const t=S(this._options.public);m(t)&&this._app.use(c.static(t)),this._app.get("*",((e,s)=>{m(t)?s.sendFile(t+"/index.html"):this._options.notfound?s.redirect(this._options.notfound):(s.status(L.NOT_FOUND),s.json({message:'FILE "index.html" NOT FOUND'}))}))}else this._app.all("*",((t,e)=>{this._options.redirect?e.redirect(this._options.redirect):(e.status(L.NOT_FOUND),e.json({message:"INVALID API"}))}));t.forEach((t=>{const e=l(),s=this._middlewares(t),r=g.parameters(t);this._extends(t).forEach((t=>{s.push(...this._middlewares(t)),r.push(...g.parameters(t))})),g.routers(t).forEach((h=>{const E=h.propertyKey.toString();if(o(t[E])){if(!o(e[h.method]))return this._notFound(h.method);const p=this._handles(t,E,s,r);return e[h.method](h.path,...p,((e,s,o)=>y(this,void 0,void 0,(function*(){const h=new v(e,s,o,t),p=this._args(h,this._indexes(E,r)),a=yield t[E](...p);h.close(n(a)||i(a)?a:void 0)}))))}this._notFound(`${t.name}.${E}`)})),this._extends(t).forEach((h=>{g.routers(h).forEach((h=>{const E=h.propertyKey.toString();if(o(t[E])){if(!o(e[h.method]))return this._notFound(h.method);const p=this._handles(t,E,s,r);return e[h.method](h.path,...p,((e,s,o)=>y(this,void 0,void 0,(function*(){const h=new v(e,s,o,t),p=this._args(h,this._indexes(E,r)),a=yield t[E](...p);h.close(n(a)||i(a)?a:void 0)}))))}this._notFound(`${t.constructor.name}.${E}`)}))})),this._controllers.use(g.path(t,"/"),e)}))}init(){}error(t,s){e.error(t)}log(t,s){e.info(t)}_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(o(t[e.method])){const s=this._indexes(e.method,r);return(r,h,E)=>y(this,void 0,void 0,(function*(){const p=new v(r,h,E,t),a=o(e.argument)?e.argument(p):e.argument,_=yield t[e.method](...this._args(p,s,a));(n(_)||i(_))&&p.close(_)}))}this._notFound(e.method)})).filter(o)}_extends(t){return g.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[...g.middleware(t)].reverse()}_notFound(t){this.error(new Error(`Router "${t}" not found`),"express")}_port(t){return`Server listening on port ${t}`}listen(){return y(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=R.createServer(this._app),this._server.listen(this._options.port,(()=>this.log(this._port(this._options.port),"express")))),!this._server){const t=R.createServer(((t,e)=>{const s=80==this._options.port?":80":"";e.writeHead(L.MOVED_PERMANENTLY,{Location:`https://${t.headers.host}${s}${t.url}`}),e.end()})),e=A.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new I((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 x=t=>e=>{g.path(e,(null==t?void 0:t.length)?t:"/")},C=t=>e=>{g.extends(e).push(t)};var F;!function(t){t.ALL="all",t.GET="get",t.POST="post",t.PUT="put",t.HEAD="head",t.DELETE="delete",t.OPTIONS="options",t.PATCH="patch"}(F||(F={}));const M=t=>e=>(s,r)=>{g.routers(s).push({method:t,path:(null==e?void 0:e.length)?e:"/",propertyKey:r})},k=M(F.ALL),w=M(F.DELETE),b=M(F.GET),H=M(F.HEAD),G=(t,e)=>(s,r)=>{g.middleware(s).push({method:t,argument:e,propertyKey:r})},Q=M(F.OPTIONS),K=M(F.PATCH),Y=M(F.POST),j=M(F.PUT),q=t=>e=>(s,r,i)=>{g.parameters(s).push({callback:t,key:e,propertyKey:r,parameterIndex:i})},V=q((({app:t})=>t)),B=q((({argument:t})=>t)),W=(t,e)=>h(e)?t:t[e],$=q((({ctx:t,key:e})=>W(t.req.body,e))),X=q((({ctx:t,key:e})=>W(t.req.cookies,e))),z=q((({ctx:t})=>t)),Z=q((({ctx:t})=>E(t.req.files))),J=q((({ctx:t,key:e})=>W(t.req.headers,e))),tt=q((({ctx:t,key:e})=>W(t.req.params,e))),et=q((({ctx:t,key:e})=>W(t.req.query,e))),st=q((({ctx:t})=>t.req.session));class rt{static file(t,e){const s=a()+"."+e;return m(f(t,s))?rt.file(t,e):s}static middleware(t,e){const s=rt._cache(e);s.upload||(s.upload=rt.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=U.diskStorage({destination:(t,s,r)=>r(null,e),filename:(t,s,r)=>y(this,void 0,void 0,(function*(){return r(null,rt.file(e,s.originalname.split(".").pop()||"bin"))}))}),r=(null==t?void 0:t.types)||[];return U({storage:s,fileFilter(t,e,s){return y(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)}}rt._cache=p();export{k as All,V as App,P as Application,B as Argument,$ as Body,v as Context,x as Controller,X as Cookies,z as Ctx,w as Delete,C as Extends,Z as Files,b as Get,H as Head,J as Headers,g as Metadata,F as Methods,G as Middleware,Q as Options,tt as Params,K as Patch,Y as Post,j as Put,et as Query,st as Session,L as Status,rt as Upload,M as methodFactory,q as parameterFactory};
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Decorators for express",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "./dist/index.cjs.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
7
8
|
"module": "./dist/index.esm.js",
|
|
@@ -60,6 +61,6 @@
|
|
|
60
61
|
"eslint": "^9.12.0",
|
|
61
62
|
"rollup-plugin-dts": "^6.1.1",
|
|
62
63
|
"tslib": "^2.7.0",
|
|
63
|
-
"typescript-eslint": "^8.
|
|
64
|
+
"typescript-eslint": "^8.9.0"
|
|
64
65
|
}
|
|
65
66
|
}
|