Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Extract @rallly/api-core package (RAL-1172) (#2366)

Luke Vella2042ec808957
diff --git a/apps/web/package.json b/apps/web/package.json
index a997aa59e..2db269fad 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -36,6 +36,7 @@
"@radix-ui/react-select": "^2.2.2",
"@radix-ui/react-slot": "^1.1.2",
"@radix-ui/react-switch": "^1.0.2",
+ "@rallly/api-core": "workspace:*",
"@rallly/billing": "workspace:*",
"@rallly/database": "workspace:*",
"@rallly/emails": "workspace:*",
diff --git a/apps/web/src/app/api/private/[...route]/route.test.ts b/apps/web/src/app/api/private/[...route]/route.test.ts
index 0c4b0ec83..ebe2d14b4 100644
--- a/apps/web/src/app/api/private/[...route]/route.test.ts
+++ b/apps/web/src/app/api/private/[...route]/route.test.ts
@@ -8,12 +8,12 @@ const mockCreatePoll = vi.fn();
const mockGetPollResults = vi.fn();
const mockGetPollParticipants = vi.fn();
-vi.mock("@/features/poll/mutations", () => ({
+vi.mock("@rallly/api-core/polls/mutations", () => ({
deletePoll: (...args: unknown[]) => mockDeletePoll(...args),
createPoll: (...args: unknown[]) => mockCreatePoll(...args),
}));
-vi.mock("@/features/poll/data", () => ({
+vi.mock("@rallly/api-core/polls/data", () => ({
getPollResults: (...args: unknown[]) => mockGetPollResults(...args),
getPollParticipants: (...args: unknown[]) => mockGetPollParticipants(...args),
}));
diff --git a/apps/web/src/app/api/private/[...route]/route.ts b/apps/web/src/app/api/private/[...route]/route.ts
index 766710889..127a82651 100644
--- a/apps/web/src/app/api/private/[...route]/route.ts
+++ b/apps/web/src/app/api/private/[...route]/route.ts
@@ -1,4 +1,9 @@
import { RedisStore } from "@hono-rate-limiter/redis";
+import {
+ getPollParticipants,
+ getPollResults,
+} from "@rallly/api-core/polls/data";
+import { createPoll, deletePoll } from "@rallly/api-core/polls/mutations";
import { prisma } from "@rallly/database";
import { absoluteUrl, shortUrl } from "@rallly/utils/absolute-url";
import { Scalar } from "@scalar/hono-api-reference";
@@ -11,8 +16,6 @@ import {
validator,
} from "hono-openapi";
import { rateLimiter } from "hono-rate-limiter";
-import { getPollParticipants, getPollResults } from "@/features/poll/data";
-import { createPoll, deletePoll } from "@/features/poll/mutations";
import { redis } from "@/lib/kv";
import {
createPollInputSchema,
@@ -210,8 +213,8 @@ app.post(
startTime: option.startTime.toISOString(),
duration: option.duration,
})),
- adminUrl: poll.adminUrl,
- inviteUrl: poll.inviteUrl,
+ adminUrl: absoluteUrl(`/poll/${poll.id}`),
+ inviteUrl: shortUrl(`/invite/${poll.id}`),
},
});
}
@@ -301,8 +304,8 @@ app.post(
startTime: option.startTime.toISOString(),
duration: option.duration,
})),
- adminUrl: poll.adminUrl,
- inviteUrl: poll.inviteUrl,
+ adminUrl: absoluteUrl(`/poll/${poll.id}`),
+ inviteUrl: shortUrl(`/invite/${poll.id}`),
},
});
},
diff --git a/apps/web/src/app/api/private/utils/api-key.ts b/apps/web/src/app/api/private/utils/api-key.ts
index 2efc0fe3a..fde418ada 100644
--- a/apps/web/src/app/api/private/utils/api-key.ts
+++ b/apps/web/src/app/api/private/utils/api-key.ts
@@ -1,8 +1,11 @@
import crypto from "node:crypto";
+import {
+ extractApiKeyPrefix,
+ verifyApiKey,
+} from "@rallly/api-core/auth/api-key";
import { prisma } from "@rallly/database";
import { bearerAuth } from "hono/bearer-auth";
import { after } from "next/server";
-import { extractApiKeyPrefix, verifyApiKey } from "@/features/developer/utils";
export const spaceApiKeyAuth = bearerAuth({
verifyToken: async (rawKey, c) => {
diff --git a/apps/web/src/trpc/routers/api-keys.ts b/apps/web/src/trpc/routers/api-keys.ts
index 42c72214b..37a513908 100644
--- a/apps/web/src/trpc/routers/api-keys.ts
+++ b/apps/web/src/trpc/routers/api-keys.ts
@@ -1,8 +1,8 @@
+import { createApiKey } from "@rallly/api-core/auth/api-key";
import { prisma } from "@rallly/database";
import { TRPCError } from "@trpc/server";
import * as z from "zod";
import { isApiAccessEnabled } from "@/features/developer/data";
-import { createApiKey } from "@/features/developer/utils";
import { router, spaceOwnerProcedure } from "../trpc";
const apiAccessProcedure = spaceOwnerProcedure.use(async ({ ctx, next }) => {
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index bbc115e7e..e71167aaa 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -1,3 +1,4 @@
+import { getPolls } from "@rallly/api-core/polls/data";
import type { PollStatus } from "@rallly/database";
import { prisma } from "@rallly/database";
import { absoluteUrl, shortUrl } from "@rallly/utils/absolute-url";
@@ -7,7 +8,6 @@ import { after } from "next/server";
import * as z from "zod";
import { posthog } from "@/features/analytics/posthog";
import { moderateContent } from "@/features/moderation";
-import { getPolls } from "@/features/poll/data";
import { canUserManagePoll } from "@/features/poll/helpers";
import { hasPollAdminAccess } from "@/features/poll/query";
import { formatEventDateTime } from "@/features/scheduled-event/utils";
@@ -62,7 +62,10 @@ export const polls = router({
}
return {
- polls: result.polls,
+ polls: result.polls.map((poll) => ({
+ ...poll,
+ inviteLink: shortUrl(`/invite/${poll.id}`),
+ })),
nextCursor,
hasNextPage: result.hasNextPage,
total: result.total,
diff --git a/packages/api-core/biome.json b/packages/api-core/biome.json
new file mode 100644
index 000000000..c4a672504
--- /dev/null
+++ b/packages/api-core/biome.json
@@ -0,0 +1,4 @@
+{
+ "$schema": "https://biomejs.dev/schemas/2.3.14/schema.json",
+ "extends": "//"
+}
diff --git a/packages/api-core/package.json b/packages/api-core/package.json
new file mode 100644
index 000000000..4a1d704ff
--- /dev/null
+++ b/packages/api-core/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "@rallly/api-core",
+ "version": "0.0.0",
+ "private": true,
+ "scripts": {
+ "test:unit": "vitest run",
+ "type-check": "tsc --noEmit"
+ },
+ "exports": {
+ "./*": "./src/*.ts"
+ },
+ "dependencies": {
+ "@rallly/database": "workspace:*",
+ "@rallly/logger": "workspace:*",
+ "@rallly/utils": "workspace:*"
+ },
+ "devDependencies": {
+ "@rallly/tsconfig": "workspace:*",
+ "@types/node": "^24.1.0",
+ "vitest": "^4.0.16"
+ }
+}
diff --git a/apps/web/src/features/developer/utils.test.ts b/packages/api-core/src/auth/api-key.test.ts
similarity index 99%
rename from apps/web/src/features/developer/utils.test.ts
rename to packages/api-core/src/auth/api-key.test.ts
index f0fd6292c..c5e89fa1a 100644
--- a/apps/web/src/features/developer/utils.test.ts
+++ b/packages/api-core/src/auth/api-key.test.ts
@@ -4,7 +4,7 @@ import {
extractApiKeyPrefix,
randomToken,
verifyApiKey,
-} from "./utils";
+} from "./api-key";
describe("randomToken", () => {
it("should generate tokens of correct length", () => {
diff --git a/apps/web/src/features/developer/utils.ts b/packages/api-core/src/auth/api-key.ts
similarity index 100%
rename from apps/web/src/features/developer/utils.ts
rename to packages/api-core/src/auth/api-key.ts
diff --git a/apps/web/src/features/poll/data.ts b/packages/api-core/src/polls/data.ts
similarity index 98%
rename from apps/web/src/features/poll/data.ts
rename to packages/api-core/src/polls/data.ts
index 63dc33d3c..dada0b478 100644
--- a/apps/web/src/features/poll/data.ts
+++ b/packages/api-core/src/polls/data.ts
@@ -1,6 +1,5 @@
import type { PollStatus, Prisma } from "@rallly/database";
import { prisma } from "@rallly/database";
-import { shortUrl } from "@rallly/utils/absolute-url";
export async function getPollResults({
pollId,
@@ -231,7 +230,6 @@ export const getPolls = async ({
name: participant.name,
image: participant.user?.image ?? undefined,
})),
- inviteLink: shortUrl(`/invite/${poll.id}`),
}));
const totalPages = Math.ceil(totalCount / pageSize);
diff --git a/apps/web/src/features/poll/mutations.ts b/packages/api-core/src/polls/mutations.ts
similarity index 91%
rename from apps/web/src/features/poll/mutations.ts
rename to packages/api-core/src/polls/mutations.ts
index 701ba8f0c..57f1d22e3 100644
--- a/apps/web/src/features/poll/mutations.ts
+++ b/packages/api-core/src/polls/mutations.ts
@@ -1,5 +1,4 @@
import { prisma } from "@rallly/database";
-import { absoluteUrl, shortUrl } from "@rallly/utils/absolute-url";
import { nanoid } from "@rallly/utils/nanoid";
export type PollOption = {
@@ -78,11 +77,7 @@ export const createPoll = async ({
},
});
- return {
- ...poll,
- adminUrl: absoluteUrl(`/poll/${poll.id}`),
- inviteUrl: shortUrl(`/invite/${poll.id}`),
- };
+ return poll;
};
export const deletePoll = async (pollId: string, spaceId: string) => {
diff --git a/packages/api-core/tsconfig.json b/packages/api-core/tsconfig.json
new file mode 100644
index 000000000..4959f2f61
--- /dev/null
+++ b/packages/api-core/tsconfig.json
@@ -0,0 +1,8 @@
+{
+ "extends": "@rallly/tsconfig/node.json",
+ "compilerOptions": {
+ "types": ["vitest/globals", "node"],
+ "lib": ["es2020"]
+ },
+ "include": ["**/*.ts"]
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index aaafef9e1..4c40c1def 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -241,6 +241,9 @@ importers:
'@radix-ui/react-switch':
specifier: ^1.0.2
+ '@rallly/api-core':
+ specifier: workspace:*
+ version: link:../../packages/api-core
'@rallly/billing':
specifier: workspace:*
version: link:../../packages/billing
@@ -528,6 +531,28 @@ importers:
specifier: ^4.0.16
+ packages/api-core:
+ dependencies:
+ '@rallly/database':
+ specifier: workspace:*
+ version: link:../database
+ '@rallly/logger':
+ specifier: workspace:*
+ version: link:../logger
+ '@rallly/utils':
+ specifier: workspace:*
+ version: link:../utils
+ devDependencies:
+ '@rallly/tsconfig':
+ specifier: workspace:*
+ version: link:../tsconfig
+ '@types/node':
+ specifier: ^24.1.0
+ version: 24.10.11
+ vitest:
+ specifier: ^4.0.16
+
packages/billing:
dependencies:
'@radix-ui/react-radio-group':