Skip to content

Commit

Permalink
feat: apps/api-trpc hosts API
Browse files Browse the repository at this point in the history
  • Loading branch information
andrao committed Aug 15, 2024
1 parent 623a9be commit 1cabb3d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
37 changes: 37 additions & 0 deletions apps/api-trpc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@acme/api-trpc",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"clean": "git clean -xdf .next .turbo node_modules",
"dev": "pnpm with-env tsx watch --clear-screen=false ./src/index.ts",
"format": "prettier --check . --ignore-path ../../.prettierignore",
"jest": "pnpm run -C ../.. jest",
"jest:i": "RUN_JEST_INTEGRATION_TESTS=1 pnpm run jest",
"lint": "eslint .",
"test": "pnpm run jest $PWD",
"test:i": "pnpm run jest:i $PWD",
"typecheck": "tsc --noEmit",
"with-env": "dotenv -e ../../.env --"
},
"dependencies": {
"@acme/api": "workspace:*",
"@acme/constants": "workspace:*",
"@acme/db": "workspace:*",
"@acme/paths": "workspace:*",
"@andrao/tools": "^1.0.0",
"@t3-oss/env-nextjs": "^0.9.2",
"@trpc/server": "next",
"cors": "^2.8.5",
"express": "^4.19.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/supertest": "^6.0.2",
"supertest": "^7.0.0",
"tsx": "^4.16.5"
}
}
43 changes: 43 additions & 0 deletions apps/api-trpc/src/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { PORTS } from '@acme/paths';
import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod';

/**
* @const env
* @description Environment variables for the NextJS app
* @see https://vercel.com/docs/projects/environment-variables/system-environment-variables for default Vercel env vars
*/
export const env = createEnv({
/**
* Server-side
*/
server: {},

/**
* Client-side
* - For them to be exposed, prefix with `NEXT_PUBLIC_`.
*/
client: {},

/**
* Env vars shared between server & client
* - Node defaults, environment
*/
shared: {
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
PORT: z.coerce.number().default(PORTS.api_trpc),
},

/**
* Destructure all variables from `process.env` to make sure they aren't tree-shaken away.
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
PORT: process.env.PORT,
},

skipValidation:
!!process.env.CI ||
!!process.env.SKIP_ENV_VALIDATION ||
process.env.npm_lifecycle_event === 'lint',
});
37 changes: 37 additions & 0 deletions apps/api-trpc/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { APP_ROUTER } from '@acme/api';
import { dbx } from '@acme/db';
import { PATHS } from '@acme/paths';
import * as trpcExpress from '@trpc/server/adapters/express';
import cors from 'cors';
import express from 'express';
import { env } from './env';

/**
* @const app
* @description Initialized express app
*/
export const app = express();

/**
* Enable CORS
* @todo This enables CORS for all routes, consider enabling only for specific routes
*/
app.use(cors({ credentials: true }));

/**
* Host tRPC router
*/
app.use(
PATHS.api.trpc,
trpcExpress.createExpressMiddleware({
router: APP_ROUTER,
createContext: ({ req }) => ({ dbx, env, headers: req.headers }),
}),
);

/**
* Start server & listen on port
*/
export const server = app.listen(env.PORT, () => {
console.log(`Server is running on port ${env.PORT}`);
});
14 changes: 14 additions & 0 deletions apps/api-trpc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "@andrao/tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"],
"@ui/*": ["../../packages/@app-browser/ui/src/*"]
},
"plugins": [{ "name": "next" }],
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["src/env.js", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
2 changes: 1 addition & 1 deletion apps/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"clean": "git clean -xdf .next .turbo node_modules",
"dev": "pnpm with-env tsx ./src/index.ts",
"dev": "pnpm with-env tsx watch --clear-screen=false ./src/index.ts",
"format": "prettier --check . --ignore-path ../../.prettierignore",
"jest": "pnpm run -C ../.. jest",
"jest:i": "RUN_JEST_INTEGRATION_TESTS=1 pnpm run jest",
Expand Down

0 comments on commit 1cabb3d

Please sign in to comment.