backendium 0.0.10 → 0.0.12

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/src/index.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import {Request, RequestHandler} from "express";
2
2
  import {Server} from "node:http";
3
- import {BackendiumRouter, MethodType} from "./router.js";
3
+ import {BackendiumRouter} from "./router.js";
4
4
  import {EventEmitter, EventKey} from "event-emitter-typescript";
5
5
  import {WebSocketExpress, WSRequestHandler} from "websocket-express";
6
6
  import {BackendiumWebSocket} from "./ws.js";
7
- import Logger from "./logger.js";
7
+ import Logger from "./logger";
8
8
  import BackendiumResponse from "./response";
9
9
  import {ValidationError} from "checkeasy";
10
+ import type {MethodType} from "./httpRouter.js";
10
11
 
11
12
  export type BackendiumConfigType = {
12
13
  port: number,
@@ -108,5 +109,5 @@ export default class Backendium<GlobalAuthType = any> extends BackendiumRouter<G
108
109
  }
109
110
  }
110
111
 
111
- export * from "./ws.js";
112
- export * from "./router.js";
112
+ export * from "./ws";
113
+ export * from "./router";
package/src/request.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {Request} from "express";
2
2
  import Backendium from "./index.js";
3
3
  import {ValidationError, Validator} from "checkeasy";
4
- import BackendiumResponse from "./response.js";
4
+ import BackendiumResponse from "./response";
5
5
 
6
6
  export type ValidatorsType<BodyType, ParamsType, QueryType, HeadersType> = {
7
7
  bodyValidator?: Validator<BodyType>,
@@ -75,10 +75,10 @@ export default async function parseRequest<BodyType, ParamsType, QueryType, Auth
75
75
  ): Promise<Omit<Omit<BackendiumRequestType<BodyType, ParamsType, QueryType, AuthType, HeadersType, any>, "auth">, "globalAuth"> | [Buffer, ValidationError]> {
76
76
  let bodyBuffer = await getBody(request);
77
77
  try {
78
- let body = parse(bodyBuffer, bodyValidator);
79
- let params = parse(request.params, paramsValidator);
80
- let query = parse(request.query, queryValidator);
81
- let headers = parse(request.headers, headersValidator);
78
+ let body = parse(bodyBuffer, bodyValidator, "body");
79
+ let params = parse(request.params, paramsValidator, "params");
80
+ let query = parse(request.query, queryValidator, "query");
81
+ let headers = parse(request.headers, headersValidator, "headers");
82
82
  return {
83
83
  expressRequest: request, body, params, query, headers, bodyBuffer, app,
84
84
  options: {bodyValidator, paramsValidator, queryValidator, headersValidator, ...other}
package/src/router.ts CHANGED
@@ -4,201 +4,23 @@ import {NextFunction, Request, RequestHandler} from "express";
4
4
  import Backendium from "./index.js";
5
5
  import {WebSocketRouteConstructor} from "./ws.js";
6
6
  import {WSRequestHandler, WSResponse} from "websocket-express";
7
+ import {BackendiumHttpRouter, type MethodType} from "./httpRouter.js";
7
8
 
8
- export type MethodType = "use" | "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "checkout"
9
- | "connect" | "copy" | "lock" | "merge" | "mkactivity" | "mkcol" | "move" | "m-search" | "notify" | "propfind"
10
- | "proppatch" | "purge" | "report" | "search" | "subscribe" | "unsubscribe" | "trace" | "unlock" | "link" | "unlink"
11
- | "useHTTP";
12
-
13
- export type BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType> =
14
- Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>
15
- | [string, ...Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>]
16
- | [...Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>,
17
- BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType>]
18
- | [string, ...Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>,
19
- BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType>];
20
-
21
- export class BackendiumRouter<GlobalAuthType = undefined> {
9
+ export class BackendiumRouter<GlobalAuthType = undefined> extends BackendiumHttpRouter<GlobalAuthType> {
22
10
  protected _handlers: Array<[MethodType, string | undefined, Array<ReturnType<RawHandlerType<GlobalAuthType>>>] | ["ws", string, Array<(app: Backendium) => WSRequestHandler>]> = [];
23
11
  public authChecker: AuthCheckerType<GlobalAuthType> | undefined;
24
12
  public authFailed: AuthFailedType | undefined;
25
13
 
26
- constructor() {}
27
-
28
- get handlers() {return this._handlers}
29
-
30
- protected parseArgs<BodyType, ParamsType, QueryType, AuthType, HeadersType>(args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): [string | undefined, Array<RawHandlerType<GlobalAuthType>>] {
31
- let route: string | undefined = undefined, options: BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType> | undefined = undefined;
32
- let handlers: Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>> = args.map(elem => {
33
- if (typeof elem === "string") route = elem;
34
- if (typeof elem === "function") return elem;
35
- // @ts-ignore
36
- else options = elem;
37
- }).filter(elem => typeof elem === "function");
38
- return [route, handlers.map(handler => backendiumHandler(handler, options ?? {auth: false}))];
39
- }
40
-
41
- public addHandler<BodyType, ParamsType, QueryType, AuthType, HeadersType>(method: MethodType,
42
- ...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
43
- ): void {
44
- let [route, handlers] = this.parseArgs(args);
45
- this._handlers.push([method, route, handlers.map(handler => handler(this))]);
46
- }
47
-
48
- use<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
49
- ): void {
50
- this.addHandler("use", ...args);
51
- }
52
-
53
- useHTTP<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
54
- ): void {
55
- this.addHandler("useHTTP", ...args);
56
- }
57
-
58
- all<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
59
- ): void {
60
- this.addHandler("all", ...args);
61
- }
62
-
63
- get<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
64
- ): void {
65
- this.addHandler("get", ...args);
66
- }
67
-
68
- post<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
69
- ): void {
70
- this.addHandler("post", ...args);
71
- }
72
-
73
- put<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
14
+ protected pushHandlers<BodyType, ParamsType, QueryType, AuthType, HeadersType>(
15
+ method: MethodType,
16
+ path: string | undefined,
17
+ options: BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType> | undefined,
18
+ handlers: Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>
74
19
  ): void {
75
- this.addHandler("put", ...args);
20
+ this._handlers.push([method, path, handlers.map(handler => backendiumHandler(handler, options ?? { auth: false })(this))])
76
21
  }
77
22
 
78
- delete<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
79
- ): void {
80
- this.addHandler("delete", ...args);
81
- }
82
-
83
- patch<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
84
- ): void {
85
- this.addHandler("patch", ...args);
86
- }
87
-
88
- options<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
89
- ): void {
90
- this.addHandler("options", ...args);
91
- }
92
-
93
- head<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
94
- ): void {
95
- this.addHandler("head", ...args);
96
- }
97
-
98
- checkout<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
99
- ): void {
100
- this.addHandler("checkout", ...args);
101
- }
102
-
103
- connect<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
104
- ): void {
105
- this.addHandler("connect", ...args);
106
- }
107
-
108
- copy<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
109
- ): void {
110
- this.addHandler("copy", ...args);
111
- }
112
-
113
- lock<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
114
- ): void {
115
- this.addHandler("lock", ...args);
116
- }
117
-
118
- merge<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
119
- ): void {
120
- this.addHandler("merge", ...args);
121
- }
122
-
123
- mkactivity<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
124
- ): void {
125
- this.addHandler("mkactivity", ...args);
126
- }
127
-
128
- mkcol<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
129
- ): void {
130
- this.addHandler("mkcol", ...args);
131
- }
132
-
133
- move<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
134
- ): void {
135
- this.addHandler("move", ...args);
136
- }
137
-
138
- "m-search"<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
139
- ): void {
140
- this.addHandler("m-search", ...args);
141
- }
142
-
143
- notify<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
144
- ): void {
145
- this.addHandler("notify", ...args);
146
- }
147
-
148
- propfind<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
149
- ): void {
150
- this.addHandler("propfind", ...args);
151
- }
152
-
153
- proppatch<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
154
- ): void {
155
- this.addHandler("proppatch", ...args);
156
- }
157
-
158
- purge<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
159
- ): void {
160
- this.addHandler("purge", ...args);
161
- }
162
-
163
- report<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
164
- ): void {
165
- this.addHandler("report", ...args);
166
- }
167
-
168
- search<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
169
- ): void {
170
- this.addHandler("search", ...args);
171
- }
172
-
173
- subscribe<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
174
- ): void {
175
- this.addHandler("subscribe", ...args);
176
- }
177
-
178
- unsubscribe<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
179
- ): void {
180
- this.addHandler("unsubscribe", ...args);
181
- }
182
-
183
- trace<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
184
- ): void {
185
- this.addHandler("trace", ...args);
186
- }
187
-
188
- unlock<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
189
- ): void {
190
- this.addHandler("unlock", ...args);
191
- }
192
-
193
- link<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
194
- ): void {
195
- this.addHandler("link", ...args);
196
- }
197
-
198
- unlink<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>
199
- ): void {
200
- this.addHandler("unlink", ...args);
201
- }
23
+ get handlers() {return this._handlers}
202
24
 
203
25
  ws<InitDataType>(route: string) {
204
26
  let constructor = new WebSocketRouteConstructor<InitDataType>;
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  /* Visit https://aka.ms/tsconfig to read more about this file */
4
- "target": "ES2022",
4
+ "target": "es2016",
5
5
  "module": "ES2022",
6
6
  "esModuleInterop": true,
7
7
  "forceConsistentCasingInFileNames": true,