
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@trpc/next
Advanced tools
End-to-end typesafe APIs made easy
@trpc/next
Connect a tRPC router to Next.js.
Full documentation for @trpc/next
can be found here
# npm
npm install @trpc/next @trpc/react-query @tanstack/react-query
# Yarn
yarn add @trpc/next @trpc/react-query @tanstack/react-query
# pnpm
pnpm add @trpc/next @trpc/react-query @tanstack/react-query
# Bun
bun add @trpc/next @trpc/react-query @tanstack/react-query
Setup tRPC in utils/trpc.ts
.
import { createTRPCNext, httpBatchLink } from '@trpc/next';
// Import the router type from your server file
import type { AppRouter } from '../pages/api/[trpc].ts';
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
links: [
httpBatchLink({
url: 'http://localhost:3000/trpc',
}),
],
};
},
ssr: false,
});
Hook up tRPC inside _app.tsx
.
import { trpc } from '~/utils/trpc';
const App = ({ Component, pageProps }) => {
return <Component {...pageProps} />;
};
export default trpc.withTRPC(App);
Now you can query your API in any component.
import { trpc } from '~/utils/trpc';
export function Hello() {
const { data, error, status } = trpc.greeting.useQuery({
name: 'tRPC',
});
if (error) {
return <p>{error.message}</p>;
}
if (status !== 'success') {
return <p>Loading...</p>;
}
return <div>{data && <p>{data.greeting}</p>}</div>;
}
next-api-decorators is a package that provides a set of decorators to create type-safe API routes in Next.js. It uses TypeScript decorators to define API routes and handlers, offering a similar level of type safety as @trpc/next but with a different approach.
Blitz is a full-stack React framework built on top of Next.js. It includes a built-in data layer that provides a type-safe API similar to tRPC. Blitz aims to be a 'batteries-included' framework, offering more out-of-the-box features compared to @trpc/next.
GraphQL is a query language for APIs and a runtime for executing those queries. While it is not specific to Next.js, it can be integrated with Next.js to create type-safe APIs. GraphQL offers more flexibility in querying data compared to tRPC but requires more setup and boilerplate.
FAQs
The tRPC Next.js library
The npm package @trpc/next receives a total of 282,313 weekly downloads. As such, @trpc/next popularity was classified as popular.
We found that @trpc/next demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.