mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
ba6a87b699
* refactor(companion): split calcom.ts into modular service files Split the monolithic 1692-line calcom.ts file into focused modules: - auth.ts: Authentication configuration and token management - bookings.ts: Booking CRUD operations and actions - conferencing.ts: Conferencing options - event-types.ts: Event type CRUD operations - private-links.ts: Private link management for event types - request.ts: Core HTTP request functionality - schedules.ts: Schedule CRUD operations - user.ts: User profile management - utils.ts: JSON parsing and payload sanitization utilities - webhooks.ts: Webhook management (global and event type specific) - index.ts: Re-exports all functions and maintains backward compatibility This improves code organization, maintainability, and makes it easier to understand and modify specific functionality. Co-Authored-By: [email protected] <[email protected]> * fix(companion): avoid logging sensitive booking response data Replace logging of full responseText (which may contain PII like attendee emails and names) with responseLength for safer debugging. Addresses Cubic AI review feedback (confidence 9/10). Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
197 lines
4.6 KiB
TypeScript
197 lines
4.6 KiB
TypeScript
/**
|
|
* Webhook functions for Cal.com API
|
|
*/
|
|
|
|
import type { CreateWebhookInput, UpdateWebhookInput, Webhook } from "../types";
|
|
|
|
import { makeRequest } from "./request";
|
|
import { sanitizePayload } from "./utils";
|
|
|
|
/**
|
|
* Get all global webhooks
|
|
*/
|
|
export async function getWebhooks(): Promise<Webhook[]> {
|
|
try {
|
|
const response = await makeRequest<{ status: string; data: Webhook[] }>("/webhooks");
|
|
|
|
if (response?.data && Array.isArray(response.data)) {
|
|
return response.data;
|
|
}
|
|
|
|
return [];
|
|
} catch (error) {
|
|
console.error("getWebhooks error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a global webhook
|
|
*/
|
|
export async function createWebhook(input: CreateWebhookInput): Promise<Webhook> {
|
|
try {
|
|
const sanitizedInput = sanitizePayload(input as unknown as Record<string, unknown>);
|
|
const response = await makeRequest<{ status: string; data: Webhook }>("/webhooks", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(sanitizedInput),
|
|
});
|
|
|
|
if (response?.data) {
|
|
return response.data;
|
|
}
|
|
|
|
throw new Error("Invalid response from create webhook API");
|
|
} catch (error) {
|
|
console.error("createWebhook error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update a global webhook
|
|
*/
|
|
export async function updateWebhook(
|
|
webhookId: string,
|
|
updates: UpdateWebhookInput
|
|
): Promise<Webhook> {
|
|
try {
|
|
const sanitizedUpdates = sanitizePayload(updates as Record<string, unknown>);
|
|
const response = await makeRequest<{ status: string; data: Webhook }>(
|
|
`/webhooks/${webhookId}`,
|
|
{
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(sanitizedUpdates),
|
|
}
|
|
);
|
|
|
|
if (response?.data) {
|
|
return response.data;
|
|
}
|
|
|
|
throw new Error("Invalid response from update webhook API");
|
|
} catch (error) {
|
|
console.error("updateWebhook error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a global webhook
|
|
*/
|
|
export async function deleteWebhook(webhookId: string): Promise<void> {
|
|
try {
|
|
await makeRequest(`/webhooks/${webhookId}`, {
|
|
method: "DELETE",
|
|
});
|
|
} catch (error) {
|
|
console.error("deleteWebhook error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get webhooks for a specific event type
|
|
*/
|
|
export async function getEventTypeWebhooks(eventTypeId: number): Promise<Webhook[]> {
|
|
try {
|
|
const response = await makeRequest<{ status: string; data: Webhook[] }>(
|
|
`/event-types/${eventTypeId}/webhooks`
|
|
);
|
|
|
|
if (response?.data && Array.isArray(response.data)) {
|
|
return response.data;
|
|
}
|
|
|
|
return [];
|
|
} catch (error) {
|
|
console.error("getEventTypeWebhooks error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a webhook for a specific event type
|
|
*/
|
|
export async function createEventTypeWebhook(
|
|
eventTypeId: number,
|
|
input: CreateWebhookInput
|
|
): Promise<Webhook> {
|
|
try {
|
|
const sanitizedInput = sanitizePayload(input as unknown as Record<string, unknown>);
|
|
const response = await makeRequest<{ status: string; data: Webhook }>(
|
|
`/event-types/${eventTypeId}/webhooks`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(sanitizedInput),
|
|
}
|
|
);
|
|
|
|
if (response?.data) {
|
|
return response.data;
|
|
}
|
|
|
|
throw new Error("Invalid response from create event type webhook API");
|
|
} catch (error) {
|
|
console.error("createEventTypeWebhook error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update an event type webhook
|
|
*/
|
|
export async function updateEventTypeWebhook(
|
|
eventTypeId: number,
|
|
webhookId: string,
|
|
updates: UpdateWebhookInput
|
|
): Promise<Webhook> {
|
|
try {
|
|
const sanitizedUpdates = sanitizePayload(updates as Record<string, unknown>);
|
|
const response = await makeRequest<{ status: string; data: Webhook }>(
|
|
`/event-types/${eventTypeId}/webhooks/${webhookId}`,
|
|
{
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(sanitizedUpdates),
|
|
}
|
|
);
|
|
|
|
if (response?.data) {
|
|
return response.data;
|
|
}
|
|
|
|
throw new Error("Invalid response from update event type webhook API");
|
|
} catch (error) {
|
|
console.error("updateEventTypeWebhook error");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete an event type webhook
|
|
*/
|
|
export async function deleteEventTypeWebhook(
|
|
eventTypeId: number,
|
|
webhookId: string
|
|
): Promise<void> {
|
|
try {
|
|
await makeRequest(`/event-types/${eventTypeId}/webhooks/${webhookId}`, {
|
|
method: "DELETE",
|
|
});
|
|
} catch (error) {
|
|
console.error("deleteEventTypeWebhook error");
|
|
throw error;
|
|
}
|
|
}
|