Casava Common Library
This is the common shared library used across all the Node.js microservice within Casava. Common feature/code are placed in this library.
Usage
Using the library requires AWS IAM credentials loaded as enviromental variables with the following keys value pairs:
AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxx
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxx
# For publishing to SNS topics
AWS_SNS_TOPIC_ARN=xxxxxxxxxxxxxxxxxx
Initializing the library
The library exposes a single entrypoint (a singleton) for accessing services provided. so as to avoid re-validation of AWS credentials per initialization.
import { config } from "dotenv";
import casavaLibrary from "casava-common"
const libraryInstance1 = casavaLibrary(); // returns an instance of CasavaCommonLibrary
const libraryInstance2 = casavaLibrary();
libraryInstance1 == libraryInstance2 // returns true: it's a singleton.
Services:
all services provided are public properties of CasavaCommonLibrary class.
CasavaCommonLibrary.pubSub : PubSub
-
CasavaCommonLibrary.pubSub.publish: publishing to an AWS SNS Topic
const library = casavaLibrary(); const params = { message: 'Hello world', filters: [ { attributeName: 'useraction', // attribute name for message filtering attributeValues: ['registration', 'request_verification'] // values of the attribute name: must be of type string[] } ], topicArn: AWS_TOPIC_ARN // if not provided, it uses the AWS_SNS_TOPIC_ARN .env variable } const publishJob = library.pubSub.publish(params); publishJob.then((res: PublishedMessageResponse) => console.log(res)).catch(console.error); // async/await can also be used.