Skip to content

Instantly share code, notes, and snippets.

@mtimbs
mtimbs / dynamoDBClient.ts
Created March 10, 2020 14:02
example DynamoDB Client base module if not using powertools library
import { ClientConfiguration, DocumentClient } from 'aws-sdk/clients/dynamodb';
const config: ClientConfiguration = {
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
};
if (['test'].includes(process.env.NODE_ENV)) {
config.endpoint = 'http://localhost:8000';
@mtimbs
mtimbs / setEnvironment.js
Created March 10, 2020 14:10
jest setup set/overwrite environment variables before running tests
const env = {
AWS_REGION: 'local',
AWS_ACCESS_KEY: 'fake_key',
AWS_SECRET_KEY: 'fake_secret',
};
process.env = {
...process.env,
...env,
};
@mtimbs
mtimbs / SQSRecordFactory.ts
Created March 11, 2020 00:32
Utility for generating fake SQS Records to help with Unit/Integration testing
import { v4 as uuidv4 } from 'uuid';
import { SQSRecord } from 'aws-lambda';
const now = Math.round((new Date()).getTime() / 1000).toString();
export default (params?: Partial<SQSRecord>): SQSRecord => ({
messageId: uuidv4(),
receiptHandle: uuidv4(),
body: '',
@mtimbs
mtimbs / SQSEventFactory.ts
Created March 11, 2020 00:32
Utility helper to generate test SQD events for integration testing lambda handlers
import sqsRecordGenerator from '@tests/utilities/sqsRecordFactory';
import { SQSEvent, SQSRecord } from 'aws-lambda';
export default (events?: SQSRecord[]): SQSEvent => ({
Records: events || [sqsRecordGenerator()],
});
@mtimbs
mtimbs / .gitlab-ci.typescript-example.yml
Last active August 15, 2023 04:17
Example Gitlab CI config for a typescript serverless proeject
image: docker:latest
cache:
paths:
- node_modules/
stages:
- build
- precheck
- test
@mtimbs
mtimbs / Dockerfile.pipeline-node
Last active April 22, 2020 13:47
Example base image for Gitlab pipelines for node/typescript serverless projects
FROM node:12
WORKDIR /app
# install JRE for dynamo-offline (required for tests)
RUN apt-get update \
&& apt-get install -y default-jre \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
@mtimbs
mtimbs / validateIp.ts
Last active August 29, 2020 12:43
IP4 validation
// Using Loops (and Regex)
const validateIP = ip => {
const regex = /^\d+$/;
const s = IP.split('.');
if(s.length !== 4) return false;
for(let i = 0; i < s.length; i++) {
if(!regex.test(s[i])) return false;
if(s[i].length > 1 && s[i].startsWith('0')) return false;
interface DynamoUpdateParams {
UpdateExpression: string;
ExpressionAttributeNames: Record<string, string>;
ExpressionAttributeValues: Record<string, unknown>;
}
export const DynamicUpdateExpressionFromObject = (map: Record<string, unknown>): DynamoUpdateParams => {
const ExpressionAttributeNames: Record<string, string> = Object.keys(map)
.reduce((acc, curr) => ({ ...acc, ...{ [`#${curr}`]: curr } }), {});
const UpdateExpression: string = Object.values(ExpressionAttributeNames)
function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {
return Object.hasOwnProperty.call(obj, prop)
}
@mtimbs
mtimbs / tsconfig.json
Last active May 26, 2024 10:02
basic default tsconfig for use in TypeScript projects
{
"compilerOptions": {
// project options
"lib": [
"ESNext",
"dom"
], // specifies which default set of type definitions to use ("DOM", "ES6", etc)
"outDir": "lib", // .js (as well as .d.ts, .js.map, etc.) files will be emitted into this directory.,
"removeComments": true, // Strips all comments from TypeScript files when converting into JavaScript- you rarely read compiled code so this saves space
"target": "ES6", // Target environment. Most modern browsers support ES6, but you may want to set it to newer or older. (defaults to ES3)