-
-
Notifications
You must be signed in to change notification settings - Fork 57
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 #57 from ixahmedxi/trpc
feat: adds a trpc api
- Loading branch information
Showing
30 changed files
with
468 additions
and
64 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,28 @@ | ||
--- | ||
"@orbitkit/utils": patch | ||
"@orbitkit/auth": patch | ||
"@orbitkit/api": patch | ||
"@orbitkit/ui": patch | ||
"@orbitkit/docs": patch | ||
"@orbitkit/web": patch | ||
"@orbitkit/marketing": patch | ||
"@orbitkit/assets": patch | ||
"eslint-config-orbitkit": patch | ||
"@orbitkit/storybook": patch | ||
"@orbitkit/tailwind": patch | ||
"@orbitkit/tsconfig": patch | ||
"@orbitkit/vite": patch | ||
"@orbitkit/core": patch | ||
"@orbitkit/db": patch | ||
"@orbitkit/env": patch | ||
--- | ||
|
||
feat: creates a trpc api package | ||
|
||
changes in this release: | ||
|
||
- creates a new `packages/api` package that hosts a tRPC api to be used for the web application. | ||
- renames the lucia auth `getSession` function to `auth` and provides a new uncached version of it. | ||
- refactors the code in some places to be generally better. | ||
- splits the utils package from a barrel export to multi-file export. | ||
- `getBaseUrl` util now returns `window.location.origin` instead of an empty string when the `window` object is not `undefined`. |
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
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,28 @@ | ||
import { type NextRequest } from 'next/server'; | ||
|
||
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; | ||
|
||
import { appRouter, createTRPCContext } from '@orbitkit/api'; | ||
import { env } from '@orbitkit/env/web'; | ||
|
||
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
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,60 @@ | ||
'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 { type inferRouterInputs, type inferRouterOutputs } from '@trpc/server'; | ||
import superjson from 'superjson'; | ||
|
||
import { type AppRouter } from '@orbitkit/api'; | ||
import { getBaseUrl } from '@orbitkit/utils/url'; | ||
|
||
const createQueryClient = () => new QueryClient(); | ||
|
||
let clientQueryClientSingleton: QueryClient | undefined = undefined; | ||
const getQueryClient = () => { | ||
if (typeof window === 'undefined') { | ||
return createQueryClient(); | ||
} | ||
return (clientQueryClientSingleton ??= createQueryClient()); | ||
}; | ||
|
||
export const api = createTRPCReact<AppRouter>(); | ||
|
||
export type RouterInputs = inferRouterInputs<AppRouter>; | ||
export type RouterOutputs = inferRouterOutputs<AppRouter>; | ||
|
||
export function TRPCReactProvider(props: { children: React.ReactNode }) { | ||
const queryClient = getQueryClient(); | ||
|
||
const [trpcClient] = useState(() => | ||
api.createClient({ | ||
links: [ | ||
loggerLink({ | ||
enabled: (op) => | ||
process.env.NODE_ENV === 'development' || | ||
(op.direction === 'down' && op.result instanceof Error), | ||
}), | ||
unstable_httpBatchStreamLink({ | ||
transformer: superjson, | ||
url: getBaseUrl() + '/api/trpc', | ||
headers: () => { | ||
const headers = new Headers(); | ||
headers.set('x-trpc-source', 'nextjs-react'); | ||
return headers; | ||
}, | ||
}), | ||
], | ||
}), | ||
); | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<api.Provider client={trpcClient} queryClient={queryClient}> | ||
{props.children} | ||
</api.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,17 @@ | ||
import 'server-only'; | ||
|
||
import { cache } from 'react'; | ||
import { headers } from 'next/headers'; | ||
|
||
import { createCaller, createTRPCContext } from '@orbitkit/api'; | ||
|
||
const createContext = cache(async () => { | ||
const heads = new Headers(headers()); | ||
heads.set('x-trpc-source', 'rsc'); | ||
|
||
return createTRPCContext({ | ||
headers: heads, | ||
}); | ||
}); | ||
|
||
export const api = createCaller(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
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,34 @@ | ||
{ | ||
"name": "@orbitkit/api", | ||
"version": "0.1.3", | ||
"private": true, | ||
"description": "A tRPC API package used in the web application", | ||
"license": "MIT", | ||
"author": "OrbitKit", | ||
"sideEffects": false, | ||
"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:^", | ||
"@orbitkit/env": "workspace:^", | ||
"@orbitkit/utils": "workspace:^", | ||
"@trpc/server": "next", | ||
"superjson": "^2.2.1", | ||
"zod": "^3.23.8" | ||
}, | ||
"devDependencies": { | ||
"@orbitkit/tsconfig": "workspace:^", | ||
"@types/node": "^20.12.11", | ||
"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,12 @@ | ||
import { helloRouter } from './routers/hello'; | ||
import { createCallerFactory, createRouter } from './trpc'; | ||
|
||
export const appRouter = createRouter({ | ||
hello: helloRouter, | ||
}); | ||
|
||
export type AppRouter = typeof appRouter; | ||
|
||
export const createCaller = createCallerFactory(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,9 @@ | ||
import { createRouter, protectedProcedure } from '../trpc'; | ||
|
||
export const helloRouter = createRouter({ | ||
protected: protectedProcedure.query(({ ctx }) => { | ||
return { | ||
message: `Hello, ${ctx.user.name ?? 'world'} from tRPC!`, | ||
}; | ||
}), | ||
}); |
Oops, something went wrong.