I am duplicating a configuration variable wherever I am Using. I want to make that common in one method and use that method's variable across the project.
common.ts
Notification(){
User.findOne({ email: data.email }, (err, user) => {
if (err) {
console.log(err);
}
else {
if (user.device_info[0].platform == 'Android') {
var Notification_config = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: 'arn:aws:sns:us-west-1:XXXXXXXX:app/GCM/Test',
});
console.log("Andriod");
}
else {
console.log("iOS");
var Notification_config = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.IOS,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: 'arn:aws:sns:us-west-1:XXXXXX:app/APNS_SANDBOX/Test',
sandbox: true
});
}
})
}
abc.ts
Notification_config.addUser('dev_token', JSON.stringify({
some: 'extra data'
}), function (err, endpointArn) {
if (err) {
throw err;
}
// Send a simple String or data to the client
Notification_config.sendMessage(endpointArn, ` sent you a request`, function (err, messageId) {
if (err) {
res.send(err)
}
req; res;
console.log('Message sent, ID was: ' + messageId);
});
What I tried
function push_notification(email) {
User.findOne({
email: email
}, (err, data) => {
if (err) {
console.log(err)
} else {
if (data.device_info[0].platform == 'Android') {
var platform = SNS.SUPPORTED_PLATFORMS.ANDROID;
var arn = 'arn:aws:sns:us-east-1:XXXXXX:app/GCM/Test'
//console.log("Andriod");
} else {
var platform = SNS.SUPPORTED_PLATFORMS.IOS;
var arn = 'arn:aws:sns:us-east-1:XXXXX:app/APNS_SANDBOX/Test'
//console.log("iOS");
}
var Notification_config = new SNS({
platform: platform,
accessKeyId: cache.get('AMAZON_ACCESS_KEY'),
secretAccessKey: cache.get('AMAZON_SECRET_KEY'),
region: cache.get('AMAZON_REGION'),
platformApplicationArn: arn,
sandbox: true
});
}
return Notification_config;
});
}
export default push_notification;
I want the variable Notification_config from common.ts to be used in abc.ts. I have tried to return the Notification_config to method and export default Notification in common.ts and import it in abc.ts but It couldn't find the variable. Can someone help me out.