serverless-simple-middleware 0.0.58 → 0.0.60

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.
@@ -22,14 +22,15 @@ export declare class HandlerResponse {
22
22
  callback: any;
23
23
  completed: boolean;
24
24
  result: any | Promise<any> | undefined;
25
- private corsHeaders;
26
25
  private cookies;
27
26
  private crossOrigin?;
27
+ private customHeaders;
28
28
  constructor(callback: any);
29
29
  ok(body?: {}, code?: number): any;
30
30
  fail(body?: {}, code?: number): any;
31
31
  addCookie(key: string, value: string, domain?: string, specifyCrossOrigin?: true, path?: string): void;
32
32
  setCrossOrigin: (origin?: string | undefined) => void;
33
+ addHeader: (header: string, value: string) => void;
33
34
  }
34
35
  export interface HandlerAuxBase {
35
36
  [key: string]: any;
@@ -58,26 +58,30 @@ var HandlerRequest = /** @class */ (function () {
58
58
  return HandlerRequest;
59
59
  }());
60
60
  exports.HandlerRequest = HandlerRequest;
61
+ var CORS_HEADERS = {
62
+ 'Access-Control-Allow-Origin': '*',
63
+ 'Access-Control-Allow-Headers': 'X-Version',
64
+ 'Access-Control-Allow-Credentials': true,
65
+ };
61
66
  var HandlerResponse = /** @class */ (function () {
62
67
  function HandlerResponse(callback) {
63
68
  var _this = this;
64
69
  this.setCrossOrigin = function (origin) {
65
70
  _this.crossOrigin = origin;
66
71
  };
72
+ this.addHeader = function (header, value) {
73
+ _this.customHeaders[header] = value;
74
+ };
67
75
  this.callback = callback;
68
76
  this.completed = false;
69
- this.corsHeaders = {
70
- 'Access-Control-Allow-Origin': '*',
71
- 'Access-Control-Allow-Headers': 'X-Version',
72
- 'Access-Control-Allow-Credentials': true,
73
- };
74
77
  this.cookies = [];
78
+ this.customHeaders = {};
75
79
  }
76
80
  HandlerResponse.prototype.ok = function (body, code) {
77
81
  if (body === void 0) { body = {}; }
78
82
  if (code === void 0) { code = 200; }
79
83
  logger.stupid("ok", body);
80
- var headers = __assign({}, this.corsHeaders);
84
+ var headers = __assign({}, CORS_HEADERS, this.customHeaders);
81
85
  if (this.crossOrigin) {
82
86
  headers['Access-Control-Allow-Origin'] = this.crossOrigin;
83
87
  }
@@ -100,7 +104,7 @@ var HandlerResponse = /** @class */ (function () {
100
104
  logger.stupid("fail", body);
101
105
  var result = this.callback(null, {
102
106
  statusCode: code,
103
- headers: this.corsHeaders,
107
+ headers: CORS_HEADERS,
104
108
  body: JSON.stringify(body),
105
109
  });
106
110
  this.completed = true;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "serverless-simple-middleware",
3
3
  "description": "Simple middleware to translate the interface of lambda's handler to request => response",
4
- "version": "0.0.58",
4
+ "version": "0.0.60",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "VoyagerX",
@@ -50,30 +50,33 @@ export class HandlerRequest {
50
50
  }
51
51
  }
52
52
 
53
+ const CORS_HEADERS = {
54
+ 'Access-Control-Allow-Origin': '*',
55
+ 'Access-Control-Allow-Headers': 'X-Version',
56
+ 'Access-Control-Allow-Credentials': true,
57
+ };
58
+
53
59
  export class HandlerResponse {
54
60
  public callback: any;
55
61
  public completed: boolean;
56
62
  public result: any | Promise<any> | undefined;
57
63
 
58
- private corsHeaders: { [header: string]: any };
59
64
  private cookies: string[];
60
65
  private crossOrigin?: string;
66
+ private customHeaders: { [header: string]: any };
61
67
 
62
68
  constructor(callback: any) {
63
69
  this.callback = callback;
64
70
  this.completed = false;
65
- this.corsHeaders = {
66
- 'Access-Control-Allow-Origin': '*',
67
- 'Access-Control-Allow-Headers': 'X-Version',
68
- 'Access-Control-Allow-Credentials': true,
69
- };
70
71
  this.cookies = [];
72
+ this.customHeaders = {};
71
73
  }
72
74
 
73
75
  public ok(body = {}, code = 200) {
74
76
  logger.stupid(`ok`, body);
75
77
  const headers = {
76
- ...this.corsHeaders,
78
+ ...CORS_HEADERS,
79
+ ...this.customHeaders,
77
80
  };
78
81
  if (this.crossOrigin) {
79
82
  headers['Access-Control-Allow-Origin'] = this.crossOrigin;
@@ -96,7 +99,7 @@ export class HandlerResponse {
96
99
  logger.stupid(`fail`, body);
97
100
  const result = this.callback(null, {
98
101
  statusCode: code,
99
- headers: this.corsHeaders,
102
+ headers: CORS_HEADERS,
100
103
  body: JSON.stringify(body),
101
104
  });
102
105
  this.completed = true;
@@ -124,6 +127,10 @@ export class HandlerResponse {
124
127
  public setCrossOrigin = (origin?: string) => {
125
128
  this.crossOrigin = origin;
126
129
  };
130
+
131
+ public addHeader = (header: string, value: string) => {
132
+ this.customHeaders[header] = value;
133
+ };
127
134
  }
128
135
 
129
136
  export interface HandlerAuxBase {