A simple and type-safe fetcher for nextkit.
# npm
npm install read-from-fs
# yarn
yarn add read-from-fs
# pnpm
pnpm add read-from-fs
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
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;
}
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 ⭐
Licensed under the MIT License.