nextkit-fetcher
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

nextkit-fetcher

XO code style

A simple and type-safe fetcher for nextkit.

Installation ∙ npm

# npm
npm install read-from-fs

# yarn
yarn add read-from-fs

# pnpm
pnpm add read-from-fs

Usage

Use it on the frontend while fetching your own Endpoints.

import useSWR from "swr";
import { fetcher } from "nextkit-fetcher";

const Index: NextPage<Props> = (props) => {
  const { data } = useSWR("/api/user", fetcher);

  return <p>Hello {data.name}!</p>;
};

If it fails it wil throw a NextkitClientError.

Check the full API on 📖

https://nextkit-fetcher.js.org

Extra

If you are not a fan of adding dependencies, copy-paste this 👇

import { NextkitClientError } from "nextkit/client";

import type { APIResponse } from "nextkit";

export async function fetcher<T>(url: string, options?: RequestInit) {
  const request = await fetch(url, options);

  if (request.status >= 400) {
    throw new NextkitClientError(request.status, "Error While Fetching");
  }

  const body = (await request.json()) as APIResponse<T>;

  if (!body.success) {
    throw new NextkitClientError(request.status, body.message);
  }

  return body.data;
}

Support

Open an Issue, I will check it a soon as possible 👀

If you want to hurry me up a bit send me a tweet 😆

Consider supporting me on Patreon if you like my work 🚀

Don't forget to start the repo ⭐

Licence

Licensed under the MIT License.

Readme

Keywords

Package Sidebar

Install

npm i nextkit-fetcher

Weekly Downloads

4

Version

1.2.0

License

MIT

Unpacked Size

5.76 kB

Total Files

8

Last publish

Collaborators

  • ultirequiem