mikroserve 0.0.1 → 0.0.4
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/README.md +178 -1
- package/lib/MikroServe.d.mts +21 -41
- package/lib/MikroServe.d.ts +21 -41
- package/lib/MikroServe.js +102 -78
- package/lib/MikroServe.mjs +3 -1
- package/lib/{chunk-GGGGATKH.mjs → chunk-6UXWR6LD.mjs} +81 -78
- package/lib/chunk-CQPU7577.mjs +9 -0
- package/lib/chunk-RP67R3W4.mjs +43 -0
- package/lib/index.d.mts +2 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +118 -78
- package/lib/index.mjs +19 -1
- package/lib/interfaces/index.d.mts +71 -208
- package/lib/interfaces/index.d.ts +71 -208
- package/lib/utils/configDefaults.d.mts +20 -0
- package/lib/utils/configDefaults.d.ts +20 -0
- package/lib/utils/configDefaults.js +72 -0
- package/lib/utils/configDefaults.mjs +9 -0
- package/lib/utils/getTruthyEnvValue.d.mts +6 -0
- package/lib/utils/getTruthyEnvValue.d.ts +6 -0
- package/lib/utils/getTruthyEnvValue.js +33 -0
- package/lib/utils/getTruthyEnvValue.mjs +6 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -1,3 +1,180 @@
|
|
|
1
1
|
# MikroServe
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Minimalistic, ready-to-use API, built on Node.js primitives**.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
- Native Node.js [http](https://nodejs.org/api/http.html)/[https](https://nodejs.org/api/https.html) implementation, meaning maximum performance
|
|
11
|
+
- [Hono](https://hono.dev)-style API semantics for GET, POST, PATCH, PUT, DELETE operations
|
|
12
|
+
- Supports being exposed over both HTTP and HTTPS
|
|
13
|
+
- Supports custom middlewares
|
|
14
|
+
- Out-of-the-box CORS support
|
|
15
|
+
- Built-in customizable rate limiter
|
|
16
|
+
- Tiny (~3.2kb gzipped)
|
|
17
|
+
- Only a single dependency: [MikroConf](https://github.com/mikaelvesavuori/mikroconf)
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install mikroserve -S
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Quick Start
|
|
28
|
+
|
|
29
|
+
A minimum example of a tiny MikroServe API could look like the below.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { MikroServe, type Context } from 'mikroserve';
|
|
33
|
+
|
|
34
|
+
// Create an instance of MikroServe using only default values
|
|
35
|
+
const api = new MikroServe();
|
|
36
|
+
|
|
37
|
+
// Add any routes that should be exposed
|
|
38
|
+
// This will expose a GET route on the root of the API, responding with plain text
|
|
39
|
+
api.get('/', (c: Context) => c.text('Hello world!'));
|
|
40
|
+
|
|
41
|
+
// Start the server
|
|
42
|
+
api.start();
|
|
43
|
+
|
|
44
|
+
// The API is ready – go ahead and curl it in your command line of choice
|
|
45
|
+
// curl 0.0.0.0:3000
|
|
46
|
+
// The response should be "Hello world!"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Bigger example
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { MikroServe } from 'mikroserve';
|
|
53
|
+
|
|
54
|
+
// Create a new api instance
|
|
55
|
+
const api = new MikroServe({
|
|
56
|
+
port: 3000,
|
|
57
|
+
host: '0.0.0.0',
|
|
58
|
+
useHttps: false,
|
|
59
|
+
sslCert: '',
|
|
60
|
+
sslKey: '',
|
|
61
|
+
sslCa: '',
|
|
62
|
+
debug: true,
|
|
63
|
+
rateLimit: {
|
|
64
|
+
requestsPerMinute: 60,
|
|
65
|
+
enabled: true
|
|
66
|
+
},
|
|
67
|
+
allowedDomains: ['*']
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Define a global middleware for logging
|
|
71
|
+
api.use(async (ctx, next) => {
|
|
72
|
+
console.log(`Request received: ${ctx.req.method} ${ctx.path}`);
|
|
73
|
+
const start = Date.now();
|
|
74
|
+
const response = await next();
|
|
75
|
+
const duration = Date.now() - start;
|
|
76
|
+
console.log(`Request completed in ${duration}ms with status ${response.statusCode}`);
|
|
77
|
+
return response;
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Define a auth middleware
|
|
81
|
+
const requireAuth = async (ctx, next) => {
|
|
82
|
+
const token = ctx.headers.authorization?.replace('Bearer ', '');
|
|
83
|
+
|
|
84
|
+
if (!token) {
|
|
85
|
+
return ctx.status(401).json({
|
|
86
|
+
error: 'Unauthorized',
|
|
87
|
+
message: 'Authentication required'
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Validate token logic would go here
|
|
92
|
+
// For this example, we'll just check if it's "valid-token"
|
|
93
|
+
if (token !== 'valid-token') {
|
|
94
|
+
return ctx.status(401).json({
|
|
95
|
+
error: 'Unauthorized',
|
|
96
|
+
message: 'Invalid authentication token'
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Set user on context for downstream handlers
|
|
101
|
+
ctx.user = { id: '123', name: 'Example User' };
|
|
102
|
+
|
|
103
|
+
return next();
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// Basic routes
|
|
107
|
+
api.get('/', (c) => c.text('Hello, World!'));
|
|
108
|
+
|
|
109
|
+
api.get('/json', (c) => c.json({ message: 'This is JSON' }));
|
|
110
|
+
|
|
111
|
+
// Route with URL parameters
|
|
112
|
+
api.get('/users/:id', (c) => {
|
|
113
|
+
return c.json({
|
|
114
|
+
userId: c.params.id,
|
|
115
|
+
message: `User details for ${c.params.id}`
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Route with query parameters
|
|
120
|
+
api.get('/search', (c) => {
|
|
121
|
+
const query = c.query.q || '';
|
|
122
|
+
const page = Number.parseInt(c.query.page || '1');
|
|
123
|
+
|
|
124
|
+
return c.json({
|
|
125
|
+
query,
|
|
126
|
+
page,
|
|
127
|
+
results: [`Result 1 for "${query}"`, `Result 2 for "${query}"`]
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// POST route with body parsing
|
|
132
|
+
api.post('/echo', (c) =>
|
|
133
|
+
c.json({
|
|
134
|
+
message: 'Echo response',
|
|
135
|
+
body: c.body,
|
|
136
|
+
contentType: c.req.headers['content-type']
|
|
137
|
+
})
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
// Route with middleware
|
|
141
|
+
api.get('/protected', requireAuth, (c) => {
|
|
142
|
+
return c.json({
|
|
143
|
+
message: 'This is protected content',
|
|
144
|
+
user: c.user
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Route with custom status code
|
|
149
|
+
api.get('/not-found', (c) =>
|
|
150
|
+
c.status(404).json({
|
|
151
|
+
error: 'Not Found',
|
|
152
|
+
message: 'This resource was not found'
|
|
153
|
+
})
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// Route with redirection
|
|
157
|
+
api.get('/redirect', (c) => c.redirect('/redirected-to'));
|
|
158
|
+
api.get('/redirected-to', (c) => c.text('You have been redirected'));
|
|
159
|
+
|
|
160
|
+
// Error handling example
|
|
161
|
+
api.get('/error', () => {
|
|
162
|
+
throw new Error('This is a test error');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
api.start();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Create self-signed HTTPS certificates
|
|
169
|
+
|
|
170
|
+
On Mac and Linux, run:
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
openssl req -x509 -newkey rsa:2048 -keyout local-key.pem -out local-cert.pem -days 365 -nodes -subj "/CN=localhost"
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Feel free to change the key and cert names as you wish.
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT. See the `LICENSE` file.
|
package/lib/MikroServe.d.mts
CHANGED
|
@@ -1,104 +1,84 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
2
|
import https from 'node:https';
|
|
3
|
-
import {
|
|
3
|
+
import { MikroServeOptions, Middleware, RouteHandler } from './interfaces/index.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* MikroServe manages HTTP server operations with routing.
|
|
6
|
+
* @description MikroServe manages HTTP server operations with routing.
|
|
7
7
|
*/
|
|
8
8
|
declare class MikroServe {
|
|
9
|
-
private config;
|
|
10
9
|
private rateLimiter;
|
|
11
10
|
private router;
|
|
12
|
-
private
|
|
13
|
-
private sslCert?;
|
|
14
|
-
private sslKey?;
|
|
15
|
-
private sslCa?;
|
|
16
|
-
private debug;
|
|
11
|
+
private config;
|
|
17
12
|
/**
|
|
18
13
|
* @description Creates a new MikroServe instance.
|
|
19
|
-
* @param config - Server configuration options
|
|
20
14
|
*/
|
|
21
|
-
constructor(
|
|
15
|
+
constructor(options?: MikroServeOptions);
|
|
22
16
|
/**
|
|
23
|
-
* Register a global middleware
|
|
17
|
+
* @description Register a global middleware.
|
|
24
18
|
*/
|
|
25
19
|
use(middleware: Middleware): this;
|
|
26
20
|
/**
|
|
27
|
-
* Register a GET route
|
|
21
|
+
* @description Register a GET route.
|
|
28
22
|
*/
|
|
29
23
|
get(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
30
24
|
/**
|
|
31
|
-
* Register a POST route
|
|
25
|
+
* @description Register a POST route.
|
|
32
26
|
*/
|
|
33
27
|
post(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
34
28
|
/**
|
|
35
|
-
* Register a PUT route
|
|
29
|
+
* @description Register a PUT route.
|
|
36
30
|
*/
|
|
37
31
|
put(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
38
32
|
/**
|
|
39
|
-
* Register a DELETE route
|
|
33
|
+
* @description Register a DELETE route.
|
|
40
34
|
*/
|
|
41
35
|
delete(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
42
36
|
/**
|
|
43
|
-
* Register a PATCH route
|
|
37
|
+
* @description Register a PATCH route.
|
|
44
38
|
*/
|
|
45
39
|
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
46
40
|
/**
|
|
47
|
-
* Register an OPTIONS route
|
|
41
|
+
* @description Register an OPTIONS route.
|
|
48
42
|
*/
|
|
49
43
|
options(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
50
44
|
/**
|
|
51
|
-
* Creates an HTTP/HTTPS server, sets up graceful shutdown, and starts listening.
|
|
52
|
-
* @returns Running HTTP/HTTPS server
|
|
45
|
+
* @description Creates an HTTP/HTTPS server, sets up graceful shutdown, and starts listening.
|
|
53
46
|
*/
|
|
54
47
|
start(): http.Server | https.Server;
|
|
55
48
|
/**
|
|
56
|
-
* Creates and configures a server instance without starting it.
|
|
57
|
-
* @returns Configured HTTP or HTTPS server instance
|
|
49
|
+
* @description Creates and configures a server instance without starting it.
|
|
58
50
|
*/
|
|
59
51
|
createServer(): http.Server | https.Server;
|
|
60
52
|
/**
|
|
61
|
-
* Rate limiting middleware
|
|
53
|
+
* @description Rate limiting middleware.
|
|
62
54
|
*/
|
|
63
55
|
private rateLimitMiddleware;
|
|
64
56
|
/**
|
|
65
|
-
* Request handler for HTTP and HTTPS servers.
|
|
57
|
+
* @description Request handler for HTTP and HTTPS servers.
|
|
66
58
|
*/
|
|
67
59
|
private requestHandler;
|
|
68
60
|
/**
|
|
69
|
-
* Writes out a clean log to represent the duration of the request.
|
|
61
|
+
* @description Writes out a clean log to represent the duration of the request.
|
|
70
62
|
*/
|
|
71
63
|
private logDuration;
|
|
72
64
|
/**
|
|
73
|
-
* Parses the request body based on content type.
|
|
74
|
-
* @param req - HTTP request object
|
|
75
|
-
* @returns Promise resolving to the parsed body
|
|
76
|
-
* @throws Will throw if body cannot be parsed
|
|
77
|
-
*/
|
|
78
|
-
/**
|
|
79
|
-
* Parses the request body based on content type.
|
|
80
|
-
* @param req - HTTP request object
|
|
81
|
-
* @returns Promise resolving to the parsed body
|
|
82
|
-
* @throws Will throw if body cannot be parsed
|
|
65
|
+
* @description Parses the request body based on content type.
|
|
83
66
|
*/
|
|
84
67
|
private parseBody;
|
|
85
68
|
/**
|
|
86
|
-
* CORS middleware
|
|
69
|
+
* @description CORS middleware.
|
|
87
70
|
*/
|
|
88
71
|
private setCorsHeaders;
|
|
89
72
|
/**
|
|
90
|
-
* Set security headers
|
|
73
|
+
* @description Set security headers.
|
|
91
74
|
*/
|
|
92
75
|
private setSecurityHeaders;
|
|
93
76
|
/**
|
|
94
|
-
* Sends a response with appropriate headers.
|
|
95
|
-
* @param res - HTTP response object
|
|
96
|
-
* @param response - Response data and status code
|
|
77
|
+
* @description Sends a response with appropriate headers.
|
|
97
78
|
*/
|
|
98
79
|
private respond;
|
|
99
80
|
/**
|
|
100
|
-
* Sets up graceful shutdown handlers for a server.
|
|
101
|
-
* @param server - The HTTP/HTTPS server to add shutdown handlers to
|
|
81
|
+
* @description Sets up graceful shutdown handlers for a server.
|
|
102
82
|
*/
|
|
103
83
|
setupGracefulShutdown(server: http.Server | https.Server): void;
|
|
104
84
|
}
|
package/lib/MikroServe.d.ts
CHANGED
|
@@ -1,104 +1,84 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
2
|
import https from 'node:https';
|
|
3
|
-
import {
|
|
3
|
+
import { MikroServeOptions, Middleware, RouteHandler } from './interfaces/index.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* MikroServe manages HTTP server operations with routing.
|
|
6
|
+
* @description MikroServe manages HTTP server operations with routing.
|
|
7
7
|
*/
|
|
8
8
|
declare class MikroServe {
|
|
9
|
-
private config;
|
|
10
9
|
private rateLimiter;
|
|
11
10
|
private router;
|
|
12
|
-
private
|
|
13
|
-
private sslCert?;
|
|
14
|
-
private sslKey?;
|
|
15
|
-
private sslCa?;
|
|
16
|
-
private debug;
|
|
11
|
+
private config;
|
|
17
12
|
/**
|
|
18
13
|
* @description Creates a new MikroServe instance.
|
|
19
|
-
* @param config - Server configuration options
|
|
20
14
|
*/
|
|
21
|
-
constructor(
|
|
15
|
+
constructor(options?: MikroServeOptions);
|
|
22
16
|
/**
|
|
23
|
-
* Register a global middleware
|
|
17
|
+
* @description Register a global middleware.
|
|
24
18
|
*/
|
|
25
19
|
use(middleware: Middleware): this;
|
|
26
20
|
/**
|
|
27
|
-
* Register a GET route
|
|
21
|
+
* @description Register a GET route.
|
|
28
22
|
*/
|
|
29
23
|
get(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
30
24
|
/**
|
|
31
|
-
* Register a POST route
|
|
25
|
+
* @description Register a POST route.
|
|
32
26
|
*/
|
|
33
27
|
post(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
34
28
|
/**
|
|
35
|
-
* Register a PUT route
|
|
29
|
+
* @description Register a PUT route.
|
|
36
30
|
*/
|
|
37
31
|
put(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
38
32
|
/**
|
|
39
|
-
* Register a DELETE route
|
|
33
|
+
* @description Register a DELETE route.
|
|
40
34
|
*/
|
|
41
35
|
delete(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
42
36
|
/**
|
|
43
|
-
* Register a PATCH route
|
|
37
|
+
* @description Register a PATCH route.
|
|
44
38
|
*/
|
|
45
39
|
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
46
40
|
/**
|
|
47
|
-
* Register an OPTIONS route
|
|
41
|
+
* @description Register an OPTIONS route.
|
|
48
42
|
*/
|
|
49
43
|
options(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
50
44
|
/**
|
|
51
|
-
* Creates an HTTP/HTTPS server, sets up graceful shutdown, and starts listening.
|
|
52
|
-
* @returns Running HTTP/HTTPS server
|
|
45
|
+
* @description Creates an HTTP/HTTPS server, sets up graceful shutdown, and starts listening.
|
|
53
46
|
*/
|
|
54
47
|
start(): http.Server | https.Server;
|
|
55
48
|
/**
|
|
56
|
-
* Creates and configures a server instance without starting it.
|
|
57
|
-
* @returns Configured HTTP or HTTPS server instance
|
|
49
|
+
* @description Creates and configures a server instance without starting it.
|
|
58
50
|
*/
|
|
59
51
|
createServer(): http.Server | https.Server;
|
|
60
52
|
/**
|
|
61
|
-
* Rate limiting middleware
|
|
53
|
+
* @description Rate limiting middleware.
|
|
62
54
|
*/
|
|
63
55
|
private rateLimitMiddleware;
|
|
64
56
|
/**
|
|
65
|
-
* Request handler for HTTP and HTTPS servers.
|
|
57
|
+
* @description Request handler for HTTP and HTTPS servers.
|
|
66
58
|
*/
|
|
67
59
|
private requestHandler;
|
|
68
60
|
/**
|
|
69
|
-
* Writes out a clean log to represent the duration of the request.
|
|
61
|
+
* @description Writes out a clean log to represent the duration of the request.
|
|
70
62
|
*/
|
|
71
63
|
private logDuration;
|
|
72
64
|
/**
|
|
73
|
-
* Parses the request body based on content type.
|
|
74
|
-
* @param req - HTTP request object
|
|
75
|
-
* @returns Promise resolving to the parsed body
|
|
76
|
-
* @throws Will throw if body cannot be parsed
|
|
77
|
-
*/
|
|
78
|
-
/**
|
|
79
|
-
* Parses the request body based on content type.
|
|
80
|
-
* @param req - HTTP request object
|
|
81
|
-
* @returns Promise resolving to the parsed body
|
|
82
|
-
* @throws Will throw if body cannot be parsed
|
|
65
|
+
* @description Parses the request body based on content type.
|
|
83
66
|
*/
|
|
84
67
|
private parseBody;
|
|
85
68
|
/**
|
|
86
|
-
* CORS middleware
|
|
69
|
+
* @description CORS middleware.
|
|
87
70
|
*/
|
|
88
71
|
private setCorsHeaders;
|
|
89
72
|
/**
|
|
90
|
-
* Set security headers
|
|
73
|
+
* @description Set security headers.
|
|
91
74
|
*/
|
|
92
75
|
private setSecurityHeaders;
|
|
93
76
|
/**
|
|
94
|
-
* Sends a response with appropriate headers.
|
|
95
|
-
* @param res - HTTP response object
|
|
96
|
-
* @param response - Response data and status code
|
|
77
|
+
* @description Sends a response with appropriate headers.
|
|
97
78
|
*/
|
|
98
79
|
private respond;
|
|
99
80
|
/**
|
|
100
|
-
* Sets up graceful shutdown handlers for a server.
|
|
101
|
-
* @param server - The HTTP/HTTPS server to add shutdown handlers to
|
|
81
|
+
* @description Sets up graceful shutdown handlers for a server.
|
|
102
82
|
*/
|
|
103
83
|
setupGracefulShutdown(server: http.Server | https.Server): void;
|
|
104
84
|
}
|