mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-22 02:55:04 -05:00
c52df00d39
* Fix email notifications not working properly * Fix response notification not working * fix response meta schema * fix typo in docs * improve error message in webhooks
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import { responses } from "@/lib/api/response";
|
|
import { transformErrorToDetails } from "@/lib/api/validator";
|
|
import { DatabaseError, InvalidInputError } from "@formbricks/errors";
|
|
import { getApiKeyFromKey } from "@formbricks/lib/services/apiKey";
|
|
import { createWebhook, getWebhooks } from "@formbricks/lib/services/webhook";
|
|
import { ZWebhookInput } from "@formbricks/types/v1/webhooks";
|
|
import { headers } from "next/headers";
|
|
import { NextResponse } from "next/server";
|
|
|
|
export async function GET() {
|
|
const apiKey = headers().get("x-api-key");
|
|
if (!apiKey) {
|
|
return responses.notAuthenticatedResponse();
|
|
}
|
|
const apiKeyData = await getApiKeyFromKey(apiKey);
|
|
if (!apiKeyData) {
|
|
return responses.notAuthenticatedResponse();
|
|
}
|
|
|
|
// get webhooks from database
|
|
try {
|
|
const webhooks = await getWebhooks(apiKeyData.environmentId);
|
|
return NextResponse.json({ data: webhooks });
|
|
} catch (error) {
|
|
if (error instanceof DatabaseError) {
|
|
return responses.badRequestResponse(error.message);
|
|
}
|
|
return responses.internalServerErrorResponse(error.message);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const apiKey = headers().get("x-api-key");
|
|
if (!apiKey) {
|
|
return responses.notAuthenticatedResponse();
|
|
}
|
|
const apiKeyData = await getApiKeyFromKey(apiKey);
|
|
if (!apiKeyData) {
|
|
return responses.notAuthenticatedResponse();
|
|
}
|
|
const webhookInput = await request.json();
|
|
const inputValidation = ZWebhookInput.safeParse(webhookInput);
|
|
|
|
if (!inputValidation.success) {
|
|
return responses.badRequestResponse(
|
|
"Fields are missing or incorrectly formatted",
|
|
transformErrorToDetails(inputValidation.error),
|
|
true
|
|
);
|
|
}
|
|
|
|
// add webhook to database
|
|
try {
|
|
const webhook = await createWebhook(apiKeyData.environmentId, inputValidation.data);
|
|
return responses.successResponse(webhook);
|
|
} catch (error) {
|
|
if (error instanceof InvalidInputError) {
|
|
return responses.badRequestResponse(error.message);
|
|
}
|
|
if (error instanceof DatabaseError) {
|
|
return responses.internalServerErrorResponse(error.message);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|