maxserver 0.0.9 → 0.0.11
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 +50 -51
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,39 +1,36 @@
|
|
|
1
|
-
<p align="center">
|
|
1
|
+
<p align="center" style="margin-top: 20px;" >
|
|
2
2
|
<img src="https://raw.githubusercontent.com/max-matinpalo/maxserver/refs/heads/main/assets/logo.png" alt="Project logo" width="160">
|
|
3
3
|
</p>
|
|
4
4
|
<br>
|
|
5
5
|
|
|
6
6
|
# maxserver
|
|
7
|
-
‼️ **ATTENTION** ‼️ - Not download yet, check out few days later 😉
|
|
7
|
+
‼️ **ATTENTION** ‼️ - Not download yet, check out few days later 😉
|
|
8
8
|
I am simplifying and improving things, that it will work for everyone plugn play.
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
Ready node server setup based on **Fastify** to speedup api development.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
- **Auto Routes**: auto imports and registers routes and schemas
|
|
14
|
+
- **Auto Docs**: auto generates docs based on schemas
|
|
15
|
+
- **Preconfigures JWT auth, Cores, Helmet**
|
|
16
|
+
- **Auto Connect MongoDB** (optional)
|
|
17
|
+
- **Dev server**
|
|
18
|
+
<br><br>
|
|
13
19
|
|
|
14
|
-
- **Route loader**: auto-register routes and schemas
|
|
15
|
-
- **JWT auth** (cookie or `Authorization: Bearer ...`)
|
|
16
|
-
- **Autogenerates docs** (`/openapi.json`) + optional UI (`/docs`)
|
|
17
|
-
- **MongoDB** auto-connect + global `db` + `oid()` helper
|
|
18
|
-
- **Dev server** (auto reload on changes)
|
|
19
|
-
- **HTTPS** support (when configured)
|
|
20
20
|
|
|
21
|
+
- Dependencies: original fastify packages + scalar/fastify-api-reference (doc generator)
|
|
22
|
+
- The source is simple and short. Everyone shall be able to read, understand and modify if needed.
|
|
21
23
|
|
|
22
24
|
|
|
23
|
-
##
|
|
25
|
+
## Install
|
|
24
26
|
|
|
25
|
-
###
|
|
26
|
-
npm install maxserver
|
|
27
|
-
|
|
28
|
-
### Install with template
|
|
27
|
+
### Setup ready project
|
|
29
28
|
npx maxserver [appname]
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
### Install
|
|
31
|
+
npm install maxserver
|
|
32
32
|
|
|
33
33
|
## Setup
|
|
34
|
-
maxserver(options) forwards options to fastify(options).
|
|
35
|
-
It returns the fully configured Fastify server instance.
|
|
36
|
-
|
|
37
34
|
```js
|
|
38
35
|
import maxserver from "maxserver";
|
|
39
36
|
const server = await maxserver();
|
|
@@ -45,16 +42,20 @@ console.log("Server running at", address);
|
|
|
45
42
|
export default server;
|
|
46
43
|
```
|
|
47
44
|
|
|
45
|
+
**maxserver(options)** forwards options to fastify(options).
|
|
46
|
+
It returns the fully configured Fastify server instance.
|
|
47
|
+
|
|
48
|
+
|
|
48
49
|
---
|
|
49
50
|
|
|
50
|
-
## ⚙️ Configuration
|
|
51
|
+
## ⚙️ Configuration
|
|
51
52
|
Configure the server by setting variabels in your .env file.
|
|
52
53
|
|
|
53
54
|
| Variable | Default | Description |
|
|
54
55
|
| :--- | :--- | :--- |
|
|
55
56
|
| `PORT` | `3000` | Server port |
|
|
56
|
-
| `JWT_SECRET` |
|
|
57
|
-
| `MONGODB_URI` |
|
|
57
|
+
| `JWT_SECRET` | *-* | Enables JWT auth (private-by-default routes) |
|
|
58
|
+
| `MONGODB_URI` | *-* | Enables MongoDB auto-connect + global `db` |
|
|
58
59
|
| `DOCS` | `true` | Set `false` to disable docs UI at `/docs` |
|
|
59
60
|
| `STATIC_DIR` | *(optional)* | Serve static files (example: `./public`) |
|
|
60
61
|
| `CORS_ORIGIN` | `*` | Allowed CORS origins |
|
|
@@ -63,7 +64,7 @@ Configure the server by setting variabels in your .env file.
|
|
|
63
64
|
|
|
64
65
|
## 🗂️ Project Structure
|
|
65
66
|
|
|
66
|
-
**
|
|
67
|
+
**1 route = 1 handler file + 1 schema file**
|
|
67
68
|
|
|
68
69
|
Example:
|
|
69
70
|
|
|
@@ -72,33 +73,27 @@ src/
|
|
|
72
73
|
users/
|
|
73
74
|
teams/
|
|
74
75
|
forms/
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
hello.js
|
|
77
|
+
hello.schema.js
|
|
77
78
|
...
|
|
78
79
|
```
|
|
79
80
|
|
|
80
81
|
---
|
|
81
82
|
|
|
82
|
-
## 🛣️
|
|
83
|
+
## 🛣️ Handlers
|
|
83
84
|
|
|
84
85
|
### 1) Define method + path
|
|
85
|
-
|
|
86
|
+
Start each route file with a comment to define the path.
|
|
87
|
+
That comment is what the route loader uses to auto-register the route.
|
|
86
88
|
|
|
87
89
|
```js
|
|
88
90
|
// GET /teams/:id
|
|
89
91
|
```
|
|
90
92
|
|
|
91
|
-
That comment is what the route loader uses to auto-register the route.
|
|
92
93
|
|
|
93
|
-
### 2) Default export handler
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
### 2) Default export handler
|
|
96
96
|
|
|
97
|
-
If something fails, throw:
|
|
98
|
-
|
|
99
|
-
```js
|
|
100
|
-
throw createError(code, "Specific failure reason");
|
|
101
|
-
```
|
|
102
97
|
|
|
103
98
|
### Example
|
|
104
99
|
|
|
@@ -107,30 +102,26 @@ throw createError(code, "Specific failure reason");
|
|
|
107
102
|
|
|
108
103
|
export default async function (req, res) {
|
|
109
104
|
|
|
110
|
-
// 1. Read input
|
|
111
105
|
const id = req.params.id;
|
|
112
|
-
|
|
113
|
-
// 2. Load data
|
|
114
|
-
const team = { id, name: "Team A" };
|
|
115
|
-
|
|
116
|
-
// 3. Respond
|
|
106
|
+
console.log("Example id": id);
|
|
117
107
|
return team;
|
|
118
108
|
}
|
|
119
109
|
```
|
|
120
110
|
|
|
121
111
|
---
|
|
122
112
|
|
|
123
|
-
## 🧾
|
|
113
|
+
## 🧾 Define Schemas
|
|
124
114
|
Create a sibling file ending in **`.schema.js`**.
|
|
115
|
+
The schema is offcourse a jsonschema.
|
|
125
116
|
This file will be auto registered.
|
|
126
117
|
|
|
127
118
|
Schemas:
|
|
128
119
|
- validate inputs
|
|
129
120
|
- generate OpenAPI docs
|
|
130
|
-
- control
|
|
121
|
+
- control route options for example (public/private)
|
|
131
122
|
|
|
132
123
|
|
|
133
|
-
**`
|
|
124
|
+
**`Important - use default export`**
|
|
134
125
|
```js
|
|
135
126
|
export default {
|
|
136
127
|
tags: ["Teams"],
|
|
@@ -165,7 +156,18 @@ export default {
|
|
|
165
156
|
};
|
|
166
157
|
```
|
|
167
158
|
|
|
168
|
-
|
|
159
|
+
<br>
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
## 📚 API Docs
|
|
163
|
+
|
|
164
|
+
- Open in your browser **`localhost:3000/docs`**
|
|
165
|
+
- OpenAPI JSON: **`/openapi.json`**
|
|
166
|
+
|
|
167
|
+
<br>
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
169
171
|
|
|
170
172
|
## 🔐 Authentication (JWT)
|
|
171
173
|
|
|
@@ -185,7 +187,7 @@ export default {
|
|
|
185
187
|
};
|
|
186
188
|
```
|
|
187
189
|
|
|
188
|
-
|
|
190
|
+
<br>
|
|
189
191
|
|
|
190
192
|
## 🍃 MongoDB
|
|
191
193
|
Define in the env **`MONGODB_URI`** and it will auto-connect at server start and you get:
|
|
@@ -209,20 +211,17 @@ Use `createError(code, message)` to stop immediately with a clean HTTP error.
|
|
|
209
211
|
if (!user) throw createError(404, "User not found");
|
|
210
212
|
```
|
|
211
213
|
|
|
212
|
-
Rule of thumb: make the message something you would want to see at
|
|
214
|
+
Rule of thumb: make the message something you would want to see at 03:00 in logs.
|
|
213
215
|
|
|
214
216
|
---
|
|
215
217
|
|
|
216
|
-
## 📚 API Docs
|
|
217
|
-
|
|
218
|
-
- OpenAPI JSON: **`/openapi.json`**
|
|
219
|
-
- Optional UI: **`/docs`** (controlled via `DOCS=true`)
|
|
220
218
|
|
|
221
|
-
---
|
|
222
219
|
|
|
223
220
|
## 🛠️ Tips & Tools
|
|
224
221
|
|
|
225
222
|
### 🔌 Scalar API Client (Live Testing)
|
|
223
|
+
- Download the great new api client
|
|
224
|
+
- It autosyncs well with the server
|
|
226
225
|
- Add Item → Import from OpenAPI
|
|
227
226
|
- Paste: `http://localhost:3000/openapi.json`
|
|
228
227
|
- Enable watch mode for live updates
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maxserver",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "Node server setup based fastify",
|
|
5
5
|
"author": "Max Matinpalo",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/max-matinpalo/maxserver.git"
|
|
10
|
+
},
|
|
7
11
|
"main": "src/index.js",
|
|
8
12
|
"bin": {
|
|
9
13
|
"maxserver": "bin/init.js",
|