-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ixahmedxi/ahmed/trpc
trpc
- Loading branch information
Showing
20 changed files
with
446 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type NextRequest } from 'next/server'; | ||
|
||
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; | ||
|
||
import { appRouter, createTRPCContext } from '@orbitkit/trpc'; | ||
|
||
import { env } from '@/env'; | ||
|
||
const createContext = async (req: NextRequest) => { | ||
return createTRPCContext({ | ||
headers: req.headers, | ||
}); | ||
}; | ||
|
||
const handler = (req: NextRequest) => | ||
fetchRequestHandler({ | ||
endpoint: '/api/trpc', | ||
req, | ||
router: appRouter, | ||
createContext: () => createContext(req), | ||
onError: ({ path, error }) => { | ||
env.NODE_ENV === 'development' && | ||
console.error( | ||
`❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`, | ||
); | ||
}, | ||
}); | ||
|
||
export { handler as GET, handler as POST }; |
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,13 +1,20 @@ | ||
import { createEnv } from '@t3-oss/env-nextjs'; | ||
import { vercel } from '@t3-oss/env-nextjs/presets'; | ||
import { z } from 'zod'; | ||
|
||
import { env as authEnv } from '@orbitkit/auth/env'; | ||
import { env as dbEnv } from '@orbitkit/db/env'; | ||
|
||
export const env = createEnv({ | ||
extends: [dbEnv, authEnv, vercel], | ||
server: {}, | ||
server: { | ||
NODE_ENV: z.enum(['development', 'test', 'production']).optional(), | ||
PORT: z.coerce.number().default(3000), | ||
}, | ||
client: {}, | ||
runtimeEnv: {}, | ||
runtimeEnv: { | ||
NODE_ENV: process.env.NODE_ENV, | ||
PORT: process.env['PORT'], | ||
}, | ||
emptyStringAsUndefined: true, | ||
}); |
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,44 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
|
||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { loggerLink, unstable_httpBatchStreamLink } from '@trpc/client'; | ||
import { createTRPCReact } from '@trpc/react-query'; | ||
import superjson from 'superjson'; | ||
|
||
import { type AppRouter } from '@orbitkit/trpc'; | ||
|
||
import { env } from '@/env'; | ||
|
||
import { getUrl } from './shared'; | ||
|
||
export const trpc = createTRPCReact<AppRouter>(); | ||
|
||
export function TRPCReactProvider(props: { children: React.ReactNode }) { | ||
const [queryClient] = useState(() => new QueryClient()); | ||
|
||
const [trpcClient] = useState(() => | ||
trpc.createClient({ | ||
transformer: superjson, | ||
links: [ | ||
loggerLink({ | ||
enabled: (op) => | ||
env.NODE_ENV === 'development' || | ||
(op.direction === 'down' && op.result instanceof Error), | ||
}), | ||
unstable_httpBatchStreamLink({ | ||
url: getUrl(), | ||
}), | ||
], | ||
}), | ||
); | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<trpc.Provider client={trpcClient} queryClient={queryClient}> | ||
{props.children} | ||
</trpc.Provider> | ||
</QueryClientProvider> | ||
); | ||
} |
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,59 @@ | ||
import 'server-only'; | ||
|
||
import { cache } from 'react'; | ||
import { headers } from 'next/headers'; | ||
|
||
import type { AppRouter } from '@orbitkit/trpc'; | ||
|
||
import { | ||
createTRPCProxyClient, | ||
loggerLink, | ||
TRPCClientError, | ||
} from '@trpc/client'; | ||
import { callProcedure } from '@trpc/server'; | ||
import { observable } from '@trpc/server/observable'; | ||
import { type TRPCErrorResponse } from '@trpc/server/rpc'; | ||
import superjson from 'superjson'; | ||
|
||
import { appRouter, createTRPCContext } from '@orbitkit/trpc'; | ||
|
||
const createContext = cache(() => { | ||
const heads = new Headers(headers()); | ||
heads.set('x-trpc-source', 'rsc'); | ||
|
||
return createTRPCContext({ | ||
headers: heads, | ||
}); | ||
}); | ||
|
||
export const trpc = createTRPCProxyClient<AppRouter>({ | ||
transformer: superjson, | ||
links: [ | ||
loggerLink({ | ||
enabled: (op) => | ||
process.env.NODE_ENV === 'development' || | ||
(op.direction === 'down' && op.result instanceof Error), | ||
}), | ||
() => | ||
({ op }) => | ||
observable((observer) => { | ||
createContext() | ||
.then((ctx) => { | ||
return callProcedure({ | ||
procedures: appRouter._def.procedures, | ||
path: op.path, | ||
rawInput: op.input, | ||
ctx, | ||
type: op.type, | ||
}); | ||
}) | ||
.then((data) => { | ||
observer.next({ result: { data } }); | ||
observer.complete(); | ||
}) | ||
.catch((cause: TRPCErrorResponse) => { | ||
observer.error(TRPCClientError.from(cause)); | ||
}); | ||
}), | ||
], | ||
}); |
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 @@ | ||
import { type inferRouterInputs, type inferRouterOutputs } from '@trpc/server'; | ||
|
||
import { type AppRouter } from '@orbitkit/trpc'; | ||
|
||
import { env } from '@/env'; | ||
|
||
function getBaseUrl() { | ||
if (typeof window !== 'undefined') return ''; | ||
if (env.VERCEL_URL) return `https://${env.VERCEL_URL}`; | ||
return `http://localhost:${env.PORT}`; | ||
} | ||
|
||
export function getUrl() { | ||
return getBaseUrl() + '/api/trpc'; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ | |
"prettier-plugin-packagejson": "^2.4.10", | ||
"prettier-plugin-tailwindcss": "^0.5.11", | ||
"rimraf": "^5.0.5", | ||
"turbo": "^1.12.2", | ||
"turbo": "^1.12.3", | ||
"typescript": "^5.3.3" | ||
}, | ||
"packageManager": "[email protected]", | ||
|
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,7 @@ | ||
/** @type {import('eslint').Linter.Config} */ | ||
const config = { | ||
root: true, | ||
extends: ['orbitkit/base'], | ||
}; | ||
|
||
module.exports = config; |
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": "@orbitkit/trpc", | ||
"version": "0.1.0", | ||
"private": true, | ||
"description": "tRPC api for Orbitkit", | ||
"license": "MIT", | ||
"author": "Orbitkit", | ||
"type": "module", | ||
"exports": { | ||
".": "./src/index.ts" | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --cache --max-warnings 0", | ||
"typecheck": "tsc --noEmit --tsBuildInfoFile .tsbuildinfo" | ||
}, | ||
"dependencies": { | ||
"@orbitkit/auth": "workspace:^", | ||
"@orbitkit/db": "workspace:^", | ||
"@trpc/server": "^10.45.1", | ||
"superjson": "^2.2.1", | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"@orbitkit/tsconfig": "workspace:^", | ||
"@types/node": "^20.11.16", | ||
"eslint-config-orbitkit": "workspace:^" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
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,10 @@ | ||
import { greetingRouter } from './routers/greeting'; | ||
import { router } from './trpc'; | ||
|
||
export const appRouter = router({ | ||
greeting: greetingRouter, | ||
}); | ||
|
||
export type AppRouter = typeof appRouter; | ||
|
||
export { createTRPCContext } from './trpc'; |
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,21 @@ | ||
import { z } from 'zod'; | ||
|
||
import { protectedProcedure, publicProcedure, router } from '../trpc'; | ||
|
||
export const greetingRouter = router({ | ||
hello: publicProcedure | ||
.input(z.object({ name: z.string().optional() }).optional()) | ||
.query(({ input }) => { | ||
return { | ||
greeting: `Hello, ${input?.name ?? 'World'}!`, | ||
}; | ||
}), | ||
|
||
protectedHello: protectedProcedure | ||
.input(z.object({ name: z.string().optional() }).optional()) | ||
.query(({ input, ctx }) => { | ||
return { | ||
greeting: `Hello, ${input?.name ?? 'World'}! Your user ID is ${ctx.session.user.id}`, | ||
}; | ||
}), | ||
}); |
Oops, something went wrong.