backendium 0.0.0 → 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 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
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "backendium",
3
- "version": "0.0.0",
4
- "description": "Express-based javascript backend framework with websocket support",
3
+ "version": "0.0.2",
4
+ "description": "Express-based javascript backend framework with websocket support and type-safe checkeasy validators",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
8
  "build": "tsc",
8
9
  "prepack": "tsc"
package/readme.md CHANGED
@@ -3,9 +3,174 @@
3
3
  # Table of Contents
4
4
  > 1. [Installation](#installation)
5
5
  > 2. [Basics](#basics)
6
- > 3. [Registering handlers](#registering-handlers)
6
+ > 3. [Routing](#routing)
7
7
  # Installation
8
- ```
8
+ ```bash
9
9
  npm i backendium checkeasy
10
10
  ```
11
11
  # Basics
12
+ ```typescript
13
+ import Backendium from "backendium";
14
+
15
+ const app = new Backendium;
16
+
17
+ app.get("*", (request, response, app, next) => {
18
+ response.end({backendium: "0.0.0"});
19
+ });
20
+
21
+ app.start();
22
+ ```
23
+ request:
24
+ ```bash
25
+ curl http://localhost:8080/
26
+ ```
27
+ response:
28
+ ```json
29
+ {"backendium":"0.0.0"}
30
+ ```
31
+ # Routing
32
+ ## Router class
33
+ ```typescript
34
+ // main.ts
35
+ import Backendium from "backendium";
36
+ import handlers from "./handlers.js";
37
+
38
+ const app = new Backendium;
39
+
40
+ app.router(handlers);
41
+
42
+ app.start();
43
+ ```
44
+ ```typescript
45
+ // handlers.ts
46
+ import {BackendiumRouter} from "backendium/dist/router";
47
+
48
+ const router = new BackendiumRouter;
49
+
50
+ router.get("*", (request, response, app, next) => {
51
+ response.end({backendium: "0.0.0"});
52
+ });
53
+
54
+ export default router;
55
+ ```
56
+ Backendium class extends BackendiumRouter
57
+ ## Methods
58
+ ```typescript
59
+ router.get("/route", (request, response, app, next) => {
60
+ // handler
61
+ });
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
+ };
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>,