# Supwr - Framework-Agnostic SWR Library (Under development)
A modern, framework-agnostic Stale-While-Revalidate (SWR) data fetching library with adapters for React, Svelte, Solid, and Vue.
## Features
### Core Library (`@supwr/core`)
- **Framework-agnostic**: Core logic separated from framework-specific code
- **Advanced Caching**: LRU cache with TTL support
- **Request Deduplication**: Prevent duplicate requests automatically
- **Event System**: Subscribe to state changes
- **Mutation Support**: Optimistic updates with rollback
- **Revalidation Strategies**: Focus, reconnect, interval-based
- **TypeScript**: Full type safety throughout
- **Error Handling**: Comprehensive error management with retry logic
### Framework Adapters
- **React** (`@supwr/react`): Hooks-based API with `useSyncExternalStore`
- **Svelte** (`@supwr/svelte`): Store-based API with Svelte reactivity
- **Solid** (`@supwr/solid`): Signal-based API with Solid reactivity
- **Vue** (`@supwr/vue`): Composable API with Vue 3 reactivity
## Installation
### Core Library
```bash
npm install @supwr/core
```
### Framework Adapters
```bash
# React
npm install @supwr/react
# Svelte
npm install @supwr/svelte
# Solid
npm install @supwr/solid
# Vue
npm install @supwr/vue
```
## Quick Start
### Core Usage
```typescript
import { createSWR } from '@supwr/core'
const swr = createSWR({
fetcher: (key) => fetch(key).then(res => res.json()),
refreshInterval: 5000,
revalidateOnFocus: true
})
// Subscribe to changes
const unsubscribe = swr.subscribe('/api/user', (data, error, isValidating, isLoading) => {
console.log('Data:', data)
console.log('Loading:', isLoading)
})
// Manual mutation
await swr.mutate('/api/user', { name: 'New Name' })
```
### React Adapter
```tsx
import useSWR from '@supwr/react'
function UserProfile() {
const { data, error, isLoading, mutate } = useSWR(
'/api/user',
(key) => fetch(key).then(res => res.json())
)
if (isLoading) return
Loading...
if (error) return Error: {error.message}
return (
{data?.name}
)
}
```
### Svelte Adapter
```svelte
{#if $isLoading}
Loading...
{:else if $error}
Error: {$error.message}
{:else}
{$data?.name}
{/if}
```
### Solid Adapter
```tsx
import { createSWRSignals } from '@supwr/solid'
function UserProfile() {
const { data, error, isLoading, mutate } = createSWRSignals(
'/api/user',
(key) => fetch(key).then(res => res.json())
)
return (
Loading...
Error: {error()?.message}
{data()?.name}
)
}
```
### Vue Adapter
```vue
Loading...
Error: {{ error.message }}
{{ data?.name }}
```
## Architecture
### Core Components
- **Cache**: LRU cache with TTL and automatic cleanup
- **Events**: Key-specific and global event management
- **Fetcher**: Request deduplication and abort support
- **Revalidation**: Multiple revalidation strategies
- **Mutation**: Optimistic updates with rollback
- **Configuration**: Extensible configuration system
### Adapter Pattern
Each adapter:
1. Imports the core library
2. Provides framework-specific reactivity integration
3. Exports idiomatic API for the framework
4. Handles framework lifecycle management
## API Reference
### Core API
```typescript
interface SWRCore {
get(key: Key): CacheEntry | undefined
subscribe(key: Key, callback: Callback): Unsubscribe
mutate(key: Key, data?, options?): Promise
revalidate(key: Key, options?): Promise
delete(key: Key): void
clear(): void
}
```
### Configuration Options
```typescript
interface SWRConfig {
fetcher?: Fetcher
ttl?: number
refreshInterval?: number
revalidateOnFocus?: boolean
revalidateOnReconnect?: boolean
dedupingInterval?: number
errorRetryCount?: number
errorRetryInterval?: number
loadingTimeout?: number
onSuccess?: (data: Data, key: Key) => void
onError?: (error: Error, key: Key) => void
onErrorRetry?: (error, key, config, revalidate, options) => void
isPaused?: () => boolean
compare?: (a: Data, b: Data) => boolean
fallback?: Record
}
```
## Development
### Setup
```bash
# Clone repository
git clone
cd supwr
# Install dependencies
npm install
# Build all packages
npm run build
# Run tests
npm run test
# Start development
npm run dev
```
### Project Structure
```
supwr/
├── src/
│ ├── core/ # Core SWR library
│ └── adapters/ # Framework adapters
│ ├── react/ # React adapter
│ ├── svelte/ # Svelte adapter
│ ├── solid/ # Solid adapter
│ └── vue/ # Vue adapter
├── package.json # Workspace configuration
└── README.md
```
## Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request
## License
MIT © [Supwr](https://github.com/shishantbiswas/supwr)
## Acknowledgments
Inspired by:
- [SWR](https://swr.vercel.app/) - React Hooks library for data fetching
- [swrev](https://github.com/ConsoleTVs/swrev) - Framework-agnostic SWR implementation
- [solid-swr](https://github.com/tronikelis/solid-swr) - Solid implementation
- [sswr](https://github.com/ConsoleTVs/sswr) - Svelte implementation
- [swrv](https://github.com/Kong/swrv) - Vue implementation