-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Initialize express API with tRPC
- Loading branch information
Showing
6 changed files
with
791 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.