@expressots/adapter-express 1.2.1 → 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/lib/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
 
2
2
 
3
+ ## [1.2.2](https://github.com/expressots/adapter-express/compare/1.2.1...1.2.2) (2024-04-25)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * validate parameter types in param decorator by default ([b7a8657](https://github.com/expressots/adapter-express/commit/b7a8657598ce51a6fa44c3a388d38106b47ad70d))
9
+
3
10
  ## [1.2.1](https://github.com/expressots/adapter-express/compare/1.2.0...1.2.1) (2024-04-04)
4
11
 
5
12
 
@@ -1,10 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.params = exports.Principal = exports.next = exports.cookies = exports.headers = exports.body = exports.query = exports.param = exports.response = exports.request = exports.httpMethod = exports.Delete = exports.Head = exports.Patch = exports.Put = exports.Post = exports.Get = exports.all = exports.controller = exports.injectHttpContext = void 0;
3
+ exports.params = exports.principal = exports.next = exports.cookies = exports.headers = exports.body = exports.query = exports.param = exports.response = exports.request = exports.httpMethod = exports.Delete = exports.Head = exports.Patch = exports.Put = exports.Post = exports.Get = exports.All = exports.controller = exports.injectHttpContext = void 0;
4
4
  require("reflect-metadata");
5
5
  const inversify_1 = require("inversify");
6
6
  const constants_1 = require("./constants");
7
7
  exports.injectHttpContext = (0, inversify_1.inject)(constants_1.TYPE.HttpContext);
8
+ /**
9
+ * Controller decorator to define a new controller
10
+ * @param path route path
11
+ * @param middleware array of middleware to be applied to all routes in the controller
12
+ */
8
13
  function controller(path, ...middleware) {
9
14
  return (target) => {
10
15
  const currentMetadata = {
@@ -20,34 +25,99 @@ function controller(path, ...middleware) {
20
25
  };
21
26
  }
22
27
  exports.controller = controller;
23
- function all(path, ...middleware) {
28
+ /**
29
+ * Decorator to allow accept all HTTP methods
30
+ * @param path route path, wildcard
31
+ * @param middleware array of middleware to be applied to all routes defined in path logic
32
+ */
33
+ function All(path, ...middleware) {
24
34
  return httpMethod("all", path, ...middleware);
25
35
  }
26
- exports.all = all;
36
+ exports.All = All;
37
+ /**
38
+ * Decorator to allow GET HTTP method
39
+ * @param path route path
40
+ * @param middleware array of middleware to be applied to the route
41
+ */
27
42
  function Get(path, ...middleware) {
28
- return httpMethod("get", path, ...middleware);
43
+ return enhancedHttpMethod("get", path, ...middleware);
29
44
  }
30
45
  exports.Get = Get;
46
+ /**
47
+ * Decorator to allow POST HTTP method
48
+ * @param path route path
49
+ * @param middleware array of middleware to be applied to the route
50
+ */
31
51
  function Post(path, ...middleware) {
32
52
  return httpMethod("post", path, ...middleware);
33
53
  }
34
54
  exports.Post = Post;
55
+ /**
56
+ * Decorator to allow PUT HTTP method
57
+ * @param path route path
58
+ * @param middleware array of middleware to be applied to the route
59
+ */
35
60
  function Put(path, ...middleware) {
36
- return httpMethod("put", path, ...middleware);
61
+ return enhancedHttpMethod("put", path, ...middleware);
37
62
  }
38
63
  exports.Put = Put;
64
+ /**
65
+ * Decorator to allow PATCH HTTP method
66
+ * @param path route path
67
+ * @param middleware array of middleware to be applied to the route
68
+ */
39
69
  function Patch(path, ...middleware) {
40
- return httpMethod("patch", path, ...middleware);
70
+ return enhancedHttpMethod("patch", path, ...middleware);
41
71
  }
42
72
  exports.Patch = Patch;
73
+ /**
74
+ * Decorator to allow HEAD HTTP method
75
+ * @param path route path
76
+ * @param middleware array of middleware to be applied to the route
77
+ */
43
78
  function Head(path, ...middleware) {
44
79
  return httpMethod("head", path, ...middleware);
45
80
  }
46
81
  exports.Head = Head;
82
+ /**
83
+ * Decorator to allow DELETE HTTP method
84
+ * @param path route path
85
+ * @param middleware array of middleware to be applied to the route
86
+ */
47
87
  function Delete(path, ...middleware) {
48
- return httpMethod("delete", path, ...middleware);
88
+ return enhancedHttpMethod("delete", path, ...middleware);
49
89
  }
50
90
  exports.Delete = Delete;
91
+ function enhancedHttpMethod(method, path, ...middleware) {
92
+ return (target, key) => {
93
+ const metadata = {
94
+ key,
95
+ method,
96
+ middleware,
97
+ path,
98
+ target,
99
+ };
100
+ let metadataList = [];
101
+ if (!Reflect.hasOwnMetadata(constants_1.METADATA_KEY.controllerMethod, target.constructor)) {
102
+ Reflect.defineMetadata(constants_1.METADATA_KEY.controllerMethod, metadataList, target.constructor);
103
+ }
104
+ else {
105
+ metadataList = Reflect.getOwnMetadata(constants_1.METADATA_KEY.controllerMethod, target.constructor);
106
+ }
107
+ metadataList.push(metadata);
108
+ const paramsInfo = Reflect.getMetadata("design:paramtypes", target, key) || [];
109
+ metadataList.forEach((m) => {
110
+ m.middleware.unshift((req, res, next) => {
111
+ req.params &&
112
+ Object.keys(req.params).forEach((param, idx) => {
113
+ const type = paramsInfo[idx];
114
+ req.params[param] = convertToType(req.params[param], type);
115
+ });
116
+ next();
117
+ });
118
+ });
119
+ };
120
+ }
51
121
  function httpMethod(method, path, ...middleware) {
52
122
  return (target, key) => {
53
123
  const metadata = {
@@ -68,18 +138,51 @@ function httpMethod(method, path, ...middleware) {
68
138
  };
69
139
  }
70
140
  exports.httpMethod = httpMethod;
71
- /*
72
- * Parameter Decorators
141
+ /**
142
+ * Parameter decorator to inject the request object
143
+ * @returns ParameterDecorator
73
144
  */
74
145
  exports.request = paramDecoratorFactory(constants_1.PARAMETER_TYPE.REQUEST);
146
+ /**
147
+ * Parameter decorator to inject the response object
148
+ * @returns ParameterDecorator
149
+ */
75
150
  exports.response = paramDecoratorFactory(constants_1.PARAMETER_TYPE.RESPONSE);
151
+ /**
152
+ * Parameter decorator to inject parameters from the route
153
+ * @returns ParameterDecorator
154
+ */
76
155
  exports.param = paramDecoratorFactory(constants_1.PARAMETER_TYPE.PARAMS);
156
+ /**
157
+ * Parameter decorator to inject query parameters
158
+ * @returns ParameterDecorator
159
+ */
77
160
  exports.query = paramDecoratorFactory(constants_1.PARAMETER_TYPE.QUERY);
161
+ /**
162
+ * Parameter decorator to inject the request body
163
+ * @returns ParameterDecorator
164
+ */
78
165
  exports.body = paramDecoratorFactory(constants_1.PARAMETER_TYPE.BODY);
166
+ /**
167
+ * Parameter decorator to inject the request headers
168
+ * @returns ParameterDecorator
169
+ */
79
170
  exports.headers = paramDecoratorFactory(constants_1.PARAMETER_TYPE.HEADERS);
171
+ /**
172
+ * Parameter decorator to inject the request cookies
173
+ * @returns ParameterDecorator
174
+ */
80
175
  exports.cookies = paramDecoratorFactory(constants_1.PARAMETER_TYPE.COOKIES);
176
+ /**
177
+ * Parameter decorator next function
178
+ * @returns ParameterDecorator
179
+ */
81
180
  exports.next = paramDecoratorFactory(constants_1.PARAMETER_TYPE.NEXT);
82
- exports.Principal = paramDecoratorFactory(constants_1.PARAMETER_TYPE.PRINCIPAL);
181
+ /**
182
+ * Parameter decorator to inject the principal object obtained from AuthProvider
183
+ * @returns ParameterDecorator
184
+ */
185
+ exports.principal = paramDecoratorFactory(constants_1.PARAMETER_TYPE.PRINCIPAL);
83
186
  function paramDecoratorFactory(parameterType) {
84
187
  return (name) => params(parameterType, name);
85
188
  }
@@ -108,3 +211,15 @@ function params(type, parameterName) {
108
211
  };
109
212
  }
110
213
  exports.params = params;
214
+ function convertToType(value, type) {
215
+ if (type === Number) {
216
+ return Number(value);
217
+ }
218
+ else if (type === String) {
219
+ return value;
220
+ }
221
+ else if (type === Boolean) {
222
+ return value === "true" || value === "1";
223
+ }
224
+ return value;
225
+ }
@@ -1,23 +1,99 @@
1
1
  import "reflect-metadata";
2
2
  import { PARAMETER_TYPE, HTTP_VERBS_ENUM } from "./constants";
3
- import type { Controller, HandlerDecorator, Middleware } from "./interfaces";
3
+ import type { HandlerDecorator, Middleware } from "./interfaces";
4
4
  export declare const injectHttpContext: (target: import("inversify/lib/annotation/decorator_utils").DecoratorTarget<unknown>, targetKey?: string | symbol, indexOrPropertyDescriptor?: number | TypedPropertyDescriptor<unknown>) => void;
5
+ /**
6
+ * Controller decorator to define a new controller
7
+ * @param path route path
8
+ * @param middleware array of middleware to be applied to all routes in the controller
9
+ */
5
10
  export declare function controller(path: string, ...middleware: Array<Middleware>): (target: NewableFunction) => void;
6
- export declare function all(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
11
+ /**
12
+ * Decorator to allow accept all HTTP methods
13
+ * @param path route path, wildcard
14
+ * @param middleware array of middleware to be applied to all routes defined in path logic
15
+ */
16
+ export declare function All(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
17
+ /**
18
+ * Decorator to allow GET HTTP method
19
+ * @param path route path
20
+ * @param middleware array of middleware to be applied to the route
21
+ */
7
22
  export declare function Get(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
23
+ /**
24
+ * Decorator to allow POST HTTP method
25
+ * @param path route path
26
+ * @param middleware array of middleware to be applied to the route
27
+ */
8
28
  export declare function Post(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
29
+ /**
30
+ * Decorator to allow PUT HTTP method
31
+ * @param path route path
32
+ * @param middleware array of middleware to be applied to the route
33
+ */
9
34
  export declare function Put(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
35
+ /**
36
+ * Decorator to allow PATCH HTTP method
37
+ * @param path route path
38
+ * @param middleware array of middleware to be applied to the route
39
+ */
10
40
  export declare function Patch(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
41
+ /**
42
+ * Decorator to allow HEAD HTTP method
43
+ * @param path route path
44
+ * @param middleware array of middleware to be applied to the route
45
+ */
11
46
  export declare function Head(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
47
+ /**
48
+ * Decorator to allow DELETE HTTP method
49
+ * @param path route path
50
+ * @param middleware array of middleware to be applied to the route
51
+ */
12
52
  export declare function Delete(path: string, ...middleware: Array<Middleware>): HandlerDecorator;
13
53
  export declare function httpMethod(method: keyof typeof HTTP_VERBS_ENUM, path: string, ...middleware: Array<Middleware>): HandlerDecorator;
54
+ /**
55
+ * Parameter decorator to inject the request object
56
+ * @returns ParameterDecorator
57
+ */
14
58
  export declare const request: () => ParameterDecorator;
59
+ /**
60
+ * Parameter decorator to inject the response object
61
+ * @returns ParameterDecorator
62
+ */
15
63
  export declare const response: () => ParameterDecorator;
64
+ /**
65
+ * Parameter decorator to inject parameters from the route
66
+ * @returns ParameterDecorator
67
+ */
16
68
  export declare const param: (paramName?: string) => ParameterDecorator;
69
+ /**
70
+ * Parameter decorator to inject query parameters
71
+ * @returns ParameterDecorator
72
+ */
17
73
  export declare const query: (queryParamName?: string) => ParameterDecorator;
74
+ /**
75
+ * Parameter decorator to inject the request body
76
+ * @returns ParameterDecorator
77
+ */
18
78
  export declare const body: () => ParameterDecorator;
79
+ /**
80
+ * Parameter decorator to inject the request headers
81
+ * @returns ParameterDecorator
82
+ */
19
83
  export declare const headers: (headerName?: string) => ParameterDecorator;
84
+ /**
85
+ * Parameter decorator to inject the request cookies
86
+ * @returns ParameterDecorator
87
+ */
20
88
  export declare const cookies: (cookieName?: string) => ParameterDecorator;
89
+ /**
90
+ * Parameter decorator next function
91
+ * @returns ParameterDecorator
92
+ */
21
93
  export declare const next: () => ParameterDecorator;
22
- export declare const Principal: () => ParameterDecorator;
23
- export declare function params(type: PARAMETER_TYPE, parameterName?: string): (target: unknown | Controller, methodName: string | symbol, index: number) => void;
94
+ /**
95
+ * Parameter decorator to inject the principal object obtained from AuthProvider
96
+ * @returns ParameterDecorator
97
+ */
98
+ export declare const principal: () => ParameterDecorator;
99
+ export declare function params(type: PARAMETER_TYPE, parameterName?: string): ParameterDecorator;
package/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressots/adapter-express",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Expressots - modern, fast, lightweight nodejs web framework (@adapter-express)",
5
5
  "author": "",
6
6
  "main": "./lib/cjs/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressots/adapter-express",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Expressots - modern, fast, lightweight nodejs web framework (@adapter-express)",
5
5
  "author": "",
6
6
  "main": "./lib/cjs/index.js",