serverless-simple-middleware 0.0.62 → 0.0.63

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.
@@ -1,172 +1,185 @@
1
- import * as awsTypes from 'aws-lambda'; // tslint:disable-line:no-implicit-dependencies
2
- import { getLogger } from '../utils/logger';
3
-
4
- const logger = getLogger(__filename);
5
-
6
- export interface RequestAuxBase {
7
- [pluginName: string]: any;
8
- }
9
-
10
- export class HandlerRequest {
11
- public event: awsTypes.APIGatewayEvent;
12
- public context: awsTypes.APIGatewayEventRequestContext;
13
- public lastError: Error | string | undefined;
14
-
15
- private lazyBody?: any;
16
-
17
- constructor(event: any, context: any) {
18
- this.event = event;
19
- this.context = context;
20
- this.lastError = undefined;
21
- }
22
-
23
- get body() {
24
- if (!this.event.body) {
25
- return {};
26
- }
27
- if (this.lazyBody === undefined) {
28
- this.lazyBody = JSON.parse(this.event.body);
29
- }
30
- return this.lazyBody || {};
31
- }
32
-
33
- get path(): { [key: string]: string | undefined } {
34
- return this.event.pathParameters || {};
35
- }
36
-
37
- get query(): { [key: string]: string | undefined } {
38
- return this.event.queryStringParameters || {};
39
- }
40
-
41
- public header(key: string) {
42
- return this.event.headers
43
- ? this.event.headers[key] || this.event.headers[key.toLowerCase()]
44
- : undefined;
45
- }
46
-
47
- public records<T, U>(selector?: (each: T) => U) {
48
- const target = ((this.event as any).Records || []) as T[];
49
- return selector === undefined ? target : target.map(selector);
50
- }
51
- }
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
-
59
- export class HandlerResponse {
60
- public callback: any;
61
- public completed: boolean;
62
- public result: any | Promise<any> | undefined;
63
-
64
- private cookies: string[];
65
- private crossOrigin?: string;
66
- private customHeaders: { [header: string]: any };
67
-
68
- constructor(callback: any) {
69
- this.callback = callback;
70
- this.completed = false;
71
- this.cookies = [];
72
- this.customHeaders = {};
73
- }
74
-
75
- public ok(body = {}, code = 200) {
76
- logger.stupid(`ok`, body);
77
- const headers = {
78
- ...CORS_HEADERS,
79
- ...this.customHeaders,
80
- };
81
- if (this.crossOrigin) {
82
- headers['Access-Control-Allow-Origin'] = this.crossOrigin;
83
- }
84
- let multiValueHeaders = undefined;
85
- if (this.cookies.length > 0) {
86
- multiValueHeaders = { 'Set-Cookie': this.cookies };
87
- }
88
- const result = this.callback(null, {
89
- statusCode: code,
90
- headers,
91
- multiValueHeaders,
92
- body: JSON.stringify(body),
93
- });
94
- this.completed = true;
95
- return result;
96
- }
97
-
98
- public fail(body = {}, code = 500) {
99
- logger.stupid(`fail`, body);
100
- const result = this.callback(null, {
101
- statusCode: code,
102
- headers: CORS_HEADERS,
103
- body: JSON.stringify(body),
104
- });
105
- this.completed = true;
106
- return result;
107
- }
108
-
109
- public addCookie(
110
- key: string,
111
- value: string,
112
- domain?: string,
113
- specifyCrossOrigin?: true,
114
- path?: string,
115
- ) {
116
- const keyValueStr = `${key}=${value}`;
117
- const domainStr = domain ? `Domain=${domain}` : '';
118
- const sameSiteStr = specifyCrossOrigin ? 'SameSite=None' : '';
119
- const secureStr = specifyCrossOrigin ? 'Secure' : '';
120
- const pathStr = path !== undefined ? `Path=${path}` : '';
121
- const cookieStr = [keyValueStr, domainStr, sameSiteStr, secureStr, pathStr]
122
- .filter((x) => x)
123
- .join('; ');
124
- this.cookies.push(cookieStr);
125
- }
126
-
127
- public setCrossOrigin = (origin?: string) => {
128
- this.crossOrigin = origin;
129
- };
130
-
131
- public addHeader = (header: string, value: string) => {
132
- this.customHeaders[header] = value;
133
- };
134
- }
135
-
136
- export interface HandlerAuxBase {
137
- [key: string]: any;
138
- }
139
-
140
- export interface HandlerContext<A extends HandlerAuxBase> {
141
- request: HandlerRequest;
142
- response: HandlerResponse;
143
- aux: A;
144
- }
145
-
146
- export type Handler<A extends HandlerAuxBase> = (
147
- context: HandlerContext<A>,
148
- ) => any | Promise<any> | undefined;
149
-
150
- export interface HandlerPlugin<A extends HandlerAuxBase> {
151
- create: () => Promise<A> | A;
152
- begin: Handler<A>;
153
- end: Handler<A>;
154
- error: Handler<A>;
155
- }
156
-
157
- export class HandlerPluginBase<A extends HandlerAuxBase>
158
- implements HandlerPlugin<A>
159
- {
160
- public create = (): Promise<A> | A => {
161
- throw new Error('Not yet implemented');
162
- };
163
- public begin = (_: HandlerContext<A>) => {
164
- // do nothing
165
- };
166
- public end = (_: HandlerContext<A>) => {
167
- // do nothing
168
- };
169
- public error = (_: HandlerContext<A>) => {
170
- // do nothing
171
- };
172
- }
1
+ import * as awsTypes from 'aws-lambda'; // tslint:disable-line:no-implicit-dependencies
2
+ import { getLogger } from '../utils/logger';
3
+
4
+ const logger = getLogger(__filename);
5
+
6
+ export interface RequestAuxBase {
7
+ [pluginName: string]: any;
8
+ }
9
+
10
+ export class HandlerRequest {
11
+ public event: awsTypes.APIGatewayEvent;
12
+ public context: awsTypes.APIGatewayEventRequestContext;
13
+ public lastError: Error | string | undefined;
14
+
15
+ private lazyBody?: any;
16
+
17
+ constructor(event: any, context: any) {
18
+ this.event = event;
19
+ this.context = context;
20
+ this.lastError = undefined;
21
+ }
22
+
23
+ get body() {
24
+ if (!this.event.body) {
25
+ return {};
26
+ }
27
+ if (this.lazyBody === undefined) {
28
+ this.lazyBody = JSON.parse(this.event.body);
29
+ }
30
+ return this.lazyBody || {};
31
+ }
32
+
33
+ get path(): { [key: string]: string | undefined } {
34
+ return this.event.pathParameters || {};
35
+ }
36
+
37
+ get query(): { [key: string]: string | undefined } {
38
+ return this.event.queryStringParameters || {};
39
+ }
40
+
41
+ public header(key: string) {
42
+ return this.event.headers
43
+ ? this.event.headers[key] || this.event.headers[key.toLowerCase()]
44
+ : undefined;
45
+ }
46
+
47
+ public records<T, U>(selector?: (each: T) => U) {
48
+ const target = ((this.event as any).Records || []) as T[];
49
+ return selector === undefined ? target : target.map(selector);
50
+ }
51
+ }
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
+
59
+ export class HandlerResponse {
60
+ public callback: any;
61
+ public completed: boolean;
62
+ public result: any | Promise<any> | undefined;
63
+
64
+ private cookies: string[];
65
+ private crossOrigin?: string;
66
+ private customHeaders: { [header: string]: any };
67
+
68
+ constructor(callback: any) {
69
+ this.callback = callback;
70
+ this.completed = false;
71
+ this.cookies = [];
72
+ this.customHeaders = {};
73
+ }
74
+
75
+ public ok(body = {}, code = 200) {
76
+ logger.stupid(`ok`, body);
77
+ const headers = {
78
+ ...CORS_HEADERS,
79
+ ...this.customHeaders,
80
+ };
81
+ if (this.crossOrigin) {
82
+ headers['Access-Control-Allow-Origin'] = this.crossOrigin;
83
+ }
84
+ let multiValueHeaders = undefined;
85
+ if (this.cookies.length > 0) {
86
+ multiValueHeaders = { 'Set-Cookie': this.cookies };
87
+ }
88
+ const result = this.callback(null, {
89
+ statusCode: code,
90
+ headers,
91
+ multiValueHeaders,
92
+ body: JSON.stringify(body),
93
+ });
94
+ this.completed = true;
95
+ return result;
96
+ }
97
+
98
+ public fail(body = {}, code = 500) {
99
+ logger.stupid(`fail`, body);
100
+ const result = this.callback(null, {
101
+ statusCode: code,
102
+ headers: CORS_HEADERS,
103
+ body: JSON.stringify(body),
104
+ });
105
+ this.completed = true;
106
+ return result;
107
+ }
108
+
109
+ public addCookie(
110
+ key: string,
111
+ value: string,
112
+ domain?: string,
113
+ sameSite?: 'None' | 'Lax' | 'Strict',
114
+ secure?: boolean,
115
+ path?: string,
116
+ httpOnly?: boolean,
117
+ maxAgeSeconds?: number,
118
+ ) {
119
+ const keyValueStr = `${key}=${value}`;
120
+ const domainStr = domain ? `Domain=${domain}` : '';
121
+ const sameSiteStr = sameSite ? `SameSite=${sameSite}` : '';
122
+ const secureStr = secure ? 'Secure' : '';
123
+ const pathStr = path !== undefined ? `Path=${path}` : '';
124
+ const httpOnlyStr = httpOnly ? 'HttpOnly' : '';
125
+ const maxAgeStr =
126
+ maxAgeSeconds || maxAgeSeconds === 0 ? `Max-Age=${maxAgeSeconds}` : '';
127
+ const cookieStr = [
128
+ keyValueStr,
129
+ domainStr,
130
+ sameSiteStr,
131
+ secureStr,
132
+ pathStr,
133
+ httpOnlyStr,
134
+ maxAgeStr,
135
+ ]
136
+ .filter(x => x)
137
+ .join('; ');
138
+ this.cookies.push(cookieStr);
139
+ }
140
+
141
+ public setCrossOrigin = (origin?: string) => {
142
+ this.crossOrigin = origin;
143
+ };
144
+
145
+ public addHeader = (header: string, value: string) => {
146
+ this.customHeaders[header] = value;
147
+ };
148
+ }
149
+
150
+ export interface HandlerAuxBase {
151
+ [key: string]: any;
152
+ }
153
+
154
+ export interface HandlerContext<A extends HandlerAuxBase> {
155
+ request: HandlerRequest;
156
+ response: HandlerResponse;
157
+ aux: A;
158
+ }
159
+
160
+ export type Handler<A extends HandlerAuxBase> = (
161
+ context: HandlerContext<A>,
162
+ ) => any | Promise<any> | undefined;
163
+
164
+ export interface HandlerPlugin<A extends HandlerAuxBase> {
165
+ create: () => Promise<A> | A;
166
+ begin: Handler<A>;
167
+ end: Handler<A>;
168
+ error: Handler<A>;
169
+ }
170
+
171
+ export class HandlerPluginBase<A extends HandlerAuxBase>
172
+ implements HandlerPlugin<A> {
173
+ public create = (): Promise<A> | A => {
174
+ throw new Error('Not yet implemented');
175
+ };
176
+ public begin = (_: HandlerContext<A>) => {
177
+ // do nothing
178
+ };
179
+ public end = (_: HandlerContext<A>) => {
180
+ // do nothing
181
+ };
182
+ public error = (_: HandlerContext<A>) => {
183
+ // do nothing
184
+ };
185
+ }