fix: cached functions return dates as strings (#2541)

Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
This commit is contained in:
Matti Nannt
2024-04-26 17:18:10 +02:00
committed by GitHub
parent 5856a77064
commit 15e6e60b62
45 changed files with 6751 additions and 13960 deletions
+3 -4
View File
@@ -1,16 +1,15 @@
import "server-only";
import { unstable_cache } from "next/cache";
import { ZId } from "@formbricks/types/environment";
import { cache } from "../cache";
import { hasUserEnvironmentAccess } from "../environment/auth";
import { validateInputs } from "../utils/validate";
import { apiKeyCache } from "./cache";
import { getApiKey } from "./service";
export const canUserAccessApiKey = async (userId: string, apiKeyId: string): Promise<boolean> =>
await unstable_cache(
export const canUserAccessApiKey = (userId: string, apiKeyId: string): Promise<boolean> =>
cache(
async () => {
validateInputs([userId, ZId], [apiKeyId, ZId]);
+8 -14
View File
@@ -2,22 +2,21 @@ import "server-only";
import { Prisma } from "@prisma/client";
import { createHash, randomBytes } from "crypto";
import { unstable_cache } from "next/cache";
import { prisma } from "@formbricks/database";
import { TApiKey, TApiKeyCreateInput, ZApiKey, ZApiKeyCreateInput } from "@formbricks/types/apiKeys";
import { TApiKey, TApiKeyCreateInput, ZApiKeyCreateInput } from "@formbricks/types/apiKeys";
import { ZOptionalNumber, ZString } from "@formbricks/types/common";
import { ZId } from "@formbricks/types/environment";
import { DatabaseError, InvalidInputError } from "@formbricks/types/errors";
import { cache } from "../cache";
import { ITEMS_PER_PAGE } from "../constants";
import { getHash } from "../crypto";
import { formatDateFields } from "../utils/datetime";
import { validateInputs } from "../utils/validate";
import { apiKeyCache } from "./cache";
export const getApiKey = async (apiKeyId: string): Promise<TApiKey | null> => {
const apiKey = await unstable_cache(
export const getApiKey = (apiKeyId: string): Promise<TApiKey | null> =>
cache(
async () => {
validateInputs([apiKeyId, ZString]);
@@ -46,11 +45,9 @@ export const getApiKey = async (apiKeyId: string): Promise<TApiKey | null> => {
tags: [apiKeyCache.tag.byId(apiKeyId)],
}
)();
return apiKey ? formatDateFields(apiKey, ZApiKey) : null;
};
export const getApiKeys = async (environmentId: string, page?: number): Promise<TApiKey[]> => {
const apiKeys = await unstable_cache(
export const getApiKeys = (environmentId: string, page?: number): Promise<TApiKey[]> =>
cache(
async () => {
validateInputs([environmentId, ZId], [page, ZOptionalNumber]);
@@ -76,8 +73,6 @@ export const getApiKeys = async (environmentId: string, page?: number): Promise<
tags: [apiKeyCache.tag.byEnvironmentId(environmentId)],
}
)();
return apiKeys.map((apiKey) => formatDateFields(apiKey, ZApiKey));
};
export const hashApiKey = (key: string): string => createHash("sha256").update(key).digest("hex");
@@ -113,10 +108,10 @@ export const createApiKey = async (
}
};
export const getApiKeyFromKey = async (apiKey: string): Promise<TApiKey | null> => {
export const getApiKeyFromKey = (apiKey: string): Promise<TApiKey | null> => {
const hashedKey = getHash(apiKey);
const apiKeyData = await unstable_cache(
return cache(
async () => {
validateInputs([apiKey, ZString]);
@@ -145,7 +140,6 @@ export const getApiKeyFromKey = async (apiKey: string): Promise<TApiKey | null>
tags: [apiKeyCache.tag.byHashedKey(hashedKey)],
}
)();
return apiKeyData ? formatDateFields(apiKeyData, ZApiKey) : null;
};
export const deleteApiKey = async (id: string): Promise<TApiKey | null> => {