@dma-sdk/utils 1.0.4 → 1.0.5

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,26 @@
1
+ export declare class StdError extends Error {
2
+ private readonly error;
3
+ constructor(error: ErrorBody | string);
4
+ get: () => {
5
+ stack: string | undefined;
6
+ internalCode: string | undefined;
7
+ message: string;
8
+ };
9
+ toJSON: () => ErrorBody;
10
+ }
11
+ export type KResponse = {
12
+ statusCode: number;
13
+ body: string;
14
+ headers?: {
15
+ [key: string]: string;
16
+ };
17
+ };
18
+ export interface ErrorBody {
19
+ internalCode?: string;
20
+ message: string;
21
+ trace?: string;
22
+ }
23
+ export declare class ResponseUtil {
24
+ static readonly successRespond: (code: number, body: any) => KResponse;
25
+ static readonly errorRespond: (code: number, body: ErrorBody | StdError) => KResponse;
26
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResponseUtil = exports.StdError = void 0;
4
+ const logger_1 = require("./logger");
5
+ class StdError extends Error {
6
+ constructor(error) {
7
+ super(typeof error === 'string' ? error : error.message);
8
+ this.get = () => {
9
+ return {
10
+ stack: this.error.trace,
11
+ internalCode: this.error.internalCode,
12
+ message: this.error.message,
13
+ };
14
+ };
15
+ this.toJSON = () => {
16
+ return this.error;
17
+ };
18
+ Object.setPrototypeOf(this, StdError.prototype);
19
+ if (typeof error !== 'string') {
20
+ this.name = 'StdError';
21
+ this.stack = error.trace;
22
+ this.error = error;
23
+ }
24
+ else {
25
+ this.error = {
26
+ message: error,
27
+ };
28
+ }
29
+ }
30
+ }
31
+ exports.StdError = StdError;
32
+ const stdHeaders = {
33
+ 'Content-Type': 'application/json',
34
+ 'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
35
+ 'Cross-Origin-Resource-Policy': 'cross-origin', // Fix issue with e2e tests
36
+ };
37
+ class ResponseUtil {
38
+ }
39
+ exports.ResponseUtil = ResponseUtil;
40
+ ResponseUtil.successRespond = (code, body) => {
41
+ logger_1.Logger.info(`SUCCESS RESPONSE: ${JSON.stringify(body)}`);
42
+ return {
43
+ statusCode: code,
44
+ body: JSON.stringify(body),
45
+ headers: stdHeaders,
46
+ };
47
+ };
48
+ ResponseUtil.errorRespond = (code, body) => {
49
+ logger_1.Logger.error(`ERROR RESPONSE: ${JSON.stringify(body)}`);
50
+ return {
51
+ statusCode: code,
52
+ body: JSON.stringify(body),
53
+ headers: stdHeaders,
54
+ };
55
+ };
package/dist/logger.d.ts CHANGED
@@ -6,5 +6,6 @@ export declare class Logger {
6
6
  static debug: (data: any) => void;
7
7
  static info: (data: any) => void;
8
8
  static warn: (data: any) => void;
9
+ static error: (data: any) => void;
9
10
  static notice: (data: any) => void;
10
11
  }
package/dist/logger.js CHANGED
@@ -25,6 +25,10 @@ Logger.warn = (data) => _a.log({
25
25
  level: 'WARNING',
26
26
  data,
27
27
  });
28
+ Logger.error = (data) => _a.log({
29
+ level: 'ERROR',
30
+ data,
31
+ });
28
32
  Logger.notice = (data) => _a.log({
29
33
  level: 'NOTICE',
30
34
  data,
@@ -0,0 +1,73 @@
1
+ import { Logger } from './logger'
2
+
3
+ export class StdError extends Error {
4
+ private readonly error: ErrorBody
5
+
6
+ constructor(error: ErrorBody | string) {
7
+ super(typeof error === 'string' ? error : error.message)
8
+
9
+ Object.setPrototypeOf(this, StdError.prototype)
10
+
11
+ if (typeof error !== 'string') {
12
+ this.name = 'StdError'
13
+ this.stack = error.trace
14
+ this.error = error
15
+ } else {
16
+ this.error = {
17
+ message: error,
18
+ }
19
+ }
20
+ }
21
+
22
+ get = () => {
23
+ return {
24
+ stack: this.error.trace,
25
+ internalCode: this.error.internalCode,
26
+ message: this.error.message,
27
+ }
28
+ }
29
+
30
+ toJSON = () => {
31
+ return this.error
32
+ }
33
+ }
34
+
35
+ export type KResponse = {
36
+ statusCode: number
37
+ body: string
38
+ headers?: { [key: string]: string }
39
+ }
40
+
41
+ export interface ErrorBody {
42
+ internalCode?: string
43
+ message: string
44
+ trace?: string
45
+ }
46
+
47
+
48
+ const stdHeaders = {
49
+ 'Content-Type': 'application/json',
50
+ 'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
51
+ 'Cross-Origin-Resource-Policy': 'cross-origin', // Fix issue with e2e tests
52
+ }
53
+
54
+ export class ResponseUtil {
55
+ static readonly successRespond = (code: number, body: any): KResponse => {
56
+ Logger.info(`SUCCESS RESPONSE: ${JSON.stringify(body)}`)
57
+ return {
58
+ statusCode: code,
59
+ body: JSON.stringify(body),
60
+ headers: stdHeaders,
61
+ }
62
+ }
63
+
64
+ static readonly errorRespond = (code: number, body: ErrorBody | StdError): KResponse => {
65
+ Logger.error(`ERROR RESPONSE: ${JSON.stringify(body)}`)
66
+
67
+ return {
68
+ statusCode: code,
69
+ body: JSON.stringify(body),
70
+ headers: stdHeaders,
71
+ }
72
+ }
73
+ }
package/lib/logger.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { StdError } from "./gateway-response";
2
+
1
3
  export class Logger {
2
4
  static log = (params: { level: string; data: any }) => {
3
5
  console.log(`[${Date.now()}] [${params.level}] ${JSON.stringify(params.data)}`)
@@ -24,6 +26,12 @@ export class Logger {
24
26
  data,
25
27
  })
26
28
 
29
+ static error = (data: any) =>
30
+ this.log({
31
+ level: 'ERROR',
32
+ data,
33
+ })
34
+
27
35
  static notice = (data: any) =>
28
36
  this.log({
29
37
  level: 'NOTICE',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dma-sdk/utils",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "DMA SDK – Utils",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",