Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ultirequiem/nextkit-fetcher
🚙 A simple, type-safe fetcher for nextkit
https://github.com/ultirequiem/nextkit-fetcher
next nextkit npm npm-package typescript typescript-library
Last synced: 28 days ago
JSON representation
🚙 A simple, type-safe fetcher for nextkit
- Host: GitHub
- URL: https://github.com/ultirequiem/nextkit-fetcher
- Owner: UltiRequiem
- License: mit
- Created: 2022-03-15T03:27:00.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-01T19:58:11.000Z (over 1 year ago)
- Last Synced: 2024-10-11T05:41:40.312Z (28 days ago)
- Topics: next, nextkit, npm, npm-package, typescript, typescript-library
- Language: TypeScript
- Homepage: https://nextkit-fetcher.js.org
- Size: 188 KB
- Stars: 5
- Watchers: 2
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# nextkit-fetcher
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
A simple and type-safe fetcher for [nextkit](https://github.com/alii/nextkit).
## Installation ∙ [![npm](https://img.shields.io/npm/v/nextkit-fetcher?color=blue&style=flat-square)](https://www.npmjs.com/package/nextkit-fetcher)
```sh
# npm
npm install nextkit-fetcher# yarn
yarn add nextkit-fetcher# pnpm
pnpm add nextkit-fetcher
```## Usage
Use it on the frontend while fetching your own Endpoints.
```ts
import useSWR from "swr";
import { fetcher } from "nextkit-fetcher";const Index: NextPage = (props) => {
const { data } = useSWR("/api/user", fetcher);return
Hello {data.name}!
;
};
```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 👇
```ts
import { NextkitClientError } from "nextkit/client";import type { APIResponse } from "nextkit";
export async function fetcher(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;
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](https://twitter.com/intent/tweet?text=%40UltiRequiem%20) 😆Consider [supporting me on Patreon](https://patreon.com/UltiRequiem) if you like
my work 🚀Don't forget to start the repo ⭐
## Licence
Licensed under the MIT License.