mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-08 06:41:45 -05:00
add new error classes, add types and services for api keys, use new error classes in webhook api
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import { createHash } from "crypto";
|
||||
|
||||
export const getHash = (key: string): string => createHash("sha256").update(key).digest("hex");
|
||||
@@ -12,11 +12,12 @@
|
||||
"lint:report": "eslint . --format json --output-file ../../lint-results/app-store.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@formbricks/database": "*"
|
||||
"@formbricks/database": "*",
|
||||
"@formbricks/types": "*",
|
||||
"@formbricks/errors": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@formbricks/tsconfig": "*",
|
||||
"@formbricks/types": "*",
|
||||
"eslint": "^8.41.0",
|
||||
"eslint-config-formbricks": "workspace:*",
|
||||
"typescript": "^5.0.4"
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { getHash } from "../crypto";
|
||||
import { TApiKey } from "@formbricks/types/v1/apiKeys";
|
||||
|
||||
export const getApiKey = async (apiKey: string): Promise<TApiKey | null> => {
|
||||
return await prisma.apiKey.findUnique({
|
||||
where: {
|
||||
hashedKey: getHash(apiKey),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const getApiKeyFromKey = async (apiKey: string): Promise<TApiKey | null> => {
|
||||
return await prisma.apiKey.findUnique({
|
||||
where: {
|
||||
hashedKey: getHash(apiKey),
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -47,8 +47,6 @@ export const createResponse = async (responseInput: TResponseInput): Promise<TRe
|
||||
|
||||
const response: TResponse = {
|
||||
...responsePrisma,
|
||||
createdAt: responsePrisma.createdAt.toISOString(),
|
||||
updatedAt: responsePrisma.updatedAt.toISOString(),
|
||||
person: transformPrismaPerson(responsePrisma.person),
|
||||
};
|
||||
|
||||
@@ -91,8 +89,6 @@ export const getResponse = async (responseId: string): Promise<TResponse | null>
|
||||
|
||||
const response: TResponse = {
|
||||
...responsePrisma,
|
||||
createdAt: responsePrisma.createdAt.toISOString(),
|
||||
updatedAt: responsePrisma.updatedAt.toISOString(),
|
||||
person: transformPrismaPerson(responsePrisma.person),
|
||||
};
|
||||
|
||||
@@ -150,8 +146,6 @@ export const updateResponse = async (
|
||||
|
||||
const response: TResponse = {
|
||||
...responsePrisma,
|
||||
createdAt: responsePrisma.createdAt.toISOString(),
|
||||
updatedAt: responsePrisma.updatedAt.toISOString(),
|
||||
person: transformPrismaPerson(responsePrisma.person),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { TWebhook, TWebhookInput } from "@formbricks/types/v1/webhooks";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { ResourceNotFoundError, DatabaseError, InvalidInputError } from "@formbricks/errors";
|
||||
|
||||
export const getWebhooks = async (environmentId: string): Promise<TWebhook[]> => {
|
||||
try {
|
||||
return await prisma.webhook.findMany({
|
||||
where: {
|
||||
environmentId: environmentId,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new DatabaseError(`Database error when fetching webhooks for environment ${environmentId}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const getWebhook = async (id: string): Promise<TWebhook | null> => {
|
||||
try {
|
||||
const webhook = await prisma.webhook.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
if (!webhook) {
|
||||
throw new ResourceNotFoundError("Webhook", id);
|
||||
}
|
||||
return webhook;
|
||||
} catch (error) {
|
||||
if (!(error instanceof ResourceNotFoundError)) {
|
||||
throw new DatabaseError(`Database error when fetching webhook with ID ${id}`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const createWebhook = async (
|
||||
environmentId: string,
|
||||
webhookInput: TWebhookInput
|
||||
): Promise<TWebhook> => {
|
||||
try {
|
||||
if (!webhookInput.url || !webhookInput.trigger) {
|
||||
throw new InvalidInputError("Missing URL or trigger in webhook input");
|
||||
}
|
||||
return await prisma.webhook.create({
|
||||
data: {
|
||||
url: webhookInput.url,
|
||||
triggers: [webhookInput.trigger],
|
||||
environment: {
|
||||
connect: {
|
||||
id: environmentId,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (!(error instanceof InvalidInputError)) {
|
||||
throw new DatabaseError(`Database error when creating webhook for environment ${environmentId}`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteWebhook = async (id: string): Promise<TWebhook> => {
|
||||
try {
|
||||
return await prisma.webhook.delete({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
|
||||
throw new ResourceNotFoundError("Webhook", id);
|
||||
}
|
||||
throw new DatabaseError(`Database error when deleting webhook with ID ${id}`);
|
||||
}
|
||||
};
|
||||
@@ -1,5 +1,11 @@
|
||||
{
|
||||
"extends": "@formbricks/tsconfig/js-library.json",
|
||||
"include": ["."],
|
||||
"exclude": ["dist", "build", "node_modules"]
|
||||
"exclude": ["dist", "build", "node_modules"],
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@prisma/client/*": ["@formbricks/database/client/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user