@naman_deep_singh/server-utils 1.0.1 → 1.0.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/server.d.ts +1 -0
- package/dist/server.js +48 -0
- package/package.json +1 -1
- package/src/server.ts +52 -0
package/dist/server.d.ts
CHANGED
|
@@ -55,6 +55,7 @@ export declare class ExpressServer implements ServerInstance {
|
|
|
55
55
|
private rpcMethods;
|
|
56
56
|
private socketIO?;
|
|
57
57
|
constructor(name?: string, version?: string, config?: ServerConfig);
|
|
58
|
+
private setupMiddleware;
|
|
58
59
|
start(): Promise<ServerInstance>;
|
|
59
60
|
stop(): Promise<void>;
|
|
60
61
|
getInfo(): ServerInfo;
|
package/dist/server.js
CHANGED
|
@@ -27,6 +27,54 @@ class ExpressServer {
|
|
|
27
27
|
gracefulShutdown: config.gracefulShutdown ?? true,
|
|
28
28
|
socketIO: config.socketIO
|
|
29
29
|
};
|
|
30
|
+
// Apply middleware based on configuration
|
|
31
|
+
this.setupMiddleware();
|
|
32
|
+
}
|
|
33
|
+
setupMiddleware() {
|
|
34
|
+
// Apply CORS if enabled
|
|
35
|
+
if (this.config.cors) {
|
|
36
|
+
try {
|
|
37
|
+
const cors = require('cors');
|
|
38
|
+
const corsOptions = typeof this.config.cors === 'object' ? this.config.cors : undefined;
|
|
39
|
+
this.app.use(cors(corsOptions));
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.warn('CORS middleware not available. Install cors package.');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Apply Helmet if enabled
|
|
46
|
+
if (this.config.helmet) {
|
|
47
|
+
try {
|
|
48
|
+
const helmet = require('helmet');
|
|
49
|
+
this.app.use(helmet());
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.warn('Helmet middleware not available. Install helmet package.');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Apply JSON parser if enabled
|
|
56
|
+
if (this.config.json) {
|
|
57
|
+
this.app.use(express_1.default.json());
|
|
58
|
+
}
|
|
59
|
+
// Apply custom middleware
|
|
60
|
+
if (this.config.customMiddleware && this.config.customMiddleware.length > 0) {
|
|
61
|
+
this.config.customMiddleware.forEach(middleware => {
|
|
62
|
+
this.app.use(middleware);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
// Add health check if enabled
|
|
66
|
+
if (this.config.healthCheck) {
|
|
67
|
+
const healthPath = typeof this.config.healthCheck === 'string' ? this.config.healthCheck : '/health';
|
|
68
|
+
this.app.get(healthPath, (req, res) => {
|
|
69
|
+
res.status(200).json({
|
|
70
|
+
status: 'healthy',
|
|
71
|
+
service: this.config.name,
|
|
72
|
+
version: this.config.version,
|
|
73
|
+
uptime: Date.now() - this.config.startTime.getTime(),
|
|
74
|
+
timestamp: new Date().toISOString()
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
30
78
|
}
|
|
31
79
|
async start() {
|
|
32
80
|
this.status = 'starting';
|
package/package.json
CHANGED
package/src/server.ts
CHANGED
|
@@ -88,6 +88,58 @@ export class ExpressServer implements ServerInstance {
|
|
|
88
88
|
gracefulShutdown: config.gracefulShutdown ?? true,
|
|
89
89
|
socketIO: config.socketIO
|
|
90
90
|
};
|
|
91
|
+
|
|
92
|
+
// Apply middleware based on configuration
|
|
93
|
+
this.setupMiddleware();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private setupMiddleware(): void {
|
|
97
|
+
// Apply CORS if enabled
|
|
98
|
+
if (this.config.cors) {
|
|
99
|
+
try {
|
|
100
|
+
const cors = require('cors');
|
|
101
|
+
const corsOptions = typeof this.config.cors === 'object' ? this.config.cors : undefined;
|
|
102
|
+
this.app.use(cors(corsOptions));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.warn('CORS middleware not available. Install cors package.');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Apply Helmet if enabled
|
|
109
|
+
if (this.config.helmet) {
|
|
110
|
+
try {
|
|
111
|
+
const helmet = require('helmet');
|
|
112
|
+
this.app.use(helmet());
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.warn('Helmet middleware not available. Install helmet package.');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Apply JSON parser if enabled
|
|
119
|
+
if (this.config.json) {
|
|
120
|
+
this.app.use(express.json());
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Apply custom middleware
|
|
124
|
+
if (this.config.customMiddleware && this.config.customMiddleware.length > 0) {
|
|
125
|
+
this.config.customMiddleware.forEach(middleware => {
|
|
126
|
+
this.app.use(middleware);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Add health check if enabled
|
|
131
|
+
if (this.config.healthCheck) {
|
|
132
|
+
const healthPath = typeof this.config.healthCheck === 'string' ? this.config.healthCheck : '/health';
|
|
133
|
+
this.app.get(healthPath, (req, res) => {
|
|
134
|
+
res.status(200).json({
|
|
135
|
+
status: 'healthy',
|
|
136
|
+
service: this.config.name,
|
|
137
|
+
version: this.config.version,
|
|
138
|
+
uptime: Date.now() - this.config.startTime.getTime(),
|
|
139
|
+
timestamp: new Date().toISOString()
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
91
143
|
}
|
|
92
144
|
|
|
93
145
|
async start(): Promise<ServerInstance> {
|