maxserver 0.1.29 → 0.1.31
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 → .github/README.md} +67 -51
- package/package.json +1 -1
- package/templates/package.json +16 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# maxserver
|
|
2
|
-
|
|
3
|
-
Just a Node server setup based on **Fastify** to speed up backend development.
|
|
2
|
+
Node server setup based on **Fastify** to speed up backend development.
|
|
4
3
|
maxserver stands for **maximized simplicity** and **minimum boilerplate**.
|
|
5
4
|
|
|
6
5
|
- **Auto Routes**: auto imports and registers routes and schemas
|
|
@@ -32,7 +31,7 @@ export default server;
|
|
|
32
31
|
|
|
33
32
|
## ⚙️ Configure
|
|
34
33
|
Configs can be passed to the init call to **maxserver()** or set in your .env file.
|
|
35
|
-
If you define options in env, use all upper case letters.
|
|
34
|
+
If you define options in env, use all upper case letters.
|
|
36
35
|
Any fastify options can be passed to maxserver() too.
|
|
37
36
|
|
|
38
37
|
|
|
@@ -40,8 +39,8 @@ Any fastify options can be passed to maxserver() too.
|
|
|
40
39
|
| :--- | :--- | :--- |
|
|
41
40
|
| `port` | `3000` | Server port |
|
|
42
41
|
| `secret` | *-* | Secret used for jwt and cookies |
|
|
43
|
-
| `cors` | `*` |
|
|
44
|
-
| `docs` | `true` | Set `false` to disable auto generated docs
|
|
42
|
+
| `cors` | `*` | Default all allowed |
|
|
43
|
+
| `docs` | `true` | Set `false` to disable auto generated docs |
|
|
45
44
|
| `mongodb` | *-* | MongoDB URI, if set auto-connects db |
|
|
46
45
|
| `public` | `false` | Set `true` to expose the server publicly (binds to `0.0.0.0`) |
|
|
47
46
|
| `static` | *-* | If set, serves this directory statically |
|
|
@@ -50,29 +49,15 @@ Any fastify options can be passed to maxserver() too.
|
|
|
50
49
|
|
|
51
50
|
<br>
|
|
52
51
|
|
|
53
|
-
## 🗂️ Project Structure
|
|
54
|
-
Our golden rule: **1 route = 1 handler file + 1 schema file**
|
|
55
52
|
|
|
56
|
-
|
|
53
|
+
## 🤖 Auto Routing
|
|
54
|
+
Routes are auto registered based on one small comment per file:
|
|
57
55
|
|
|
58
56
|
```
|
|
59
|
-
|
|
60
|
-
users/
|
|
61
|
-
teams/
|
|
62
|
-
forms/
|
|
63
|
-
hello.js
|
|
64
|
-
hello.schema.js
|
|
65
|
-
...
|
|
57
|
+
// METHOD /path
|
|
66
58
|
```
|
|
67
59
|
<br>
|
|
68
60
|
|
|
69
|
-
## 🤖 Auto Routing
|
|
70
|
-
|
|
71
|
-
To auto-register routes, simple add a comment of the form:
|
|
72
|
-
**// METHOD /path**
|
|
73
|
-
**// GET /user**
|
|
74
|
-
**// POST /feedback/something**
|
|
75
|
-
...
|
|
76
61
|
|
|
77
62
|
```js
|
|
78
63
|
// GET /hello
|
|
@@ -87,22 +72,33 @@ export default async function handler(req, rep) {
|
|
|
87
72
|
}
|
|
88
73
|
```
|
|
89
74
|
<br>
|
|
90
|
-
|
|
91
|
-
And remember to use default export for your handler.
|
|
92
|
-
If you don't want to autoregister some routes, then simply don't add that magic comment 😃
|
|
93
|
-
That's it.
|
|
94
|
-
|
|
95
75
|
|
|
76
|
+
Doesn't matter how nested your path is or how many params,
|
|
77
|
+
It always works this simple and you are free to position your files the way you like.
|
|
78
|
+
If you don't want to autoregister some files, then simply don't add that magic comment 😃
|
|
96
79
|
<br>
|
|
97
80
|
<br>
|
|
81
|
+
```
|
|
82
|
+
// GET /teams/:teamid
|
|
83
|
+
// PATCH /forms/:formId/questions/:questionId
|
|
84
|
+
...
|
|
85
|
+
```
|
|
86
|
+
<br/>
|
|
98
87
|
|
|
88
|
+
### 3 RULES
|
|
89
|
+
1. Add magic comment
|
|
90
|
+
2. Default export handler
|
|
91
|
+
3. One handler per file
|
|
99
92
|
|
|
100
|
-
|
|
101
|
-
Create a sibling file ending with **`.schema.js`**, so it will be auto registered. For example: **hello.js** and **hello.schema.js**
|
|
93
|
+
<br>
|
|
102
94
|
|
|
103
|
-
Besides the basic validation fields we can set fields like summary and description, which will appear in the docs. Mostly you don't need to write schemas yourself, chat gpt and gemini do it excelently.
|
|
104
95
|
|
|
96
|
+
## 🧾 Schemas
|
|
97
|
+
Files ending with **`.schema.js`** will be auto registered.
|
|
98
|
+
For example: **hello.js** and **hello.schema.js**
|
|
105
99
|
|
|
100
|
+
Besides the basic validation fields we can set fields like `tags`, `summary` and description,
|
|
101
|
+
which will appear in the docs.
|
|
106
102
|
|
|
107
103
|
|
|
108
104
|
```js
|
|
@@ -126,38 +122,30 @@ export default {
|
|
|
126
122
|
};
|
|
127
123
|
```
|
|
128
124
|
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
**‼️ Important use export default**
|
|
126
|
+
Some examples in the template folder.
|
|
131
127
|
|
|
132
|
-
<br>
|
|
133
128
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
129
|
+
### MODELS
|
|
130
|
+
You can also auto register **models** (schemas which are shared between multiple routes).
|
|
131
|
+
For example `User.schema.js`. It "magically" understand the difference betwen route specific
|
|
132
|
+
schema or generic model, by looking if a sibling file exist or not 😉
|
|
137
133
|
|
|
138
|
-
|
|
139
|
-
// Inside schema
|
|
140
|
-
|
|
141
|
-
export default {
|
|
142
|
-
routeOptions: {
|
|
143
|
-
config: {
|
|
144
|
-
preHandler: ...
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
...
|
|
148
|
-
```
|
|
134
|
+
<br>
|
|
149
135
|
|
|
150
136
|
|
|
151
|
-
<br>
|
|
152
137
|
|
|
153
138
|
## 📚 API Docs
|
|
154
|
-
|
|
155
139
|
Open in your browser **`localhost:3000/docs`**
|
|
156
|
-
|
|
140
|
+
You should find all your routes well documented.
|
|
141
|
+
And you can also easily test any route.
|
|
157
142
|
|
|
158
143
|
<br>
|
|
159
144
|
|
|
160
145
|
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
161
149
|
## 🔐 Authentication
|
|
162
150
|
JWT header and cookie based auth is preconfigured.
|
|
163
151
|
To enable auth for a route set in it's schema **auth = true**
|
|
@@ -171,6 +159,23 @@ export default {
|
|
|
171
159
|
};
|
|
172
160
|
```
|
|
173
161
|
|
|
162
|
+
## 🛠️ Route Options
|
|
163
|
+
Though we don't mostly register routes manually, we don't set route options on the register call.
|
|
164
|
+
If needed, you can wether register that route manually or just set them on the schema.
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
// Inside schema
|
|
168
|
+
|
|
169
|
+
export default {
|
|
170
|
+
routeOptions: {
|
|
171
|
+
config: {
|
|
172
|
+
preHandler: ...
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
...
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
<br>
|
|
174
179
|
<br>
|
|
175
180
|
|
|
176
181
|
## 🍃 MongoDB
|
|
@@ -210,8 +215,19 @@ Rule of thumb: make the message something you would want to see at 03:00 in logs
|
|
|
210
215
|
|
|
211
216
|
<br>
|
|
212
217
|
|
|
218
|
+
|
|
219
|
+
|
|
213
220
|
## About
|
|
214
221
|
- Dependencies: original fastify packages + scalar/fastify-api-reference
|
|
215
222
|
- The source is simple. Everyone can read, understand and modify if needed.
|
|
216
223
|
|
|
217
|
-
|
|
224
|
+
|
|
225
|
+
## Todo
|
|
226
|
+
- document how to add fastify hooks
|
|
227
|
+
- document how to pass scalar options
|
|
228
|
+
- more example and best practises
|
|
229
|
+
- document project setup with npx
|
|
230
|
+
- npx option, atm devserver macos only
|
|
231
|
+
- unit tests
|
|
232
|
+
|
|
233
|
+
|
package/package.json
CHANGED
package/templates/package.json
CHANGED
|
@@ -9,5 +9,20 @@
|
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
11
|
"private": "true",
|
|
12
|
-
"dependencies": {}
|
|
12
|
+
"dependencies": {},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"fastify",
|
|
15
|
+
"backend",
|
|
16
|
+
"framework",
|
|
17
|
+
"node-server",
|
|
18
|
+
"api-server",
|
|
19
|
+
"auto-routes",
|
|
20
|
+
"automatic-docs",
|
|
21
|
+
"scalar",
|
|
22
|
+
"openapi",
|
|
23
|
+
"jwt-auth",
|
|
24
|
+
"mongodb",
|
|
25
|
+
"minimalist",
|
|
26
|
+
"zero-boilerplate"
|
|
27
|
+
]
|
|
13
28
|
}
|