10.4.5 • Published 1 year ago

@zodios/react v10.4.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Install

> npm install @zodios/react

or

> yarn add @zodios/react

Usage

Zodios comes with a Query and Mutation hook helper.
It's a thin wrapper around React-Query but with zodios auto completion.

Zodios query hook also returns an invalidation helper to allow you to reset react query cache easily

import React from "react";
import { QueryClient, QueryClientProvider } from "react-query";
import { Zodios, asApi } from "@zodios/core";
import { ZodiosHooks } from "@zodios/react";
import { z } from "zod";

// you can define schema before declaring the API to get back the type
const userSchema = z
  .object({
    id: z.number(),
    name: z.string(),
  })
  .required();

const createUserSchema = z
  .object({
    name: z.string(),
  })
  .required();

const usersSchema = z.array(userSchema);

// you can then get back the types
type User = z.infer<typeof userSchema>;
type Users = z.infer<typeof usersSchema>;

const api = asApi([
  {
    method: "get",
    path: "/users",
    description: "Get all users",
    parameters: [
      {
        name: "q",
        type: "Query",
        schema: z.string(),
      },
      {
        name: "page",
        type: "Query",
        schema: z.string().optional(),
      },
    ],
    response: usersSchema,
  },
  {
    method: "get",
    path: "/users/:id",
    description: "Get a user",
    response: userSchema,
  },
  {
    method: "post",
    path: "/users",
    description: "Create a user",
    parameters: [
      {
        name: "body",
        type: "Body",
        schema: createUserSchema,
      },
    ],
    response: userSchema,
  },
]);
const baseUrl = "https://jsonplaceholder.typicode.com";

const zodios = new Zodios(baseUrl, api);
const zodiosHooks = new ZodiosHooks("jsonplaceholder", zodios);

const Users = () => {
  const {
    data: users,
    isLoading,
    error,
    invalidate: invalidateUsers, // zodios also provides invalidation helpers
  } = zodiosHooks.useQuery("/users");
  const { mutate } = zodiosHooks.useMutation("post", "/users", undefined, {
    onSuccess: () => invalidateUsers(),
  });

  return (
    <>
      <h1>Users</h1>
      <button onClick={() => mutate({ name: "john doe" })}>add user</button>
      {isLoading && <div>Loading...</div>}
      {error && <div>Error: {(error as Error).message}</div>}
      {users && (
        <ul>
          {users.map((user) => (
            <li key={user.id}>{user.name}</li>
          ))}
        </ul>
      )}
    </>
  );
};

// on another file
const queryClient = new QueryClient();

export const App = () => {
  return (
    <QueryClientProvider client={queryClient}>
      <Users />
    </QueryClientProvider>
  );
};
10.4.5

1 year ago

11.0.0-beta.19

1 year ago

11.0.0-beta.18

1 year ago

10.4.4

1 year ago

11.0.0-beta.17

1 year ago

11.0.0-beta.16

1 year ago

10.4.3

1 year ago

11.0.0-beta.15

1 year ago

10.4.2

1 year ago

10.4.1

1 year ago

10.4.0

1 year ago

11.0.0-beta.14

1 year ago

10.3.5

1 year ago

11.0.0-beta.13

1 year ago

10.3.4

1 year ago

10.3.3

2 years ago

10.3.2

2 years ago

10.3.1

2 years ago

10.3.0

2 years ago

10.3.0-rc.0

2 years ago

10.2.0

2 years ago

10.0.1

2 years ago

10.0.0

2 years ago

9.5.0

2 years ago

9.4.5

2 years ago

9.4.4

2 years ago

9.4.3

2 years ago

9.4.2

2 years ago

9.4.1

2 years ago

9.4.0

2 years ago

9.3.1

2 years ago

9.3.0

2 years ago

9.2.0

2 years ago

9.1.0

2 years ago

9.0.2

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

4.0.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago