Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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.