Skip to content

Commit

Permalink
feat: add disconnect event to webhook extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskbx committed Apr 7, 2021
1 parent d907354 commit 81679dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 13 additions & 0 deletions demos/server/src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ const receiver = createServer((request, response) => {
response.writeHead(403, 'unathorized')
return response.end()
}

response.writeHead(200, { 'Content-Type': 'application/json' })
return response.end(JSON.stringify({
user: {
id: 1,
name: 'John',
},
}))
}

if (request.url === '/disconnect') {
// @ts-ignore
console.log(`[${new Date().toISOString()}] RECEIVER - user ${data.context.user.name} disconnected from ${data.documentName}`)
}

if (request.url === '/create') {
Expand Down
15 changes: 13 additions & 2 deletions packages/webhook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,30 @@ export class Webhook implements Extension {
}

try {
await this.sendRequest(this.getRequestUrl(Events.Connect), {
const response = <AxiosResponse> await this.sendRequest(this.getRequestUrl(Events.Connect), {
documentName: data.documentName,
requestHeaders: data.requestHeaders,
requestParameters: Object.fromEntries(data.requestParameters.entries()),
})

return typeof response.data === 'string'
? JSON.parse(response.data)
: response.data
} catch (e) {
// eslint-disable-next-line no-throw-literal
throw null
}
}

// eslint-disable-next-line @typescript-eslint/no-empty-function,no-empty-function
async onDisconnect(data: onDisconnectPayload) {
if (!this.configuration.events.includes(Events.Connect)) {
return
}

await this.sendRequest(this.getRequestUrl(Events.Disconnect), {
documentName: data.documentName,
context: data.context,
})
}

// eslint-disable-next-line @typescript-eslint/no-empty-function,no-empty-function
Expand Down

0 comments on commit 81679dd

Please sign in to comment.