Files
calendar/packages/features/webhooks/lib/WebhookService.ts
T
Syed Ali ShahbazandGitHub 056922b6ee feat: add webhook versioning (#23861)
* add webhook version schema

* version the code

* update version from numeric to date val

* migration

* update schema and build factory

* update string

* move version picker

* tooltip instead of infobadge

* --

* fix type

* --

* fix type

* fix type

* --

* fix messed up merge

* improvements to payloadfactory

* extract version off of DB and instead keep it in IWebhookRepository

* fix webhookform

* fix type safety and routing ambiguity

* scalable with easier factory extensions and base definition

* fix types

* --

* --

* clean up prisma/client type imports

* fix

* type fix

* type fix

* cleanup

* add tests and registry changes

* unintended file inclusion

* type-fix

* select in repo

* --

* explicit return type

* --

* fix type

* fixes

* feedback 1

* feedback 2

* use enum instead of string

* fixes
2025-12-18 09:36:22 +02:00

39 lines
1.3 KiB
TypeScript

import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import type { WebhookSubscriber } from "./dto/types";
import getWebhooks from "./getWebhooks";
import type { GetSubscriberOptions } from "./getWebhooks";
import sendOrSchedulePayload from "./sendOrSchedulePayload";
const log = logger.getSubLogger({ prefix: ["[WebhookService] "] });
/** This is a WIP. With minimal methods until the API matures and stabilizes */
export class WebhookService {
private constructor(private options: GetSubscriberOptions, private webhooks: WebhookSubscriber[]) {}
static async init(options: GetSubscriberOptions) {
const webhooks = await getWebhooks(options);
return new WebhookService(options, webhooks);
}
getWebhooks() {
return this.webhooks;
}
async sendPayload(payload: Parameters<typeof sendOrSchedulePayload>[4]) {
const promises = this.webhooks.map((sub) =>
sendOrSchedulePayload(
sub.secret,
this.options.triggerEvent,
new Date().toISOString(),
sub,
payload
).catch((e) => {
log.error(
`Error executing webhook for event: ${this.options.triggerEvent}, URL: ${sub.subscriberUrl}`,
safeStringify(e)
);
})
);
await Promise.allSettled(promises);
}
}