@wilfredojsr/server-node 1.0.0
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/LICENSE +21 -0
- package/http/health-check.http +16 -0
- package/http/http-client.env.json +5 -0
- package/nodemon.json +6 -0
- package/package.json +28 -0
- package/src/commons/application.ts +51 -0
- package/src/commons/configurations.ts +9 -0
- package/src/commons/decorators/routes-register.ts +98 -0
- package/src/main.ts +6 -0
- package/src/modules/health-check/health-check.routes.ts +42 -0
- package/src/routes.ts +1 -0
- package/tsconfig.build.json +3 -0
- package/tsconfig.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wilfredojsr
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/nodemon.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wilfredojsr/server-node",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start:dev": "nodemon",
|
|
8
|
+
"start:prod": "npm run build && cd ./dist && node ./src/main.js",
|
|
9
|
+
"build": "npm run clean && tsc",
|
|
10
|
+
"clean": "rimraf dist"
|
|
11
|
+
},
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/express": "^5.0.3",
|
|
16
|
+
"@types/node": "^24.3.0",
|
|
17
|
+
"nodemon": "^3.1.10",
|
|
18
|
+
"rimraf": "^6.1.3",
|
|
19
|
+
"ts-node": "^10.9.2",
|
|
20
|
+
"typescript": "^5.9.2"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"dotenv": "^17.2.1",
|
|
24
|
+
"express": "^5.1.0",
|
|
25
|
+
"pg": "^8.16.3",
|
|
26
|
+
"reflect-metadata": "^0.2.2"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as express from 'express';
|
|
2
|
+
import { Configurations } from './configurations';
|
|
3
|
+
import {Express} from "express";
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
|
|
6
|
+
export class Application {
|
|
7
|
+
private readonly port = Configurations.get('PORT') || 3000;
|
|
8
|
+
private readonly app: Express;
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
this.app = express();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static build(options?: ApplicationOptions) {
|
|
15
|
+
const newApp = new Application();
|
|
16
|
+
if (!options) {
|
|
17
|
+
console.warn('Routes not provided. Consider using Application.build({ routes: "path/to/routes" })');
|
|
18
|
+
return newApp;
|
|
19
|
+
}
|
|
20
|
+
const pwd = process.cwd();
|
|
21
|
+
const path = join(pwd, options?.routes);
|
|
22
|
+
const routes: Record<any, any> = require(path);
|
|
23
|
+
if (!routes) {
|
|
24
|
+
console.warn('Routes not found');
|
|
25
|
+
return newApp;
|
|
26
|
+
}
|
|
27
|
+
Object.values(routes).forEach(route => (new route()['instance'](newApp.app)));
|
|
28
|
+
return newApp;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
use(middleware: any) {
|
|
32
|
+
this.app.use(middleware);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
pipe(func: (self?: Express) => void) {
|
|
37
|
+
typeof func === 'function' && func(this.app);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
start() {
|
|
42
|
+
this.app.listen(this.port, () => {
|
|
43
|
+
console.log(`Example app listening on port -> ${this.port}`);
|
|
44
|
+
});
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface ApplicationOptions {
|
|
50
|
+
routes: string;
|
|
51
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
|
|
3
|
+
const paramMetadataKey = Symbol('Param');
|
|
4
|
+
const queryMetadataKey = Symbol('Query');
|
|
5
|
+
const bodyMetadataKey = Symbol('Body');
|
|
6
|
+
|
|
7
|
+
export function Routes(path: string) {
|
|
8
|
+
return function (target: any) {
|
|
9
|
+
const constructor = target.prototype.constructor;
|
|
10
|
+
constructor.path = path;
|
|
11
|
+
|
|
12
|
+
target.prototype.instance = (app) => {
|
|
13
|
+
const keys = Object.getOwnPropertyNames(target.prototype);
|
|
14
|
+
keys.forEach(key => {
|
|
15
|
+
if (key !== 'instance' && key !== 'constructor') {
|
|
16
|
+
const el = target.prototype[key];
|
|
17
|
+
if (el instanceof IRoute) {
|
|
18
|
+
const { method, path: routePath, handler } = el;
|
|
19
|
+
const mapped = `/${(path || '').replace('/', '')}${(routePath || '').replace('/', '')}`;
|
|
20
|
+
console.log(`Route -> ${method.toUpperCase()} ${mapped}`);
|
|
21
|
+
app[method.toLowerCase()](mapped, handler);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function Route(method: string, path: string) {
|
|
30
|
+
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
31
|
+
const func = descriptor.value;
|
|
32
|
+
const route = new IRoute();
|
|
33
|
+
route.method = method;
|
|
34
|
+
route.path = path;
|
|
35
|
+
route.handler = (req, res) => {
|
|
36
|
+
let params: { index: number, name: string }[] =
|
|
37
|
+
Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || [];
|
|
38
|
+
let query: { index: number, name: string }[] =
|
|
39
|
+
Reflect.getOwnMetadata(queryMetadataKey, target, propertyKey) || [];
|
|
40
|
+
let body: { index: number, name: string }[] =
|
|
41
|
+
Reflect.getOwnMetadata(bodyMetadataKey, target, propertyKey) || [];
|
|
42
|
+
|
|
43
|
+
const args = [...params, ...query, ...body].sort((a, b) => a.index - b.index)
|
|
44
|
+
.map(param => req.params[param.name] || req.query[param.name] || (param.name === '@@body@@' ? req.body : undefined));
|
|
45
|
+
|
|
46
|
+
res.send(func.apply(target, args));
|
|
47
|
+
};
|
|
48
|
+
descriptor.value = route;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function Param(paramName: string) {
|
|
53
|
+
return function (target: Object, propertyKey: string, parameterIndex: number) {
|
|
54
|
+
let params: { index: number, name: string }[] =
|
|
55
|
+
Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || [];
|
|
56
|
+
|
|
57
|
+
params.push({
|
|
58
|
+
index: parameterIndex,
|
|
59
|
+
name: paramName
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
Reflect.defineMetadata(paramMetadataKey, params, target, propertyKey);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function Query(paramName: string) {
|
|
67
|
+
return function (target: Object, propertyKey: string, parameterIndex: number) {
|
|
68
|
+
let query: { index: number, name: string }[] =
|
|
69
|
+
Reflect.getOwnMetadata(queryMetadataKey, target, propertyKey) || [];
|
|
70
|
+
|
|
71
|
+
query.push({
|
|
72
|
+
index: parameterIndex,
|
|
73
|
+
name: paramName
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
Reflect.defineMetadata(queryMetadataKey, query, target, propertyKey);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function Body() {
|
|
81
|
+
return function (target: Object, propertyKey: string, parameterIndex: number) {
|
|
82
|
+
let body: { index: number, name: string }[] =
|
|
83
|
+
Reflect.getOwnMetadata(bodyMetadataKey, target, propertyKey) || [];
|
|
84
|
+
|
|
85
|
+
body.push({
|
|
86
|
+
index: parameterIndex,
|
|
87
|
+
name: '@@body@@'
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
Reflect.defineMetadata(bodyMetadataKey, body, target, propertyKey);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
class IRoute {
|
|
95
|
+
method: string;
|
|
96
|
+
path: string;
|
|
97
|
+
handler: (req: any, res: any) => any;
|
|
98
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Body, Param, Query, Route, Routes } from '../../commons/decorators/routes-register';
|
|
2
|
+
|
|
3
|
+
@Routes('/')
|
|
4
|
+
export class HealthCheckRoutes {
|
|
5
|
+
|
|
6
|
+
@Route('get', '/')
|
|
7
|
+
public healthCheck() {
|
|
8
|
+
return {
|
|
9
|
+
status: 'OK'
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
@Route('get', '/ping')
|
|
14
|
+
public ping() {
|
|
15
|
+
return {
|
|
16
|
+
status: 'Ping'
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Route('get', '/dynamic/:id/ping/:name')
|
|
21
|
+
public pingDynamic(@Param('id') id: string, @Param('name') name: string, @Query('t') t: string) {
|
|
22
|
+
return {
|
|
23
|
+
id,
|
|
24
|
+
name,
|
|
25
|
+
t
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Route('post', '/dynamic/:id/ping/:name')
|
|
30
|
+
public pingDynamicPost(@Param('id') id: string,
|
|
31
|
+
@Param('name') name: string,
|
|
32
|
+
@Query('t') t: string,
|
|
33
|
+
@Body() body: any
|
|
34
|
+
) {
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
name,
|
|
38
|
+
t,
|
|
39
|
+
body
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/routes.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './modules/health-check/health-check.routes'
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "commonjs",
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"removeComments": true,
|
|
6
|
+
"emitDecoratorMetadata": true,
|
|
7
|
+
"experimentalDecorators": true,
|
|
8
|
+
"allowSyntheticDefaultImports": true,
|
|
9
|
+
"target": "es2017",
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"outDir": "./dist/src",
|
|
12
|
+
"baseUrl": "./",
|
|
13
|
+
"incremental": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"strictNullChecks": false,
|
|
16
|
+
"noImplicitAny": false,
|
|
17
|
+
"strictBindCallApply": false,
|
|
18
|
+
"forceConsistentCasingInFileNames": false,
|
|
19
|
+
"noFallthroughCasesInSwitch": false,
|
|
20
|
+
"resolveJsonModule": true
|
|
21
|
+
},
|
|
22
|
+
"include": [
|
|
23
|
+
"src/**/*"
|
|
24
|
+
],
|
|
25
|
+
"exclude": [
|
|
26
|
+
"node_modules",
|
|
27
|
+
"test",
|
|
28
|
+
"dist",
|
|
29
|
+
"http",
|
|
30
|
+
"**/*spec.ts"
|
|
31
|
+
]
|
|
32
|
+
}
|