Skip to content

Commit

Permalink
feat: send/publish are marked async rather than returning a function …
Browse files Browse the repository at this point in the history
…that returns a promise
  • Loading branch information
patrickleet committed Oct 25, 2021
1 parent b33e61d commit a95826e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
18 changes: 9 additions & 9 deletions __tests__/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ describe('knativebus', () => {
expect(axios).not.toBeCalled()
})

it('throws an error if aggregate has no config', () => {
it('throws an error if aggregate has no config', async () => {
const bus = knativebus(testBusConfig)
const command = 'turtle.crawl'
const data = { id: 1 }

try {
bus.send(command, data)
await bus.send(command, data)
} catch (err) {
expect(err).toEqual(new Error('"turtle" has not been configured'))
}
Expand Down Expand Up @@ -170,34 +170,34 @@ describe('knativebus', () => {
const data = { id: 1 }

try {
bus.publish(event, data)
await bus.publish(event, data)
} catch (e) {
expect(e).toEqual(new Error('"example" events broker url has not been configured'))
}
expect(axios).not.toBeCalled()
})

it('throws an error if aggregate has no config', () => {
it('throws an error if aggregate has no config', async () => {
const bus = knativebus(testBusConfig)
const event = 'turtle.crawled'
const data = { id: 1 }

try {
bus.publish(event, data)
await bus.publish(event, data)
} catch (err) {
expect(err).toEqual(new Error('"turtle" has not been configured'))
}
})

it('publish', () => {
it('publish', async () => {
const bus = knativebus(testBusConfig)

const axios = require('axios')

const event = 'example.event-happened'
const data = { id: 1 }

bus.publish(event, data)
await bus.publish(event, data)
expect(axios).toBeCalledWith({
method: 'post',
url: testBusConfig.aggregates.example.events,
Expand All @@ -206,7 +206,7 @@ describe('knativebus', () => {
}, { timeout: 0 })
})

it('publish with retry', () => {
it('publish with retry', async () => {
const bus = knativebus({
...testBusConfig,
retry: true
Expand All @@ -217,7 +217,7 @@ describe('knativebus', () => {
const event = 'example.event-happened'
const data = { id: 1 }

bus.publish(event, data)
await bus.publish(event, data)
expect(axios).toBeCalledWith({
method: 'post',
url: testBusConfig.aggregates.example.events,
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const knativebus = (config) => {
}

return {
publish: (type, data) => {
publish: async (type, data) => {
const splitType = type.split('.')

if (splitType.length < 2) throw new Error('format events as {aggregate}.{event-happened}')
Expand All @@ -48,16 +48,18 @@ export const knativebus = (config) => {

info(`Publishing CloudEvent to KNative domain events channel (${modelDomainEventsChannel}): ${JSON.stringify(ce, null, 2)}`)

return axios({
const result = await axios({
method: 'post',
url: modelDomainEventsChannel,
data: message.body,
headers: message.headers
}, {
timeout
})

return result
},
send: (type, data) => {
send: async (type, data) => {
const splitType = type.split('.')

if (splitType.length < 2) throw new Error('format commands as {aggregate}.{command}')
Expand All @@ -83,12 +85,14 @@ export const knativebus = (config) => {

info(`Sending cloud event to KNative model broker (${modelBroker}): ${JSON.stringify(ce, null, 2)}`)

return axios({
const result = await axios({
method: 'post',
url: modelBroker,
data: message.body,
headers: message.headers
}, { timeout })

return result
}
}
}

0 comments on commit a95826e

Please sign in to comment.