1.21.0 • Published 2 years ago

@jonbilous/next-js-rpc v1.21.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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

2 years ago

1.20.0

2 years ago

1.19.0

2 years ago

1.18.1

2 years ago

1.18.0

2 years ago

1.17.4

2 years ago

1.17.3

2 years ago

1.17.2

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.15.1

2 years ago

1.15.0

2 years ago

1.14.0

2 years ago

1.13.0

2 years ago

1.12.0

2 years ago

1.11.0

2 years ago

1.10.0

2 years ago

1.9.0

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago