serverless-simple-middleware 0.0.53 → 0.0.56

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.
Files changed (51) hide show
  1. package/.prettierignore +2 -2
  2. package/README.md +4 -4
  3. package/dist/aws/config.d.ts +15 -15
  4. package/dist/aws/config.js +29 -29
  5. package/dist/aws/define.d.ts +21 -21
  6. package/dist/aws/define.js +8 -8
  7. package/dist/aws/index.d.ts +3 -3
  8. package/dist/aws/index.js +8 -8
  9. package/dist/aws/simple.d.ts +44 -44
  10. package/dist/aws/simple.js +659 -656
  11. package/dist/index.d.ts +3 -3
  12. package/dist/index.js +8 -8
  13. package/dist/middleware/aws.d.ts +25 -25
  14. package/dist/middleware/aws.js +128 -128
  15. package/dist/middleware/base.d.ts +54 -54
  16. package/dist/middleware/base.js +140 -140
  17. package/dist/middleware/build.d.ts +3 -3
  18. package/dist/middleware/build.js +234 -234
  19. package/dist/middleware/index.d.ts +12 -12
  20. package/dist/middleware/index.js +22 -22
  21. package/dist/middleware/logger.d.ts +18 -18
  22. package/dist/middleware/logger.js +71 -71
  23. package/dist/middleware/mysql.d.ts +44 -44
  24. package/dist/middleware/mysql.js +289 -289
  25. package/dist/middleware/trace.d.ts +86 -86
  26. package/dist/middleware/trace.js +255 -255
  27. package/dist/utils/index.d.ts +2 -2
  28. package/dist/utils/index.js +7 -7
  29. package/dist/utils/logger.d.ts +26 -26
  30. package/dist/utils/logger.js +73 -73
  31. package/dist/utils/misc.d.ts +1 -1
  32. package/dist/utils/misc.js +9 -9
  33. package/jest.config.js +7 -7
  34. package/package.json +61 -61
  35. package/src/aws/config.ts +46 -46
  36. package/src/aws/define.ts +29 -29
  37. package/src/aws/index.ts +3 -3
  38. package/src/aws/simple.ts +529 -531
  39. package/src/index.ts +3 -3
  40. package/src/middleware/aws.ts +78 -78
  41. package/src/middleware/base.ts +164 -164
  42. package/src/middleware/build.ts +173 -173
  43. package/src/middleware/index.ts +20 -20
  44. package/src/middleware/logger.ts +28 -28
  45. package/src/middleware/mysql.ts +210 -210
  46. package/src/middleware/trace.ts +269 -269
  47. package/src/utils/index.ts +2 -2
  48. package/src/utils/logger.ts +94 -94
  49. package/src/utils/misc.ts +11 -11
  50. package/tsconfig.json +15 -15
  51. package/tslint.json +12 -12
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './aws';
2
- export * from './middleware';
3
- export * from './utils';
1
+ export * from './aws';
2
+ export * from './middleware';
3
+ export * from './utils';
@@ -1,78 +1,78 @@
1
- import {
2
- loadAWSConfig,
3
- SimpleAWS,
4
- SimpleAWSConfig,
5
- SimpleAWSConfigLoadParam,
6
- } from '../aws';
7
- import { getLogger } from '../utils';
8
- import { HandlerAuxBase, HandlerPluginBase } from './base';
9
-
10
- const logger = getLogger(__filename);
11
-
12
- type InitializerMapper = (
13
- aws: SimpleAWS,
14
- env: {},
15
- ) => { [name: string]: () => Promise<boolean> };
16
-
17
- const initialize = async (aws: SimpleAWS, mapper: InitializerMapper) => {
18
- const env = process.env;
19
- const mapping = mapper(aws, env);
20
- const successes = await Promise.all(
21
- Object.keys(mapping).map(name => mapping[name]()),
22
- );
23
- return Object.keys(mapping).reduce(
24
- (result, name, index) => ({ ...result, [name]: successes[index] }),
25
- {},
26
- );
27
- };
28
-
29
- export interface AWSPluginOptions {
30
- config?: SimpleAWSConfigLoadParam;
31
- mapper?: InitializerMapper;
32
- }
33
-
34
- export interface AWSPluginAux extends HandlerAuxBase {
35
- aws: SimpleAWS;
36
- awsConfig: SimpleAWSConfig;
37
- }
38
-
39
- export class AWSPlugin extends HandlerPluginBase<AWSPluginAux> {
40
- private options?: AWSPluginOptions;
41
- private aws?: SimpleAWS;
42
- private config: SimpleAWSConfig;
43
-
44
- constructor(options?: AWSPluginOptions) {
45
- super();
46
- this.options = options;
47
- this.config = new SimpleAWSConfig();
48
- }
49
-
50
- public create = async () => {
51
- // Setup only once.
52
- if (!this.aws) {
53
- const { config, mapper } = this.options || {
54
- config: undefined,
55
- mapper: undefined,
56
- };
57
-
58
- if (config) {
59
- logger.debug(`Load aws config from ${config}`);
60
- this.config = await loadAWSConfig(config);
61
- }
62
-
63
- this.aws = new SimpleAWS(this.config);
64
-
65
- if (mapper) {
66
- logger.debug(`Initialize aws components with mapper.`);
67
- await initialize(this.aws, mapper);
68
- }
69
- }
70
- return {
71
- aws: this.aws,
72
- awsConfig: this.config,
73
- };
74
- };
75
- }
76
-
77
- const build = (options?: AWSPluginOptions) => new AWSPlugin(options);
78
- export default build;
1
+ import {
2
+ loadAWSConfig,
3
+ SimpleAWS,
4
+ SimpleAWSConfig,
5
+ SimpleAWSConfigLoadParam,
6
+ } from '../aws';
7
+ import { getLogger } from '../utils';
8
+ import { HandlerAuxBase, HandlerPluginBase } from './base';
9
+
10
+ const logger = getLogger(__filename);
11
+
12
+ type InitializerMapper = (
13
+ aws: SimpleAWS,
14
+ env: {},
15
+ ) => { [name: string]: () => Promise<boolean> };
16
+
17
+ const initialize = async (aws: SimpleAWS, mapper: InitializerMapper) => {
18
+ const env = process.env;
19
+ const mapping = mapper(aws, env);
20
+ const successes = await Promise.all(
21
+ Object.keys(mapping).map(name => mapping[name]()),
22
+ );
23
+ return Object.keys(mapping).reduce(
24
+ (result, name, index) => ({ ...result, [name]: successes[index] }),
25
+ {},
26
+ );
27
+ };
28
+
29
+ export interface AWSPluginOptions {
30
+ config?: SimpleAWSConfigLoadParam;
31
+ mapper?: InitializerMapper;
32
+ }
33
+
34
+ export interface AWSPluginAux extends HandlerAuxBase {
35
+ aws: SimpleAWS;
36
+ awsConfig: SimpleAWSConfig;
37
+ }
38
+
39
+ export class AWSPlugin extends HandlerPluginBase<AWSPluginAux> {
40
+ private options?: AWSPluginOptions;
41
+ private aws?: SimpleAWS;
42
+ private config: SimpleAWSConfig;
43
+
44
+ constructor(options?: AWSPluginOptions) {
45
+ super();
46
+ this.options = options;
47
+ this.config = new SimpleAWSConfig();
48
+ }
49
+
50
+ public create = async () => {
51
+ // Setup only once.
52
+ if (!this.aws) {
53
+ const { config, mapper } = this.options || {
54
+ config: undefined,
55
+ mapper: undefined,
56
+ };
57
+
58
+ if (config) {
59
+ logger.debug(`Load aws config from ${config}`);
60
+ this.config = await loadAWSConfig(config);
61
+ }
62
+
63
+ this.aws = new SimpleAWS(this.config);
64
+
65
+ if (mapper) {
66
+ logger.debug(`Initialize aws components with mapper.`);
67
+ await initialize(this.aws, mapper);
68
+ }
69
+ }
70
+ return {
71
+ aws: this.aws,
72
+ awsConfig: this.config,
73
+ };
74
+ };
75
+ }
76
+
77
+ const build = (options?: AWSPluginOptions) => new AWSPlugin(options);
78
+ export default build;
@@ -1,164 +1,164 @@
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 } {
34
- return this.event.pathParameters || {};
35
- }
36
-
37
- get query(): { [key: string]: string } {
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
- export class HandlerResponse {
54
- public callback: any;
55
- public completed: boolean;
56
- public result: any | Promise<any> | undefined;
57
-
58
- private corsHeaders: { [header: string]: any };
59
- private cookies: string[];
60
- private crossOrigin?: string;
61
-
62
- constructor(callback: any) {
63
- this.callback = callback;
64
- 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
- this.cookies = [];
71
- }
72
-
73
- public ok(body = {}, code = 200) {
74
- logger.stupid(`ok`, body);
75
- const headers = {
76
- ...this.corsHeaders,
77
- };
78
- if (this.crossOrigin) {
79
- headers['Access-Control-Allow-Origin'] = this.crossOrigin;
80
- }
81
- let multiValueHeaders = undefined;
82
- if (this.cookies.length > 0) {
83
- multiValueHeaders = { 'Set-Cookie': this.cookies };
84
- }
85
- const result = this.callback(null, {
86
- statusCode: code,
87
- headers,
88
- multiValueHeaders,
89
- body: JSON.stringify(body),
90
- });
91
- this.completed = true;
92
- return result;
93
- }
94
-
95
- public fail(body = {}, code = 500) {
96
- logger.stupid(`fail`, body);
97
- const result = this.callback(null, {
98
- statusCode: code,
99
- headers: this.corsHeaders,
100
- body: JSON.stringify(body),
101
- });
102
- this.completed = true;
103
- return result;
104
- }
105
-
106
- public addCookie(
107
- key: string,
108
- value: string,
109
- domain?: string,
110
- specifyCrossOrigin?: true,
111
- path?: string,
112
- ) {
113
- const keyValueStr = `${key}=${value}`;
114
- const domainStr = domain ? `Domain=${domain}` : '';
115
- const sameSiteStr = specifyCrossOrigin ? 'SameSite=None' : '';
116
- const secureStr = specifyCrossOrigin ? 'Secure' : '';
117
- const pathStr = path !== undefined ? `Path=${path}` : '';
118
- const cookieStr = [keyValueStr, domainStr, sameSiteStr, secureStr, pathStr]
119
- .filter(x => x)
120
- .join('; ');
121
- this.cookies.push(cookieStr);
122
- }
123
-
124
- public setCrossOrigin = (origin?: string) => {
125
- this.crossOrigin = origin;
126
- };
127
- }
128
-
129
- export interface HandlerAuxBase {
130
- [key: string]: any;
131
- }
132
-
133
- export interface HandlerContext<A extends HandlerAuxBase> {
134
- request: HandlerRequest;
135
- response: HandlerResponse;
136
- aux: A;
137
- }
138
-
139
- export type Handler<A extends HandlerAuxBase> = (
140
- context: HandlerContext<A>,
141
- ) => any | Promise<any> | undefined;
142
-
143
- export interface HandlerPlugin<A extends HandlerAuxBase> {
144
- create: () => Promise<A> | A;
145
- begin: Handler<A>;
146
- end: Handler<A>;
147
- error: Handler<A>;
148
- }
149
-
150
- export class HandlerPluginBase<A extends HandlerAuxBase>
151
- implements HandlerPlugin<A> {
152
- public create = (): Promise<A> | A => {
153
- throw new Error('Not yet implemented');
154
- };
155
- public begin = (_: HandlerContext<A>) => {
156
- // do nothing
157
- };
158
- public end = (_: HandlerContext<A>) => {
159
- // do nothing
160
- };
161
- public error = (_: HandlerContext<A>) => {
162
- // do nothing
163
- };
164
- }
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 } {
34
+ return this.event.pathParameters || {};
35
+ }
36
+
37
+ get query(): { [key: string]: string } {
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
+ export class HandlerResponse {
54
+ public callback: any;
55
+ public completed: boolean;
56
+ public result: any | Promise<any> | undefined;
57
+
58
+ private corsHeaders: { [header: string]: any };
59
+ private cookies: string[];
60
+ private crossOrigin?: string;
61
+
62
+ constructor(callback: any) {
63
+ this.callback = callback;
64
+ 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
+ this.cookies = [];
71
+ }
72
+
73
+ public ok(body = {}, code = 200) {
74
+ logger.stupid(`ok`, body);
75
+ const headers = {
76
+ ...this.corsHeaders,
77
+ };
78
+ if (this.crossOrigin) {
79
+ headers['Access-Control-Allow-Origin'] = this.crossOrigin;
80
+ }
81
+ let multiValueHeaders = undefined;
82
+ if (this.cookies.length > 0) {
83
+ multiValueHeaders = { 'Set-Cookie': this.cookies };
84
+ }
85
+ const result = this.callback(null, {
86
+ statusCode: code,
87
+ headers,
88
+ multiValueHeaders,
89
+ body: JSON.stringify(body),
90
+ });
91
+ this.completed = true;
92
+ return result;
93
+ }
94
+
95
+ public fail(body = {}, code = 500) {
96
+ logger.stupid(`fail`, body);
97
+ const result = this.callback(null, {
98
+ statusCode: code,
99
+ headers: this.corsHeaders,
100
+ body: JSON.stringify(body),
101
+ });
102
+ this.completed = true;
103
+ return result;
104
+ }
105
+
106
+ public addCookie(
107
+ key: string,
108
+ value: string,
109
+ domain?: string,
110
+ specifyCrossOrigin?: true,
111
+ path?: string,
112
+ ) {
113
+ const keyValueStr = `${key}=${value}`;
114
+ const domainStr = domain ? `Domain=${domain}` : '';
115
+ const sameSiteStr = specifyCrossOrigin ? 'SameSite=None' : '';
116
+ const secureStr = specifyCrossOrigin ? 'Secure' : '';
117
+ const pathStr = path !== undefined ? `Path=${path}` : '';
118
+ const cookieStr = [keyValueStr, domainStr, sameSiteStr, secureStr, pathStr]
119
+ .filter(x => x)
120
+ .join('; ');
121
+ this.cookies.push(cookieStr);
122
+ }
123
+
124
+ public setCrossOrigin = (origin?: string) => {
125
+ this.crossOrigin = origin;
126
+ };
127
+ }
128
+
129
+ export interface HandlerAuxBase {
130
+ [key: string]: any;
131
+ }
132
+
133
+ export interface HandlerContext<A extends HandlerAuxBase> {
134
+ request: HandlerRequest;
135
+ response: HandlerResponse;
136
+ aux: A;
137
+ }
138
+
139
+ export type Handler<A extends HandlerAuxBase> = (
140
+ context: HandlerContext<A>,
141
+ ) => any | Promise<any> | undefined;
142
+
143
+ export interface HandlerPlugin<A extends HandlerAuxBase> {
144
+ create: () => Promise<A> | A;
145
+ begin: Handler<A>;
146
+ end: Handler<A>;
147
+ error: Handler<A>;
148
+ }
149
+
150
+ export class HandlerPluginBase<A extends HandlerAuxBase>
151
+ implements HandlerPlugin<A> {
152
+ public create = (): Promise<A> | A => {
153
+ throw new Error('Not yet implemented');
154
+ };
155
+ public begin = (_: HandlerContext<A>) => {
156
+ // do nothing
157
+ };
158
+ public end = (_: HandlerContext<A>) => {
159
+ // do nothing
160
+ };
161
+ public error = (_: HandlerContext<A>) => {
162
+ // do nothing
163
+ };
164
+ }