mikroserve 0.0.8 → 0.0.10

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 CHANGED
@@ -18,7 +18,7 @@
18
18
  - Supports custom middlewares
19
19
  - Out-of-the-box CORS support
20
20
  - Built-in customizable rate limiter
21
- - Tiny (5kb gzipped)
21
+ - Tiny (~5kb gzipped)
22
22
  - Only a single dependency: [MikroConf](https://github.com/mikaelvesavuori/mikroconf)
23
23
 
24
24
  ## Installation
@@ -40,8 +40,24 @@ import { MikroServe, type Context } from 'mikroserve';
40
40
  const api = new MikroServe();
41
41
 
42
42
  // Add any routes that should be exposed
43
+
43
44
  // This will expose a GET route on the root of the API, responding with plain text
44
- api.get('/', (c: Context) => c.text('Hello world!'));
45
+ api.get('/', async (c: Context) => c.text('Hello world!'));
46
+
47
+ // JSON responses are as easy as...
48
+ api.get('/users/:userId', async (c: Context) => c.json({ name: 'Sam Person', id: 'abc123', createdAt: 1743323168 }));
49
+
50
+ // Example POST request with JSON response and custom status code
51
+ api.post('/users/:userId', async (c: Context) => {
52
+ const { name } = c.body; // Body is ready to use, no need for parsing
53
+ const userId = c.params.userId;
54
+
55
+ // Do your things...
56
+
57
+ return c.json({ success: true }, 201);
58
+ });
59
+
60
+ // MikroServe supports raw, binary, text, form, json, html, redirect response types
45
61
 
46
62
  // Start the server
47
63
  api.start();
@@ -76,8 +92,8 @@ const api = new MikroServe({
76
92
  });
77
93
 
78
94
  // Define a global middleware for logging
79
- api.use(async (ctx, next) => {
80
- console.log(`Request received: ${ctx.req.method} ${ctx.path}`);
95
+ api.use(async (c, next) => {
96
+ console.log(`Request received: ${c.req.method} ${c.path}`);
81
97
  const start = Date.now();
82
98
  const response = await next();
83
99
  const duration = Date.now() - start;
@@ -86,11 +102,11 @@ api.use(async (ctx, next) => {
86
102
  });
87
103
 
88
104
  // Define a auth middleware
89
- const requireAuth = async (ctx, next) => {
90
- const token = ctx.headers.authorization?.replace('Bearer ', '');
105
+ const requireAuth = async (c, next) => {
106
+ const token = c.headers.authorization?.replace('Bearer ', '');
91
107
 
92
108
  if (!token) {
93
- return ctx.status(401).json({
109
+ return c.status(401).json({
94
110
  error: 'Unauthorized',
95
111
  message: 'Authentication required'
96
112
  });
@@ -99,14 +115,14 @@ const requireAuth = async (ctx, next) => {
99
115
  // Validate token logic would go here
100
116
  // For this example, we'll just check if it's "valid-token"
101
117
  if (token !== 'valid-token') {
102
- return ctx.status(401).json({
118
+ return c.status(401).json({
103
119
  error: 'Unauthorized',
104
120
  message: 'Invalid authentication token'
105
121
  });
106
122
  }
107
123
 
108
124
  // Set user on context for downstream handlers
109
- ctx.user = { id: '123', name: 'Example User' };
125
+ c.user = { id: '123', name: 'Example User' };
110
126
 
111
127
  return next();
112
128
  };
@@ -192,6 +208,7 @@ All of the settings already presented in the above examples can be provided in m
192
208
  | --port | `<number>` | port | PORT |
193
209
  | --host | `<string>` | host | HOST |
194
210
  | --https | none (is flag) | useHttps | |
211
+ | --http2 | none (is flag) | useHttp2 | |
195
212
  | --cert | `<string>` | sslCert | |
196
213
  | --key | `<string>` | sslKey | |
197
214
  | --ca | `<string>` | sslCa | |
package/lib/MikroServe.js CHANGED
@@ -438,7 +438,7 @@ var MikroServe = class {
438
438
  this.setupGracefulShutdown(server);
439
439
  server.listen(port, host, () => {
440
440
  const address = server.address();
441
- const protocol = this.config.useHttps ? "https" : "http";
441
+ const protocol = this.config.useHttps || this.config.useHttp2 ? "https" : "http";
442
442
  console.log(
443
443
  `MikroServe running at ${protocol}://${address.address !== "::" ? address.address : "localhost"}:${address.port}`
444
444
  );
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MikroServe
3
- } from "./chunk-SLBFEKEH.mjs";
3
+ } from "./chunk-TQN6BEGA.mjs";
4
4
  import "./chunk-ZFBBESGU.mjs";
5
5
  import "./chunk-GUYBTPZH.mjs";
6
6
  import "./chunk-YOHL3T54.mjs";
@@ -91,7 +91,7 @@ var MikroServe = class {
91
91
  this.setupGracefulShutdown(server);
92
92
  server.listen(port, host, () => {
93
93
  const address = server.address();
94
- const protocol = this.config.useHttps ? "https" : "http";
94
+ const protocol = this.config.useHttps || this.config.useHttp2 ? "https" : "http";
95
95
  console.log(
96
96
  `MikroServe running at ${protocol}://${address.address !== "::" ? address.address : "localhost"}:${address.port}`
97
97
  );
package/lib/index.js CHANGED
@@ -440,7 +440,7 @@ var MikroServe = class {
440
440
  this.setupGracefulShutdown(server);
441
441
  server.listen(port, host, () => {
442
442
  const address = server.address();
443
- const protocol = this.config.useHttps ? "https" : "http";
443
+ const protocol = this.config.useHttps || this.config.useHttp2 ? "https" : "http";
444
444
  console.log(
445
445
  `MikroServe running at ${protocol}://${address.address !== "::" ? address.address : "localhost"}:${address.port}`
446
446
  );
package/lib/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MikroServe
3
- } from "./chunk-SLBFEKEH.mjs";
3
+ } from "./chunk-TQN6BEGA.mjs";
4
4
  import "./chunk-ZFBBESGU.mjs";
5
5
  import "./chunk-GUYBTPZH.mjs";
6
6
  import "./chunk-YOHL3T54.mjs";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikroserve",
3
3
  "description": "Minimalistic, ready-to-use API, built on Node.js primitives.",
4
- "version": "0.0.8",
4
+ "version": "0.0.10",
5
5
  "author": "Mikael Vesavuori",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -35,7 +35,6 @@
35
35
  "mikroserve": "lib/index.js"
36
36
  },
37
37
  "scripts": {
38
- "start": "npx tsx src/index.ts",
39
38
  "test": "npm run test:licenses && npm run test:types && npm run lint && npm run test:unit",
40
39
  "test:types": "npx type-coverage --at-least 85 --strict --ignore-files \"tests/**/*.ts\" --ignore-files \"*.ts\" --ignore-files \"src/application/errors/*.ts\" --ignore-files \"testdata/*.ts\"",
41
40
  "test:licenses": "npx license-compliance --direct --allow 'MIT;ISC;0BSD;BSD-2-Clause;BSD-3-Clause;Apache-2.0;Unlicense;CC0-1.0'",