Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
Deploy the API through a serverless entrypoint
Lewis Carhartdeff7b4561c9
diff --git a/apps/api/api/index.ts b/apps/api/api/index.tsnew file mode 100644index 00000000..f9f1b8d4--- /dev/null+++ b/apps/api/api/index.ts@@ -0,0 +1,38 @@+import "reflect-metadata";+import type { IncomingMessage, ServerResponse } from "node:http";+import { createApp } from "../src/create-app";++type ExpressInstance = (req: IncomingMessage, res: ServerResponse) => void;++/**+ * The serverless entrypoint.+ *+ * Nest is built once per instance and reused: `instancePromise` is module+ * scope, so a warm invocation skips the whole `NestFactory.create` — module+ * resolution, the Prisma connection, route mapping — and only runs the+ * request. Storing the promise rather than the app also means two requests+ * arriving during a cold start share one boot instead of racing two.+ *+ * `app.init()` and not `app.listen()`: there is no port to bind here. Vercel+ * hands us `(req, res)` and Express takes it from there.+ */+let instancePromise: Promise<ExpressInstance> | null = null;++function getInstance(): Promise<ExpressInstance> {+ if (!instancePromise) {+ instancePromise = (async () => {+ const app = await createApp();+ await app.init();+ return app.getHttpAdapter().getInstance() as ExpressInstance;+ })();+ }+ return instancePromise;+}++export default async function handler(+ req: IncomingMessage,+ res: ServerResponse,+): Promise<void> {+ const instance = await getInstance();+ instance(req, res);+}diff --git a/apps/api/src/create-app.ts b/apps/api/src/create-app.tsnew file mode 100644index 00000000..c960a645--- /dev/null+++ b/apps/api/src/create-app.ts@@ -0,0 +1,37 @@+import { ValidationPipe } from "@nestjs/common";+import { NestFactory } from "@nestjs/core";+import {+ ExpressAdapter,+ type NestExpressApplication,+} from "@nestjs/platform-express";+import helmet from "helmet";+import { AppModule } from "./app.module";+import { ContextLogger } from "./logging/context-logger";++/**+ * Builds the application without starting a listener.+ *+ * `main.ts` calls this and then listens; the serverless handler in `api/`+ * calls it and hands the Express instance each request instead. Everything+ * that shapes the app — pipes, helmet, the logger — lives here so the two+ * entrypoints cannot drift apart.+ */+export async function createApp(): Promise<NestExpressApplication> {+ const app = await NestFactory.create<NestExpressApplication>(+ AppModule,+ new ExpressAdapter(),+ { bodyParser: false, logger: new ContextLogger() },+ );++ app.use(helmet());+ app.useGlobalPipes(+ new ValidationPipe({+ whitelist: true,+ forbidNonWhitelisted: true,+ transform: true,+ transformOptions: { enableImplicitConversion: true },+ }),+ );++ return app;+}diff --git a/apps/api/src/main.ts b/apps/api/src/main.tsindex b02da4de..f6e3bead 100644--- a/apps/api/src/main.ts+++ b/apps/api/src/main.ts@@ -1,26 +1,8 @@-import { Logger, ValidationPipe } from "@nestjs/common";-import { NestFactory } from "@nestjs/core";-import helmet from "helmet";-import { AppModule } from "./app.module";-import { ContextLogger } from "./logging/context-logger";+import { Logger } from "@nestjs/common";+import { createApp } from "./create-app";async function bootstrap() {- const logger = new ContextLogger();-- const app = await NestFactory.create(AppModule, {- bodyParser: false,- logger,- });-- app.use(helmet());- app.useGlobalPipes(- new ValidationPipe({- whitelist: true,- forbidNonWhitelisted: true,- transform: true,- transformOptions: { enableImplicitConversion: true },- }),- );+ const app = await createApp();app.enableShutdownHooks();const port = process.env.PORT ?? 3001;diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.jsonindex fea922ce..8480473c 100644--- a/apps/api/tsconfig.json+++ b/apps/api/tsconfig.json@@ -2,36 +2,10 @@"$schema": "https://json.schemastore.org/tsconfig","extends": "@crm/typescript-config/nestjs.json","compilerOptions": {- "types": [- "bun"- ],+ "types": ["bun"],"experimentalDecorators": true,- "emitDecoratorMetadata": true,- "baseUrl": ".",- "paths": {- "@crm/auth": [- "../../packages/auth/src/index.ts"- ],- "@crm/auth/client": [- "../../packages/auth/src/client.ts"- ],- "@crm/db": [- "../../packages/db/src/index.ts"- ],- "@crm/db/client": [- "../../packages/db/src/client.ts"- ],- "@crm/db/enums": [- "../../packages/db/src/generated/prisma/enums.ts"- ]- }+ "emitDecoratorMetadata": true},- "include": [- "src/**/*.ts",- "test/**/*.ts"- ],- "exclude": [- "node_modules",- "dist"- ]+ "include": ["src/**/*.ts", "api/**/*.ts", "test/**/*.ts"],+ "exclude": ["node_modules", "dist"]}diff --git a/apps/api/vercel.json b/apps/api/vercel.jsonnew file mode 100644index 00000000..6ee95f18--- /dev/null+++ b/apps/api/vercel.json@@ -0,0 +1,4 @@+{+ "$schema": "https://openapi.vercel.sh/vercel.json",+ "rewrites": [{ "source": "/(.*)", "destination": "/api" }]+}