maxserver 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 +52 -49
- package/package.json +5 -1
- package/src/index.js +1 -1
package/README.md
CHANGED
|
@@ -1,35 +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 😉
|
|
8
|
+
I am simplifying and improving things, that it will work for everyone plugn play.
|
|
7
9
|
|
|
8
|
-
-> Node server setup based on **Fastify** with a new simple route loader.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
- **JWT auth** (cookie or `Authorization: Bearer ...`)
|
|
12
|
-
- **Autogenerates docs** (`/openapi.json`) + optional UI (`/docs`)
|
|
13
|
-
- **MongoDB** auto-connect + global `db` + `oid()` helper
|
|
14
|
-
- **Dev server** (auto reload on changes)
|
|
15
|
-
- **HTTPS** support (when configured)
|
|
11
|
+
Ready node server setup based on **Fastify** to speedup api development.
|
|
16
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>
|
|
17
19
|
|
|
18
20
|
|
|
19
|
-
|
|
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.
|
|
20
23
|
|
|
21
|
-
### Install
|
|
22
|
-
npm install maxserver
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
npx maxserver [appname]
|
|
25
|
+
## Install
|
|
26
26
|
|
|
27
|
+
### Setup ready project
|
|
28
|
+
npx maxserver [appname]
|
|
27
29
|
|
|
30
|
+
### Install
|
|
31
|
+
npm install maxserver
|
|
28
32
|
|
|
29
33
|
## Setup
|
|
30
|
-
maxserver(options) forwards options to fastify(options).
|
|
31
|
-
It returns the fully configured Fastify server instance.
|
|
32
|
-
|
|
33
34
|
```js
|
|
34
35
|
import maxserver from "maxserver";
|
|
35
36
|
const server = await maxserver();
|
|
@@ -41,16 +42,20 @@ console.log("Server running at", address);
|
|
|
41
42
|
export default server;
|
|
42
43
|
```
|
|
43
44
|
|
|
45
|
+
**maxserver(options)** forwards options to fastify(options).
|
|
46
|
+
It returns the fully configured Fastify server instance.
|
|
47
|
+
|
|
48
|
+
|
|
44
49
|
---
|
|
45
50
|
|
|
46
|
-
## ⚙️ Configuration
|
|
51
|
+
## ⚙️ Configuration
|
|
47
52
|
Configure the server by setting variabels in your .env file.
|
|
48
53
|
|
|
49
54
|
| Variable | Default | Description |
|
|
50
55
|
| :--- | :--- | :--- |
|
|
51
56
|
| `PORT` | `3000` | Server port |
|
|
52
|
-
| `JWT_SECRET` |
|
|
53
|
-
| `MONGODB_URI` |
|
|
57
|
+
| `JWT_SECRET` | *-* | Enables JWT auth (private-by-default routes) |
|
|
58
|
+
| `MONGODB_URI` | *-* | Enables MongoDB auto-connect + global `db` |
|
|
54
59
|
| `DOCS` | `true` | Set `false` to disable docs UI at `/docs` |
|
|
55
60
|
| `STATIC_DIR` | *(optional)* | Serve static files (example: `./public`) |
|
|
56
61
|
| `CORS_ORIGIN` | `*` | Allowed CORS origins |
|
|
@@ -59,7 +64,7 @@ Configure the server by setting variabels in your .env file.
|
|
|
59
64
|
|
|
60
65
|
## 🗂️ Project Structure
|
|
61
66
|
|
|
62
|
-
**
|
|
67
|
+
**1 route = 1 handler file + 1 schema file**
|
|
63
68
|
|
|
64
69
|
Example:
|
|
65
70
|
|
|
@@ -68,33 +73,27 @@ src/
|
|
|
68
73
|
users/
|
|
69
74
|
teams/
|
|
70
75
|
forms/
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
hello.js
|
|
77
|
+
hello.schema.js
|
|
73
78
|
...
|
|
74
79
|
```
|
|
75
80
|
|
|
76
81
|
---
|
|
77
82
|
|
|
78
|
-
## 🛣️
|
|
83
|
+
## 🛣️ Handlers
|
|
79
84
|
|
|
80
85
|
### 1) Define method + path
|
|
81
|
-
|
|
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.
|
|
82
88
|
|
|
83
89
|
```js
|
|
84
90
|
// GET /teams/:id
|
|
85
91
|
```
|
|
86
92
|
|
|
87
|
-
That comment is what the route loader uses to auto-register the route.
|
|
88
93
|
|
|
89
|
-
### 2) Default export handler
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
### 2) Default export handler
|
|
92
96
|
|
|
93
|
-
If something fails, throw:
|
|
94
|
-
|
|
95
|
-
```js
|
|
96
|
-
throw createError(code, "Specific failure reason");
|
|
97
|
-
```
|
|
98
97
|
|
|
99
98
|
### Example
|
|
100
99
|
|
|
@@ -103,30 +102,26 @@ throw createError(code, "Specific failure reason");
|
|
|
103
102
|
|
|
104
103
|
export default async function (req, res) {
|
|
105
104
|
|
|
106
|
-
// 1. Read input
|
|
107
105
|
const id = req.params.id;
|
|
108
|
-
|
|
109
|
-
// 2. Load data
|
|
110
|
-
const team = { id, name: "Team A" };
|
|
111
|
-
|
|
112
|
-
// 3. Respond
|
|
106
|
+
console.log("Example id": id);
|
|
113
107
|
return team;
|
|
114
108
|
}
|
|
115
109
|
```
|
|
116
110
|
|
|
117
111
|
---
|
|
118
112
|
|
|
119
|
-
## 🧾
|
|
113
|
+
## 🧾 Define Schemas
|
|
120
114
|
Create a sibling file ending in **`.schema.js`**.
|
|
115
|
+
The schema is offcourse a jsonschema.
|
|
121
116
|
This file will be auto registered.
|
|
122
117
|
|
|
123
118
|
Schemas:
|
|
124
119
|
- validate inputs
|
|
125
120
|
- generate OpenAPI docs
|
|
126
|
-
- control
|
|
121
|
+
- control route options for example (public/private)
|
|
127
122
|
|
|
128
123
|
|
|
129
|
-
**`
|
|
124
|
+
**`Important - use default export`**
|
|
130
125
|
```js
|
|
131
126
|
export default {
|
|
132
127
|
tags: ["Teams"],
|
|
@@ -161,7 +156,18 @@ export default {
|
|
|
161
156
|
};
|
|
162
157
|
```
|
|
163
158
|
|
|
164
|
-
|
|
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
|
+
|
|
165
171
|
|
|
166
172
|
## 🔐 Authentication (JWT)
|
|
167
173
|
|
|
@@ -181,7 +187,7 @@ export default {
|
|
|
181
187
|
};
|
|
182
188
|
```
|
|
183
189
|
|
|
184
|
-
|
|
190
|
+
<br>
|
|
185
191
|
|
|
186
192
|
## 🍃 MongoDB
|
|
187
193
|
Define in the env **`MONGODB_URI`** and it will auto-connect at server start and you get:
|
|
@@ -209,16 +215,13 @@ Rule of thumb: make the message something you would want to see at 02:00 in logs
|
|
|
209
215
|
|
|
210
216
|
---
|
|
211
217
|
|
|
212
|
-
## 📚 API Docs
|
|
213
|
-
|
|
214
|
-
- OpenAPI JSON: **`/openapi.json`**
|
|
215
|
-
- Optional UI: **`/docs`** (controlled via `DOCS=true`)
|
|
216
218
|
|
|
217
|
-
---
|
|
218
219
|
|
|
219
220
|
## 🛠️ Tips & Tools
|
|
220
221
|
|
|
221
222
|
### 🔌 Scalar API Client (Live Testing)
|
|
223
|
+
- Download the great new api client
|
|
224
|
+
- It autosyncs well with the server
|
|
222
225
|
- Add Item → Import from OpenAPI
|
|
223
226
|
- Paste: `http://localhost:3000/openapi.json`
|
|
224
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.10",
|
|
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",
|
package/src/index.js
CHANGED
|
@@ -22,7 +22,7 @@ export default async function maxserver(options = {}) {
|
|
|
22
22
|
trustProxy: true,
|
|
23
23
|
https: getHttpsOptions() || undefined,
|
|
24
24
|
|
|
25
|
-
// To allow writing example
|
|
25
|
+
// To allow writing example value fields to schemas for doucumentation
|
|
26
26
|
ajv: { customOptions: { strictSchema: false } },
|
|
27
27
|
...options,
|
|
28
28
|
});
|