Skip to content

Commit

Permalink
feat(server): support experimental webhook api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 15, 2024
1 parent b4843eb commit 8b8bbfb
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ class SatoriServer extends Service<SatoriServer.Config> {
koa.status = 200
})

ctx.server.post(path + '/v1/admin/webhook.create', async (koa) => {
if (checkAuth(koa)) return
const webhook: SatoriServer.Webhook = transformKey(koa.request.body, camelCase)
const index = config.webhooks.findIndex(({ url }) => url === webhook.url)
if (index === -1) {
config.webhooks.push(webhook)
ctx.scope.update(config, false)
}
koa.body = {}
koa.status = 200
})

ctx.server.post(path + '/v1/admin/webhook.delete', async (koa) => {
if (checkAuth(koa)) return
const url = koa.request.body.url
const index = config.webhooks.findIndex(webhook => webhook.url === url)
if (index !== -1) {
config.webhooks.splice(index, 1)
ctx.scope.update(config, false)
}
koa.body = {}
koa.status = 200
})

const buffer: Session[] = []

const timeout = setInterval(() => {
Expand Down Expand Up @@ -236,7 +260,7 @@ class SatoriServer extends Service<SatoriServer.Config> {
}
for (const webhook of config.webhooks) {
if (!webhook.enabled) continue
ctx.http.post(webhook.endpoint, body, {
ctx.http.post(webhook.url, body, {
headers: webhook.token ? {
Authorization: `Bearer ${webhook.token}`,
} : {},
Expand All @@ -262,13 +286,13 @@ namespace SatoriServer {

export interface Webhook {
enabled?: boolean
endpoint: string
url: string
token?: string
}

export const Webhook: Schema<Webhook> = Schema.object({
enabled: Schema.boolean().default(true),
endpoint: Schema.string(),
url: Schema.string(),
token: Schema.string(),
})

Expand Down

0 comments on commit 8b8bbfb

Please sign in to comment.