Skip to content

Commit

Permalink
Merge pull request #6 from Bhutan-NDI/reuse-connection
Browse files Browse the repository at this point in the history
Added reuse connection event
  • Loading branch information
ankita-p17 authored Jul 22, 2024
2 parents cf13673 + 0574734 commit e847989
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/events/ReuseConnectionEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { ServerConfig } from '../utils/ServerConfig'
import type { Agent, HandshakeReusedEvent } from '@credo-ts/core'

import { OutOfBandEventTypes } from '@credo-ts/core'

import { sendWebSocketEvent } from './WebSocketEvents'
import { sendWebhookEvent } from './WebhookEvent'

export const reuseConnectionEvents = async (agent: Agent, config: ServerConfig) => {
agent.events.on(OutOfBandEventTypes.HandshakeReused, async (event: HandshakeReusedEvent) => {
const body = {
connectionRecord: event.payload.connectionRecord.toJSON(),
outOfBandRecord: event.payload.outOfBandRecord.toJSON(),
reuseThreadId: event.payload.reuseThreadId,
...event.metadata,
}

// Only send webhook if webhook url is configured
if (config.webhookUrl) {
await sendWebhookEvent(config.webhookUrl + '/connections', body, agent.config.logger)
}

if (config.socketServer) {
// Always emit websocket event to clients (could be 0)
sendWebSocketEvent(config.socketServer, {
...event,
payload: {
...event.payload,
connectionRecord: body,
},
})
}
})
}

Check failure on line 34 in src/events/ReuseConnectionEvents.ts

View workflow job for this annotation

GitHub Actions / Validate

Insert `⏎`
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { questionAnswerEvents } from './events/QuestionAnswerEvents'
import { RegisterRoutes } from './routes/routes'
import { SecurityMiddleware } from './securityMiddleware'
import { maxRateLimit, windowMs } from './utils/util'
import { reuseConnectionEvents } from './events/ReuseConnectionEvents'

Check failure on line 23 in src/server.ts

View workflow job for this annotation

GitHub Actions / Validate

`./events/ReuseConnectionEvents` import should occur before import of `./routes/routes`

import { ValidateError, type Exception } from 'tsoa'

Expand All @@ -35,6 +36,7 @@ export const setupServer = async (agent: Agent, config: ServerConfig, apiKey?: s
connectionEvents(agent, config)
credentialEvents(agent, config)
proofEvents(agent, config)
reuseConnectionEvents(agent, config)
}

// Use body parser to read sent json payloads
Expand Down

0 comments on commit e847989

Please sign in to comment.