Spoosh

Introduction

A type-safe API client with a powerful plugin system

Spoosh is a lightweight, type-safe API client with a powerful plugin system. It provides a clean, intuitive way to define API schemas and make HTTP requests with full TypeScript support.

Key Features

  • Type-Safe Schema Definition - Define your API once, get autocomplete and type checking everywhere
  • Plugin System - Extend functionality with caching, retry, polling, optimistic updates, and more
  • Framework Bindings - Hooks for React, with more frameworks coming soon
  • Server Adapters - First-class support for Hono with automatic type inference
  • Cache Invalidation - Automatic tag-based cache invalidation after mutations
  • Next.js Integration - Server-side cache revalidation with App Router

Quick Example

import { createSpoosh } from "@spoosh/core";
import { createReactSpoosh } from "@spoosh/react";
import { cachePlugin } from "@spoosh/plugin-cache";
import { invalidationPlugin } from "@spoosh/plugin-invalidation";

// Define your API schema
type ApiSchema = {
  users: {
    $get: Endpoint<User[]>;
    $post: Endpoint<User, CreateUserBody>;
  };
};

// Create the client with plugins
const spoosh = createSpoosh<ApiSchema>({
  baseUrl: "/api",
  plugins: [
    cachePlugin({ staleTime: 5000 }),
    invalidationPlugin(), // Auto-refresh queries after mutations
  ],
});

// Export hooks
export const { useRead, useWrite } = createReactSpoosh(spoosh);
// Use in your components
function UserList() {
  const { data, loading, error } = useRead((api) => api.users.$get());

  if (loading) return <Spinner />;
  if (error) return <Error message={error.message} />;

  return (
    <ul>
      {data?.map((user) => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
}

Packages

PackageDescription
@spoosh/coreCore client and plugin system
@spoosh/reactReact bindings (useRead, useWrite, useInfiniteRead)
@spoosh/honoHono type adapter
@spoosh/openapiOpenAPI spec generator

Plugins

PluginDescription
@spoosh/plugin-cacheResponse caching with stale time
@spoosh/plugin-invalidationAuto-refresh queries after mutations (recommended)
@spoosh/plugin-retryAutomatic retry with configurable attempts
@spoosh/plugin-optimisticOptimistic updates with rollback
@spoosh/plugin-pollingAutomatic polling at intervals
@spoosh/plugin-debounceRequest debouncing
@spoosh/plugin-throttleRequest throttling
@spoosh/plugin-deduplicationPrevents duplicate in-flight requests
@spoosh/plugin-prefetchPreload data before needed
@spoosh/plugin-refetchRefetch on focus/reconnect
@spoosh/plugin-initial-dataShow data before fetch completes
@spoosh/plugin-nextjsNext.js server revalidation
@spoosh/plugin-debugDebug logging

Next Steps

On this page