@digitraffic/common 2022.11.28-1 → 2022.12.2-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
@@ -1,7 +1,12 @@
1
1
  import { DTDatabase, DTTransaction } from "./database";
2
+ export interface CachedValue<T> {
3
+ content: T;
4
+ last_updated: Date;
5
+ }
2
6
  export declare enum JSON_CACHE_KEY {
3
7
  NAUTICAL_WARNINGS_ACTIVE = "nautical-warnings-active",
4
8
  NAUTICAL_WARNINGS_ARCHIVED = "nautical-warnings-archived"
5
9
  }
6
10
  export declare function updateCachedJson<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY, value: T): Promise<null>;
7
11
  export declare function getJsonFromCache<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY): Promise<T | null>;
12
+ export declare function getFromCache<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY): Promise<CachedValue<T> | null>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getJsonFromCache = exports.updateCachedJson = exports.JSON_CACHE_KEY = void 0;
3
+ exports.getFromCache = exports.getJsonFromCache = exports.updateCachedJson = exports.JSON_CACHE_KEY = void 0;
4
4
  const pg_promise_1 = require("pg-promise");
5
5
  const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_updated)
6
6
  values ($1, $2, now())
@@ -9,11 +9,11 @@ const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_
9
9
  const SQL_GET_CACHE_VALUE = `select content, last_updated from cached_json
10
10
  where cache_id = $1`;
11
11
  const PS_UPDATE_CACHE_VALUE = new pg_promise_1.PreparedStatement({
12
- name: 'update-cache-value',
12
+ name: "update-cache-value",
13
13
  text: SQL_UPDATE_CACHE_VALUE,
14
14
  });
15
15
  const PS_GET_CACHE_VALUE = new pg_promise_1.PreparedStatement({
16
- name: 'get-cache-value',
16
+ name: "get-cache-value",
17
17
  text: SQL_GET_CACHE_VALUE,
18
18
  });
19
19
  var JSON_CACHE_KEY;
@@ -26,7 +26,13 @@ function updateCachedJson(db, cacheKey, value) {
26
26
  }
27
27
  exports.updateCachedJson = updateCachedJson;
28
28
  function getJsonFromCache(db, cacheKey) {
29
- return db.oneOrNone(PS_GET_CACHE_VALUE, [cacheKey]).then(value => value?.content ?? null);
29
+ return db
30
+ .oneOrNone(PS_GET_CACHE_VALUE, [cacheKey])
31
+ .then((value) => value?.content ?? null);
30
32
  }
31
33
  exports.getJsonFromCache = getJsonFromCache;
34
+ function getFromCache(db, cacheKey) {
35
+ return db.oneOrNone(PS_GET_CACHE_VALUE, [cacheKey]);
36
+ }
37
+ exports.getFromCache = getFromCache;
32
38
  //# sourceMappingURL=cached.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2022.11.28-1",
3
+ "version": "2022.12.02-1",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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
+ }
@@ -1,35 +1,54 @@
1
- import {PreparedStatement} from "pg-promise";
2
- import {DTDatabase, DTTransaction} from "./database";
1
+ import { PreparedStatement } from "pg-promise";
2
+ import { DTDatabase, DTTransaction } from "./database";
3
3
 
4
- const SQL_UPDATE_CACHE_VALUE =
5
- `insert into cached_json(cache_id, content, last_updated)
4
+ export interface CachedValue<T> {
5
+ content: T;
6
+ last_updated: Date;
7
+ }
8
+
9
+ const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_updated)
6
10
  values ($1, $2, now())
7
11
  on conflict(cache_id) do
8
12
  update set content = $2, last_updated = now()`;
9
13
 
10
- const SQL_GET_CACHE_VALUE =
11
- `select content, last_updated from cached_json
14
+ const SQL_GET_CACHE_VALUE = `select content, last_updated from cached_json
12
15
  where cache_id = $1`;
13
16
 
14
17
  const PS_UPDATE_CACHE_VALUE = new PreparedStatement({
15
- name: 'update-cache-value',
18
+ name: "update-cache-value",
16
19
  text: SQL_UPDATE_CACHE_VALUE,
17
20
  });
18
21
 
19
22
  const PS_GET_CACHE_VALUE = new PreparedStatement({
20
- name: 'get-cache-value',
23
+ name: "get-cache-value",
21
24
  text: SQL_GET_CACHE_VALUE,
22
25
  });
23
26
 
24
27
  export enum JSON_CACHE_KEY {
25
- NAUTICAL_WARNINGS_ACTIVE = 'nautical-warnings-active',
26
- NAUTICAL_WARNINGS_ARCHIVED = 'nautical-warnings-archived'
28
+ NAUTICAL_WARNINGS_ACTIVE = "nautical-warnings-active",
29
+ NAUTICAL_WARNINGS_ARCHIVED = "nautical-warnings-archived",
27
30
  }
28
31
 
29
- export function updateCachedJson<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY, value: T): Promise<null> {
32
+ export function updateCachedJson<T>(
33
+ db: DTDatabase | DTTransaction,
34
+ cacheKey: JSON_CACHE_KEY,
35
+ value: T
36
+ ): Promise<null> {
30
37
  return db.none(PS_UPDATE_CACHE_VALUE, [cacheKey, value]);
31
38
  }
32
39
 
33
- export function getJsonFromCache<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY): Promise<T | null> {
34
- return db.oneOrNone(PS_GET_CACHE_VALUE, [cacheKey]).then(value => value?.content ?? null);
40
+ export function getJsonFromCache<T>(
41
+ db: DTDatabase | DTTransaction,
42
+ cacheKey: JSON_CACHE_KEY
43
+ ): Promise<T | null> {
44
+ return db
45
+ .oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey])
46
+ .then((value) => value?.content ?? null);
47
+ }
48
+
49
+ export function getFromCache<T>(
50
+ db: DTDatabase | DTTransaction,
51
+ cacheKey: JSON_CACHE_KEY
52
+ ): Promise<CachedValue<T> | null> {
53
+ return db.oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey]);
35
54
  }