@andrewcaires/express 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -15
- package/dist/index.cjs.js +2 -2
- package/dist/index.d.ts +88 -97
- package/dist/index.esm.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,55 +29,72 @@ import {
|
|
|
29
29
|
Params,
|
|
30
30
|
Post,
|
|
31
31
|
Put,
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
Res,
|
|
33
|
+
Responses
|
|
34
34
|
|
|
35
35
|
} from "@andrewcaires/express";
|
|
36
|
-
import { NextFunction } from "express";
|
|
36
|
+
import { NextFunction, Response } from "express";
|
|
37
37
|
|
|
38
38
|
@Controller("/test")
|
|
39
39
|
class TestController {
|
|
40
40
|
|
|
41
|
-
public middleware(
|
|
41
|
+
public middleware(
|
|
42
|
+
@Argument() arg: string,
|
|
43
|
+
@Next() next: NextFunction
|
|
44
|
+
) {
|
|
42
45
|
|
|
43
46
|
console.log("middleware", arg);
|
|
44
47
|
|
|
45
|
-
next();
|
|
48
|
+
return next();
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
@Post()
|
|
49
52
|
@Middleware("middleware", "add")
|
|
50
|
-
public add(
|
|
53
|
+
public add(
|
|
54
|
+
@Res() res: Response,
|
|
55
|
+
@Body() body: any
|
|
56
|
+
) {
|
|
51
57
|
|
|
52
|
-
|
|
58
|
+
return Responses.data(res, body);
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
@Get()
|
|
56
62
|
@Middleware("middleware", "all")
|
|
57
|
-
public all(
|
|
63
|
+
public all(
|
|
64
|
+
@Res() res: Response
|
|
65
|
+
) {
|
|
58
66
|
|
|
59
|
-
|
|
67
|
+
return Responses.list(res, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
@Delete("/:id")
|
|
63
71
|
@Middleware("middleware", "del")
|
|
64
|
-
public del(
|
|
72
|
+
public del(
|
|
73
|
+
@Res() res: Response,
|
|
74
|
+
@Params("id") id: string
|
|
75
|
+
) {
|
|
65
76
|
|
|
66
|
-
|
|
77
|
+
return Responses.success(res, id);
|
|
67
78
|
}
|
|
68
79
|
|
|
69
80
|
@Get("/:id")
|
|
70
81
|
@Middleware("middleware", "get")
|
|
71
|
-
public get(
|
|
82
|
+
public get(
|
|
83
|
+
@Res() res: Response,
|
|
84
|
+
@Params("id") id: string
|
|
85
|
+
) {
|
|
72
86
|
|
|
73
|
-
|
|
87
|
+
return Responses.data(res, { id, name: "test" });
|
|
74
88
|
}
|
|
75
89
|
|
|
76
90
|
@Put("/:id")
|
|
77
91
|
@Middleware("middleware", "set")
|
|
78
|
-
public set(
|
|
92
|
+
public set(
|
|
93
|
+
@Res() res: Response,
|
|
94
|
+
@Params("id") id: string
|
|
95
|
+
) {
|
|
79
96
|
|
|
80
|
-
|
|
97
|
+
return Responses.success(res, id);
|
|
81
98
|
}
|
|
82
99
|
}
|
|
83
100
|
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v1.0.
|
|
2
|
+
* @andrewcaires/express v1.0.3
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2023 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
"use strict";require("reflect-metadata");var e=require("@andrewcaires/utils.js"),t=require("cors"),s=require("express"),r=require("fs"),o=require("http"),n=require("https"),
|
|
7
|
+
"use strict";require("reflect-metadata");var e=require("@andrewcaires/utils.js"),t=require("cors"),s=require("express"),r=require("fs"),o=require("http"),n=require("https"),a=require("net"),i=require("path");function E(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 p,_=E(o),h=E(n);function T(e,t,s,r){return new(s||(s=Promise))((function(o,n){function a(e){try{E(r.next(e))}catch(e){n(e)}}function i(e){try{E(r.throw(e))}catch(e){n(e)}}function E(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(a,i)}E((r=r.apply(e,t||[])).next())}))}exports.MetadataKeys=void 0,(p=exports.MetadataKeys||(exports.MetadataKeys={})).MIDDLEWARE="@express_middleware",p.PATH="@express_path",p.ROUTERS="@express_routers",p.PARAMETERS="@express_parameters";const d=(e,t,s)=>Reflect.hasMetadata(e,t)?Reflect.getMetadata(e,t):s;class u{}u.CONTINUE=100,u.SWITCHING_PROTOCOLS=101,u.PROCESSING=102,u.OK=200,u.CREATED=201,u.ACCEPTED=202,u.NON_AUTHORITATIVE_INFORMATION=203,u.NO_CONTENT=204,u.RESET_CONTENT=205,u.PARTIAL_CONTENT=206,u.MULTI_STATUS=207,u.ALREADY_REPORTED=208,u.IM_USED=226,u.MULTIPLE_CHOICES=300,u.MOVED_PERMANENTLY=301,u.FOUND=302,u.SEE_OTHER=303,u.NOT_MODIFIED=304,u.USE_PROXY=305,u.TEMPORARY_REDIRECT=307,u.PERMANENT_REDIRECT=308,u.BAD_REQUEST=400,u.UNAUTHORIZED=401,u.PAYMENT_REQUIRED=402,u.FORBIDDEN=403,u.NOT_FOUND=404,u.METHOD_NOT_ALLOWED=405,u.NOT_ACCEPTABLE=406,u.PROXY_AUTHENTICATION_REQUIRED=407,u.REQUEST_TIMEOUT=408,u.CONFLICT=409,u.GONE=410,u.LENGTH_REQUIRED=411,u.PRECONDITION_FAILED=412,u.PAYLOAD_TOO_LARGE=413,u.REQUEST_URI_TOO_LONG=414,u.UNSUPPORTED_MEDIA_TYPE=415,u.REQUESTED_RANGE_NOT_SATISFIABLE=416,u.EXPECTATION_FAILED=417,u.IM_A_TEAPOT=418,u.MISDIRECTED_REQUEST=421,u.UNPROCESSABLE_ENTITY=422,u.LOCKED=423,u.FAILED_DEPENDENCY=424,u.UPGRADE_REQUIRED=426,u.PRECONDITION_REQUIRED=428,u.TOO_MANY_REQUESTS=429,u.REQUEST_HEADER_FIELDS_TOO_LARGE=431,u.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,u.UNAVAILABLE_FOR_LEGAL_REASONS=451,u.CLIENT_CLOSED_REQUEST=499,u.INTERNAL_SERVER_ERROR=500,u.NOT_IMPLEMENTED=501,u.BAD_GATEWAY=502,u.SERVICE_UNAVAILABLE=503,u.GATEWAY_TIMEOUT=504,u.HTTP_VERSION_NOT_SUPPORTED=505,u.VARIANT_ALSO_NEGOCIATES=506,u.INSUFFICIENT_STORAGE=507,u.LOOP_DETECTED=508,u.NOT_EXTENDED=510,u.NETWORK_AUTHENTICATION_REQUIRED=511,u.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class c{static success(e,t){return e.status(u.OK).json({message:t})}static data(e,t){return e.status(u.OK).json(Object.assign({},t))}static error(e,t){return e.status(u.INTERNAL_SERVER_ERROR).json({message:t})}static list(e,t){return e.status(u.OK).json([...t])}static notfound(e,t){return e.status(u.NOT_FOUND).json({message:t})}static validation(e,t){return e.status(u.BAD_REQUEST).json({message:t})}static unauthorized(e,t){return e.status(u.UNAUTHORIZED).json({message:t})}}class R extends e.EventEmitter{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(o,n){var a;super(),this._app=s(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3},this._router=s.Router(),this._controlles=s.Router(),this._options=Object.assign(Object.assign({},this._default),n),this._app.use(s.json()),this._app.disable("x-powered-by"),this._app.use(s.urlencoded({extended:!0})),this._options.cross&&this._app.use(t()),this._app.use(this._router);const E=(null===(a=this._options.path)||void 0===a?void 0:a.length)?this._options.path:"/";if(this._app.use(E,this._controlles),this._app.all(E+"/*",((e,t)=>c.notfound(t,"Invalid API"))),this._options.public){const e=i.resolve(this._options.public);r.existsSync(e)&&this._app.use(s.static(e)),this._app.get("*",((t,s)=>{if(!r.existsSync(e))return c.notfound(s,'File "index.html" not found');s.sendFile(e+"/index.html")}))}o.forEach((t=>{const r=t.constructor,o=this._path(r),n=this._routers(r),a=this._middlewares(r),i=this._parameters(r),E=s.Router();n.forEach((s=>{const o=s.name.toString();if(e.isFunction(t[o])){if(!e.isFunction(E[s.method]))return this.error(new Error('Method "'+s.method+'" Not Found.'),"Express");const r=this._handles(t,o,a,i),n=this._indexes(o,i);E[s.method](s.path,...r,((e,s,r)=>t[o](...this._args(e,s,r,n))))}else this.error(new Error('Router "'+r.name+"."+o+'" Not Found.'),"Express")})),this._controlles.use(o,E)}))}error(t,s){e.isUndefined(s)?console.error(t):console.error(s+":",t)}log(t,s){e.isUndefined(s)?console.error(t):console.error(s+":",t)}_args(e,t,s,r,o){const n=[],a={req:e,res:t,next:s,key:"",app:this,argument:o};return r.forEach((e=>{n[e.index]=e.callback(Object.assign(Object.assign({},a),{key:e.key}))})),n}_handles(t,s,r,o){return r.filter((e=>e.name==s)).map((s=>{if(e.isFunction(t[s.method])){const e=this._indexes(s.method,o);return(r,o,n)=>t[s.method](...this._args(r,o,n,e,s.argument))}this.error(new Error('Middleware "'+s.method+'" Not Found.'),"Express")})).filter(e.isFunction)}_indexes(e,t){const s=[];return t.forEach((t=>t.name==e&&s.push(t))),s}_middlewares(e){return[...d(exports.MetadataKeys.MIDDLEWARE,e,[])].reverse()}_parameters(e){return d(exports.MetadataKeys.PARAMETERS,e,[])}_path(e){return d(exports.MetadataKeys.PATH,e,"/")}_routers(e){return d(exports.MetadataKeys.ROUTERS,e,[])}listen(){return T(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=_.createServer(this._app),this._server.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port+".","Express")))),!this._server){const e=_.createServer(((e,t)=>{const s=80==this._options.port?":80":"";t.writeHead(301,{Location:"https://"+e.headers.host+s+e.url}),t.end()})),t=h.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new a.Server((s=>{s.once("data",(r=>{s.pause(),s.unshift(r),(22===r[0]?t:e).emit("connection",s),process.nextTick((()=>s.resume()))}))})).listen(this._options.port,(()=>this.log("Server started on port "+this._options.port+".","Express"))),this._server=t}this._server||(443==this._options.port&&e.listen(80,(()=>this.log("Server started on port 80","Express"))),t.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port+".","Express"))),this._server=t)}}))}}var O;exports.Methods=void 0,(O=exports.Methods||(exports.Methods={})).GET="get",O.POST="post",O.PUT="put",O.HEAD="head",O.DELETE="delete",O.OPTIONS="options",O.PATCH="patch";const l=e=>t=>(s,r)=>{const o=s.constructor,n=d(exports.MetadataKeys.ROUTERS,o,[]);n.push({path:(null==t?void 0:t.length)?t:"/",method:e,name:r}),Reflect.defineMetadata(exports.MetadataKeys.ROUTERS,n,o)},A=l(exports.Methods.GET),N=l(exports.Methods.POST),x=l(exports.Methods.PUT),I=l(exports.Methods.DELETE),D=l(exports.Methods.PATCH),S=l(exports.Methods.HEAD),M=l(exports.Methods.OPTIONS),P=e=>t=>(s,r,o)=>{const n=s.constructor,a=d(exports.MetadataKeys.PARAMETERS,n,[]);a.push({callback:e,index:o,key:(null==t?void 0:t.length)?t:"",name:r}),Reflect.defineMetadata(exports.MetadataKeys.PARAMETERS,a,n)},U=(t,s)=>e.isUndefined(s)?t:t[s],f=P((({app:e})=>e)),m=P((({argument:e})=>e)),L=P((({req:e,key:t})=>U(e.body,t))),y=P((({req:e,key:t})=>U(e.cookies,t))),C=P((({req:e,key:t})=>U(e.headers,t))),v=P((({next:e})=>e)),g=P((({req:e,key:t})=>U(e.params,t))),F=P((({req:e,key:t})=>U(e.query,t))),H=P((({req:e})=>e)),K=H,b=P((({res:e})=>e)),j=b;exports.App=f,exports.Application=R,exports.Argument=m,exports.Body=L,exports.Controller=e=>t=>Reflect.defineMetadata(exports.MetadataKeys.PATH,(null==e?void 0:e.length)?e:"/",t),exports.Cookies=y,exports.Delete=I,exports.Get=A,exports.Head=S,exports.Headers=C,exports.Metadata=d,exports.Middleware=(e,t)=>(s,r)=>{const o=s.constructor,n=d(exports.MetadataKeys.MIDDLEWARE,o,[]);n.push({name:r,method:e,argument:t}),Reflect.defineMetadata(exports.MetadataKeys.MIDDLEWARE,n,o)},exports.Next=v,exports.Options=M,exports.Params=g,exports.Patch=D,exports.Post=N,exports.Put=x,exports.Query=F,exports.Req=K,exports.Request=H,exports.Res=j,exports.Response=b,exports.Responses=c,exports.Status=u,exports.methodFactory=l,exports.parameterFactory=P;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,22 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v1.0.
|
|
2
|
+
* @andrewcaires/express v1.0.3
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2023 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
7
|
import { EventEmitter, TypeCallbackFunction } from '@andrewcaires/utils.js';
|
|
8
|
-
import {
|
|
8
|
+
import { Request as Request$1, Response as Response$1, NextFunction, Express, Router } from 'express';
|
|
9
9
|
import * as http from 'http';
|
|
10
10
|
import * as https from 'https';
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
static data(res: Response$1, obj: object): ResponseJson;
|
|
20
|
-
error(message: string): ResponseJson;
|
|
21
|
-
static error(res: Response$1, message: string): ResponseJson;
|
|
22
|
-
list(arr: Array<any>): ResponseJson;
|
|
23
|
-
static list(res: Response$1, arr: Array<any>): ResponseJson;
|
|
24
|
-
notfound(message: string): ResponseJson;
|
|
25
|
-
static notfound(res: Response$1, message: string): ResponseJson;
|
|
26
|
-
validation(message: string): ResponseJson;
|
|
27
|
-
static validation(res: Response$1, message: string): ResponseJson;
|
|
28
|
-
unauthorized(message: string): ResponseJson;
|
|
29
|
-
static unauthorized(res: Response$1, message: string): ResponseJson;
|
|
12
|
+
interface IArgs {
|
|
13
|
+
req: Request$1;
|
|
14
|
+
res: Response$1;
|
|
15
|
+
next: NextFunction;
|
|
16
|
+
key: string;
|
|
17
|
+
app: Application;
|
|
18
|
+
argument: any;
|
|
30
19
|
}
|
|
31
|
-
|
|
32
20
|
interface IOptions {
|
|
33
21
|
cert?: Buffer;
|
|
34
22
|
cross?: boolean;
|
|
@@ -38,15 +26,6 @@ interface IOptions {
|
|
|
38
26
|
port?: number;
|
|
39
27
|
public?: string;
|
|
40
28
|
}
|
|
41
|
-
interface IParameters {
|
|
42
|
-
req: Request$1;
|
|
43
|
-
res: Response$1;
|
|
44
|
-
next: NextFunction;
|
|
45
|
-
key: string;
|
|
46
|
-
app: Application;
|
|
47
|
-
send: Responses;
|
|
48
|
-
argument: any;
|
|
49
|
-
}
|
|
50
29
|
declare class Application extends EventEmitter {
|
|
51
30
|
private readonly _app;
|
|
52
31
|
private _server?;
|
|
@@ -98,10 +77,10 @@ declare const methodFactory: (method: Methods) => (path?: string) => MethodDecor
|
|
|
98
77
|
declare const Get: (path?: string) => MethodDecorator;
|
|
99
78
|
declare const Post: (path?: string) => MethodDecorator;
|
|
100
79
|
declare const Put: (path?: string) => MethodDecorator;
|
|
101
|
-
declare const Head: (path?: string) => MethodDecorator;
|
|
102
80
|
declare const Delete: (path?: string) => MethodDecorator;
|
|
103
|
-
declare const Options: (path?: string) => MethodDecorator;
|
|
104
81
|
declare const Patch: (path?: string) => MethodDecorator;
|
|
82
|
+
declare const Head: (path?: string) => MethodDecorator;
|
|
83
|
+
declare const Options: (path?: string) => MethodDecorator;
|
|
105
84
|
|
|
106
85
|
interface IMiddlewares {
|
|
107
86
|
name: string | symbol;
|
|
@@ -129,70 +108,82 @@ declare const Request: (key?: string) => ParameterDecorator;
|
|
|
129
108
|
declare const Req: (key?: string) => ParameterDecorator;
|
|
130
109
|
declare const Response: (key?: string) => ParameterDecorator;
|
|
131
110
|
declare const Res: (key?: string) => ParameterDecorator;
|
|
132
|
-
declare const Send: (key?: string) => ParameterDecorator;
|
|
133
111
|
|
|
134
|
-
|
|
135
|
-
declare
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
declare
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
112
|
+
type ResponseJson = Response$1<any, Record<string, any>>;
|
|
113
|
+
declare class Responses {
|
|
114
|
+
static success(res: Response$1, message: string): ResponseJson;
|
|
115
|
+
static data(res: Response$1, obj: object): ResponseJson;
|
|
116
|
+
static error(res: Response$1, message: string): ResponseJson;
|
|
117
|
+
static list(res: Response$1, arr: Array<any>): ResponseJson;
|
|
118
|
+
static notfound(res: Response$1, message: string): ResponseJson;
|
|
119
|
+
static validation(res: Response$1, message: string): ResponseJson;
|
|
120
|
+
static unauthorized(res: Response$1, message: string): ResponseJson;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
declare class Status {
|
|
124
|
+
static readonly CONTINUE = 100;
|
|
125
|
+
static readonly SWITCHING_PROTOCOLS = 101;
|
|
126
|
+
static readonly PROCESSING = 102;
|
|
127
|
+
static readonly OK = 200;
|
|
128
|
+
static readonly CREATED = 201;
|
|
129
|
+
static readonly ACCEPTED = 202;
|
|
130
|
+
static readonly NON_AUTHORITATIVE_INFORMATION = 203;
|
|
131
|
+
static readonly NO_CONTENT = 204;
|
|
132
|
+
static readonly RESET_CONTENT = 205;
|
|
133
|
+
static readonly PARTIAL_CONTENT = 206;
|
|
134
|
+
static readonly MULTI_STATUS = 207;
|
|
135
|
+
static readonly ALREADY_REPORTED = 208;
|
|
136
|
+
static readonly IM_USED = 226;
|
|
137
|
+
static readonly MULTIPLE_CHOICES = 300;
|
|
138
|
+
static readonly MOVED_PERMANENTLY = 301;
|
|
139
|
+
static readonly FOUND = 302;
|
|
140
|
+
static readonly SEE_OTHER = 303;
|
|
141
|
+
static readonly NOT_MODIFIED = 304;
|
|
142
|
+
static readonly USE_PROXY = 305;
|
|
143
|
+
static readonly TEMPORARY_REDIRECT = 307;
|
|
144
|
+
static readonly PERMANENT_REDIRECT = 308;
|
|
145
|
+
static readonly BAD_REQUEST = 400;
|
|
146
|
+
static readonly UNAUTHORIZED = 401;
|
|
147
|
+
static readonly PAYMENT_REQUIRED = 402;
|
|
148
|
+
static readonly FORBIDDEN = 403;
|
|
149
|
+
static readonly NOT_FOUND = 404;
|
|
150
|
+
static readonly METHOD_NOT_ALLOWED = 405;
|
|
151
|
+
static readonly NOT_ACCEPTABLE = 406;
|
|
152
|
+
static readonly PROXY_AUTHENTICATION_REQUIRED = 407;
|
|
153
|
+
static readonly REQUEST_TIMEOUT = 408;
|
|
154
|
+
static readonly CONFLICT = 409;
|
|
155
|
+
static readonly GONE = 410;
|
|
156
|
+
static readonly LENGTH_REQUIRED = 411;
|
|
157
|
+
static readonly PRECONDITION_FAILED = 412;
|
|
158
|
+
static readonly PAYLOAD_TOO_LARGE = 413;
|
|
159
|
+
static readonly REQUEST_URI_TOO_LONG = 414;
|
|
160
|
+
static readonly UNSUPPORTED_MEDIA_TYPE = 415;
|
|
161
|
+
static readonly REQUESTED_RANGE_NOT_SATISFIABLE = 416;
|
|
162
|
+
static readonly EXPECTATION_FAILED = 417;
|
|
163
|
+
static readonly IM_A_TEAPOT = 418;
|
|
164
|
+
static readonly MISDIRECTED_REQUEST = 421;
|
|
165
|
+
static readonly UNPROCESSABLE_ENTITY = 422;
|
|
166
|
+
static readonly LOCKED = 423;
|
|
167
|
+
static readonly FAILED_DEPENDENCY = 424;
|
|
168
|
+
static readonly UPGRADE_REQUIRED = 426;
|
|
169
|
+
static readonly PRECONDITION_REQUIRED = 428;
|
|
170
|
+
static readonly TOO_MANY_REQUESTS = 429;
|
|
171
|
+
static readonly REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
|
|
172
|
+
static readonly CONNECTION_CLOSED_WITHOUT_RESPONSE = 444;
|
|
173
|
+
static readonly UNAVAILABLE_FOR_LEGAL_REASONS = 451;
|
|
174
|
+
static readonly CLIENT_CLOSED_REQUEST = 499;
|
|
175
|
+
static readonly INTERNAL_SERVER_ERROR = 500;
|
|
176
|
+
static readonly NOT_IMPLEMENTED = 501;
|
|
177
|
+
static readonly BAD_GATEWAY = 502;
|
|
178
|
+
static readonly SERVICE_UNAVAILABLE = 503;
|
|
179
|
+
static readonly GATEWAY_TIMEOUT = 504;
|
|
180
|
+
static readonly HTTP_VERSION_NOT_SUPPORTED = 505;
|
|
181
|
+
static readonly VARIANT_ALSO_NEGOCIATES = 506;
|
|
182
|
+
static readonly INSUFFICIENT_STORAGE = 507;
|
|
183
|
+
static readonly LOOP_DETECTED = 508;
|
|
184
|
+
static readonly NOT_EXTENDED = 510;
|
|
185
|
+
static readonly NETWORK_AUTHENTICATION_REQUIRED = 511;
|
|
186
|
+
static readonly NETWORK_CONNECTION_TIMEOUT_ERROR = 599;
|
|
187
|
+
}
|
|
197
188
|
|
|
198
|
-
export {
|
|
189
|
+
export { App, Application, Argument, Body, Controller, Cookies, Delete, Get, Head, Headers, IArgs, IMiddlewares, IOptions, IParameter, IRouter, Metadata, MetadataKeys, Methods, Middleware, Next, Options, Params, Patch, Post, Put, Query, Req, Request, Res, Response, ResponseJson, Responses, Status, methodFactory, parameterFactory };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/express v1.0.
|
|
2
|
+
* @andrewcaires/express v1.0.3
|
|
3
3
|
* Decorators for express
|
|
4
4
|
* (c) 2023 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
import"reflect-metadata";import{EventEmitter as t,isFunction as e,isUndefined as s}from"@andrewcaires/utils.js";import r from"cors";import o,{Router as
|
|
7
|
+
import"reflect-metadata";import{EventEmitter as t,isFunction as e,isUndefined as s}from"@andrewcaires/utils.js";import r from"cors";import o,{Router as E,json as n,urlencoded as i}from"express";import{existsSync as a}from"fs";import*as _ from"http";import*as T from"https";import{Server as h}from"net";import{resolve as p}from"path";function R(t,e,s,r){return new(s||(s=Promise))((function(o,E){function n(t){try{a(r.next(t))}catch(t){E(t)}}function i(t){try{a(r.throw(t))}catch(t){E(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,i)}a((r=r.apply(t,e||[])).next())}))}var O;!function(t){t.MIDDLEWARE="@express_middleware",t.PATH="@express_path",t.ROUTERS="@express_routers",t.PARAMETERS="@express_parameters"}(O||(O={}));const c=(t,e,s)=>Reflect.hasMetadata(t,e)?Reflect.getMetadata(t,e):s;class u{}u.CONTINUE=100,u.SWITCHING_PROTOCOLS=101,u.PROCESSING=102,u.OK=200,u.CREATED=201,u.ACCEPTED=202,u.NON_AUTHORITATIVE_INFORMATION=203,u.NO_CONTENT=204,u.RESET_CONTENT=205,u.PARTIAL_CONTENT=206,u.MULTI_STATUS=207,u.ALREADY_REPORTED=208,u.IM_USED=226,u.MULTIPLE_CHOICES=300,u.MOVED_PERMANENTLY=301,u.FOUND=302,u.SEE_OTHER=303,u.NOT_MODIFIED=304,u.USE_PROXY=305,u.TEMPORARY_REDIRECT=307,u.PERMANENT_REDIRECT=308,u.BAD_REQUEST=400,u.UNAUTHORIZED=401,u.PAYMENT_REQUIRED=402,u.FORBIDDEN=403,u.NOT_FOUND=404,u.METHOD_NOT_ALLOWED=405,u.NOT_ACCEPTABLE=406,u.PROXY_AUTHENTICATION_REQUIRED=407,u.REQUEST_TIMEOUT=408,u.CONFLICT=409,u.GONE=410,u.LENGTH_REQUIRED=411,u.PRECONDITION_FAILED=412,u.PAYLOAD_TOO_LARGE=413,u.REQUEST_URI_TOO_LONG=414,u.UNSUPPORTED_MEDIA_TYPE=415,u.REQUESTED_RANGE_NOT_SATISFIABLE=416,u.EXPECTATION_FAILED=417,u.IM_A_TEAPOT=418,u.MISDIRECTED_REQUEST=421,u.UNPROCESSABLE_ENTITY=422,u.LOCKED=423,u.FAILED_DEPENDENCY=424,u.UPGRADE_REQUIRED=426,u.PRECONDITION_REQUIRED=428,u.TOO_MANY_REQUESTS=429,u.REQUEST_HEADER_FIELDS_TOO_LARGE=431,u.CONNECTION_CLOSED_WITHOUT_RESPONSE=444,u.UNAVAILABLE_FOR_LEGAL_REASONS=451,u.CLIENT_CLOSED_REQUEST=499,u.INTERNAL_SERVER_ERROR=500,u.NOT_IMPLEMENTED=501,u.BAD_GATEWAY=502,u.SERVICE_UNAVAILABLE=503,u.GATEWAY_TIMEOUT=504,u.HTTP_VERSION_NOT_SUPPORTED=505,u.VARIANT_ALSO_NEGOCIATES=506,u.INSUFFICIENT_STORAGE=507,u.LOOP_DETECTED=508,u.NOT_EXTENDED=510,u.NETWORK_AUTHENTICATION_REQUIRED=511,u.NETWORK_CONNECTION_TIMEOUT_ERROR=599;class N{static success(t,e){return t.status(u.OK).json({message:e})}static data(t,e){return t.status(u.OK).json(Object.assign({},e))}static error(t,e){return t.status(u.INTERNAL_SERVER_ERROR).json({message:e})}static list(t,e){return t.status(u.OK).json([...e])}static notfound(t,e){return t.status(u.NOT_FOUND).json({message:e})}static validation(t,e){return t.status(u.BAD_REQUEST).json({message:e})}static unauthorized(t,e){return t.status(u.UNAUTHORIZED).json({message:e})}}class A extends t{get instance(){return this._app}get router(){return this._router}get server(){return this._server}constructor(t,s){var _;super(),this._app=o(),this._options={},this._default={cross:!1,hybrid:!1,path:"/",port:3e3},this._router=E(),this._controlles=E(),this._options=Object.assign(Object.assign({},this._default),s),this._app.use(n()),this._app.disable("x-powered-by"),this._app.use(i({extended:!0})),this._options.cross&&this._app.use(r()),this._app.use(this._router);const T=(null===(_=this._options.path)||void 0===_?void 0:_.length)?this._options.path:"/";if(this._app.use(T,this._controlles),this._app.all(T+"/*",((t,e)=>N.notfound(e,"Invalid API"))),this._options.public){const t=p(this._options.public);a(t)&&this._app.use(o.static(t)),this._app.get("*",((e,s)=>{if(!a(t))return N.notfound(s,'File "index.html" not found');s.sendFile(t+"/index.html")}))}t.forEach((t=>{const s=t.constructor,r=this._path(s),o=this._routers(s),n=this._middlewares(s),i=this._parameters(s),a=E();o.forEach((r=>{const o=r.name.toString();if(e(t[o])){if(!e(a[r.method]))return this.error(new Error('Method "'+r.method+'" Not Found.'),"Express");const s=this._handles(t,o,n,i),E=this._indexes(o,i);a[r.method](r.path,...s,((e,s,r)=>t[o](...this._args(e,s,r,E))))}else this.error(new Error('Router "'+s.name+"."+o+'" Not Found.'),"Express")})),this._controlles.use(r,a)}))}error(t,e){s(e)?console.error(t):console.error(e+":",t)}log(t,e){s(e)?console.error(t):console.error(e+":",t)}_args(t,e,s,r,o){const E=[],n={req:t,res:e,next:s,key:"",app:this,argument:o};return r.forEach((t=>{E[t.index]=t.callback(Object.assign(Object.assign({},n),{key:t.key}))})),E}_handles(t,s,r,o){return r.filter((t=>t.name==s)).map((s=>{if(e(t[s.method])){const e=this._indexes(s.method,o);return(r,o,E)=>t[s.method](...this._args(r,o,E,e,s.argument))}this.error(new Error('Middleware "'+s.method+'" Not Found.'),"Express")})).filter(e)}_indexes(t,e){const s=[];return e.forEach((e=>e.name==t&&s.push(e))),s}_middlewares(t){return[...c(O.MIDDLEWARE,t,[])].reverse()}_parameters(t){return c(O.PARAMETERS,t,[])}_path(t){return c(O.PATH,t,"/")}_routers(t){return c(O.ROUTERS,t,[])}listen(){return R(this,void 0,void 0,(function*(){if(this._options.cert&&this._options.key||(this._server=_.createServer(this._app),this._server.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port+".","Express")))),!this._server){const t=_.createServer(((t,e)=>{const s=80==this._options.port?":80":"";e.writeHead(301,{Location:"https://"+t.headers.host+s+t.url}),e.end()})),e=T.createServer({key:this._options.key,cert:this._options.cert},this._app);if(this._options.hybrid){new h((s=>{s.once("data",(r=>{s.pause(),s.unshift(r),(22===r[0]?e:t).emit("connection",s),process.nextTick((()=>s.resume()))}))})).listen(this._options.port,(()=>this.log("Server started on port "+this._options.port+".","Express"))),this._server=e}this._server||(443==this._options.port&&t.listen(80,(()=>this.log("Server started on port 80","Express"))),e.listen(this._options.port,(()=>this.log("Server started on port "+this._options.port+".","Express"))),this._server=e)}}))}}const d=t=>e=>Reflect.defineMetadata(O.PATH,(null==t?void 0:t.length)?t:"/",e);var l;!function(t){t.GET="get",t.POST="post",t.PUT="put",t.HEAD="head",t.DELETE="delete",t.OPTIONS="options",t.PATCH="patch"}(l||(l={}));const I=t=>e=>(s,r)=>{const o=s.constructor,E=c(O.ROUTERS,o,[]);E.push({path:(null==e?void 0:e.length)?e:"/",method:t,name:r}),Reflect.defineMetadata(O.ROUTERS,E,o)},D=I(l.GET),S=I(l.POST),m=I(l.PUT),f=I(l.DELETE),U=I(l.PATCH),P=I(l.HEAD),L=I(l.OPTIONS),C=(t,e)=>(s,r)=>{const o=s.constructor,E=c(O.MIDDLEWARE,o,[]);E.push({name:r,method:t,argument:e}),Reflect.defineMetadata(O.MIDDLEWARE,E,o)},M=t=>e=>(s,r,o)=>{const E=s.constructor,n=c(O.PARAMETERS,E,[]);n.push({callback:t,index:o,key:(null==e?void 0:e.length)?e:"",name:r}),Reflect.defineMetadata(O.PARAMETERS,n,E)},g=(t,e)=>s(e)?t:t[e],v=M((({app:t})=>t)),x=M((({argument:t})=>t)),H=M((({req:t,key:e})=>g(t.body,e))),y=M((({req:t,key:e})=>g(t.cookies,e))),F=M((({req:t,key:e})=>g(t.headers,e))),k=M((({next:t})=>t)),G=M((({req:t,key:e})=>g(t.params,e))),Q=M((({req:t,key:e})=>g(t.query,e))),b=M((({req:t})=>t)),w=b,j=M((({res:t})=>t)),Y=j;export{v as App,A as Application,x as Argument,H as Body,d as Controller,y as Cookies,f as Delete,D as Get,P as Head,F as Headers,c as Metadata,O as MetadataKeys,l as Methods,C as Middleware,k as Next,L as Options,G as Params,U as Patch,S as Post,m as Put,Q as Query,w as Req,b as Request,Y as Res,j as Response,N as Responses,u as Status,I as methodFactory,M as parameterFactory};
|