npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@daemoniorum/api

v0.1.1

Published

API client library with Axios, retry logic, and TanStack Query integration

Readme

@daemoniorum/api

API client library with Axios, retry logic, and TanStack Query integration for React applications.

Features

| Feature | Description | |---------|-------------| | Axios Client | Pre-configured with retry logic, timeouts, and interceptors | | Token Refresh | Automatic token refresh with request queuing | | Error Handling | Structured ApiError class with status codes | | TanStack Query | Ready-to-use hooks for queries and mutations | | TypeScript | Full type safety throughout |

Installation

npm install @daemoniorum/api axios @tanstack/react-query
# or
pnpm add @daemoniorum/api axios @tanstack/react-query

Quick Start

1. Create API Client

import { createApiClient } from '@daemoniorum/api/client'

const api = createApiClient({
  baseUrl: 'https://api.example.com',
  getToken: () => localStorage.getItem('token'),
  refreshToken: async () => {
    const response = await fetch('/auth/refresh', {
      method: 'POST',
      body: JSON.stringify({
        refreshToken: localStorage.getItem('refresh_token'),
      }),
    })
    return response.json()
  },
  onAuthFailure: () => {
    window.location.href = '/login'
  },
})

2. Setup Query Client

import { QueryClientProvider } from '@tanstack/react-query'
import { createQueryClient } from '@daemoniorum/api/query'

const queryClient = createQueryClient()

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <YourApp />
    </QueryClientProvider>
  )
}

3. Use Query Hooks

import { useApiQuery, useApiMutation } from '@daemoniorum/api/query'

// Fetch data
function useUsers() {
  return useApiQuery(api, ['users'], '/users')
}

// Create mutation
function useCreateUser() {
  return useApiMutation(api, 'POST', '/users', {
    invalidateKeys: [['users']],
  })
}

// Use in component
function UserList() {
  const { data, isLoading, error } = useUsers()
  const createUser = useCreateUser()

  if (isLoading) return <div>Loading...</div>
  if (error) return <div>Error: {error.message}</div>

  return (
    <div>
      {data.map(user => <UserRow key={user.id} user={user} />)}
      <button onClick={() => createUser.mutate({ name: 'New User' })}>
        Add User
      </button>
    </div>
  )
}

API Reference

Client

createApiClient(config)

Create a configured Axios instance.

interface ApiClientConfig {
  baseUrl: string
  timeout?: number              // Default: 20000ms
  headers?: Record<string, string>
  getToken?: () => string | null | Promise<string | null>
  refreshToken?: () => Promise<TokenRefreshResult>
  storageKeys?: {
    accessToken?: string        // Default: 'access_token'
    refreshToken?: string       // Default: 'refresh_token'
    tokenType?: string          // Default: 'token_type'
  }
  retry?: {
    retries?: number            // Default: 2
    exponentialDelay?: boolean  // Default: true
    retryCondition?: (error: unknown) => boolean
    onRetry?: (retryCount: number, error: unknown) => void
  }
  onAuthFailure?: () => void
}

ApiError

Structured error class with helper methods.

try {
  await api.get('/protected')
} catch (error) {
  if (error instanceof ApiError) {
    if (error.isAuthError()) {
      // Handle 401
    }
    if (error.isValidationError()) {
      // Handle 400/422
    }
    if (error.isRetryable) {
      // Can retry this request
    }
  }
}

Query Hooks

useApiQuery(client, queryKey, url, options?)

Fetch data with caching.

const { data, isLoading, error, refetch } = useApiQuery(
  api,
  ['users', { active: true }],
  '/users',
  {
    config: { params: { active: true } },
    staleTime: 1000 * 60 * 5,
    enabled: !!userId,
  }
)

useApiMutation(client, method, url, options?)

Create, update, or delete data.

const mutation = useApiMutation(api, 'POST', '/users', {
  invalidateKeys: [['users']],
  onSuccess: (data) => {
    console.log('Created:', data)
  },
})

mutation.mutate({ name: 'John', email: '[email protected]' })

useApiInfiniteQuery(client, queryKey, url, options?)

Paginated/infinite scroll queries.

const {
  data,
  fetchNextPage,
  hasNextPage,
  isFetchingNextPage,
} = useApiInfiniteQuery<PaginatedResponse<User>>(
  api,
  ['users', 'infinite'],
  '/users',
  { pageSize: 20 }
)

// Flatten pages
const allUsers = data?.pages.flatMap(page => page.content) ?? []

Utilities

useInvalidateQueries()

Manually invalidate cached queries.

const invalidate = useInvalidateQueries()

// Refresh users list
invalidate(['users'])

usePrefetchQuery(client)

Prefetch data on hover/focus.

const prefetch = usePrefetchQuery(api)

<Link
  to={`/users/${id}`}
  onMouseEnter={() => prefetch(['user', id], `/users/${id}`)}
>
  View User
</Link>

Modules

| Module | Import | Description | |--------|--------|-------------| | Client | @daemoniorum/api/client | Axios client factory | | Query | @daemoniorum/api/query | TanStack Query integration | | Types | @daemoniorum/api/types | TypeScript types |

Error Codes

| Code | Status | Description | |------|--------|-------------| | NETWORK_ERROR | - | Unable to reach server | | OFFLINE | - | Browser is offline | | TIMEOUT | - | Request timed out | | UNAUTHORIZED | 401 | Authentication required | | FORBIDDEN | 403 | Permission denied | | NOT_FOUND | 404 | Resource not found | | VALIDATION_ERROR | 400/422 | Invalid input | | RATE_LIMITED | 429 | Too many requests | | SERVER_ERROR | 5xx | Server error |

License

MIT