@koj/svelte-api-client
v1.1.2
Published
Koj's Fetch-powered API client for webapps
Downloads
23
Readme
🥅 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.
| | Status | | - | - | | Build | | | Health | | | PRs | |
⭐️ 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:
npm install @koj/svelte-api-client
Import the api
method and use it like so:
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.