wnodex 0.1.0 → 0.2.1
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 +5 -91
- 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 +42 -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,17 @@
|
|
|
1
1
|
# wnodex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Web Node Express: an extensible and robust express.js server class designed for effortless customization and rapid deployment
|
|
4
4
|
|
|
5
|
-
## Table of
|
|
5
|
+
--- ## Table of Content
|
|
6
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
7
|
- [License](#license)
|
|
20
8
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
- **Express App Instantiation**: Quickly create a new Express application instance.
|
|
24
|
-
- **Configuration Validation**: Validate and store configuration using Zod schemas.
|
|
25
|
-
- **Middleware Setup**: Automatically applies global middlewares (helmet, cors, compression, rate-limit, etc.).
|
|
26
|
-
- **Logging**: Integrated with Pino for structured logging.
|
|
27
|
-
- **Error Handling**: Centralized error handler for Express.
|
|
28
|
-
- **Server Startup**: Simple method to start the server and listen on a specified port.
|
|
29
|
-
- **Graceful Shutdown**: Built-in support for graceful server shutdown and async cleanup chores.
|
|
30
|
-
|
|
31
|
-
## Usage
|
|
32
|
-
|
|
33
|
-
### Import and Instantiate
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
import { Wnodex } from 'wnodex';
|
|
37
|
-
|
|
38
|
-
const wnodex = new Wnodex({ port: 3000 });
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### Access the Logger
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
const log = wnodex.getLogger();
|
|
45
|
-
log.info('Custom log message');
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### Get Validated Config
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
const config = wnodex.getConfig();
|
|
52
|
-
console.log(config.port); // 3000
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### Access the Express App
|
|
56
|
-
|
|
57
|
-
```ts
|
|
58
|
-
const app = wnodex.getApp();
|
|
59
|
-
app.get('/health', (req, res) => res.send('OK'));
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### Start the Server
|
|
63
|
-
|
|
64
|
-
```ts
|
|
65
|
-
await wnodex.start();
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Graceful Shutdown
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
await wnodex.shutdown(async () => {
|
|
72
|
-
await db.close();
|
|
73
|
-
await cache.clear();
|
|
74
|
-
});
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## API Reference
|
|
78
|
-
|
|
79
|
-
### class `Wnodex`
|
|
80
|
-
|
|
81
|
-
#### Constructor
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
new Wnodex(config: WnodexConfigInput)
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
- `config`: Configuration input validated by `WnodexConfigSchema`.
|
|
88
|
-
|
|
89
|
-
#### Methods
|
|
90
|
-
|
|
91
|
-
- `getLogger(): Logger` — Returns the logger instance.
|
|
92
|
-
- `getConfig(): WnodexConfigOutput` — Returns the validated configuration.
|
|
93
|
-
- `getApp(): Application` — Returns the Express app instance.
|
|
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.
|
|
9
|
+
---
|
|
96
10
|
|
|
97
11
|
## License
|
|
98
12
|
|
|
99
13
|
This project is licensed under the MIT License.
|
|
100
14
|
|
|
101
|
-
Copyright (c)
|
|
15
|
+
**Copyright (c) 2026 Davide Di Criscito**
|
|
102
16
|
|
|
103
|
-
For the full details, see the [LICENSE](
|
|
17
|
+
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wnodex",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"private": false,
|
|
5
|
-
"
|
|
5
|
+
"description": "Web Node Express: an extensible and robust express.js server class designed for effortless customization and rapid deployment",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"wnodex"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/wnodex/wnodex#readme",
|
|
6
10
|
"bugs": {
|
|
7
|
-
"url": "https://github.com/
|
|
11
|
+
"url": "https://github.com/wnodex/wnodex/issues"
|
|
8
12
|
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/
|
|
15
|
+
"url": "git+https://github.com/wnodex/wnodex.git",
|
|
12
16
|
"directory": "packages/wnodex"
|
|
13
17
|
},
|
|
14
18
|
"license": "MIT",
|
|
15
|
-
"
|
|
19
|
+
"type": "module",
|
|
16
20
|
"exports": {
|
|
17
21
|
"./package.json": "./package.json",
|
|
18
22
|
".": {
|
|
@@ -25,30 +29,53 @@
|
|
|
25
29
|
"module": "./dist/index.js",
|
|
26
30
|
"types": "./dist/index.d.ts",
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"
|
|
32
|
+
"@types/body-parser": "^1.19.6",
|
|
33
|
+
"body-parser": "^2.2.2",
|
|
29
34
|
"cookie-parser": "^1.4.7",
|
|
30
|
-
"cors": "^2.8.
|
|
31
|
-
"express": "^5.1
|
|
35
|
+
"cors": "^2.8.6",
|
|
36
|
+
"express": "^5.2.1",
|
|
32
37
|
"express-rate-limit": "^8.2.1",
|
|
33
|
-
"express-session": "^1.
|
|
38
|
+
"express-session": "^1.19.0",
|
|
34
39
|
"helmet": "^8.1.0",
|
|
35
40
|
"hpp": "^0.2.3",
|
|
36
41
|
"passport": "^0.7.0",
|
|
37
|
-
"pino": "^10.
|
|
38
|
-
"pino-pretty": "^13.1.
|
|
39
|
-
"
|
|
40
|
-
"
|
|
42
|
+
"pino": "^10.3.0",
|
|
43
|
+
"pino-pretty": "^13.1.3",
|
|
44
|
+
"zod": "^4.3.6",
|
|
45
|
+
"@wnodex/compression": "0.2.1",
|
|
46
|
+
"@wnodex/cors": "0.2.1",
|
|
47
|
+
"@wnodex/body-parser": "0.2.1",
|
|
48
|
+
"@wnodex/cookie-parser": "0.2.1",
|
|
49
|
+
"@wnodex/errors": "0.2.1",
|
|
50
|
+
"@wnodex/helmet": "0.2.1",
|
|
51
|
+
"@wnodex/passport": "0.2.1",
|
|
52
|
+
"@wnodex/hpp": "0.2.1",
|
|
53
|
+
"@wnodex/logger": "0.2.1",
|
|
54
|
+
"@wnodex/session": "0.2.1",
|
|
55
|
+
"@wnodex/rate-limit": "0.2.1"
|
|
41
56
|
},
|
|
42
57
|
"devDependencies": {
|
|
43
58
|
"@types/compression": "^1.8.1",
|
|
44
59
|
"@types/cookie-parser": "^1.4.10",
|
|
45
60
|
"@types/cors": "^2.8.19",
|
|
46
|
-
"@types/express": "^5.0.
|
|
61
|
+
"@types/express": "^5.0.6",
|
|
47
62
|
"@types/express-session": "^1.18.2",
|
|
48
63
|
"@types/hpp": "^0.2.7",
|
|
49
|
-
"@types/
|
|
64
|
+
"@types/node": "^25.0.10",
|
|
65
|
+
"@types/passport": "^1.0.17",
|
|
66
|
+
"@types/supertest": "^6.0.3",
|
|
67
|
+
"rolldown": "1.0.0-rc.1",
|
|
68
|
+
"supertest": "^7.2.2",
|
|
69
|
+
"typescript": "5.9.2",
|
|
70
|
+
"vitest": "^4.0.18",
|
|
71
|
+
"@wnodex/typescript-config": "0.2.1"
|
|
50
72
|
},
|
|
51
73
|
"publishConfig": {
|
|
52
74
|
"access": "public"
|
|
75
|
+
},
|
|
76
|
+
"scripts": {
|
|
77
|
+
"build": "rolldown -c && tsc",
|
|
78
|
+
"test": "vitest run",
|
|
79
|
+
"test:watch": "vitest"
|
|
53
80
|
}
|
|
54
81
|
}
|