1.21.0 • Published 3 years ago
@jonbilous/next-js-rpc v1.21.0
next-js-rpc
- Quickly create type-safe RPC endpoints for Next.JS
- Request validation with Zod
- Client side fetching with React Query
Install
npm i zod react-query @jonbilous/next-js-rpc
Next.JS API
import { createHandler } from "@jonbilous/next-js-rpc/";
import { getUserSession } from "utils/ctx";
import db from "utils/db";
import zod from "zod";
const ctx = {
user: getUserSession,
};
const schema = zod.null();
const getLocations = createHandler({
url: "/api/functions",
fn: async (params, ctx) => {
return db.location.findMany();
},
schema,
ctx,
});
export type LocationQuery = typeof getLocations;
export default getLocations;
Next.JS Page
import { client } from "@jonbilous/next-js-rpc";
import type { GetServerSideProps, NextPage } from "next";
import type { LocationQuery } from "pages/api/functions";
import getLocations from "pages/api/functions";
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const locations = await getLocations.ssr(null, ctx);
// import handler and call .ssr() to use on the server
return { props: { locations } };
};
const Home: NextPage = (props) => {
const query = client.useQuery < LocationQuery > ("/api/functions", null);
// import type on the client and pass to useQuery - url, request type and response types will be inferred
return <div></div>;
};
export default Home;
1.21.0
3 years ago
1.20.0
3 years ago
1.19.0
3 years ago
1.18.1
3 years ago
1.18.0
3 years ago
1.17.4
3 years ago
1.17.3
3 years ago
1.17.2
3 years ago
1.17.1
3 years ago
1.17.0
3 years ago
1.16.1
3 years ago
1.16.0
3 years ago
1.15.1
3 years ago
1.15.0
3 years ago
1.14.0
3 years ago
1.13.0
3 years ago
1.12.0
3 years ago
1.11.0
3 years ago
1.10.0
3 years ago
1.9.0
3 years ago
1.8.0
3 years ago
1.7.0
3 years ago
1.6.0
3 years ago
1.5.0
3 years ago
1.4.0
3 years ago
1.3.1
3 years ago
1.3.0
3 years ago
1.2.0
3 years ago
1.1.2
3 years ago
1.1.1
3 years ago
1.1.0
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago