Skip to content

Commit

Permalink
Merge branch 'add-custom-channels' of https://github.com/p16/notifme-sdk
Browse files Browse the repository at this point in the history
 into p16-add-custom-channels
  • Loading branch information
BDav24 committed Jan 2, 2019
2 parents 89dc5d5 + d31c350 commit 0587c7f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,35 @@ const smsCheapStrategy = (providers) => async (request) => {

If you would like to see another provider or channel, please [upvote the corresponding issue](https://github.com/notifme/notifme-sdk/issues) (or create one if it does not exist yet).

### 3. Send a notification
### 3. Custom channels

If you want to have custom channels (and providers) you can add them.

Example:

</p></details>
<details><summary>Custom channel and provider</summary><p>

```
new NotifmeSdk({
channels: {
socket: {
multiProviderStrategy: 'fallback',
providers: [
{
type: 'custom',
id: 'my-socket-sender',
send: async () => {
return 'custom-socket-id'
}
}
]
}
}
})
```

### 4. Send a notification

#### Parameters

Expand Down Expand Up @@ -1050,7 +1078,7 @@ Examples:
| Success<br>(when Promise resolves) | `{status: 'success', channels: {sms: {id: 'id-116561976', providerId: 'sms-default-provider'}}}` |
| Error<br>(here Notification Catcher is not running) | `{status: 'error', channels: {sms: {id: undefined, providerId: 'sms-notificationcatcher-provider'}}, errors: {sms: 'connect ECONNREFUSED 127.0.0.1:1025'}}` |
### 4. In production
### 5. In production
#### Recommended options
Expand Down
48 changes: 48 additions & 0 deletions __tests__/custom-channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* @flow */
/* global jest, test, expect */
import NotifmeSdk from '../src'

jest.mock('../src/util/logger', () => ({
info: jest.fn(),
warn: jest.fn()
}))

const request = {
socket: {
to: '[email protected]',
text: 'Hi John'
}
}

test.only('socket', async () => {
let socketCalled = false
const sdk = new NotifmeSdk({
channels: {
socket: {
multiProviderStrategy: 'fallback',
providers: [
{
type: 'custom',
id: 'my-socket-sender',
send: async () => {
socketCalled = true
return 'custom-socket-id'
}
}
]
}
}
})
const result = await sdk.send(request)

expect(socketCalled).toBe(true)
expect(result).toEqual({
status: 'success',
channels: {
socket: {
id: 'custom-socket-id',
providerId: 'my-socket-sender'
}
}
})
})
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const CHANNELS = {
}
export type ChannelType = $Keys<typeof CHANNELS>

export type NotificationRequestType = {|
export type NotificationRequestType = {
metadata?: {
id?: string,
userId?: string
Expand All @@ -37,7 +37,7 @@ export type NotificationRequestType = {|
webpush?: WebpushRequestType,
slack?: SlackRequestType
// TODO?: other channels (slack, messenger, skype, telegram, kik, spark...)
|}
}

export type NotificationStatusType = {
status: 'success' | 'error',
Expand Down Expand Up @@ -90,7 +90,7 @@ export default class NotifmeSdk {
const providers = providerFactory(mergedOptions.channels)
const strategies = strategyProvidersFactory(mergedOptions.channels)

this.sender = new Sender(Object.keys(CHANNELS), providers, strategies)
this.sender = new Sender([...(Object.keys(CHANNELS)), ...(Object.keys(providers))], providers, strategies)
}

mergeWithDefaultConfig ({ channels, ...rest }: OptionsType) {
Expand All @@ -100,6 +100,7 @@ export default class NotifmeSdk {
channels: rest.useNotificationCatcher
? NotificationCatcherProvider.getConfig(Object.keys(CHANNELS))
: {
...channels,
email: {
providers: [],
multiProviderStrategy: 'fallback',
Expand Down
4 changes: 4 additions & 0 deletions src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export default function factory (channels: ChannelOptionsType): ProvidersType {

case 'slack':
return slackFactory(config)

default: {
return config
}
}
})

Expand Down

0 comments on commit 0587c7f

Please sign in to comment.