External Communications Manager
Wrapper for your all the external communication you'll need for your applications. Hope you will love the flavour.
Installation
You can install the External Communications Manager package from npm using the following command:
npm install external-communications-manager
Summary
There are many feature embedded into this one single package viz.
1. API via axios
2. Mailer
2.1 Email via aws-sdk
2.2 SMS via 2factor
3. Message Queue via Kafka
4. Storage
4.1 AWS-S3 Bucket via aws-sdk and multer
4.2 Cache
4.2.1 Redis
4.2.2 Context Memory - Redis [and if redis instance missing, in memory context]
4.3 Local Hard Disk (HDD) via multer
4.4 SFTP via multer-sftp
Usage Guidelines
API
Default ENV Configurations
# how many times should the api call be tried before declaring failed
API_FAIL_DEFAULT_RETRY_COUNT = 3
# should a message be published to topic in case api fails 1/0 where 0 is false and non-0+ is true
API_FAIL_SHOULD_NOTIFY_KAFKA = 0
# if api call is failed, which topic should the details be published to
MQTOPIC_API_FAIL_ALERT = apicallfailed
# after how many seconds the request to be aborted, to avoid waiting forever and blocking main thread (in miliseconds)
API_ABORT_REQUEST_AFTER_MS = 15000
Usage Example
const { ResourceAPI } = require('external-communications-manager');
# returns promise, thus must be awaited
ResourceAPI.https.get(someURL, headers, data)).data;
ResourceAPI.https.patch(someURL, headers, data)).data;
ResourceAPI.https.put(someURL, headers, data)).data;
ResourceAPI.https.post(someURL, headers, data)).data;
ResourceAPI.https.delete(someURL, headers, data)).data;
Mailer
Default ENV Configurations
# EMAIL
AWS_KEY =
AWS_API_VERSION = 2010-12-01
AWS_SECRET =
AWS_SES_REGION = us-west-2
# sender's email
AWS_FROM = hello@world.com
# can accommodate multiple comma separated emails, automatically filters out empty entries and trims extra spaces
AWS_REPLY = hello@world.com
# SMS --supports international numbers --replace MY_TEMPLATE_NAME with appropriate template name
TWOFACTOR_API_URL = https://2factor.in/API/V1/{{accessKey}}/SMS/{{contact}}/{{passkey}}/MY_TEMPLATE_NAME
TWOFACTOR_ACCESS_KEY =
Usage Example
const {
Mailer
} = require('external-communications-manager');
# Email - returns promise,
#
#
#
Mailer.email.send({
emails : void 0,
email : 'hello@world.com',
subject: 'Some Subject',
body : 'Some body message, either simple text or html, if html, pass second argument as true',
}, false);
# SMS - returns promise
Mailer.sms.send({
mobile: `receiver's mobile, replaces {{contact}} in 2factor api-url`,
from: 'from',
template: 'template-name',
var1: 'replaces {{passkey}}',
var2: 'if template has any other variable to be replaced',
var3: 'if template has any other variable to be replaced'
})
Message Queue
Default ENV Configurations
#
# KAFKA - BROKER can be given multiple (comma separated), groupid is mandatory
KAFKA_USERNAME =
KAFKA_PASSWORD =
KAFKA_CLIENT_ID = myapp
# in comma separated, spaces will be trimmed, empty entries will be filtered out
KAFKA_BROKER = <ip>:<port>
KAFKA_MECHANISM = plain
KAFKA_GROUP_ID = mygroup
Usage Example
# publish --returns promise
MessageQueue.Kafka.publish('some-topic-name', {
key: String(Date.now()), // or any unique identifier
value: JSON.stringify({}), //any valid JSON data in stringified form
headers: { //good to add metadata for better debugging later on
source: "myapp",
action: "test",
type: "registration"
},
});
# subscribe - returns promise
MessageQueue.Kafka.subscribe('some-topic-name', function() {
//callback function
});
Storage
Default ENV Configurations
#
#
#
AWS_S3_API_VERSION = 2006-03-01
AWS_ACCESS_KEY =
AWS_SECRET_ACCESS_KEY =
AWS_S3_REGION = ap-south-1
AWS_S3_DEFAULT_BUCKET = mybucket
AWS_MAX_FILE_SIZE = 40960
#
#
#
FS_LOCAL_TEMP_DIR = data/temp/myfiles/
#
#
#
REDIS_HOST = <ip>
REDIS_PORT = <port>
REDIS_PWD =
Note
This package will help you streamline all external accesses like api, message-queue, file uploads etc. Import and make use of it. As simple as that.