backendium 0.0.3 → 0.0.5

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/index.d.ts CHANGED
@@ -50,3 +50,5 @@ export default class Backendium<GlobalAuthType = any> extends BackendiumRouter<G
50
50
  start(callback?: (server: Server) => void): Server;
51
51
  startAsync(): Promise<Server>;
52
52
  }
53
+ export * from "./ws";
54
+ export * from "./router";
package/dist/index.js CHANGED
@@ -85,3 +85,5 @@ export default class Backendium extends BackendiumRouter {
85
85
  });
86
86
  }
87
87
  }
88
+ export * from "./ws";
89
+ export * from "./router";
package/dist/router.js CHANGED
@@ -116,7 +116,7 @@ export class BackendiumRouter {
116
116
  this.addHandler("unlink", ...args);
117
117
  }
118
118
  ws(route) {
119
- const constructor = new WebSocketRouteConstructor();
119
+ let constructor = new WebSocketRouteConstructor;
120
120
  this._handlers.push(["ws", route, [(app) => (request, response, next) => {
121
121
  constructor._handle(request, response, next, app);
122
122
  }]]);
package/dist/ws.d.ts CHANGED
@@ -96,6 +96,6 @@ export declare class WebSocketRouteConstructor<InitDataType> {
96
96
  on<E extends EventKey<BackendiumWebSocketEvents<InitDataType>>>(event: E, subscriber: (...args: BackendiumWebSocketEvents<InitDataType>[E]) => void): WebSocketRouteConstructor<InitDataType>;
97
97
  once<E extends EventKey<BackendiumWebSocketEvents<InitDataType>>>(event: E, subscriber: (...args: BackendiumWebSocketEvents<InitDataType>[E]) => void): WebSocketRouteConstructor<InitDataType>;
98
98
  off<E extends EventKey<BackendiumWebSocketEvents<InitDataType>>>(event: E, subscriber: (...args: BackendiumWebSocketEvents<InitDataType>[E]) => void): WebSocketRouteConstructor<InitDataType>;
99
- requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): void;
99
+ requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): WebSocketRouteConstructor<InitDataType>;
100
100
  }
101
101
  export {};
package/dist/ws.js CHANGED
@@ -333,6 +333,7 @@ export class WebSocketRouteConstructor {
333
333
  app.logger.wsInitFailed(url);
334
334
  }
335
335
  }));
336
+ return this;
336
337
  }
337
338
  }
338
339
  // @TODO error handling +termination logging
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backendium",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
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
@@ -114,30 +114,38 @@ curl http://localhost:8080/validated/headers -H "n:2"
114
114
  ```
115
115
  # Authorization
116
116
  ```typescript
117
+ const USERS: {[key: number]: string} = {
118
+ 54: "sizoff"
119
+ }
120
+
117
121
  router.post<Buffer, {}, {}, string>("/auth", (request, response) => {
118
122
  console.log(request.auth); // type of request.auth is string
119
123
  response.end();
120
124
  }, {
121
125
  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
126
+ if (typeof request.headers.auth !== "string" || !(Number(request.headers.auth) in USERS)) return null; // auth failed
127
+ return USERS[Number(request.headers.auth)]; // return auth data
124
128
  }
125
129
  });
126
130
  ```
127
131
  ```bash
128
- curl http://localhost:8080/auth -H "auth:backendium" -d ""
132
+ curl http://localhost:8080/auth -H "auth:54" -d ""
129
133
  ```
130
134
  ## Global (for router)
131
135
  ```typescript
132
136
  const router = new BackendiumRouter<string>;
133
137
 
138
+ const USERS: {[key: number]: string} = {
139
+ 54: "sizoff"
140
+ }
141
+
134
142
  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
143
+ if (typeof request.headers.auth !== "string" || !(Number(request.headers.auth) in USERS)) return null; // auth failed
144
+ return USERS[Number(request.headers.auth)]; // return auth data
137
145
  });
138
146
 
139
147
  router.post("/auth", (request, response) => {
140
- console.log(request.globalAuth); // type of request.globalAuth is string
148
+ console.log(request.globalAuth); // type of request.auth is string
141
149
  response.end();
142
150
  }, {
143
151
  auth: true
@@ -193,7 +201,7 @@ router.ws("/ws")
193
201
  .event<number>("sqrt", (data, socket) => {
194
202
  console.log(data);
195
203
  socket.emit("response", Math.sqrt(data));
196
- }, int())
204
+ }, int());
197
205
  ```
198
206
  only Backendium connect
199
207
  ```typescript
@@ -203,4 +211,25 @@ websocketRequest()
203
211
  .then(socket => {
204
212
  socket.emit("sqrt", 2);
205
213
  });
214
+ ```
215
+ ## Init
216
+ ```typescript
217
+ router.ws<string>("/ws/init")
218
+ .event("test", (data, socket) => {
219
+ socket.send(socket.initData);
220
+ })
221
+ .requireInit<number>((socket, data) => {
222
+ if (!(data in USERS)) return null; // auth failed
223
+ return USERS[data]; // return auth data
224
+ }, int());
225
+ ```
226
+ ```typescript
227
+ websocketRequest<number>()
228
+ .on("message", data => {
229
+ console.log(data.toString());
230
+ })
231
+ .send("ws://localhost:8080/ws/init", 54)
232
+ .then(socket => {
233
+ socket.emit("test");
234
+ });
206
235
  ```
package/src/index.ts CHANGED
@@ -106,4 +106,7 @@ export default class Backendium<GlobalAuthType = any> extends BackendiumRouter<G
106
106
  this.start(resolve);
107
107
  });
108
108
  }
109
- }
109
+ }
110
+
111
+ export * from "./ws";
112
+ export * from "./router";
package/src/router.ts CHANGED
@@ -200,8 +200,8 @@ export class BackendiumRouter<GlobalAuthType = undefined> {
200
200
  this.addHandler("unlink", ...args);
201
201
  }
202
202
 
203
- ws<InitDataType>(route: string): WebSocketRouteConstructor<InitDataType> {
204
- const constructor = new WebSocketRouteConstructor<InitDataType>();
203
+ ws<InitDataType>(route: string) {
204
+ let constructor = new WebSocketRouteConstructor<InitDataType>;
205
205
  this._handlers.push(["ws", route, [(app: Backendium) => (request: Request, response: WSResponse, next: NextFunction) => {
206
206
  constructor._handle(request, response, next, app);
207
207
  }]]);
package/src/ws.ts CHANGED
@@ -341,7 +341,7 @@ export class WebSocketRouteConstructor<InitDataType> {
341
341
  return this;
342
342
  }
343
343
 
344
- public requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>) {
344
+ public requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): WebSocketRouteConstructor<InitDataType> {
345
345
  this.initRequired = true;
346
346
  this.on("init", async (socket, data, app, url) => {
347
347
  let [mainData, parsed] = validator ? parse(data, validator) : [data, true];
@@ -365,6 +365,7 @@ export class WebSocketRouteConstructor<InitDataType> {
365
365
  else app.logger.wsInitFailed(url);
366
366
  }
367
367
  });
368
+ return this;
368
369
  }
369
370
  }
370
371