Skip to content

Commit

Permalink
fix: review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns committed Oct 10, 2023
1 parent 93e6466 commit 1f6ccb5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
54 changes: 24 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import generateDocs from './lib/docs.js'
import errorLogger from './middlewares/errorLogger.js'
import validateConnection from './middlewares/validateConnection.js'
import { initializeConfigs } from './lib/configs.js'
import { getParsedAsyncAPI } from './lib/asyncapiFile.js'
import { getChannelNames, getParsedAsyncAPI } from './lib/asyncapiFile.js'
import { getSelectedServerNames } from './lib/servers.js'
import { EnrichedEvent, AuthEvent } from './lib/adapter.js'
import { ClusterEvent } from './lib/cluster.js'
Expand Down Expand Up @@ -59,7 +59,7 @@ export default async function GleeAppInitializer() {
await registerAuth(GLEE_AUTH_DIR)

const parsedAsyncAPI = await getParsedAsyncAPI()
const channelNames = parsedAsyncAPI.channels().all().map(e => e.address())
const channelNames = getChannelNames(parsedAsyncAPI)

const app = new Glee(config)

Expand All @@ -79,34 +79,29 @@ export default async function GleeAppInitializer() {

channelNames.forEach((channelName) => {
const channel = parsedAsyncAPI.channels().get(channelName)
if (channel.operations().filterByReceive().length !==0) {

channel.operations().filterByReceive().forEach(operation => {
const receiveOperation = operation
const operationId = operation.operationId()
if (operationId) {
const schema = {
onOf: receiveOperation.messages().filterByReceive().map(m => m.payload())
} as any
app.use(channelName, validate(schema), (event, next) => {
triggerFunction({
app,
operationId,
message: event
}).then(next).catch(next)
})
}
})
}
if (channel.operations().filterBySend().length !== 0) {
channel.operations().filterBySend().forEach(operation => {
const sendOperation = operation
channel.operations().filterByReceive().forEach(operation => {
const operationId = operation.operationId()

if (operationId) {
const schema = {
onOf: sendOperation.messages().filterBySend().map(m => m.payload())
oneOf: operation.messages().filterByReceive().map(m => m.payload())
} as any
app.useOutbound(channelName, validate(schema), json2string)
})
}
app.use(channelName, validate(schema), (event, next) => {
triggerFunction({
app,
operationId,
message: event
}).then(next).catch(next)
})
}
})

channel.operations().filterBySend().forEach(operation => {
const schema = {
oneOf: operation.messages().filterBySend().map(m => m.payload())
} as any
app.useOutbound(channelName, validate(schema), json2string)
})
})

app.on('adapter:auth', async (e: AuthEvent) => {
Expand Down Expand Up @@ -165,8 +160,7 @@ export default async function GleeAppInitializer() {
app.on('adapter:server:ready', async (e: EnrichedEvent) => {
logLineWithIcon(
':zap:',
`Server ${
e.serverName
`Server ${e.serverName
} is ready to accept connections on ${e.server.url()}.`,
{
highlightedWords: [e.serverName],
Expand Down
9 changes: 9 additions & 0 deletions src/lib/asyncapiFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ export async function getParsedAsyncAPI(): Promise<AsyncAPIDocument> {
const {document} = await parser.parse(asyncapiFileContent)
return toAsyncAPIDocument(document)
}


export function getChannelNames(parsedAsyncAPI: AsyncAPIDocument) {
return parsedAsyncAPI.channels().all().map(e => e.id())
}

export function getChannelAddress(parsedAsyncAPI: AsyncAPIDocument, channelName: string) {
return parsedAsyncAPI.channels().get(channelName).address()
}

0 comments on commit 1f6ccb5

Please sign in to comment.