backendium 0.0.1 → 0.0.3

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/dist/logger.js CHANGED
@@ -137,7 +137,7 @@ export default class Logger {
137
137
  }
138
138
  wsInit(url) {
139
139
  let prefix = this.getPrefix("wsInit");
140
- this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.red("done")], [prefix, "Websocket init on url", url, "done"]);
140
+ this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.green("done")], [prefix, "Websocket init on url", url, "done"]);
141
141
  }
142
142
  wsInitFailed(url) {
143
143
  let prefix = this.getPrefix("wsInitFailed");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backendium",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Express-based javascript backend framework with websocket support and type-safe checkeasy validators",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/readme.md CHANGED
@@ -54,7 +54,153 @@ router.get("*", (request, response, app, next) => {
54
54
  export default router;
55
55
  ```
56
56
  Backendium class extends BackendiumRouter
57
- ## GET
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
+ ### js build-in websockets:
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
+ ```
177
+ ### [Backendium connect](https://github.com/vssizoff/backendiumConnect)
178
+ ```typescript
179
+ import {websocketRequest} from "backendium-connect";
180
+
181
+ websocketRequest()
182
+ .on("message", (data, socket) => {
183
+ console.log(data); // data is Buffer
184
+ })
185
+ .send("ws://localhost:8080/ws")
186
+ .then(socket => {
187
+ socket.send("test");
188
+ });
189
+ ```
190
+ ## Events
191
+ ```typescript
192
+ router.ws("/ws")
193
+ .event<number>("sqrt", (data, socket) => {
194
+ console.log(data);
195
+ socket.emit("response", Math.sqrt(data));
196
+ }, int())
197
+ ```
198
+ only Backendium connect
199
+ ```typescript
200
+ websocketRequest()
201
+ .event<number>("response", data => console.log(data), float())
202
+ .send("ws://localhost:8080/ws")
203
+ .then(socket => {
204
+ socket.emit("sqrt", 2);
205
+ });
60
206
  ```
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>,
package/src/logger.ts CHANGED
@@ -164,7 +164,7 @@ export default class Logger {
164
164
 
165
165
  wsInit(url: string) {
166
166
  let prefix = this.getPrefix("wsInit");
167
- this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.red("done")],
167
+ this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.green("done")],
168
168
  [prefix, "Websocket init on url", url, "done"]);
169
169
  }
170
170