Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added reuse connection event #6

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@
connectionEvents(agent, config)
credentialEvents(agent, config)
proofEvents(agent, config)
reuseConnectionEvents(agent, config)
}

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