Skip to content

Commit e6497c8

Browse files
authored
perf: use repository for apiKeys list query & caching in /settings/developer/api-keys RSC (calcom#21776)
1 parent cbb0209 commit e6497c8

4 files changed

Lines changed: 50 additions & 23 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use server";
22

3-
import { revalidatePath } from "next/cache";
3+
import { revalidateTag } from "next/cache";
44

55
export async function revalidateApiKeysList() {
6-
revalidatePath("/settings/developer/api-keys");
6+
revalidateTag("viewer.apiKeys.list");
77
}

apps/web/app/(use-page-wrapper)/settings/(settings-layout)/developer/api-keys/page.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { createRouterCaller } from "app/_trpc/context";
21
import { _generateMetadata } from "app/_utils";
2+
import { unstable_cache } from "next/cache";
3+
import { cookies, headers } from "next/headers";
4+
import { redirect } from "next/navigation";
35

6+
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
47
import { APP_NAME } from "@calcom/lib/constants";
5-
import { apiKeysRouter } from "@calcom/trpc/server/routers/viewer/apiKeys/_router";
8+
import { ApiKeyRepository } from "@calcom/lib/server/repository/apiKey";
9+
10+
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
611

712
import ApiKeysView from "~/settings/developer/api-keys-view";
813

@@ -15,9 +20,24 @@ export const generateMetadata = async () =>
1520
"/settings/developer/api-keys"
1621
);
1722

23+
const getCachedApiKeys = unstable_cache(
24+
async (userId: number) => {
25+
return await ApiKeyRepository.findApiKeysFromUserId({ userId });
26+
},
27+
undefined,
28+
{ revalidate: 3600, tags: ["viewer.apiKeys.list"] } // Cache for 1 hour
29+
);
30+
1831
const Page = async () => {
19-
const caller = await createRouterCaller(apiKeysRouter);
20-
const apiKeys = await caller.list();
32+
const session = await getServerSession({ req: buildLegacyRequest(await headers(), await cookies()) });
33+
34+
if (!session) {
35+
redirect("/auth/login?callbackUrl=/settings/developer/api-keys");
36+
}
37+
38+
const userId = session.user.id;
39+
const apiKeys = await getCachedApiKeys(userId);
40+
2141
return <ApiKeysView apiKeys={apiKeys} />;
2242
};
2343

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import prisma from "@calcom/prisma";
2+
3+
export class ApiKeyRepository {
4+
static async findApiKeysFromUserId({ userId }: { userId: number }) {
5+
return await prisma.apiKey.findMany({
6+
where: {
7+
userId,
8+
OR: [
9+
{
10+
NOT: {
11+
appId: "zapier",
12+
},
13+
},
14+
{
15+
appId: null,
16+
},
17+
],
18+
},
19+
orderBy: { createdAt: "desc" },
20+
});
21+
}
22+
}
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import prisma from "@calcom/prisma";
1+
import { ApiKeyRepository } from "@calcom/lib/server/repository/apiKey";
22

33
import type { TrpcSessionUser } from "../../../types";
44

@@ -9,20 +9,5 @@ type ListOptions = {
99
};
1010

1111
export const listHandler = async ({ ctx }: ListOptions) => {
12-
return await prisma.apiKey.findMany({
13-
where: {
14-
userId: ctx.user.id,
15-
OR: [
16-
{
17-
NOT: {
18-
appId: "zapier",
19-
},
20-
},
21-
{
22-
appId: null,
23-
},
24-
],
25-
},
26-
orderBy: { createdAt: "desc" },
27-
});
12+
return ApiKeyRepository.findApiKeysFromUserId({ userId: ctx.user.id });
2813
};

0 commit comments

Comments
 (0)