Skip to content

Commit

Permalink
api: Initialize express API with tRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
busybox11 committed May 23, 2024
1 parent 406379f commit fa48eb0
Show file tree
Hide file tree
Showing 6 changed files with 791 additions and 16 deletions.
31 changes: 31 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"dev": "tsx watch ./src/index.ts",
"build": "tsc",
"start": "tsx ./src/index.ts",
"lint": "eslint --ext .ts src",
"format": "prettier --write src/**/*.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@trpc/server": "^10.45.2",
"express": "^4.19.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@repo/eslint-config": "workspace:^",
"@repo/typescript-config": "workspace:^",
"@types/express": "^4.17.21",
"@types/node": "^20.11.24",
"eslint": "^8.57.0",
"tsx": "^4.10.5",
"typescript": "^5.3.3"
}
}
11 changes: 11 additions & 0 deletions apps/api/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const SPOTIFY_SCOPES = [
"user-read-currently-playing",
"user-read-playback-state",
"user-modify-playback-state",
"user-read-playback-position",
"user-read-recently-played",
"user-top-read",
"user-library-read",
"user-library-modify",
"streaming",
];
17 changes: 17 additions & 0 deletions apps/api/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { CreateFastifyContextOptions } from "@trpc/server/adapters/fastify";

export async function createContextInner() {
return {};
}

export async function createContext({ req, res }: CreateFastifyContextOptions) {
const server = req.server;

return {
server,
req,
res,
};
}

export type Context = Awaited<ReturnType<typeof createContext>>;
23 changes: 23 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express from "express";
import * as trpcExpress from "@trpc/server/adapters/express";
import { appRouter } from "./router";
import { createContext } from "./context";

const app = express();

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.use(
"/trpc",
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
})
);

app.set("trust proxy", true);
app.listen(4000, () => {
console.log(`NowPlaying API listening on port 4000`);
});
11 changes: 11 additions & 0 deletions apps/api/src/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { initTRPC } from "@trpc/server";
import type { Context } from "./context";

const t = initTRPC.context<Context>().create();

export const appRouter = t.router({
hello: t.procedure.query(({ ctx }) => {
return ctx.server.info;
}),
});
export type AppRouter = typeof appRouter;
Loading

0 comments on commit fa48eb0

Please sign in to comment.