Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(ee): add limits swr and api route

Marc Seitz5f4df9e1e049
diff --git a/ee/limits/handler.ts b/ee/limits/handler.ts
new file mode 100644
index 00000000..8305df83
--- /dev/null
+++ b/ee/limits/handler.ts
@@ -0,0 +1,32 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import { getServerSession } from "next-auth";
+import { CustomUser } from "@/lib/types";
+import { getLimits } from "@/ee/limits/server";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "GET") {
+ // GET /api/:teamId/limits
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const { teamId } = req.query as { teamId: string };
+ const userId = (session.user as CustomUser).id;
+
+ try {
+ const limits = await getLimits({ teamId, userId });
+
+ return res.status(200).json(limits);
+ } catch (error) {
+ return res.status(500).json((error as Error).message);
+ }
+ } else {
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}
diff --git a/ee/limits/swr-handler.ts b/ee/limits/swr-handler.ts
new file mode 100644
index 00000000..1db08d16
--- /dev/null
+++ b/ee/limits/swr-handler.ts
@@ -0,0 +1,30 @@
+import { useTeam } from "@/context/team-context";
+import useSWR from "swr";
+import { fetcher } from "@/lib/utils";
+
+export type LimitProps = {
+ datarooms: number;
+ users: number;
+ domains: number;
+ customDomainOnPro: boolean;
+ customDomainInDataroom: boolean;
+};
+
+export function useLimits() {
+ const teamInfo = useTeam();
+ const teamId = teamInfo?.currentTeam?.id;
+
+ const { data, error } = useSWR<LimitProps | null>(
+ teamId && `/api/teams/${teamId}/limits`,
+ fetcher,
+ {
+ dedupingInterval: 30000,
+ },
+ );
+
+ return {
+ limits: data,
+ error,
+ loading: !data && !error,
+ };
+}
diff --git a/lib/swr/use-limits.ts b/lib/swr/use-limits.ts
new file mode 100644
index 00000000..964d7b53
--- /dev/null
+++ b/lib/swr/use-limits.ts
@@ -0,0 +1,3 @@
+import { useLimits } from "@/ee/limits/swr-handler";
+
+export default useLimits;
diff --git a/pages/api/teams/[teamId]/limits.ts b/pages/api/teams/[teamId]/limits.ts
new file mode 100644
index 00000000..4d076eae
--- /dev/null
+++ b/pages/api/teams/[teamId]/limits.ts
@@ -0,0 +1,3 @@
+import limitsHandler from "@/ee/limits/handler";
+
+export default limitsHandler;