wnodex 0.1.0 → 0.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/.turbo/turbo-build.log +7 -0
- package/LICENSE +1 -1
- package/README.md +73 -67
- package/dist/{schemas/wnodex-config.d.ts → config.d.ts} +14 -9
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +22 -0
- package/dist/index.d.ts +68 -38
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +126 -41
- package/dist/setup-config.d.ts +3 -0
- package/dist/setup-config.d.ts.map +1 -0
- package/dist/setup-config.js +9 -0
- package/dist/setup-middlewares.d.ts +4 -0
- package/dist/setup-middlewares.d.ts.map +1 -0
- package/dist/setup-middlewares.js +32 -0
- package/package.json +51 -15
- package/rolldown.config.ts +20 -0
- package/src/config.ts +27 -0
- package/src/index.ts +149 -38
- package/src/setup-config.ts +17 -0
- package/src/setup-middlewares.ts +49 -0
- package/tests/body-parser.schema.test.ts +57 -0
- package/tests/compression.schema.test.ts +27 -0
- package/tests/cookie-parser.schema.test.ts +56 -0
- package/tests/cors.schema.test.ts +35 -0
- package/tests/helmet.schema.test.ts +33 -0
- package/tests/hpp.schema.test.ts +31 -0
- package/tests/index.test.ts +105 -0
- package/tests/passport.schema.test.ts +33 -0
- package/tests/rate-limit.schema.test.ts +56 -0
- package/tests/session.schema.test.ts +80 -0
- package/tsconfig.json +8 -8
- package/vitest.config.ts +13 -0
- package/dist/consts/cors.d.ts +0 -15
- package/dist/consts/cors.d.ts.map +0 -1
- package/dist/consts/cors.js +0 -29
- package/dist/consts/error-codes.d.ts +0 -10
- package/dist/consts/error-codes.d.ts.map +0 -1
- package/dist/consts/error-codes.js +0 -12
- package/dist/consts/index.d.ts +0 -8
- package/dist/consts/index.d.ts.map +0 -1
- package/dist/consts/index.js +0 -10
- package/dist/consts/log-levels.d.ts +0 -8
- package/dist/consts/log-levels.d.ts.map +0 -1
- package/dist/consts/log-levels.js +0 -10
- package/dist/consts/node-env.d.ts +0 -7
- package/dist/consts/node-env.d.ts.map +0 -1
- package/dist/consts/node-env.js +0 -9
- package/dist/consts/port.d.ts +0 -6
- package/dist/consts/port.d.ts.map +0 -1
- package/dist/consts/port.js +0 -26
- package/dist/consts/rate-limit.d.ts +0 -9
- package/dist/consts/rate-limit.d.ts.map +0 -1
- package/dist/consts/rate-limit.js +0 -11
- package/dist/consts/session.d.ts +0 -6
- package/dist/consts/session.d.ts.map +0 -1
- package/dist/consts/session.js +0 -8
- package/dist/errors/base-error.d.ts +0 -57
- package/dist/errors/base-error.d.ts.map +0 -1
- package/dist/errors/base-error.js +0 -93
- package/dist/errors/config-error.d.ts +0 -20
- package/dist/errors/config-error.d.ts.map +0 -1
- package/dist/errors/config-error.js +0 -30
- package/dist/errors/http-error.d.ts +0 -17
- package/dist/errors/http-error.d.ts.map +0 -1
- package/dist/errors/http-error.js +0 -24
- package/dist/errors/index.d.ts +0 -5
- package/dist/errors/index.d.ts.map +0 -1
- package/dist/errors/index.js +0 -7
- package/dist/errors/validation-error.d.ts +0 -19
- package/dist/errors/validation-error.d.ts.map +0 -1
- package/dist/errors/validation-error.js +0 -29
- package/dist/helpers/get-node-env.d.ts +0 -13
- package/dist/helpers/get-node-env.d.ts.map +0 -1
- package/dist/helpers/get-node-env.js +0 -27
- package/dist/helpers/index.d.ts +0 -5
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/index.js +0 -7
- package/dist/helpers/is-development.d.ts +0 -9
- package/dist/helpers/is-development.d.ts.map +0 -1
- package/dist/helpers/is-development.js +0 -16
- package/dist/helpers/setup-config.d.ts +0 -12
- package/dist/helpers/setup-config.d.ts.map +0 -1
- package/dist/helpers/setup-config.js +0 -21
- package/dist/helpers/setup-middlewares.d.ts +0 -12
- package/dist/helpers/setup-middlewares.d.ts.map +0 -1
- package/dist/helpers/setup-middlewares.js +0 -45
- package/dist/logger.d.ts +0 -3
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -21
- package/dist/middlewares/compression.d.ts +0 -18
- package/dist/middlewares/compression.d.ts.map +0 -1
- package/dist/middlewares/compression.js +0 -26
- package/dist/middlewares/cookie-parser.ts.d.ts +0 -20
- package/dist/middlewares/cookie-parser.ts.d.ts.map +0 -1
- package/dist/middlewares/cookie-parser.ts.js +0 -33
- package/dist/middlewares/cors.d.ts +0 -22
- package/dist/middlewares/cors.d.ts.map +0 -1
- package/dist/middlewares/cors.js +0 -34
- package/dist/middlewares/error-handler.d.ts +0 -21
- package/dist/middlewares/error-handler.d.ts.map +0 -1
- package/dist/middlewares/error-handler.js +0 -43
- package/dist/middlewares/helmet.d.ts +0 -16
- package/dist/middlewares/helmet.d.ts.map +0 -1
- package/dist/middlewares/helmet.js +0 -28
- package/dist/middlewares/hpp.d.ts +0 -18
- package/dist/middlewares/hpp.d.ts.map +0 -1
- package/dist/middlewares/hpp.js +0 -30
- package/dist/middlewares/index.d.ts +0 -10
- package/dist/middlewares/index.d.ts.map +0 -1
- package/dist/middlewares/index.js +0 -12
- package/dist/middlewares/passport.d.ts +0 -16
- package/dist/middlewares/passport.d.ts.map +0 -1
- package/dist/middlewares/passport.js +0 -27
- package/dist/middlewares/rate-limit.d.ts +0 -23
- package/dist/middlewares/rate-limit.d.ts.map +0 -1
- package/dist/middlewares/rate-limit.js +0 -35
- package/dist/middlewares/session.d.ts +0 -16
- package/dist/middlewares/session.d.ts.map +0 -1
- package/dist/middlewares/session.js +0 -23
- package/dist/schemas/compression.d.ts +0 -5
- package/dist/schemas/compression.d.ts.map +0 -1
- package/dist/schemas/compression.js +0 -5
- package/dist/schemas/cookie-parser.d.ts +0 -9
- package/dist/schemas/cookie-parser.d.ts.map +0 -1
- package/dist/schemas/cookie-parser.js +0 -14
- package/dist/schemas/cors.d.ts +0 -6
- package/dist/schemas/cors.d.ts.map +0 -1
- package/dist/schemas/cors.js +0 -9
- package/dist/schemas/error-metadata.d.ts +0 -18
- package/dist/schemas/error-metadata.d.ts.map +0 -1
- package/dist/schemas/error-metadata.js +0 -12
- package/dist/schemas/helmet.d.ts +0 -6
- package/dist/schemas/helmet.d.ts.map +0 -1
- package/dist/schemas/helmet.js +0 -8
- package/dist/schemas/hpp.d.ts +0 -5
- package/dist/schemas/hpp.d.ts.map +0 -1
- package/dist/schemas/hpp.js +0 -8
- package/dist/schemas/index.d.ts +0 -13
- package/dist/schemas/index.d.ts.map +0 -1
- package/dist/schemas/index.js +0 -15
- package/dist/schemas/node-env.d.ts +0 -16
- package/dist/schemas/node-env.d.ts.map +0 -1
- package/dist/schemas/node-env.js +0 -8
- package/dist/schemas/passport.d.ts +0 -6
- package/dist/schemas/passport.d.ts.map +0 -1
- package/dist/schemas/passport.js +0 -8
- package/dist/schemas/port.d.ts +0 -4
- package/dist/schemas/port.d.ts.map +0 -1
- package/dist/schemas/port.js +0 -16
- package/dist/schemas/rate-limit.d.ts +0 -9
- package/dist/schemas/rate-limit.d.ts.map +0 -1
- package/dist/schemas/rate-limit.js +0 -26
- package/dist/schemas/session.d.ts +0 -18
- package/dist/schemas/session.d.ts.map +0 -1
- package/dist/schemas/session.js +0 -32
- package/dist/schemas/wnodex-config.d.ts.map +0 -1
- package/dist/schemas/wnodex-config.js +0 -24
- package/dist/tsconfig.lib.tsbuildinfo +0 -1
- package/dist/wnodex.d.ts +0 -69
- package/dist/wnodex.d.ts.map +0 -1
- package/dist/wnodex.js +0 -131
- package/src/consts/cors.ts +0 -27
- package/src/consts/error-codes.ts +0 -9
- package/src/consts/index.ts +0 -7
- package/src/consts/log-levels.ts +0 -7
- package/src/consts/node-env.ts +0 -6
- package/src/consts/port.ts +0 -23
- package/src/consts/rate-limit.ts +0 -9
- package/src/consts/session.ts +0 -6
- package/src/errors/base-error.ts +0 -111
- package/src/errors/config-error.ts +0 -31
- package/src/errors/http-error.ts +0 -21
- package/src/errors/index.ts +0 -4
- package/src/errors/validation-error.ts +0 -30
- package/src/helpers/get-node-env.ts +0 -27
- package/src/helpers/index.ts +0 -4
- package/src/helpers/is-development.ts +0 -15
- package/src/helpers/setup-config.ts +0 -25
- package/src/helpers/setup-middlewares.ts +0 -56
- package/src/logger.ts +0 -22
- package/src/middlewares/compression.ts +0 -32
- package/src/middlewares/cookie-parser.ts.ts +0 -37
- package/src/middlewares/cors.ts +0 -35
- package/src/middlewares/error-handler.ts +0 -54
- package/src/middlewares/helmet.ts +0 -29
- package/src/middlewares/hpp.ts +0 -31
- package/src/middlewares/index.ts +0 -9
- package/src/middlewares/passport.ts +0 -30
- package/src/middlewares/rate-limit.ts +0 -39
- package/src/middlewares/session.ts +0 -25
- package/src/schemas/compression.ts +0 -8
- package/src/schemas/cookie-parser.ts +0 -21
- package/src/schemas/cors.ts +0 -13
- package/src/schemas/error-metadata.ts +0 -15
- package/src/schemas/helmet.ts +0 -11
- package/src/schemas/hpp.ts +0 -9
- package/src/schemas/index.ts +0 -12
- package/src/schemas/node-env.ts +0 -11
- package/src/schemas/passport.ts +0 -11
- package/src/schemas/port.ts +0 -22
- package/src/schemas/rate-limit.ts +0 -33
- package/src/schemas/session.ts +0 -40
- package/src/schemas/wnodex-config.ts +0 -26
- package/src/wnodex.ts +0 -152
- package/tsconfig.lib.json +0 -13
- package/tsconfig.tsbuildinfo +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,103 +1,109 @@
|
|
|
1
1
|
# wnodex
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- [Features](#features)
|
|
8
|
-
- [Usage](#usage)
|
|
9
|
-
- [Import and Instantiate](#import-and-instantiate)
|
|
10
|
-
- [Access the Logger](#access-the-logger)
|
|
11
|
-
- [Get Validated Config](#get-validated-config)
|
|
12
|
-
- [Access the Express App](#access-the-express-app)
|
|
13
|
-
- [Start the Server](#start-the-server)
|
|
14
|
-
- [Graceful Shutdown](#graceful-shutdown)
|
|
15
|
-
- [API Reference](#api-reference)
|
|
16
|
-
- [class Wnodex](#class-wnodex)
|
|
17
|
-
- [Constructor](#constructor)
|
|
18
|
-
- [Methods](#methods)
|
|
19
|
-
- [License](#license)
|
|
3
|
+
> Web Node Express: an extensible and robust express.js server class designed for effortless customization and rapid deployment.
|
|
4
|
+
|
|
5
|
+
`wnodex` is a TypeScript-first framework that wraps Express.js to provide a modern, configuration-driven, and extensible foundation for building Node.js web servers. It comes with a suite of integrated middlewares for common tasks like security, body parsing, and logging, all manageable from a single configuration object.
|
|
20
6
|
|
|
21
7
|
## Features
|
|
22
8
|
|
|
23
|
-
- **
|
|
24
|
-
- **
|
|
25
|
-
- **
|
|
26
|
-
- **
|
|
27
|
-
- **
|
|
28
|
-
- **
|
|
29
|
-
-
|
|
9
|
+
- **Configuration-Driven**: Centralize your server setup in one object.
|
|
10
|
+
- **TypeScript First**: Written entirely in TypeScript for a great developer experience.
|
|
11
|
+
- **Extensible**: Composed of modular packages that can be used independently.
|
|
12
|
+
- **Sensible Defaults**: Pre-configured with best practices for security and performance.
|
|
13
|
+
- **Graceful Shutdown**: Built-in support for safely stopping the server.
|
|
14
|
+
- **Integrated Tooling**: Includes middlewares for:
|
|
15
|
+
- Security headers (`@wnodex/helmet`)
|
|
16
|
+
- Cross-Origin Resource Sharing (`@wnodex/cors`)
|
|
17
|
+
- Body and cookie parsing (`@wnodex/body-parser`, `@wnodex/cookie-parser`)
|
|
18
|
+
- Response compression (`@wnodex/compression`)
|
|
19
|
+
- Rate limiting (`@wnodex/rate-limit`)
|
|
20
|
+
- Parameter pollution protection (`@wnodex/hpp`)
|
|
21
|
+
- Session management (`@wnodex/session`)
|
|
22
|
+
- Authentication (`@wnodex/passport`)
|
|
23
|
+
- Structured logging (`@wnodex/logger`)
|
|
24
|
+
- Centralized error handling (`@wnodex/errors`)
|
|
30
25
|
|
|
31
|
-
##
|
|
26
|
+
## Why use it?
|
|
32
27
|
|
|
33
|
-
|
|
28
|
+
`wnodex` solves the problem of boilerplate and inconsistent configuration in Express.js applications. Instead of manually setting up and managing a dozen different middlewares, `wnodex` provides a cohesive ecosystem where everything is designed to work together. This leads to faster development, cleaner code, and more secure applications by default.
|
|
34
29
|
|
|
35
|
-
|
|
36
|
-
import { Wnodex } from 'wnodex';
|
|
30
|
+
## Installation
|
|
37
31
|
|
|
38
|
-
|
|
39
|
-
```
|
|
32
|
+
You can install the package using your favorite package manager:
|
|
40
33
|
|
|
41
|
-
|
|
34
|
+
**pnpm**
|
|
42
35
|
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
log.info('Custom log message');
|
|
36
|
+
```bash
|
|
37
|
+
pnpm add wnodex
|
|
46
38
|
```
|
|
47
39
|
|
|
48
|
-
|
|
40
|
+
**npm**
|
|
49
41
|
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
console.log(config.port); // 3000
|
|
42
|
+
```bash
|
|
43
|
+
npm install wnodex
|
|
53
44
|
```
|
|
54
45
|
|
|
55
|
-
|
|
46
|
+
**yarn**
|
|
56
47
|
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
app.get('/health', (req, res) => res.send('OK'));
|
|
48
|
+
```bash
|
|
49
|
+
yarn add wnodex
|
|
60
50
|
```
|
|
61
51
|
|
|
62
|
-
|
|
52
|
+
**bun**
|
|
63
53
|
|
|
64
|
-
```
|
|
65
|
-
|
|
54
|
+
```bash
|
|
55
|
+
bun add wnodex
|
|
66
56
|
```
|
|
67
57
|
|
|
68
|
-
|
|
58
|
+
## Usage
|
|
69
59
|
|
|
70
|
-
|
|
71
|
-
await wnodex.shutdown(async () => {
|
|
72
|
-
await db.close();
|
|
73
|
-
await cache.clear();
|
|
74
|
-
});
|
|
75
|
-
```
|
|
60
|
+
Create a new `Wnodex` instance, passing your configuration to the constructor. Then, call the `start()` method.
|
|
76
61
|
|
|
77
|
-
|
|
62
|
+
```typescript
|
|
63
|
+
import { Wnodex } from 'wnodex';
|
|
78
64
|
|
|
79
|
-
|
|
65
|
+
// 1. Create a server instance with your configuration
|
|
66
|
+
const server = new Wnodex({
|
|
67
|
+
port: process.env.PORT || 3000,
|
|
68
|
+
helmet: true,
|
|
69
|
+
cors: {
|
|
70
|
+
origin: 'https://my-app.com',
|
|
71
|
+
},
|
|
72
|
+
session: {
|
|
73
|
+
secret: 'my-session-secret',
|
|
74
|
+
resave: false,
|
|
75
|
+
saveUninitialized: false,
|
|
76
|
+
},
|
|
77
|
+
passport: true,
|
|
78
|
+
});
|
|
80
79
|
|
|
81
|
-
|
|
80
|
+
// 2. Get the underlying Express app to define routes
|
|
81
|
+
const app = server.getApp();
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
app.get('/', (req, res) => {
|
|
84
|
+
res.send('Hello from wnodex!');
|
|
85
|
+
});
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
// 3. Start the server
|
|
88
|
+
server.start().catch((err) => {
|
|
89
|
+
const logger = server.getLogger();
|
|
90
|
+
logger.error(err, 'Failed to start server');
|
|
91
|
+
});
|
|
88
92
|
|
|
89
|
-
|
|
93
|
+
// Handle graceful shutdown on OS signals
|
|
94
|
+
const gracefulShutdown = async () => {
|
|
95
|
+
await server.shutdown();
|
|
96
|
+
process.exit(0);
|
|
97
|
+
};
|
|
90
98
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
- `start(): Promise<void>` — Starts the server and listens on the configured port.
|
|
95
|
-
- `shutdown(chores?: () => Promise<void> | void): Promise<void>` — Gracefully shuts down the server, running optional async cleanup chores.
|
|
99
|
+
process.on('SIGINT', gracefulShutdown);
|
|
100
|
+
process.on('SIGTERM', gracefulShutdown);
|
|
101
|
+
```
|
|
96
102
|
|
|
97
103
|
## License
|
|
98
104
|
|
|
99
105
|
This project is licensed under the MIT License.
|
|
100
106
|
|
|
101
|
-
Copyright (c)
|
|
107
|
+
**Copyright (c) 2026 Davide Di Criscito**
|
|
102
108
|
|
|
103
|
-
For the full details, see the [LICENSE](
|
|
109
|
+
For the full details, see the [LICENSE](LICENSE) file.
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import z from 'zod';
|
|
2
2
|
export declare const WnodexConfigSchema: z.ZodObject<{
|
|
3
|
-
port: z.ZodDefault<z.ZodOptional<z.
|
|
3
|
+
port: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>>>;
|
|
4
|
+
bodyParsers: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
5
|
+
json: z.ZodOptional<z.ZodCustom<import("body-parser").OptionsJson, import("body-parser").OptionsJson>>;
|
|
6
|
+
urlencoded: z.ZodOptional<z.ZodCustom<import("body-parser").OptionsUrlencoded, import("body-parser").OptionsUrlencoded>>;
|
|
7
|
+
}, z.z.core.$strip>]>>>;
|
|
4
8
|
helmet: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodCustom<import("helmet").HelmetOptions, import("helmet").HelmetOptions>]>>>;
|
|
5
9
|
cors: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodCustom<import("cors").CorsOptions, import("cors").CorsOptions>]>>>;
|
|
6
10
|
compression: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
@@ -8,11 +12,11 @@ export declare const WnodexConfigSchema: z.ZodObject<{
|
|
|
8
12
|
windowMs: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
9
13
|
max: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
10
14
|
message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
11
|
-
}, z.core.$strip>]>>>;
|
|
15
|
+
}, z.z.core.$strip>]>>>;
|
|
12
16
|
cookieParser: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
13
17
|
secret: z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>;
|
|
14
18
|
options: z.ZodOptional<z.ZodCustom<import("cookie-parser").CookieParseOptions, import("cookie-parser").CookieParseOptions>>;
|
|
15
|
-
}, z.core.$strip>]>>>;
|
|
19
|
+
}, z.z.core.$strip>]>>>;
|
|
16
20
|
hpp: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodArray<z.ZodString>]>>>;
|
|
17
21
|
session: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
|
|
18
22
|
secret: z.ZodString;
|
|
@@ -22,10 +26,11 @@ export declare const WnodexConfigSchema: z.ZodObject<{
|
|
|
22
26
|
secure: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
23
27
|
maxAge: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
24
28
|
httpOnly: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
25
|
-
}, z.core.$strip>>>;
|
|
26
|
-
}, z.core.$strip>]>>>;
|
|
27
|
-
passport: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.
|
|
28
|
-
}, z.core.$strip>;
|
|
29
|
+
}, z.z.core.$strip>>>;
|
|
30
|
+
}, z.z.core.$strip>]>>>;
|
|
31
|
+
passport: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodCustom<import("passport").PassportStatic, import("passport").PassportStatic>]>>>;
|
|
32
|
+
}, z.z.core.$strip>;
|
|
33
|
+
export type WnodexConfig = z.infer<typeof WnodexConfigSchema>;
|
|
29
34
|
export type WnodexConfigInput = z.input<typeof WnodexConfigSchema>;
|
|
30
35
|
export type WnodexConfigOutput = z.output<typeof WnodexConfigSchema>;
|
|
31
|
-
//# sourceMappingURL=
|
|
36
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAC;AAWpB,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAW7B,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAC9D,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AACnE,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,kBAAkB,CAAC,CAAC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { BodyParserOptionsSchema } from '@wnodex/body-parser';
|
|
3
|
+
import { CompressionOptionsSchema } from '@wnodex/compression';
|
|
4
|
+
import { CookieParserOptionsSchema } from '@wnodex/cookie-parser';
|
|
5
|
+
import { CorsOptionsSchema } from '@wnodex/cors';
|
|
6
|
+
import { HelmetOptionsSchema } from '@wnodex/helmet';
|
|
7
|
+
import { HppOptionsSchema } from '@wnodex/hpp';
|
|
8
|
+
import { PassportOptionsSchema } from '@wnodex/passport';
|
|
9
|
+
import { RateLimitOptionsSchema } from '@wnodex/rate-limit';
|
|
10
|
+
import { SessionOptionsSchema } from '@wnodex/session';
|
|
11
|
+
export const WnodexConfigSchema = z.object({
|
|
12
|
+
port: z.union([z.number(), z.string()]).optional().default(3000),
|
|
13
|
+
bodyParsers: BodyParserOptionsSchema,
|
|
14
|
+
helmet: HelmetOptionsSchema,
|
|
15
|
+
cors: CorsOptionsSchema,
|
|
16
|
+
compression: CompressionOptionsSchema,
|
|
17
|
+
rateLimit: RateLimitOptionsSchema,
|
|
18
|
+
cookieParser: CookieParserOptionsSchema,
|
|
19
|
+
hpp: HppOptionsSchema,
|
|
20
|
+
session: SessionOptionsSchema,
|
|
21
|
+
passport: PassportOptionsSchema,
|
|
22
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,39 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
1
|
+
import { type Application } from 'express';
|
|
2
|
+
import type { Logger } from 'pino';
|
|
3
|
+
import { type WnodexConfigInput, type WnodexConfigOutput } from './config.js';
|
|
4
|
+
/**
|
|
5
|
+
* Main application class for the Wnodex server.
|
|
6
|
+
*
|
|
7
|
+
* Handles Express app instantiation, configuration validation,
|
|
8
|
+
* middleware setup, logging, error handling, server startup, and graceful shutdown.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Wnodex {
|
|
11
|
+
private readonly app;
|
|
12
|
+
private readonly config;
|
|
13
|
+
private readonly logger;
|
|
14
|
+
private server?;
|
|
15
|
+
/**
|
|
16
|
+
* Constructs a new Wnodex server instance.
|
|
17
|
+
* Initializes the Express app, logger, validates and stores configuration, and applies middlewares.
|
|
18
|
+
* @param config Configuration input for Wnodex, validated by WnodexConfigSchema.
|
|
19
|
+
* @example
|
|
20
|
+
* const wnodex = new Wnodex({ port: 3000 });
|
|
21
|
+
*/
|
|
22
|
+
constructor(config: WnodexConfigInput);
|
|
23
|
+
/**
|
|
24
|
+
* Provides access to the application's logger instance.
|
|
25
|
+
* @returns Logger instance.
|
|
26
|
+
* @example
|
|
27
|
+
* const log = wnodex.getLogger();
|
|
28
|
+
*/
|
|
29
|
+
getLogger(): Logger;
|
|
30
|
+
/**
|
|
31
|
+
* Returns the validated Wnodex configuration.
|
|
32
|
+
* @returns WnodexConfigOutput object.
|
|
33
|
+
* @example
|
|
34
|
+
* const config = wnodex.getConfig();
|
|
35
|
+
*/
|
|
36
|
+
getConfig(): WnodexConfigOutput;
|
|
37
|
+
/**
|
|
38
|
+
* Public getter for the express app instance.
|
|
39
|
+
* @returns The express application instance.
|
|
40
|
+
* @example
|
|
41
|
+
* const app = Wnodex.getApp();
|
|
42
|
+
*/
|
|
43
|
+
getApp(): Application;
|
|
44
|
+
/**
|
|
45
|
+
* Starts the Wnodex server and listens on the specified port.
|
|
46
|
+
* Resolves when the server is ready.
|
|
47
|
+
* @returns Promise that resolves once server is running.
|
|
48
|
+
* @example
|
|
49
|
+
* await wnodex.start();
|
|
50
|
+
*/
|
|
51
|
+
start(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Gracefully shuts down the Wnodex server.
|
|
54
|
+
* Stops accepting new connections, allows active connections to finish,
|
|
55
|
+
* runs optional asynchronous cleanup chores, and closes the server.
|
|
56
|
+
* @param chores An optional async callback function to perform cleanup tasks
|
|
57
|
+
* before shutdown (e.g., closing DB connections, clearing cache). This function is awaited before closing the server.
|
|
58
|
+
*
|
|
59
|
+
* @returns Promise that resolves when the server has stopped gracefully.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* await wnodex.shutdown(async () => {
|
|
63
|
+
* await db.close();
|
|
64
|
+
* await cache.clear();
|
|
65
|
+
* });
|
|
66
|
+
*/
|
|
67
|
+
shutdown(chores?: () => Promise<void> | void): Promise<void>;
|
|
68
|
+
}
|
|
39
69
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAgB,EAAE,KAAK,WAAW,EAAE,MAAM,SAAS,CAAC;AAEpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAI9E;;;;;GAKG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAc;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,MAAM,CAAC,CAAS;IAExB;;;;;;OAMG;gBACS,MAAM,EAAE,iBAAiB;IAcrC;;;;;OAKG;IACI,SAAS,IAAI,MAAM;IAI1B;;;;;OAKG;IACI,SAAS,IAAI,kBAAkB;IAItC;;;;;OAKG;IACI,MAAM,IAAI,WAAW;IAM5B;;;;;;OAMG;IACI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAmB7B;;;;;;;;;;;;;;OAcG;IACU,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CA+B1E"}
|
package/dist/index.js
CHANGED
|
@@ -1,41 +1,126 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { logger } from '@wnodex/logger';
|
|
3
|
+
import { setupConfig } from './setup-config.js';
|
|
4
|
+
import { setupMiddlewares } from './setup-middlewares.js';
|
|
5
|
+
/**
|
|
6
|
+
* Main application class for the Wnodex server.
|
|
7
|
+
*
|
|
8
|
+
* Handles Express app instantiation, configuration validation,
|
|
9
|
+
* middleware setup, logging, error handling, server startup, and graceful shutdown.
|
|
10
|
+
*/
|
|
11
|
+
export class Wnodex {
|
|
12
|
+
app;
|
|
13
|
+
config;
|
|
14
|
+
logger;
|
|
15
|
+
server;
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a new Wnodex server instance.
|
|
18
|
+
* Initializes the Express app, logger, validates and stores configuration, and applies middlewares.
|
|
19
|
+
* @param config Configuration input for Wnodex, validated by WnodexConfigSchema.
|
|
20
|
+
* @example
|
|
21
|
+
* const wnodex = new Wnodex({ port: 3000 });
|
|
22
|
+
*/
|
|
23
|
+
constructor(config) {
|
|
24
|
+
this.app = express();
|
|
25
|
+
// Logger
|
|
26
|
+
this.logger = logger;
|
|
27
|
+
this.app.set('logger', logger);
|
|
28
|
+
// Config
|
|
29
|
+
this.config = setupConfig(config);
|
|
30
|
+
// Global Middlewares
|
|
31
|
+
setupMiddlewares(this.app, this.config);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Provides access to the application's logger instance.
|
|
35
|
+
* @returns Logger instance.
|
|
36
|
+
* @example
|
|
37
|
+
* const log = wnodex.getLogger();
|
|
38
|
+
*/
|
|
39
|
+
getLogger() {
|
|
40
|
+
return this.logger;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns the validated Wnodex configuration.
|
|
44
|
+
* @returns WnodexConfigOutput object.
|
|
45
|
+
* @example
|
|
46
|
+
* const config = wnodex.getConfig();
|
|
47
|
+
*/
|
|
48
|
+
getConfig() {
|
|
49
|
+
return this.config;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Public getter for the express app instance.
|
|
53
|
+
* @returns The express application instance.
|
|
54
|
+
* @example
|
|
55
|
+
* const app = Wnodex.getApp();
|
|
56
|
+
*/
|
|
57
|
+
getApp() {
|
|
58
|
+
return this.app;
|
|
59
|
+
}
|
|
60
|
+
// --- Wnodex Start Helper ---
|
|
61
|
+
/**
|
|
62
|
+
* Starts the Wnodex server and listens on the specified port.
|
|
63
|
+
* Resolves when the server is ready.
|
|
64
|
+
* @returns Promise that resolves once server is running.
|
|
65
|
+
* @example
|
|
66
|
+
* await wnodex.start();
|
|
67
|
+
*/
|
|
68
|
+
start() {
|
|
69
|
+
const logger = this.getLogger();
|
|
70
|
+
const { port } = this.getConfig();
|
|
71
|
+
return new Promise((resolve) => {
|
|
72
|
+
this.server = this.app.listen(port, () => {
|
|
73
|
+
logger.info(`Server running @ http://localhost:${port}`);
|
|
74
|
+
resolve();
|
|
75
|
+
});
|
|
76
|
+
// Attach error handler immediately after server creation
|
|
77
|
+
this.server?.on('error', (err) => {
|
|
78
|
+
logger.error(err, 'Server error:');
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
// --- Wnodex Graceful Shutdown ---
|
|
83
|
+
/**
|
|
84
|
+
* Gracefully shuts down the Wnodex server.
|
|
85
|
+
* Stops accepting new connections, allows active connections to finish,
|
|
86
|
+
* runs optional asynchronous cleanup chores, and closes the server.
|
|
87
|
+
* @param chores An optional async callback function to perform cleanup tasks
|
|
88
|
+
* before shutdown (e.g., closing DB connections, clearing cache). This function is awaited before closing the server.
|
|
89
|
+
*
|
|
90
|
+
* @returns Promise that resolves when the server has stopped gracefully.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* await wnodex.shutdown(async () => {
|
|
94
|
+
* await db.close();
|
|
95
|
+
* await cache.clear();
|
|
96
|
+
* });
|
|
97
|
+
*/
|
|
98
|
+
async shutdown(chores) {
|
|
99
|
+
const logger = this.getLogger();
|
|
100
|
+
if (!this.server) {
|
|
101
|
+
logger.warn('Server not running.');
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
logger.info('Shutdown initiated...');
|
|
105
|
+
if (chores) {
|
|
106
|
+
try {
|
|
107
|
+
await chores();
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
logger.error(error, 'Error during cleanup chores');
|
|
111
|
+
// Proceed with shutdown even if chores fail
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
this.server.close((err) => {
|
|
116
|
+
if (err) {
|
|
117
|
+
logger.error(err, 'Error during server shutdown');
|
|
118
|
+
reject(err);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
logger.info('Server stopped gracefully.');
|
|
122
|
+
resolve();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-config.d.ts","sourceRoot":"","sources":["../src/setup-config.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAEnB,MAAM,aAAa,CAAC;AAErB,eAAO,MAAM,WAAW,GAAI,QAAQ,iBAAiB,KAAG,kBAQvD,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ConfigError } from '@wnodex/errors';
|
|
2
|
+
import { WnodexConfigSchema, } from './config.js';
|
|
3
|
+
export const setupConfig = (config) => {
|
|
4
|
+
const result = WnodexConfigSchema.safeParse(config);
|
|
5
|
+
if (!result.success) {
|
|
6
|
+
throw new ConfigError('Invalid wnodex configuration', result.error);
|
|
7
|
+
}
|
|
8
|
+
return result.data;
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-middlewares.d.ts","sourceRoot":"","sources":["../src/setup-middlewares.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAatC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,eAAO,MAAM,gBAAgB,GAC3B,KAAK,WAAW,EAChB,QAAQ,kBAAkB,KACzB,IA8BF,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { configureBodyParsers } from '@wnodex/body-parser';
|
|
2
|
+
import { configureCompression } from '@wnodex/compression';
|
|
3
|
+
import { configureCookieParser } from '@wnodex/cookie-parser';
|
|
4
|
+
import { configureCors } from '@wnodex/cors';
|
|
5
|
+
import { errorHandler } from '@wnodex/errors';
|
|
6
|
+
import { configureHelmet } from '@wnodex/helmet';
|
|
7
|
+
import { configureHpp } from '@wnodex/hpp';
|
|
8
|
+
import { configurePassport } from '@wnodex/passport';
|
|
9
|
+
import { configureRateLimit } from '@wnodex/rate-limit';
|
|
10
|
+
import { configureSession } from '@wnodex/session';
|
|
11
|
+
export const setupMiddlewares = (app, config) => {
|
|
12
|
+
// Helmet
|
|
13
|
+
configureHelmet(app, config.helmet);
|
|
14
|
+
// CORS
|
|
15
|
+
configureCors(app, config.cors);
|
|
16
|
+
// Body Parsers
|
|
17
|
+
configureBodyParsers(app, config.bodyParsers);
|
|
18
|
+
// cookie-parser
|
|
19
|
+
configureCookieParser(app, config.cookieParser);
|
|
20
|
+
// compression
|
|
21
|
+
configureCompression(app, config.compression);
|
|
22
|
+
// express-rate-limit
|
|
23
|
+
configureRateLimit(app, config.rateLimit);
|
|
24
|
+
// hpp
|
|
25
|
+
configureHpp(app, config.hpp);
|
|
26
|
+
// express-session - Session management
|
|
27
|
+
configureSession(app, config.session);
|
|
28
|
+
// passport - Authentication middleware
|
|
29
|
+
configurePassport(app, config.passport);
|
|
30
|
+
// Error Handler (keep it last)
|
|
31
|
+
app.use(errorHandler);
|
|
32
|
+
};
|