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

react-service-base-api

v0.0.1

Published

A TypeScript-based API client library with built-in error handling, authentication support, and mock capabilities for development

Readme

BaseApi

A TypeScript-based API client library with built-in error handling, authentication support, and mock capabilities for development.

Features

  • 🚀 Simple & Intuitive: Easy-to-use API client with a clean interface
  • 🔒 Authentication Support: Built-in token-based authentication handling
  • 🛡️ Error Handling: Comprehensive error handling with custom error types
  • 🧪 Mock Support: Built-in mock API responses for development and testing
  • 📝 TypeScript: Full TypeScript support with proper type definitions
  • 🔄 HTTP Methods: Support for GET, POST, PUT, DELETE operations

Installation

npm install baseapi

Quick Start

import { apiClient, ApiResponse } from 'baseapi';

// Basic GET request
const response = await apiClient.get<User[]>('/users');
console.log(response.data);

// POST request with data
const newUser = await apiClient.post<User>('/users', {
  name: 'John Doe',
  email: '[email protected]'
});

// Authentication
apiClient.setAuthToken('your-jwt-token');
const protectedData = await apiClient.get<ProtectedData>('/protected');

API Reference

ApiClient

The main API client class that handles HTTP requests.

Constructor

new ApiClient(baseURL?: string)
  • baseURL (optional): Base URL for all API requests. Defaults to /api

Methods

get<T>(endpoint: string, params?: Record<string, any>): Promise<ApiResponse<T>>

Performs a GET request.

post<T>(endpoint: string, data?: any): Promise<ApiResponse<T>>

Performs a POST request with optional data.

put<T>(endpoint: string, data?: any): Promise<ApiResponse<T>>

Performs a PUT request with optional data.

delete<T>(endpoint: string): Promise<ApiResponse<T>>

Performs a DELETE request.

setAuthToken(token: string): void

Sets the authorization token for subsequent requests.

removeAuthToken(): void

Removes the authorization token.

Types

ApiResponse<T>

interface ApiResponse<T> {
  data: T;
  success: boolean;
  message?: string;
  error?: string;
}

ApiError

interface ApiError {
  message: string;
  status?: number;
  code?: string;
}

Mock API Support

For development and testing, you can use the mock API response helper:

import { mockApiResponse } from 'baseapi';

// Mock a successful response with 500ms delay
const mockUsers = await mockApiResponse([
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Smith' }
], 500);

Error Handling

The library provides comprehensive error handling:

try {
  const response = await apiClient.get<User>('/users/123');
  console.log(response.data);
} catch (error) {
  if (error instanceof ApiError) {
    console.error(`API Error: ${error.message} (Status: ${error.status})`);
  } else {
    console.error('Unexpected error:', error);
  }
}

Development

# Install dependencies
npm install

# Build the project
npm run build

# Watch for changes during development
npm run dev

# Run the example
npm start

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Changelog

1.0.0

  • Initial release
  • Basic API client functionality
  • Authentication support
  • Mock API capabilities
  • TypeScript support