serverless-simple-middleware 0.0.74 → 0.0.75

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