-
-
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.
app: Base working tRPC router and env
- Loading branch information
Showing
22 changed files
with
625 additions
and
55 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
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
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
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
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 |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { initTRPC } from "@trpc/server"; | ||
import type { Context } from "./context"; | ||
|
||
const t = initTRPC.context<Context>().create(); | ||
import SuperJSON from "superjson"; | ||
|
||
const t = initTRPC.context<Context>().create({ | ||
transformer: SuperJSON, | ||
errorFormatter({ shape }) { | ||
return shape; | ||
}, | ||
}); | ||
|
||
export const appRouter = t.router({ | ||
hello: t.procedure.query(({ ctx }) => { | ||
return ctx.server.info; | ||
hello: t.procedure.query(() => { | ||
return "world"; | ||
}), | ||
}); | ||
export type AppRouter = typeof appRouter; |
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 @@ | ||
export type { AppRouter } from "./router"; |
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,12 @@ | ||
{ | ||
"extends": "@nowplaying/typescript-config/base.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@api/*": ["./src/*"] | ||
}, | ||
"outDir": "dist" | ||
}, | ||
"include": ["./src/**/*.ts", "./src/**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
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
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
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
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,18 @@ | ||
"use client"; | ||
|
||
import { trpc } from "@/utils/trpc"; | ||
|
||
function TestTrpc() { | ||
const trpcHello = trpc.hello.useQuery(); | ||
|
||
if (trpcHello.data) { | ||
return <div>{trpcHello.data}</div>; | ||
} | ||
|
||
if (trpcHello.error) { | ||
return <div>{trpcHello.error.message}</div>; | ||
} | ||
|
||
return <div>Loading...</div>; | ||
} | ||
export default trpc.withTRPC(TestTrpc); |
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
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
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
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,61 @@ | ||
import type { AppRouter } from "@nowplaying/api/src/types"; | ||
|
||
import { httpBatchLink, loggerLink } from "@trpc/client"; | ||
import { createTRPCNext } from "@trpc/next"; | ||
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server"; | ||
import type _fastify from "fastify"; | ||
import superjson from "superjson"; | ||
|
||
function getBaseUrl() { | ||
return process.env.NODE_ENV === "development" | ||
? "http://localhost:4000" | ||
: "https://trpc.nowplayi.ng"; | ||
} | ||
|
||
function getHeaders() { | ||
const baseHeaders = { | ||
"Access-Control-Allow-Origin": "*", | ||
} as Record<string, string>; | ||
|
||
return baseHeaders; | ||
} | ||
|
||
export const trpc = createTRPCNext<AppRouter>({ | ||
config({ ctx: _ctx }) { | ||
return { | ||
queryClientConfig: { | ||
defaultOptions: { | ||
queries: { | ||
refetchOnWindowFocus: false, | ||
}, | ||
}, | ||
}, | ||
links: [ | ||
loggerLink({ | ||
enabled: (opts) => | ||
process.env.NODE_ENV === "development" || | ||
(opts.direction === "down" && opts.result instanceof Error), | ||
}), | ||
httpBatchLink({ | ||
transformer: superjson, | ||
url: `${getBaseUrl()}/trpc`, | ||
fetch: (url, options) => { | ||
const headers = getHeaders(); | ||
return fetch(url, { | ||
...options, | ||
headers: { | ||
...options?.headers, | ||
...headers, | ||
}, | ||
}); | ||
}, | ||
}), | ||
], | ||
}; | ||
}, | ||
transformer: superjson, | ||
ssr: false, | ||
}); | ||
|
||
export type RouterInputs = inferRouterInputs<AppRouter>; | ||
export type RouterOutputs = inferRouterOutputs<AppRouter>; |
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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"name": "@repo/typescript-config", | ||
"name": "@nowplaying/typescript-config", | ||
"version": "0.0.0", | ||
"private": true, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} | ||
} |
Oops, something went wrong.