Skip to content

Common utils, types, exceptions, constants and services

Notifications You must be signed in to change notification settings

techuila/aws-typescript-toolkit

Repository files navigation

AWS Typescript Toolkit

Common utils, types, exceptions, constants and services

Features:

  • Base handler for lamba resource
  • Common:
    • Utils
    • Types
    • Services
    • Exceptions:
      • Backend Error
      • Database Error
    • Config
      • getConfig()
        • VERSION
        • DEPLOYMENT_NAME
        • TABLE_NAME
        • PLATFORM_PREFIX

Installation

yarn install aws-typescript-toolkit

Exports

import { Services, BaseHandler, Constants, Construct, Exceptions, Types, Utils } from 'aws-typescript-toolkit';

const { Bucket, DeepLink, Dynamodb, Event, Logger, SSM } = Service;
const { createConstructs, setResources, ConstructMiddleware } = Construct;
const { BaseError, BackendError, DatabaseError } = Exceptions;
const { createFileNameHandler, streamToString, flattenInput, constructName, generateName, getConstructs } = Utils;
const { ValueOf, IAWSLambdaHandler, GqlInput, StackProps, ConstructTypes, IConstruct, INewConstruct, Callback, GenericExtend, gql } = Types;
const { getConfig } = Config;

const { VERSION, DEPLOYMENT_NAME, PLATFORM_PREFIX, TABLE_NAME } = getConfig();

Construct

Utilities to simplify setting up of constructs.

  • createConstructs

Usage is similar to createSlice from Redux Toolkit.

// cdk/constructs/lambda/EntityName.ts

import { Construct, Services, Config } from 'aws-typescript-toolkit'

const { createConstructs } = Construct;
const { SSMActions } = Services.SSM
const { getConfig } = Config;

const { TABLE_NAME } = getConfig();

const EntityName = createConstructs({
  type: NodejsFunction,
  constructs: {
    handleHelloWorld: {
      props: { 
        environment: { TABLE_NAME } 
      }, 
      output: true,
      callback: async (scope, construct) => {
        // Add event source trigger
        const eventBusArn = Fn.importValue('EventsPlatform-EventBus');
        const eventBus = EventBus.fromEventBusArn(scope, 'ImportedEventBus', eventBusArn);
        eventBus.grantPutEventsTo(construct);

        // Fetch param store
        const ssmClient = new SSMActions();
        const parameterInput = {
          Name: `/${DEPLOYMENT_NAME}/key`,
        };
        const parameter = await ssmClient.getParameter(parameterInput);

        // Add Environments
        construct.addEnvironment('BRANCH_KEY', parameter?.Value ?? '');
        construct.addEnvironment('EVENT_BUS_NAME', eventBus.eventBusName);
      },
    },
  },
});

export const { handleHelloWorld } = EntityName;

Config

Gets the config file of the cdk project platform.json.

Expected Content

{
  "name": "cdk-project-name",
  "version": "1.0.0",
  "table_name": "cdk-project-table"
}

To get the these values: VERSION, DEPLOYMENT_NAME, PLATFORM_PREFIX, TABLE_PREFIX, TABLE_NAME, from the config file, call the getConfig().

About

Common utils, types, exceptions, constants and services

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published