@digitraffic/common 2022.11.28-1 → 2022.12.1-1
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.
@@ -0,0 +1,20 @@
|
|
1
|
+
export type LoggingHandler<RESPONSE> = (methodName: string, method: () => Promise<RESPONSE>) => Promise<RESPONSE>;
|
2
|
+
export type ErrorHandler<RESPONSE> = (error: unknown) => RESPONSE;
|
3
|
+
/**
|
4
|
+
* Factory class for creating lambda-handler functions. You can set functionality to handle logging and error-handling,
|
5
|
+
* with the defaults:
|
6
|
+
* * No error handling
|
7
|
+
* * Execution time logging
|
8
|
+
*
|
9
|
+
* You should instantiate HandlerFactory in your project with desired error handling and use the factory instance for
|
10
|
+
* creating handler-functions for your lambdas.
|
11
|
+
*/
|
12
|
+
export declare class HandlerFactory<RESPONSE> {
|
13
|
+
private loggingHandler;
|
14
|
+
private errorHandler;
|
15
|
+
constructor();
|
16
|
+
withLoggingHandler(loggingHandler: LoggingHandler<RESPONSE>): this;
|
17
|
+
withErrorHandler(errorHandler: ErrorHandler<RESPONSE>): this;
|
18
|
+
createEventHandler(methodName: string, handler: (event: unknown) => Promise<RESPONSE>): (event: unknown) => Promise<RESPONSE>;
|
19
|
+
}
|
20
|
+
export declare function createNoLoggingHandler<RESPONSE>(): LoggingHandler<RESPONSE>;
|
@@ -0,0 +1,59 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.createNoLoggingHandler = exports.HandlerFactory = void 0;
|
4
|
+
/**
|
5
|
+
* Factory class for creating lambda-handler functions. You can set functionality to handle logging and error-handling,
|
6
|
+
* with the defaults:
|
7
|
+
* * No error handling
|
8
|
+
* * Execution time logging
|
9
|
+
*
|
10
|
+
* You should instantiate HandlerFactory in your project with desired error handling and use the factory instance for
|
11
|
+
* creating handler-functions for your lambdas.
|
12
|
+
*/
|
13
|
+
class HandlerFactory {
|
14
|
+
constructor() {
|
15
|
+
this.loggingHandler = createDefaultLoggingHandler();
|
16
|
+
this.errorHandler = (error) => {
|
17
|
+
throw error;
|
18
|
+
};
|
19
|
+
}
|
20
|
+
withLoggingHandler(loggingHandler) {
|
21
|
+
this.loggingHandler = loggingHandler;
|
22
|
+
return this;
|
23
|
+
}
|
24
|
+
withErrorHandler(errorHandler) {
|
25
|
+
this.errorHandler = errorHandler;
|
26
|
+
return this;
|
27
|
+
}
|
28
|
+
createEventHandler(methodName, handler) {
|
29
|
+
return async (event) => {
|
30
|
+
return await this.loggingHandler(methodName, async () => {
|
31
|
+
try {
|
32
|
+
return await handler(event);
|
33
|
+
}
|
34
|
+
catch (error) {
|
35
|
+
return this.errorHandler(error);
|
36
|
+
}
|
37
|
+
});
|
38
|
+
};
|
39
|
+
}
|
40
|
+
}
|
41
|
+
exports.HandlerFactory = HandlerFactory;
|
42
|
+
function createNoLoggingHandler() {
|
43
|
+
return (methodName, method) => {
|
44
|
+
return method();
|
45
|
+
};
|
46
|
+
}
|
47
|
+
exports.createNoLoggingHandler = createNoLoggingHandler;
|
48
|
+
function createDefaultLoggingHandler() {
|
49
|
+
return (methodName, method) => {
|
50
|
+
const start = Date.now();
|
51
|
+
try {
|
52
|
+
return method();
|
53
|
+
}
|
54
|
+
finally {
|
55
|
+
console.info("method=%s tookMs=%d", methodName, Date.now() - start);
|
56
|
+
}
|
57
|
+
};
|
58
|
+
}
|
59
|
+
//# sourceMappingURL=handler-factory.js.map
|
package/package.json
CHANGED
@@ -0,0 +1,70 @@
|
|
1
|
+
export type LoggingHandler<RESPONSE> = (
|
2
|
+
methodName: string,
|
3
|
+
method: () => Promise<RESPONSE>
|
4
|
+
) => Promise<RESPONSE>;
|
5
|
+
export type ErrorHandler<RESPONSE> = (error: unknown) => RESPONSE;
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Factory class for creating lambda-handler functions. You can set functionality to handle logging and error-handling,
|
9
|
+
* with the defaults:
|
10
|
+
* * No error handling
|
11
|
+
* * Execution time logging
|
12
|
+
*
|
13
|
+
* You should instantiate HandlerFactory in your project with desired error handling and use the factory instance for
|
14
|
+
* creating handler-functions for your lambdas.
|
15
|
+
*/
|
16
|
+
export class HandlerFactory<RESPONSE> {
|
17
|
+
private loggingHandler: LoggingHandler<RESPONSE>;
|
18
|
+
private errorHandler: ErrorHandler<RESPONSE>;
|
19
|
+
|
20
|
+
constructor() {
|
21
|
+
this.loggingHandler = createDefaultLoggingHandler();
|
22
|
+
|
23
|
+
this.errorHandler = (error: unknown) => {
|
24
|
+
throw error;
|
25
|
+
};
|
26
|
+
}
|
27
|
+
|
28
|
+
withLoggingHandler(loggingHandler: LoggingHandler<RESPONSE>) {
|
29
|
+
this.loggingHandler = loggingHandler;
|
30
|
+
return this;
|
31
|
+
}
|
32
|
+
|
33
|
+
withErrorHandler(errorHandler: ErrorHandler<RESPONSE>) {
|
34
|
+
this.errorHandler = errorHandler;
|
35
|
+
return this;
|
36
|
+
}
|
37
|
+
|
38
|
+
createEventHandler(
|
39
|
+
methodName: string,
|
40
|
+
handler: (event: unknown) => Promise<RESPONSE>
|
41
|
+
) {
|
42
|
+
return async (event: unknown) => {
|
43
|
+
return await this.loggingHandler(methodName, async () => {
|
44
|
+
try {
|
45
|
+
return await handler(event);
|
46
|
+
} catch (error) {
|
47
|
+
return this.errorHandler(error);
|
48
|
+
}
|
49
|
+
});
|
50
|
+
};
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
export function createNoLoggingHandler<RESPONSE>(): LoggingHandler<RESPONSE> {
|
55
|
+
return (methodName: string, method: () => Promise<RESPONSE>) => {
|
56
|
+
return method();
|
57
|
+
};
|
58
|
+
}
|
59
|
+
|
60
|
+
function createDefaultLoggingHandler<RESPONSE>(): LoggingHandler<RESPONSE> {
|
61
|
+
return (methodName: string, method: () => Promise<RESPONSE>) => {
|
62
|
+
const start = Date.now();
|
63
|
+
|
64
|
+
try {
|
65
|
+
return method();
|
66
|
+
} finally {
|
67
|
+
console.info("method=%s tookMs=%d", methodName, Date.now() - start);
|
68
|
+
}
|
69
|
+
};
|
70
|
+
}
|