Files
calendar/apps/web/lib/hooks/settings/platform/oauth-clients/useOAuthClientWebhooks.ts
T
dda4b17a7c feat: Platform OAuthClient Webhooks (#16134)
* wip

* fixup! Merge branch 'main' into platform-oauth-client-webhooks

* wip

* fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! Merge branch 'platform-oauth-client-webhooks' of github.com:calcom/cal.com into platform-oauth-client-webhooks

* fixup! fixup! Merge branch 'platform-oauth-client-webhooks' of github.com:calcom/cal.com into platform-oauth-client-webhooks

---------

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2024-08-09 17:02:39 +02:00

85 lines
2.5 KiB
TypeScript

import { useMutation, useQuery } from "@tanstack/react-query";
import type { ApiSuccessResponse } from "@calcom/platform-types";
import type { WebhookTriggerEvents } from "@calcom/prisma/enums";
type Input = {
active: boolean;
subscriberUrl: string;
triggers: WebhookTriggerEvents[];
secret?: string;
payloadTemplate?: string;
};
type Output = {
id: string;
OAuthClientId: string;
active: boolean;
subscriberUrl: string;
triggers: WebhookTriggerEvents[];
secret?: string;
payloadTemplate: string | undefined | null;
};
export const useOAuthClientWebhooks = (clientId: string) => {
const query = useQuery<ApiSuccessResponse<Output[]>>({
queryKey: ["oauth-clients-webhooks", "findMany", clientId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: !!clientId,
});
return { ...query, data: query.data?.data ?? [], status: query.data?.status };
};
export const useOAuthClientWebhook = (clientId: string, webhookId: string) => {
const query = useQuery<ApiSuccessResponse<Output>>({
queryKey: ["oauth-clients-webhooks", "findOne", clientId, webhookId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks/${webhookId}`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: !!clientId,
});
return { ...query, data: query.data?.data ?? [], status: query.data?.status };
};
export const useUpdateOAuthClientWebhook = (clientId: string) => {
const mutation = useMutation<
ApiSuccessResponse<Output>,
unknown,
{ webhookId: string; body: Partial<Input> }
>({
mutationFn: ({ webhookId, body }) => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks/${webhookId}`, {
method: "PATCH",
headers: { "Content-type": "application/json" },
body: JSON.stringify(body),
}).then((res) => res.json());
},
});
return mutation;
};
export const useCreateOAuthClientWebhook = (clientId: string) => {
const mutation = useMutation<ApiSuccessResponse<Output>, unknown, Input>({
mutationFn: (body: Input) => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks`, {
method: "POST",
headers: { "Content-type": "application/json" },
body: JSON.stringify(body),
}).then((res) => res.json());
},
});
return mutation;
};