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

@chain1/api-client

v1.0.1

Published

Advanced Axios-based API client with version checking, interceptors, and error handling for Stochain applications

Downloads

12

Readme

@chain1/api-client

Advanced Axios-based API client with version checking, interceptors, and comprehensive error handling for React Native applications.

Features

  • ✅ Automatic version headers in all requests
  • ✅ Built-in version checking with Supabase
  • ✅ Version blocking with user-friendly alerts
  • ✅ Request/Response interceptors
  • ✅ TypeScript support
  • ✅ Configurable timeouts and headers
  • ✅ RESTful methods (GET, POST, PUT, DELETE, PATCH)

Installation

npm install @chain1/api-client

Peer Dependencies

npm install axios react-native-device-info

Usage

Basic Setup

import ApiClient from '@chain1/api-client';

const apiClient = new ApiClient({
  baseURL: 'https://api.example.com',
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json',
  },
});

With Version Checking

import ApiClient from '@chain1/api-client';

const apiClient = new ApiClient({
  baseURL: 'https://api.example.com',
  enableVersionCheck: true,
  versionCheckConfig: {
    supabaseUrl: 'https://your-project.supabase.co',
    supabaseAnonKey: 'your-anon-key',
    appName: 'MyApp',
    downloadUrl: 'https://example.com/download',
  },
});

// Check version manually
try {
  const result = await apiClient.checkVersion();
  if (result.is_blocked) {
    console.log('App version is blocked!');
  }
  if (result.requires_update) {
    console.log('Update available:', result.latest_version);
  }
} catch (error) {
  console.error('Version check failed:', error);
}

Making API Calls

// GET request
const users = await apiClient.get('/users');

// POST request
const response = await apiClient.post('/users', {
  name: 'John Doe',
  email: '[email protected]',
});

// PUT request
const updated = await apiClient.put('/users/123', {
  name: 'Jane Doe',
});

// DELETE request
await apiClient.delete('/users/123');

// PATCH request
const patched = await apiClient.patch('/users/123', {
  email: '[email protected]',
});

Advanced Usage

// Change base URL dynamically
apiClient.setBaseUrl('https://api-v2.example.com');

// Access raw Axios instance
const axiosInstance = apiClient.getAxiosInstance();

// Add custom interceptors
axiosInstance.interceptors.request.use((config) => {
  // Custom logic
  return config;
});

API Reference

Constructor

new ApiClient(config: ApiClientConfig)

ApiClientConfig:

  • baseURL (string, required): Base URL for API requests
  • timeout (number, optional): Request timeout in ms (default: 30000)
  • headers (object, optional): Default headers for all requests
  • enableVersionCheck (boolean, optional): Enable automatic version checking
  • versionCheckConfig (VersionCheckConfig, optional): Version check configuration

VersionCheckConfig:

  • supabaseUrl (string, required): Supabase project URL
  • supabaseAnonKey (string, required): Supabase anonymous key
  • appName (string, optional): App name for headers
  • downloadUrl (string, optional): URL for app download when blocked

Methods

checkVersion(version?, platform?): Promise<VersionCheckResult>

Check app version against server.

const result = await apiClient.checkVersion();
console.log(result.is_blocked, result.requires_update, result.latest_version);

setBaseUrl(url: string): void

Change the base URL dynamically.

apiClient.setBaseUrl('https://new-api.example.com');

get<T>(url, config?): Promise<T>

Perform GET request.

const data = await apiClient.get<User[]>('/users');

post<T>(url, body?, config?): Promise<AxiosResponse<T>>

Perform POST request.

const response = await apiClient.post('/users', userData);

put<T>(url, body?, config?): Promise<AxiosResponse<T>>

Perform PUT request.

delete<T>(url, config?): Promise<AxiosResponse<T>>

Perform DELETE request.

patch<T>(url, body?, config?): Promise<AxiosResponse<T>>

Perform PATCH request.

getAxiosInstance(): AxiosInstance

Access the underlying Axios instance for advanced customization.

Automatic Headers

All requests automatically include:

  • X-App-Version: App version from device info
  • X-Build-Number: Build number from device info
  • X-Platform: Platform (ios/android)
  • X-App-Name: App name (if configured)

Version Blocking

When server responds with HTTP 426 (Upgrade Required), the client:

  1. Shows alert dialog with version information
  2. Provides option to download latest version
  3. Allows user to exit the app
  4. Prevents further API usage until updated

Error Handling

try {
  const data = await apiClient.get('/users');
} catch (error) {
  if (error.response?.status === 401) {
    console.log('Unauthorized');
  } else if (error.response?.status === 426) {
    console.log('Version blocked');
  } else {
    console.error('API Error:', error);
  }
}

TypeScript Support

Full TypeScript support with type definitions included.

interface User {
  id: string;
  name: string;
  email: string;
}

const users = await apiClient.get<User[]>('/users');

License

MIT