diff --git a/example/example.js b/example/example.js index 8f48bda..f39e1d6 100644 --- a/example/example.js +++ b/example/example.js @@ -16,16 +16,20 @@ import Notifier from '@uci-utils/notify' { user: process.env.GMAIL_SMTP_USER, pass:process.env.GMAIL_SMTP_PW }, - to: process.env.NOTIFY_EMAIL_RECIPIENTS, + to: process.env.NOTIFY_EMAIL_RECIPIENTS.split(','), subject:'uci notification' } } }) + console.log('registered services\n',notify.getServicesList()) + console.log('registered services\n',notify.getService()) // console.log (notify._services.pushsafer._req) // notify.disableService('pushsafer') // notify.enableService('pushsafer') - // console.log(await notify.send('a test message','pushsafer')) + console.log('pushsafter only', await notify.send('a pushsafter only message','pushsafer')) + console.log('not email', await notify.send('all but email message','!email')) + console.log('email only', await notify.send('email only message','email')) let res = await notify.send('simple message to all services') // let res = await notify.send({subject:'subject as message',text:'this details'}) // if (res.error) console.log('errors',res) diff --git a/example/package.json b/example/package.json index aeacfd7..04e839e 100644 --- a/example/package.json +++ b/example/package.json @@ -4,7 +4,7 @@ "description": "notify example application", "main": "example.js", "scripts": { - "start": "node -r esm example.js" + "start": "./run" }, "author": "", "license": "ISC", diff --git a/example/run b/example/run new file mode 100755 index 0000000..2d3e2cb --- /dev/null +++ b/example/run @@ -0,0 +1,7 @@ +#!/bin/bash +export PS_API_KEY=DLq9cXqNwBptT4CxD5cQ +export PS_DEVICE_ID=gs1992 +export GMAIL_SMTP_USER=kebler.net@gmail.com +export GMAIL_SMTP_PW=seniobjybxwuhjvj +export NOTIFY_EMAIL_RECIPIENTS='kebler.net@gmail.com,dgkebler@gmail.com' +node -r esm example.js diff --git a/package.json b/package.json index 85d8f10..33bf26f 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "@uci-utils/notify", - "version": "0.1.3", + "version": "0.1.5", "description": "Integrated Notification System with service plugins", "main": "src/notify", "scripts": { - "example:dev": "./node_modules/.bin/nodemon -r esm ./examples/example.js", + "example:dev": "./node_modules/.bin/nodemon -r esm ./example/example.js", "test": "./node_modules/.bin/mocha -r esm --timeout 30000" }, "author": "David Kebler", @@ -26,5 +26,8 @@ "app-root-path": "^3.0.0", "await-to-js": "^2.1.1", "jsonfile": "^6.0.1" + }, + "devDependencies": { + "esm": "^3.2.25" } } diff --git a/src/notify.js b/src/notify.js index 3eb542f..d640155 100644 --- a/src/notify.js +++ b/src/notify.js @@ -71,12 +71,15 @@ class Notifier { } async send (msg,service,opts={}) { - if (service) { + if (service!==null || service==='!email') { if (this._services[service]) return await this._services[service].send(msg,opts) } let response = {} + let services = Object.entries(this._services) + if (service==='!email') services = services.filter(service => service[0]!=='email') + // console.log('services for message send', services.map(service=>service[0])) await Promise.all( - Object.entries(this._services).map(async service => { + services.map(async service => { let res = this._disabled.includes(service[0]) ? 'disabled' : await service[1].send(msg,opts[service[0]]) if (res.error) response.error ? response.error[service[0]] = res.error : response = { error:{[service[0]]:res.error}} response[service[0]]=res @@ -86,7 +89,12 @@ class Notifier { return response } - getService(name) { return this._services[name]} + getServicesList() { return Object.keys(this._services) } + + getService(name) { + if (name==null) return this._services + return this._services[name] + } } // end Notifier class