mirror of
https://github.com/formbricks/formbricks.git
synced 2026-03-19 00:23:35 -05:00
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: Matthias Nannt <mail@matthiasnannt.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import "server-only";
|
|
import { Prisma } from "@prisma/client";
|
|
import { createHash, randomBytes } from "crypto";
|
|
import { prisma } from "@formbricks/database";
|
|
import { apiKeyCache } from "@formbricks/lib/apiKey/cache";
|
|
import { validateInputs } from "@formbricks/lib/utils/validate";
|
|
import { TApiKey, TApiKeyCreateInput, ZApiKeyCreateInput } from "@formbricks/types/api-keys";
|
|
import { ZId } from "@formbricks/types/common";
|
|
import { DatabaseError } from "@formbricks/types/errors";
|
|
|
|
export const deleteApiKey = async (id: string): Promise<TApiKey | null> => {
|
|
validateInputs([id, ZId]);
|
|
|
|
try {
|
|
const deletedApiKeyData = await prisma.apiKey.delete({
|
|
where: {
|
|
id: id,
|
|
},
|
|
});
|
|
|
|
apiKeyCache.revalidate({
|
|
id: deletedApiKeyData.id,
|
|
hashedKey: deletedApiKeyData.hashedKey,
|
|
environmentId: deletedApiKeyData.environmentId,
|
|
});
|
|
|
|
return deletedApiKeyData;
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError(error.message);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const hashApiKey = (key: string): string => createHash("sha256").update(key).digest("hex");
|
|
|
|
export const createApiKey = async (
|
|
environmentId: string,
|
|
apiKeyData: TApiKeyCreateInput
|
|
): Promise<TApiKey> => {
|
|
validateInputs([environmentId, ZId], [apiKeyData, ZApiKeyCreateInput]);
|
|
try {
|
|
const key = randomBytes(16).toString("hex");
|
|
const hashedKey = hashApiKey(key);
|
|
|
|
const result = await prisma.apiKey.create({
|
|
data: {
|
|
...apiKeyData,
|
|
hashedKey,
|
|
environment: { connect: { id: environmentId } },
|
|
},
|
|
});
|
|
|
|
apiKeyCache.revalidate({
|
|
id: result.id,
|
|
hashedKey: result.hashedKey,
|
|
environmentId: result.environmentId,
|
|
});
|
|
|
|
return { ...result, apiKey: key };
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
throw new DatabaseError(error.message);
|
|
}
|
|
throw error;
|
|
}
|
|
};
|