npm package: @koj/svelte-api-client
Description: Koj's Fetch-powered API client for webapps
Author: Anand Chowdhary
Latest version: 1.1.2

# 🥅 Koj Svelte API Client

This repository contains Koj's Fetch API-powered client for interacting with our API. It is very opinionated, so usage is only recommended in Koj's Svelte projects.

<!-- prettier-ignore-start -->
|   | Status |
| - | - |
| Build | [![Node CI](https://github.com/koj-co/svelte-api-client/workflows/Node%20CI/badge.svg)](https://github.com/koj-co/svelte-api-client/actions?query=workflow%3A%22Node+CI%22) [![Dependencies](https://img.shields.io/librariesio/github/koj-co/svelte-api-client)](https://libraries.io/github/koj-co/svelte-api-client) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/koj-co/svelte-api-client)](https://github.com/koj-co/svelte-api-client/releases) [![Snyk Vulnerabilities for GitHub Repo](https://img.shields.io/snyk/vulnerabilities/github/koj-co/svelte-api-client)](https://snyk.io/test/github/koj-co/svelte-api-client) |
| Health | [![CLA Assistant](https://github.com/koj-co/svelte-api-client/workflows/CLA%20Assistant/badge.svg)](https://github.com/koj-co/svelte-api-client/actions?query=workflow%3A%22CLA+Assistant%22) [![Pull Request Labeler](https://github.com/koj-co/svelte-api-client/workflows/Pull%20Request%20Labeler/badge.svg)](https://github.com/koj-co/svelte-api-client/actions?query=workflow%3A%22Pull+Request+Labeler%22) |
| PRs | [![PR Generator CI](https://github.com/koj-co/svelte-api-client/workflows/PR%20Generator%20CI/badge.svg)](https://github.com/koj-co/svelte-api-client/actions?query=workflow%3A%22PR+Generator+CI%22) [![Merge PRs](https://github.com/koj-co/svelte-api-client/workflows/Merge%20PRs/badge.svg)](https://github.com/koj-co/svelte-api-client/actions?query=workflow%3A%22Merge+PRs%22) |
<!-- prettier-ignore-end -->

## ⭐️ Features

- Wrapper around the native Fetch API (95%+ support)
- Tracks logged in users and their permissions
- Authorization helper `can` based on logged in scopes
- Built-in refreshing expired access tokens before requests
- Response caching in IndexedDB or local storage
- Error handling (text responses from error codes)
- Supports different request/responses:
  - Responses can be JSON or Blob objects
  - Requests can be JSON or FormData

## 💻 Getting started

Install the dependency from npm:

```bash
npm install @koj/svelte-api-client
```

Import the `api` method and use it like so:

```ts
import { api } from "@koj/svelte-api-client";
import type { User } from "@koj/types";

let user: User | undefined = undefined;
const getUser = async () => {
  user = await api<User>({
    method: "GET",
    url: "/users/12345",
    onCachedResponse: (data) => (user = data),
  });
};
```

In the above example, the `api` method expects a type parameter which determines its response time. Note that JSON responses are automatically parsed. The `onCachedResponse` function is used to get a "faster" responses from a client side-stored cache, using the stale-while-revalidate pattern.

## 📄 License

[MIT](./LICENSE) © [Koj](https://koj.co)

<p align="center">
  <a href="https://koj.co">
    <img width="44" alt="Koj" src="https://kojcdn.com/v1598284251/website-v2/koj-github-footer_m089ze.svg">
  </a>
</p>
<p align="center">
  <sub>An open source project by <a href="https://koj.co">Koj</a>. <br> <a href="https://koj.co">Furnish your home in style, for as low as CHF175/month →</a></sub>
</p>