@webtypen/webframez-core 0.0.43 → 0.0.45

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.
@@ -96,30 +96,35 @@ class RouterFacade {
96
96
  if (!req || !req.url) {
97
97
  return null;
98
98
  }
99
+ const cleanUrl = req.url.indexOf("?") > 0
100
+ ? req.url.substring(0, req.url.indexOf("?"))
101
+ : req.url.indexOf("#") > 0
102
+ ? req.url.substring(0, req.url.indexOf("#"))
103
+ : req.url;
99
104
  if (req.method === "GET") {
100
- return this.dissolveRoute(this.routesGET, req.url);
105
+ return this.dissolveRoute(this.routesGET, cleanUrl);
101
106
  }
102
107
  else if (req.method === "POST") {
103
- return this.dissolveRoute(this.routesPOST, req.url);
108
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
104
109
  }
105
110
  else if (req.method === "PUT") {
106
- return this.dissolveRoute(this.routesPUT, req.url);
111
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
107
112
  }
108
113
  else if (req.method === "DELETE") {
109
- return this.dissolveRoute(this.routesDELETE, req.url);
114
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
110
115
  }
111
116
  else if (req.method === "OPTIONS") {
112
117
  if (req.headers["access-control-request-method"] === "GET") {
113
- return this.dissolveRoute(this.routesGET, req.url);
118
+ return this.dissolveRoute(this.routesGET, cleanUrl);
114
119
  }
115
120
  else if (req.headers["access-control-request-method"] === "POST") {
116
- return this.dissolveRoute(this.routesPOST, req.url);
121
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
117
122
  }
118
123
  else if (req.headers["access-control-request-method"] === "PUT") {
119
- return this.dissolveRoute(this.routesPUT, req.url);
124
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
120
125
  }
121
126
  else if (req.headers["access-control-request-method"] === "DELETE") {
122
- return this.dissolveRoute(this.routesDELETE, req.url);
127
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
123
128
  }
124
129
  }
125
130
  return null;
@@ -167,7 +172,11 @@ class RouterFacade {
167
172
  */
168
173
  sendError(res, code, message) {
169
174
  res.writeHead(code, { "Content-Type": "text/html" });
170
- res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
175
+ res.end(message
176
+ ? message
177
+ : code === 404
178
+ ? "Oops! Page not found ..."
179
+ : "Oops! There was an unexpected error ...");
171
180
  }
172
181
  /**
173
182
  * Processes a request
@@ -213,11 +222,27 @@ class RouterFacade {
213
222
  });
214
223
  })
215
224
  : null;
225
+ const urlParams = {};
226
+ if (req && req.url && req.url.indexOf("?") > 0) {
227
+ const temp = req.url.substring(req.url.indexOf("?") + 1);
228
+ if (temp && temp.trim() !== "") {
229
+ const entries = temp.split("=");
230
+ for (let entry of entries) {
231
+ if (entry.indexOf("=") > 0) {
232
+ urlParams[entry.substring(0, entry.indexOf("="))] = entry.substring(entry.indexOf("=") + 1);
233
+ }
234
+ else {
235
+ urlParams[entry] = true;
236
+ }
237
+ }
238
+ }
239
+ }
216
240
  const request = this.mode === "aws-lambda" && customRequest
217
241
  ? {
218
242
  body: bodyPlain &&
219
243
  customRequest.headers["content-type"] === "application/json" &&
220
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
244
+ (bodyPlain.trim().substring(0, 1) === "[" ||
245
+ bodyPlain.trim().substring(0, 1) === "{")
221
246
  ? JSON.parse(bodyPlain)
222
247
  : bodyPlain
223
248
  ? qs_1.default.parse(options && options.event && options.event.isBase64Encoded
@@ -226,6 +251,7 @@ class RouterFacade {
226
251
  : {},
227
252
  bodyPlain: bodyPlain,
228
253
  params: route && route.params ? route.params : {},
254
+ urlParams: urlParams,
229
255
  headers: customRequest.headers,
230
256
  method: customRequest.method,
231
257
  }
@@ -233,13 +259,15 @@ class RouterFacade {
233
259
  ? {
234
260
  body: bodyPlain &&
235
261
  req.headers["content-type"] === "application/json" &&
236
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
262
+ (bodyPlain.trim().substring(0, 1) === "[" ||
263
+ bodyPlain.trim().substring(0, 1) === "{")
237
264
  ? JSON.parse(bodyPlain)
238
265
  : bodyPlain
239
266
  ? qs_1.default.parse(bodyPlain)
240
267
  : {},
241
268
  bodyPlain: bodyPlain,
242
269
  params: route && route.params ? route.params : {},
270
+ urlParams: urlParams,
243
271
  httpVersionMajor: req.httpVersionMajor,
244
272
  httpVersionMinor: req.httpVersionMinor,
245
273
  httpVersion: req.httpVersion,
@@ -251,14 +279,20 @@ class RouterFacade {
251
279
  }
252
280
  : null;
253
281
  // Check-Middleware
254
- if (route && route.options && route.options.middleware && route.options.middleware.length > 0) {
282
+ if (route &&
283
+ route.options &&
284
+ route.options.middleware &&
285
+ route.options.middleware.length > 0) {
255
286
  for (let middlewareKey of route.options.middleware) {
256
287
  const middleware = this.kernel.middleware[middlewareKey];
257
288
  if (!middleware) {
258
289
  if (this.mode === "aws-lambda") {
259
290
  return {
260
291
  statusCode: 500,
261
- body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
292
+ body: JSON.stringify({
293
+ status: "error",
294
+ message: "Unknown middleware `" + middlewareKey + "` ...",
295
+ }),
262
296
  headers: response.headers,
263
297
  };
264
298
  }
@@ -279,7 +313,10 @@ class RouterFacade {
279
313
  if (this.mode === "aws-lambda") {
280
314
  return {
281
315
  statusCode: 500,
282
- body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
316
+ body: JSON.stringify({
317
+ status: "error",
318
+ message: "Middleware run error `" + middlewareKey + "` ...",
319
+ }),
283
320
  headers: response.headers,
284
321
  };
285
322
  }
@@ -293,11 +330,21 @@ class RouterFacade {
293
330
  catch (e) {
294
331
  // Send Middleware-Abort
295
332
  if (this.mode === "aws-lambda") {
296
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }), headers: response.headers };
333
+ return {
334
+ statusCode: 500,
335
+ body: JSON.stringify({ status: "error", message: e.data }),
336
+ headers: response.headers,
337
+ };
297
338
  }
298
339
  else if (res) {
299
- res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
300
- res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
340
+ res.writeHead(e.status, typeof e.data === "object"
341
+ ? { "Content-Type": "application/json" }
342
+ : { "Content-Type": "text/html" });
343
+ res.end(typeof e.data === "object"
344
+ ? JSON.stringify(e.data)
345
+ : e.data
346
+ ? e.data
347
+ : " ");
301
348
  }
302
349
  return;
303
350
  }
@@ -328,7 +375,10 @@ class RouterFacade {
328
375
  if (this.mode === "aws-lambda") {
329
376
  return {
330
377
  statusCode: 500,
331
- body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }),
378
+ body: JSON.stringify({
379
+ status: "error",
380
+ message: "Controller `" + split[0] + "` not found ...",
381
+ }),
332
382
  headers: response.headers,
333
383
  };
334
384
  }
@@ -346,7 +396,11 @@ class RouterFacade {
346
396
  try {
347
397
  yield route.component(request, response);
348
398
  if (this.mode === "aws-lambda") {
349
- return { statusCode: response.statusCode, body: JSON.stringify(response.content), headers: response.headers };
399
+ return {
400
+ statusCode: response.statusCode,
401
+ body: JSON.stringify(response.content),
402
+ headers: response.headers,
403
+ };
350
404
  }
351
405
  else if (res && req) {
352
406
  res.end();
@@ -357,7 +411,10 @@ class RouterFacade {
357
411
  if (this.mode === "aws-lambda") {
358
412
  return {
359
413
  statusCode: 500,
360
- body: JSON.stringify({ status: "error", message: "Error running the component ..." }),
414
+ body: JSON.stringify({
415
+ status: "error",
416
+ message: "Error running the component ...",
417
+ }),
361
418
  headers: response.headers,
362
419
  };
363
420
  }
@@ -371,7 +428,10 @@ class RouterFacade {
371
428
  if (this.mode === "aws-lambda") {
372
429
  return {
373
430
  statusCode: 500,
374
- body: JSON.stringify({ status: "error", message: "Route component not found ..." }),
431
+ body: JSON.stringify({
432
+ status: "error",
433
+ message: "Route component not found ...",
434
+ }),
375
435
  headers: response.headers,
376
436
  };
377
437
  }
@@ -383,7 +443,8 @@ class RouterFacade {
383
443
  });
384
444
  }
385
445
  requestConsoleLog(req, httpCode) {
386
- if (Config_1.Config.get("application.router.requests.logConsole") !== true && !Config_1.Config.get("application.router.requests.logFile")) {
446
+ if (Config_1.Config.get("application.router.requests.logConsole") !== true &&
447
+ !Config_1.Config.get("application.router.requests.logFile")) {
387
448
  return;
388
449
  }
389
450
  const date = new Date();
@@ -425,7 +486,9 @@ class RouterFacade {
425
486
  try {
426
487
  const filepath = Config_1.Config.get("application.router.requests.logFile");
427
488
  if (filepath && filepath.trim() !== "") {
428
- fs_1.default.appendFileSync(filepath.trim().substring(0, 1) === "/" ? filepath : process.cwd() + "/" + filepath, logString + "\n");
489
+ fs_1.default.appendFileSync(filepath.trim().substring(0, 1) === "/"
490
+ ? filepath
491
+ : process.cwd() + "/" + filepath, logString + "\n");
429
492
  }
430
493
  }
431
494
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.0.43",
3
+ "version": "0.0.45",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -127,23 +127,30 @@ class RouterFacade {
127
127
  return null;
128
128
  }
129
129
 
130
+ const cleanUrl =
131
+ req.url.indexOf("?") > 0
132
+ ? req.url.substring(0, req.url.indexOf("?"))
133
+ : req.url.indexOf("#") > 0
134
+ ? req.url.substring(0, req.url.indexOf("#"))
135
+ : req.url;
136
+
130
137
  if (req.method === "GET") {
131
- return this.dissolveRoute(this.routesGET, req.url);
138
+ return this.dissolveRoute(this.routesGET, cleanUrl);
132
139
  } else if (req.method === "POST") {
133
- return this.dissolveRoute(this.routesPOST, req.url);
140
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
134
141
  } else if (req.method === "PUT") {
135
- return this.dissolveRoute(this.routesPUT, req.url);
142
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
136
143
  } else if (req.method === "DELETE") {
137
- return this.dissolveRoute(this.routesDELETE, req.url);
144
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
138
145
  } else if (req.method === "OPTIONS") {
139
146
  if (req.headers["access-control-request-method"] === "GET") {
140
- return this.dissolveRoute(this.routesGET, req.url);
147
+ return this.dissolveRoute(this.routesGET, cleanUrl);
141
148
  } else if (req.headers["access-control-request-method"] === "POST") {
142
- return this.dissolveRoute(this.routesPOST, req.url);
149
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
143
150
  } else if (req.headers["access-control-request-method"] === "PUT") {
144
- return this.dissolveRoute(this.routesPUT, req.url);
151
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
145
152
  } else if (req.headers["access-control-request-method"] === "DELETE") {
146
- return this.dissolveRoute(this.routesDELETE, req.url);
153
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
147
154
  }
148
155
  }
149
156
  return null;
@@ -196,7 +203,13 @@ class RouterFacade {
196
203
  */
197
204
  sendError(res: ServerResponse, code: number, message?: string) {
198
205
  res.writeHead(code, { "Content-Type": "text/html" });
199
- res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
206
+ res.end(
207
+ message
208
+ ? message
209
+ : code === 404
210
+ ? "Oops! Page not found ..."
211
+ : "Oops! There was an unexpected error ..."
212
+ );
200
213
  }
201
214
 
202
215
  /**
@@ -205,7 +218,11 @@ class RouterFacade {
205
218
  * @param req
206
219
  * @param res
207
220
  */
208
- async handleRequest(req: null | IncomingMessage, res: null | ServerResponse, options?: any) {
221
+ async handleRequest(
222
+ req: null | IncomingMessage,
223
+ res: null | ServerResponse,
224
+ options?: any
225
+ ) {
209
226
  let customRequest = null;
210
227
  if (!req && this.mode === "aws-lambda" && options && options.event) {
211
228
  customRequest = {
@@ -222,7 +239,9 @@ class RouterFacade {
222
239
  throw new Error("Missing Kernel in Router ...");
223
240
  }
224
241
 
225
- const route = this.dissolve(customRequest !== null && this.mode === "aws-lambda" ? customRequest : req);
242
+ const route = this.dissolve(
243
+ customRequest !== null && this.mode === "aws-lambda" ? customRequest : req
244
+ );
226
245
  const response = new Response({ mode: this.mode });
227
246
  if (res) {
228
247
  response.setServerResponse(res);
@@ -247,13 +266,31 @@ class RouterFacade {
247
266
  })
248
267
  : null;
249
268
 
269
+ const urlParams: any = {};
270
+ if (req && req.url && req.url.indexOf("?") > 0) {
271
+ const temp = req.url.substring(req.url.indexOf("?") + 1);
272
+ if (temp && temp.trim() !== "") {
273
+ const entries = temp.split("=");
274
+ for (let entry of entries) {
275
+ if (entry.indexOf("=") > 0) {
276
+ urlParams[entry.substring(0, entry.indexOf("="))] = entry.substring(
277
+ entry.indexOf("=") + 1
278
+ );
279
+ } else {
280
+ urlParams[entry] = true;
281
+ }
282
+ }
283
+ }
284
+ }
285
+
250
286
  const request =
251
287
  this.mode === "aws-lambda" && customRequest
252
288
  ? {
253
289
  body:
254
290
  bodyPlain &&
255
291
  customRequest.headers["content-type"] === "application/json" &&
256
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
292
+ (bodyPlain.trim().substring(0, 1) === "[" ||
293
+ bodyPlain.trim().substring(0, 1) === "{")
257
294
  ? JSON.parse(bodyPlain)
258
295
  : bodyPlain
259
296
  ? qs.parse(
@@ -265,6 +302,7 @@ class RouterFacade {
265
302
 
266
303
  bodyPlain: bodyPlain,
267
304
  params: route && route.params ? route.params : {},
305
+ urlParams: urlParams,
268
306
  headers: customRequest.headers,
269
307
  method: customRequest.method,
270
308
  }
@@ -273,13 +311,15 @@ class RouterFacade {
273
311
  body:
274
312
  bodyPlain &&
275
313
  req.headers["content-type"] === "application/json" &&
276
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
314
+ (bodyPlain.trim().substring(0, 1) === "[" ||
315
+ bodyPlain.trim().substring(0, 1) === "{")
277
316
  ? JSON.parse(bodyPlain)
278
317
  : bodyPlain
279
318
  ? qs.parse(bodyPlain)
280
319
  : {},
281
320
  bodyPlain: bodyPlain,
282
321
  params: route && route.params ? route.params : {},
322
+ urlParams: urlParams,
283
323
  httpVersionMajor: req.httpVersionMajor,
284
324
  httpVersionMinor: req.httpVersionMinor,
285
325
  httpVersion: req.httpVersion,
@@ -292,19 +332,31 @@ class RouterFacade {
292
332
  : null;
293
333
 
294
334
  // Check-Middleware
295
- if (route && route.options && route.options.middleware && route.options.middleware.length > 0) {
335
+ if (
336
+ route &&
337
+ route.options &&
338
+ route.options.middleware &&
339
+ route.options.middleware.length > 0
340
+ ) {
296
341
  for (let middlewareKey of route.options.middleware) {
297
342
  const middleware = this.kernel.middleware[middlewareKey];
298
343
  if (!middleware) {
299
344
  if (this.mode === "aws-lambda") {
300
345
  return {
301
346
  statusCode: 500,
302
- body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
347
+ body: JSON.stringify({
348
+ status: "error",
349
+ message: "Unknown middleware `" + middlewareKey + "` ...",
350
+ }),
303
351
  headers: response.headers,
304
352
  };
305
353
  } else if (req && res) {
306
354
  this.requestConsoleLog(req, 500);
307
- return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
355
+ return this.sendError(
356
+ res,
357
+ 500,
358
+ "Unknown middleware `" + middlewareKey + "` ..."
359
+ );
308
360
  }
309
361
  }
310
362
 
@@ -325,22 +377,44 @@ class RouterFacade {
325
377
  if (this.mode === "aws-lambda") {
326
378
  return {
327
379
  statusCode: 500,
328
- body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
380
+ body: JSON.stringify({
381
+ status: "error",
382
+ message: "Middleware run error `" + middlewareKey + "` ...",
383
+ }),
329
384
  headers: response.headers,
330
385
  };
331
386
  } else if (req && res) {
332
387
  this.requestConsoleLog(req, 500);
333
- return this.sendError(res, 500, "Error in middleware `" + middlewareKey + "` ...");
388
+ return this.sendError(
389
+ res,
390
+ 500,
391
+ "Error in middleware `" + middlewareKey + "` ..."
392
+ );
334
393
  }
335
394
  }
336
395
  });
337
396
  } catch (e: any) {
338
397
  // Send Middleware-Abort
339
398
  if (this.mode === "aws-lambda") {
340
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }), headers: response.headers };
399
+ return {
400
+ statusCode: 500,
401
+ body: JSON.stringify({ status: "error", message: e.data }),
402
+ headers: response.headers,
403
+ };
341
404
  } else if (res) {
342
- res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
343
- res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
405
+ res.writeHead(
406
+ e.status,
407
+ typeof e.data === "object"
408
+ ? { "Content-Type": "application/json" }
409
+ : { "Content-Type": "text/html" }
410
+ );
411
+ res.end(
412
+ typeof e.data === "object"
413
+ ? JSON.stringify(e.data)
414
+ : e.data
415
+ ? e.data
416
+ : " "
417
+ );
344
418
  }
345
419
  return;
346
420
  }
@@ -372,7 +446,10 @@ class RouterFacade {
372
446
  if (this.mode === "aws-lambda") {
373
447
  return {
374
448
  statusCode: 500,
375
- body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }),
449
+ body: JSON.stringify({
450
+ status: "error",
451
+ message: "Controller `" + split[0] + "` not found ...",
452
+ }),
376
453
  headers: response.headers,
377
454
  };
378
455
  } else if (res) {
@@ -393,7 +470,11 @@ class RouterFacade {
393
470
  await route.component(request, response);
394
471
 
395
472
  if (this.mode === "aws-lambda") {
396
- return { statusCode: response.statusCode, body: JSON.stringify(response.content), headers: response.headers };
473
+ return {
474
+ statusCode: response.statusCode,
475
+ body: JSON.stringify(response.content),
476
+ headers: response.headers,
477
+ };
397
478
  } else if (res && req) {
398
479
  res.end();
399
480
  this.requestConsoleLog(req, response.statusCode);
@@ -402,7 +483,10 @@ class RouterFacade {
402
483
  if (this.mode === "aws-lambda") {
403
484
  return {
404
485
  statusCode: 500,
405
- body: JSON.stringify({ status: "error", message: "Error running the component ..." }),
486
+ body: JSON.stringify({
487
+ status: "error",
488
+ message: "Error running the component ...",
489
+ }),
406
490
  headers: response.headers,
407
491
  };
408
492
  } else if (res && req) {
@@ -414,7 +498,10 @@ class RouterFacade {
414
498
  if (this.mode === "aws-lambda") {
415
499
  return {
416
500
  statusCode: 500,
417
- body: JSON.stringify({ status: "error", message: "Route component not found ..." }),
501
+ body: JSON.stringify({
502
+ status: "error",
503
+ message: "Route component not found ...",
504
+ }),
418
505
  headers: response.headers,
419
506
  };
420
507
  } else if (res && req) {
@@ -425,7 +512,10 @@ class RouterFacade {
425
512
  }
426
513
 
427
514
  requestConsoleLog(req: IncomingMessage, httpCode: number) {
428
- if (Config.get("application.router.requests.logConsole") !== true && !Config.get("application.router.requests.logFile")) {
515
+ if (
516
+ Config.get("application.router.requests.logConsole") !== true &&
517
+ !Config.get("application.router.requests.logFile")
518
+ ) {
429
519
  return;
430
520
  }
431
521
 
@@ -471,7 +561,12 @@ class RouterFacade {
471
561
  try {
472
562
  const filepath = Config.get("application.router.requests.logFile");
473
563
  if (filepath && filepath.trim() !== "") {
474
- fs.appendFileSync(filepath.trim().substring(0, 1) === "/" ? filepath : process.cwd() + "/" + filepath, logString + "\n");
564
+ fs.appendFileSync(
565
+ filepath.trim().substring(0, 1) === "/"
566
+ ? filepath
567
+ : process.cwd() + "/" + filepath,
568
+ logString + "\n"
569
+ );
475
570
  }
476
571
  } catch (e) {
477
572
  console.error(e);