backendium 0.0.1 → 0.0.2
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/dist/handler.js +1 -1
- package/package.json +1 -1
- package/readme.md +117 -1
- package/src/handler.ts +1 -1
package/dist/handler.js
CHANGED
|
@@ -31,7 +31,7 @@ export function defaultErrorHandler(request, response, data, app, error, message
|
|
|
31
31
|
}
|
|
32
32
|
export function defaultValidationErrorHandler(request, response, app, data, error, message) {
|
|
33
33
|
response.status(400);
|
|
34
|
-
response.end(message !== null && message !== void 0 ? message : "Validation failed");
|
|
34
|
+
response.end(message !== null && message !== void 0 ? message : "Validation failed"); // @TODO
|
|
35
35
|
}
|
|
36
36
|
export default function backendiumHandler(handler, _a) {
|
|
37
37
|
var { auth, authChecker, authFailed, errorHandler, errorMessage, validationErrorHandler, validationErrorMessage } = _a, options = __rest(_a, ["auth", "authChecker", "authFailed", "errorHandler", "errorMessage", "validationErrorHandler", "validationErrorMessage"]);
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -54,7 +54,123 @@ router.get("*", (request, response, app, next) => {
|
|
|
54
54
|
export default router;
|
|
55
55
|
```
|
|
56
56
|
Backendium class extends BackendiumRouter
|
|
57
|
-
##
|
|
57
|
+
## Methods
|
|
58
58
|
```typescript
|
|
59
|
+
router.get("/route", (request, response, app, next) => {
|
|
60
|
+
// handler
|
|
61
|
+
});
|
|
59
62
|
|
|
63
|
+
router.post("/route", (request, response, app, next) => {
|
|
64
|
+
// handler
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
### Supported methods:
|
|
68
|
+
"get", "post", "put", "delete", "patch", "options", "head", "checkout", "connect", "copy", "lock", "merge", "mkactivity", "mkcol", "move", "m-search", "notify", "propfind", "proppatch", "purge", "report", "search", "subscribe", "unsubscribe", "trace", "unlock", "link", "unlink"
|
|
69
|
+
# Request validation ([checkeasy](https://github.com/smbwain/checkeasy))
|
|
70
|
+
Checkeasy imports:
|
|
71
|
+
```typescript
|
|
72
|
+
import {int, object, string, strToInt} from "checkeasy";
|
|
73
|
+
```
|
|
74
|
+
```typescript
|
|
75
|
+
router.post<{n: number}>("/validated", (request, response) => {
|
|
76
|
+
console.log(request.body);
|
|
77
|
+
response.end(Math.sqrt(request.body.n));
|
|
78
|
+
}, {
|
|
79
|
+
bodyValidator: object({
|
|
80
|
+
n: int()
|
|
81
|
+
})
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
```bash
|
|
85
|
+
curl http://localhost:8080/validated -d '{"n": 2}'
|
|
86
|
+
```
|
|
87
|
+
## Query
|
|
88
|
+
```typescript
|
|
89
|
+
router.get<Buffer /*default type for request body*/, {}, {n: number}>("/validated/query", (request, response) => {
|
|
90
|
+
console.log(request.query);
|
|
91
|
+
response.end(Math.sqrt(request.query.n));
|
|
92
|
+
}, {
|
|
93
|
+
queryValidator: object({
|
|
94
|
+
n: strToInt()
|
|
95
|
+
})
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
```bash
|
|
99
|
+
curl "http://localhost:8080/validated/query?n=2"
|
|
100
|
+
```
|
|
101
|
+
## Headers
|
|
102
|
+
```typescript
|
|
103
|
+
router.get<Buffer, {}, {}, undefined, {n: number}>("/validated/headers", (request, response) => {
|
|
104
|
+
console.log(request.headers);
|
|
105
|
+
response.end(Math.sqrt(request.headers.n));
|
|
106
|
+
}, {
|
|
107
|
+
headersValidator: object({
|
|
108
|
+
n: strToInt()
|
|
109
|
+
}, {ignoreUnknown: true})
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
```bash
|
|
113
|
+
curl http://localhost:8080/validated/headers -H "n:2"
|
|
114
|
+
```
|
|
115
|
+
# Authorization
|
|
116
|
+
```typescript
|
|
117
|
+
router.post<Buffer, {}, {}, string>("/auth", (request, response) => {
|
|
118
|
+
console.log(request.auth); // type of request.auth is string
|
|
119
|
+
response.end();
|
|
120
|
+
}, {
|
|
121
|
+
authChecker(request, response) {
|
|
122
|
+
if (typeof request.headers.auth !== "string" || request.headers.auth !== "backendium") return null; // auth failed
|
|
123
|
+
return request.headers.auth; // return auth data
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
```bash
|
|
128
|
+
curl http://localhost:8080/auth -H "auth:backendium" -d ""
|
|
129
|
+
```
|
|
130
|
+
## Global (for router)
|
|
131
|
+
```typescript
|
|
132
|
+
const router = new BackendiumRouter<string>;
|
|
133
|
+
|
|
134
|
+
router.setAuth((request, response) => {
|
|
135
|
+
if (typeof request.headers.auth !== "string" || request.headers.auth !== "backendium") return null; // auth failed
|
|
136
|
+
return request.headers.auth; // return auth data
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
router.post("/auth", (request, response) => {
|
|
140
|
+
console.log(request.globalAuth); // type of request.globalAuth is string
|
|
141
|
+
response.end();
|
|
142
|
+
}, {
|
|
143
|
+
auth: true
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
# Dynamic routing
|
|
147
|
+
More info: https://expressjs.com/en/guide/routing.html#route-paths
|
|
148
|
+
```typescript
|
|
149
|
+
router.get<Buffer, {n: number}>("/dynamic/:n", (request, response) => {
|
|
150
|
+
console.log(request.params);
|
|
151
|
+
response.end(Math.sqrt(request.params.n));
|
|
152
|
+
}, {
|
|
153
|
+
paramsValidator: object({
|
|
154
|
+
n: strToInt()
|
|
155
|
+
})
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
```bash
|
|
159
|
+
curl http://localhost:8080/dynamic/2
|
|
160
|
+
```
|
|
161
|
+
# Websocket
|
|
162
|
+
```typescript
|
|
163
|
+
router.ws("/ws")
|
|
164
|
+
.on("message", (data, socket) => {
|
|
165
|
+
console.log(data.toString()); // data is Buffer
|
|
166
|
+
socket.send(data);
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
request:
|
|
170
|
+
```javascript
|
|
171
|
+
const connection = new WebSocket("ws://localhost:8080/ws");
|
|
172
|
+
connection.send("test");
|
|
173
|
+
connection.onmessage = (message) => {
|
|
174
|
+
console.log(message.data);
|
|
175
|
+
};
|
|
60
176
|
```
|
package/src/handler.ts
CHANGED
|
@@ -23,7 +23,7 @@ export function defaultErrorHandler(request: Request, response: BackendiumRespon
|
|
|
23
23
|
|
|
24
24
|
export function defaultValidationErrorHandler(request: Request, response: BackendiumResponse, app: Backendium, data: Buffer, error: ValidationError, message: string) {
|
|
25
25
|
response.status(400);
|
|
26
|
-
response.end(message ?? "Validation failed");
|
|
26
|
+
response.end(message ?? "Validation failed"); // @TODO
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export default function backendiumHandler<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>(handler: BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>,
|