@xube/kit-aws-data-infrastructure 0.0.71

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.
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@xube/kit-aws-data-infrastructure",
3
+ "version": "0.0.71",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "generate": "npx ts-node src/generator.ts"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+ssh://git@github.com/XubeLtd/dev-kit.git"
13
+ },
14
+ "author": "Xube Pty Ltd",
15
+ "license": "MIT",
16
+ "bugs": {
17
+ "url": "https://github.com/XubeLtd/dev-kit/issues"
18
+ },
19
+ "homepage": "https://github.com/XubeLtd/dev-kit#readme",
20
+ "devDependencies": {
21
+ "@types/node": "^20.4.7",
22
+ "@xube/kit-build": "^0.0.71",
23
+ "ts-node": "^10.9.1",
24
+ "typescript": "^5.1.6"
25
+ },
26
+ "dependencies": {
27
+ "@xube/kit-aws": "^0.0.71",
28
+ "@xube/kit-aws-infrastructure": "^0.0.71",
29
+ "@xube/kit-constants": "^0.0.71",
30
+ "@xube/kit-data": "^0.0.71",
31
+ "@xube/kit-data-schema": "^0.0.71",
32
+ "@xube/kit-log": "^0.0.71",
33
+ "aws-cdk-lib": "^2.100.0",
34
+ "aws-lambda": "^1.0.7",
35
+ "constructs": "^10.3.0"
36
+ }
37
+ }
@@ -0,0 +1,23 @@
1
+ import { XubeRestAPI, XubeRestAPIProps } from "@xube/kit-aws-infrastructure";
2
+ import { Construct } from "constructs";
3
+ import { GetDataAPI } from "./get";
4
+ import { ITable } from "aws-cdk-lib/aws-dynamodb";
5
+
6
+ interface DataAPIManagerProps extends XubeRestAPIProps {
7
+ dataTable: ITable;
8
+ }
9
+
10
+ export class DataAPIManager extends Construct {
11
+ dataApi: XubeRestAPI;
12
+
13
+ constructor(scope: Construct, id: string, props: DataAPIManagerProps) {
14
+ super(scope, id);
15
+ this.dataApi = new XubeRestAPI(scope, id + "xube-api", props);
16
+
17
+ const getData = new GetDataAPI(this, id + "-get-data-api", {
18
+ name: props.name + "-get-data",
19
+ dataAPI: this.dataApi.restAPI,
20
+ dataTable: props.dataTable,
21
+ });
22
+ }
23
+ }
package/src/api/get.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { DATA_TABLE_NAME_ENV_VAR } from "@xube/kit-data-schema";
2
+ import { IRestApi, LambdaIntegration } from "aws-cdk-lib/aws-apigateway";
3
+ import { ITable } from "aws-cdk-lib/aws-dynamodb";
4
+ import { HttpMethod, Runtime } from "aws-cdk-lib/aws-lambda";
5
+ import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
6
+ import { Construct } from "constructs";
7
+ import { join } from "path";
8
+
9
+ export interface GetDataAPIProps {
10
+ dataAPI: IRestApi;
11
+ name: string;
12
+ dataTable: ITable;
13
+ }
14
+
15
+ export class GetDataAPI extends Construct {
16
+ getDataFunction: NodejsFunction;
17
+
18
+ constructor(scope: Construct, id: string, props: GetDataAPIProps) {
19
+ super(scope, id);
20
+
21
+ this.getDataFunction = new NodejsFunction(scope, id + "-get-data", {
22
+ entry: join(__dirname, "../functions/get-data.js"),
23
+ runtime: Runtime.NODEJS_18_X,
24
+ functionName: props.name,
25
+ environment: {
26
+ [DATA_TABLE_NAME_ENV_VAR]: props.dataTable.tableName,
27
+ },
28
+ });
29
+
30
+ props.dataAPI.root.addMethod(
31
+ HttpMethod.POST,
32
+ new LambdaIntegration(this.getDataFunction)
33
+ );
34
+ }
35
+ }
@@ -0,0 +1,37 @@
1
+ import { XubeRestAPIProps } from "@xube/kit-aws-infrastructure";
2
+ import { AttributeType, ITable, Table } from "aws-cdk-lib/aws-dynamodb";
3
+ import { Construct } from "constructs";
4
+ import { PARTITION_KEY, SORT_KEY } from "../../kit-aws-schema/src";
5
+ import { DataAPIManager } from "./api/data-api";
6
+
7
+ export interface DataManagementProps extends XubeRestAPIProps {
8
+ dataTable?: Table;
9
+ }
10
+
11
+ export class DataManagement extends Construct {
12
+ table: Table;
13
+
14
+ constructor(scope: Construct, id: string, props: DataManagementProps) {
15
+ super(scope, id);
16
+
17
+ this.table =
18
+ props.dataTable ??
19
+ new Table(scope, id + "-table", {
20
+ tableName: (props.name ?? `data-management`) + "-table",
21
+ partitionKey: {
22
+ name: PARTITION_KEY,
23
+ type: AttributeType.STRING,
24
+ },
25
+ sortKey: {
26
+ name: SORT_KEY,
27
+ type: AttributeType.STRING,
28
+ },
29
+ });
30
+
31
+ const dataApi = new DataAPIManager(this, id + "-data-api", {
32
+ ...props,
33
+ name: props.name ?? `data-management`,
34
+ dataTable: this.table,
35
+ });
36
+ }
37
+ }
@@ -0,0 +1,52 @@
1
+ import {
2
+ DATA_TABLE_NAME_ENV_VAR,
3
+ isGetDataByDateRangeRequest,
4
+ } from "@xube/kit-data-schema";
5
+ import {
6
+ APIGatewayProxyResult,
7
+ APIGatewayProxyWithCognitoAuthorizerEvent,
8
+ } from "aws-lambda";
9
+ import { StatusCode } from "../../../kit-constants/src";
10
+ import { getDataForDateRange } from "@xube/kit-data/src/get/get-data";
11
+ import { XubeLog } from "@xube/kit-log";
12
+
13
+ const dataTableName = process.env[DATA_TABLE_NAME_ENV_VAR] ?? "";
14
+
15
+ export const handler = async (
16
+ event: APIGatewayProxyWithCognitoAuthorizerEvent
17
+ ): Promise<APIGatewayProxyResult> => {
18
+ const log = XubeLog.getInstance();
19
+
20
+ if (!dataTableName) {
21
+ log.error(`Could not find data table name in environment variables`);
22
+ return {
23
+ body: `Could not find data table name in environment variables`,
24
+ statusCode: StatusCode.InternalError,
25
+ };
26
+ }
27
+
28
+ if (!event.body) {
29
+ log.warn(`No body provided`);
30
+ return {
31
+ body: `No body provided`,
32
+ statusCode: StatusCode.BadRequest,
33
+ };
34
+ }
35
+
36
+ const request: unknown = JSON.parse(event.body);
37
+
38
+ if (!isGetDataByDateRangeRequest(request)) {
39
+ 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
+ };
44
+ }
45
+
46
+ const response = await getDataForDateRange(request, dataTableName, log);
47
+
48
+ return {
49
+ body: JSON.stringify(response),
50
+ statusCode: response.statusCode,
51
+ };
52
+ };
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./device-management";
2
+ export * from "./api/data-api";
3
+ export * from "./api/get";
package/tsconfig.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "extends": "../kit-build/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist"
6
+ },
7
+ "exclude": ["**/*.test.ts", "**/*.mock.ts", "dist"],
8
+ "references": [
9
+ {
10
+ "path": "../kit-generator"
11
+ },
12
+ {
13
+ "path": "../kit-constants"
14
+ },
15
+ {
16
+ "path": "../kit-log"
17
+ },
18
+ {
19
+ "path": "../kit-schema"
20
+ },
21
+ {
22
+ "path": "../kit-aws-schema"
23
+ },
24
+ {
25
+ "path": "../kit-aws"
26
+ },
27
+ {
28
+ "path": "../kit-aws-infrastructure"
29
+ },
30
+ {
31
+ "path": "../kit-data"
32
+ },
33
+ ]
34
+ }