@xube/kit-aws-data-infrastructure 0.0.76 → 0.0.78

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.
@@ -5,6 +5,8 @@ const kit_aws_data_schema_1 = require("@xube/kit-aws-data-schema");
5
5
  const kit_constants_1 = require("@xube/kit-constants");
6
6
  const kit_aws_data_1 = require("@xube/kit-aws-data");
7
7
  const kit_log_1 = require("@xube/kit-log");
8
+ const zod_1 = require("zod");
9
+ const zod_validation_error_1 = require("zod-validation-error");
8
10
  const dataTableName = process.env[kit_aws_data_schema_1.DATA_TABLE_NAME_ENV_VAR] ?? "";
9
11
  const handler = async (event) => {
10
12
  const log = kit_log_1.XubeLog.getInstance();
@@ -23,17 +25,41 @@ const handler = async (event) => {
23
25
  };
24
26
  }
25
27
  const request = JSON.parse(event.body);
26
- if (!(0, kit_aws_data_schema_1.isGetDataByDateRangeRequest)(request)) {
28
+ try {
29
+ if ((0, kit_aws_data_schema_1.isGetDataByDateRangeRequest)(request)) {
30
+ log.info(`Request body is a valid Get Data By Date Range request`);
31
+ const response = await (0, kit_aws_data_1.getDataForDateRange)(request, dataTableName, log);
32
+ if (response.hasFailed() || !response.data) {
33
+ if (response.statusCode == kit_constants_1.StatusCode.NotFound) {
34
+ return {
35
+ body: response.error ?? `Could not find any data for the request`,
36
+ statusCode: response.statusCode,
37
+ };
38
+ }
39
+ }
40
+ else {
41
+ return {
42
+ body: JSON.stringify(response.data),
43
+ statusCode: response.statusCode,
44
+ };
45
+ }
46
+ }
47
+ }
48
+ catch (e) {
27
49
  log.warn(`Request body is not a valid Get Data By Date Range request`);
28
- return {
29
- body: `Request body is not a valid Get Data By Date Range request`,
30
- statusCode: kit_constants_1.StatusCode.BadRequest,
31
- };
50
+ if (e instanceof zod_1.z.ZodError) {
51
+ const validationError = (0, zod_validation_error_1.fromZodError)(e);
52
+ log.warn(`Validation error: ${JSON.stringify(validationError)}`);
53
+ return {
54
+ body: validationError.message,
55
+ statusCode: kit_constants_1.StatusCode.BadRequest,
56
+ };
57
+ }
32
58
  }
33
- const response = await (0, kit_aws_data_1.getDataForDateRange)(request, dataTableName, log);
59
+ log.error(`Could not get data by date range with request: ${event.body}`);
34
60
  return {
35
- body: JSON.stringify(response),
36
- statusCode: response.statusCode,
61
+ body: `An error occurred when getting data by date range`,
62
+ statusCode: kit_constants_1.StatusCode.InternalError,
37
63
  };
38
64
  };
39
65
  exports.handler = handler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xube/kit-aws-data-infrastructure",
3
- "version": "0.0.76",
3
+ "version": "0.0.78",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -19,19 +19,21 @@
19
19
  "homepage": "https://github.com/XubeLtd/dev-kit#readme",
20
20
  "devDependencies": {
21
21
  "@types/node": "^20.4.7",
22
- "@xube/kit-build": "^0.0.76",
22
+ "@xube/kit-build": "^0.0.78",
23
23
  "ts-node": "^10.9.1",
24
24
  "typescript": "^5.1.6"
25
25
  },
26
26
  "dependencies": {
27
- "@xube/kit-aws": "^0.0.76",
28
- "@xube/kit-aws-data": "^0.0.76",
29
- "@xube/kit-aws-data-schema": "^0.0.76",
30
- "@xube/kit-aws-infrastructure": "^0.0.76",
31
- "@xube/kit-constants": "^0.0.76",
32
- "@xube/kit-log": "^0.0.76",
27
+ "@xube/kit-aws": "^0.0.78",
28
+ "@xube/kit-aws-data": "^0.0.78",
29
+ "@xube/kit-aws-data-schema": "^0.0.78",
30
+ "@xube/kit-aws-infrastructure": "^0.0.78",
31
+ "@xube/kit-constants": "^0.0.78",
32
+ "@xube/kit-log": "^0.0.78",
33
33
  "aws-cdk-lib": "^2.100.0",
34
34
  "aws-lambda": "^1.0.7",
35
- "constructs": "^10.3.0"
35
+ "constructs": "^10.3.0",
36
+ "zod": "^3.22.4",
37
+ "zod-validation-error": "^2.0.0"
36
38
  }
37
39
  }
@@ -9,6 +9,8 @@ import {
9
9
  import { StatusCode } from "@xube/kit-constants";
10
10
  import { getDataForDateRange } from "@xube/kit-aws-data";
11
11
  import { XubeLog } from "@xube/kit-log";
12
+ import { z } from "zod";
13
+ import { fromZodError } from "zod-validation-error";
12
14
 
13
15
  const dataTableName = process.env[DATA_TABLE_NAME_ENV_VAR] ?? "";
14
16
 
@@ -35,18 +37,42 @@ export const handler = async (
35
37
 
36
38
  const request: unknown = JSON.parse(event.body);
37
39
 
38
- if (!isGetDataByDateRangeRequest(request)) {
40
+ try {
41
+ if (isGetDataByDateRangeRequest(request)) {
42
+ log.info(`Request body is a valid Get Data By Date Range request`);
43
+ const response = await getDataForDateRange(request, dataTableName, log);
44
+
45
+ if (response.hasFailed() || !response.data) {
46
+ if (response.statusCode == StatusCode.NotFound) {
47
+ return {
48
+ body: response.error ?? `Could not find any data for the request`,
49
+ statusCode: response.statusCode,
50
+ };
51
+ }
52
+ } else {
53
+ return {
54
+ body: JSON.stringify(response.data),
55
+ statusCode: response.statusCode,
56
+ };
57
+ }
58
+ }
59
+ } catch (e) {
39
60
  log.warn(`Request body is not a valid Get Data By Date Range request`);
40
- return {
41
- body: `Request body is not a valid Get Data By Date Range request`,
42
- statusCode: StatusCode.BadRequest,
43
- };
61
+
62
+ if (e instanceof z.ZodError) {
63
+ const validationError = fromZodError(e);
64
+ log.warn(`Validation error: ${JSON.stringify(validationError)}`);
65
+ return {
66
+ body: validationError.message,
67
+ statusCode: StatusCode.BadRequest,
68
+ };
69
+ }
44
70
  }
45
71
 
46
- const response = await getDataForDateRange(request, dataTableName, log);
72
+ log.error(`Could not get data by date range with request: ${event.body}`);
47
73
 
48
- return {
49
- body: JSON.stringify(response),
50
- statusCode: response.statusCode,
51
- };
74
+ return {
75
+ body: `An error occurred when getting data by date range`,
76
+ statusCode: StatusCode.InternalError,
77
+ };
52
78
  };