@webtypen/webframez-core 0.0.35 → 0.0.37

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.
@@ -5,5 +5,8 @@ export declare class LambdaApplication {
5
5
  boot(event: any, context: any, options?: any): Promise<void | {
6
6
  statusCode: number;
7
7
  body: string;
8
+ headers: {
9
+ [key: string]: string;
10
+ };
8
11
  }>;
9
12
  }
@@ -4,6 +4,9 @@ export declare class Response {
4
4
  res?: ServerResponse;
5
5
  statusCode: number;
6
6
  content?: any | null | undefined;
7
+ headers: {
8
+ [key: string]: string;
9
+ };
7
10
  /**
8
11
  * Mode
9
12
  */
@@ -4,6 +4,7 @@ exports.Response = void 0;
4
4
  class Response {
5
5
  constructor(options) {
6
6
  this.statusCode = 200;
7
+ this.headers = {};
7
8
  /**
8
9
  * Mode
9
10
  */
@@ -41,6 +42,7 @@ class Response {
41
42
  * @returns
42
43
  */
43
44
  header(type, value) {
45
+ this.headers[type] = value;
44
46
  if (!this.res) {
45
47
  return this;
46
48
  }
@@ -109,6 +109,9 @@ declare class RouterFacade {
109
109
  handleRequest(req: null | IncomingMessage, res: null | ServerResponse, options?: any): Promise<void | {
110
110
  statusCode: number;
111
111
  body: string;
112
+ headers: {
113
+ [key: string]: string;
114
+ };
112
115
  }>;
113
116
  requestConsoleLog(req: IncomingMessage, httpCode: number): void;
114
117
  }
@@ -256,6 +256,7 @@ class RouterFacade {
256
256
  return {
257
257
  statusCode: 500,
258
258
  body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
259
+ headers: response.headers,
259
260
  };
260
261
  }
261
262
  else if (req && res) {
@@ -276,6 +277,7 @@ class RouterFacade {
276
277
  return {
277
278
  statusCode: 500,
278
279
  body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
280
+ headers: response.headers,
279
281
  };
280
282
  }
281
283
  else if (req && res) {
@@ -288,7 +290,7 @@ class RouterFacade {
288
290
  catch (e) {
289
291
  // Send Middleware-Abort
290
292
  if (this.mode === "aws-lambda") {
291
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }) };
293
+ return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }), headers: response.headers };
292
294
  }
293
295
  else if (res) {
294
296
  res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
@@ -299,11 +301,20 @@ class RouterFacade {
299
301
  }
300
302
  }
301
303
  // Handle OPTIONS-Request
302
- if (req && res && req.method === "OPTIONS") {
303
- res.statusCode = 200;
304
- res.setHeader("Content-Length", "0");
305
- res.end();
306
- this.requestConsoleLog(req, 200);
304
+ if (request && request.method === "OPTIONS") {
305
+ if (this.mode === "aws-lambda") {
306
+ return {
307
+ statusCode: 200,
308
+ body: "",
309
+ headers: response.headers,
310
+ };
311
+ }
312
+ else if (res && req) {
313
+ res.statusCode = 200;
314
+ res.setHeader("Content-Length", "0");
315
+ res.end();
316
+ this.requestConsoleLog(req, 200);
317
+ }
307
318
  return;
308
319
  }
309
320
  // Resolve route-component (handle controller definition)
@@ -312,7 +323,11 @@ class RouterFacade {
312
323
  const controller = this.kernel.controller[split[0]];
313
324
  if (!controller) {
314
325
  if (this.mode === "aws-lambda") {
315
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }) };
326
+ return {
327
+ statusCode: 500,
328
+ body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }),
329
+ headers: response.headers,
330
+ };
316
331
  }
317
332
  else if (res) {
318
333
  res.writeHead(500, { "Content-Type": "text/html" });
@@ -328,7 +343,7 @@ class RouterFacade {
328
343
  try {
329
344
  yield route.component(request, response);
330
345
  if (this.mode === "aws-lambda") {
331
- return { statusCode: response.statusCode, body: JSON.stringify(response.content) };
346
+ return { statusCode: response.statusCode, body: JSON.stringify(response.content), headers: response.headers };
332
347
  }
333
348
  else if (res && req) {
334
349
  res.end();
@@ -337,7 +352,11 @@ class RouterFacade {
337
352
  }
338
353
  catch (e) {
339
354
  if (this.mode === "aws-lambda") {
340
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Error running the component ..." }) };
355
+ return {
356
+ statusCode: 500,
357
+ body: JSON.stringify({ status: "error", message: "Error running the component ..." }),
358
+ headers: response.headers,
359
+ };
341
360
  }
342
361
  else if (res && req) {
343
362
  this.sendError(res, 500);
@@ -347,7 +366,11 @@ class RouterFacade {
347
366
  }
348
367
  else {
349
368
  if (this.mode === "aws-lambda") {
350
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Route component not found ..." }) };
369
+ return {
370
+ statusCode: 500,
371
+ body: JSON.stringify({ status: "error", message: "Route component not found ..." }),
372
+ headers: response.headers,
373
+ };
351
374
  }
352
375
  else if (res && req) {
353
376
  this.sendError(res, 404);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
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",
@@ -4,6 +4,7 @@ export class Response {
4
4
  res?: ServerResponse;
5
5
  statusCode: number = 200;
6
6
  content?: any | null | undefined;
7
+ headers: { [key: string]: string } = {};
7
8
 
8
9
  /**
9
10
  * Mode
@@ -49,6 +50,8 @@ export class Response {
49
50
  * @returns
50
51
  */
51
52
  header(type: string, value: string) {
53
+ this.headers[type] = value;
54
+
52
55
  if (!this.res) {
53
56
  return this;
54
57
  }
@@ -294,6 +294,7 @@ class RouterFacade {
294
294
  return {
295
295
  statusCode: 500,
296
296
  body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
297
+ headers: response.headers,
297
298
  };
298
299
  } else if (req && res) {
299
300
  this.requestConsoleLog(req, 500);
@@ -319,6 +320,7 @@ class RouterFacade {
319
320
  return {
320
321
  statusCode: 500,
321
322
  body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
323
+ headers: response.headers,
322
324
  };
323
325
  } else if (req && res) {
324
326
  this.requestConsoleLog(req, 500);
@@ -329,7 +331,7 @@ class RouterFacade {
329
331
  } catch (e: any) {
330
332
  // Send Middleware-Abort
331
333
  if (this.mode === "aws-lambda") {
332
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }) };
334
+ return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }), headers: response.headers };
333
335
  } else if (res) {
334
336
  res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
335
337
  res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
@@ -340,11 +342,19 @@ class RouterFacade {
340
342
  }
341
343
 
342
344
  // Handle OPTIONS-Request
343
- if (req && res && req.method === "OPTIONS") {
344
- res.statusCode = 200;
345
- res.setHeader("Content-Length", "0");
346
- res.end();
347
- this.requestConsoleLog(req, 200);
345
+ if (request && request.method === "OPTIONS") {
346
+ if (this.mode === "aws-lambda") {
347
+ return {
348
+ statusCode: 200,
349
+ body: "",
350
+ headers: response.headers,
351
+ };
352
+ } else if (res && req) {
353
+ res.statusCode = 200;
354
+ res.setHeader("Content-Length", "0");
355
+ res.end();
356
+ this.requestConsoleLog(req, 200);
357
+ }
348
358
  return;
349
359
  }
350
360
 
@@ -354,7 +364,11 @@ class RouterFacade {
354
364
  const controller = this.kernel.controller[split[0]];
355
365
  if (!controller) {
356
366
  if (this.mode === "aws-lambda") {
357
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }) };
367
+ return {
368
+ statusCode: 500,
369
+ body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }),
370
+ headers: response.headers,
371
+ };
358
372
  } else if (res) {
359
373
  res.writeHead(500, { "Content-Type": "text/html" });
360
374
  res.end("Controller `" + split[0] + "` not found ...");
@@ -373,14 +387,18 @@ class RouterFacade {
373
387
  await route.component(request, response);
374
388
 
375
389
  if (this.mode === "aws-lambda") {
376
- return { statusCode: response.statusCode, body: JSON.stringify(response.content) };
390
+ return { statusCode: response.statusCode, body: JSON.stringify(response.content), headers: response.headers };
377
391
  } else if (res && req) {
378
392
  res.end();
379
393
  this.requestConsoleLog(req, response.statusCode);
380
394
  }
381
395
  } catch (e) {
382
396
  if (this.mode === "aws-lambda") {
383
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Error running the component ..." }) };
397
+ return {
398
+ statusCode: 500,
399
+ body: JSON.stringify({ status: "error", message: "Error running the component ..." }),
400
+ headers: response.headers,
401
+ };
384
402
  } else if (res && req) {
385
403
  this.sendError(res, 500);
386
404
  this.requestConsoleLog(req, 500);
@@ -388,7 +406,11 @@ class RouterFacade {
388
406
  }
389
407
  } else {
390
408
  if (this.mode === "aws-lambda") {
391
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Route component not found ..." }) };
409
+ return {
410
+ statusCode: 500,
411
+ body: JSON.stringify({ status: "error", message: "Route component not found ..." }),
412
+ headers: response.headers,
413
+ };
392
414
  } else if (res && req) {
393
415
  this.sendError(res, 404);
394
416
  this.requestConsoleLog(req, 404);