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 🙏

© 2026 – Pkg Stats / Ryan Hefner

redux-saga-service-wrapper

v1.1.0

Published

Redux Saga Service Wrapper - Streamlined service calls with error handling and cancellation support

Readme

Redux Saga Service Wrapper

A streamlined TypeScript wrapper for managing Redux Saga service calls with enhanced error handling, automatic cancellation support, and type safety.

npm version TypeScript

🚀 Features

  • Single Saga Management: Handle all service calls with one reusable saga
  • Type Safety: Full TypeScript support with generic types
  • Error Handling: Centralized error management with status code mapping
  • Automatic Cancellation: Built-in Axios cancel token support
  • Timeout Support: Configurable request timeouts
  • Modern: Uses latest Axios and Redux Saga versions

📦 Installation

npm install redux-saga-service-wrapper
# or
yarn add redux-saga-service-wrapper

🛠️ Usage

Basic Service Setup

import axios from 'axios';

export const endpoints = {
  users: () => `/api/users`,
  posts: (id: string) => `/api/posts/${id}`
};

export const getUsers = (): Promise<AxiosResponse<User[]>> => 
  axios.get(endpoints.users());

export const getPost = (id: string): Promise<AxiosResponse<Post>> => 
  axios.get(endpoints.posts(id));

export const createPost = (data: CreatePostRequest): Promise<AxiosResponse<Post>> => 
  axios.post(endpoints.posts(''), data);

GET Service Call

import { serviceWrapperSaga } from 'redux-saga-service-wrapper';

function* getUsersSaga() {
  try {
    const response = yield serviceWrapperSaga(getUsers);
    const users = response.data;
    // Handle successful response
    yield put(setUsers(users));
  } catch (error) {
    // Handle error
    yield put(setError(error.message));
  }
}

POST Service Call with Options

function* createPostSaga(action: CreatePostAction) {
  try {
    const response = yield serviceWrapperSaga(
      createPost,
      {
        timeout: 10000, // 10 second timeout
        handleError: new Map([
          [400, () => console.log('Bad Request')],
          [401, () => console.log('Unauthorized')],
          [500, () => console.log('Server Error')]
        ])
      },
      action.payload
    );
    
    yield put(postCreated(response.data));
  } catch (error) {
    yield put(createPostFailed(error.message));
  }
}

🔧 API Reference

serviceWrapperSaga<T, R>(fn, options?, ...args)

Parameters

  • fn: (...args: T) => Promise<AxiosResponse<R>> - The service function to call
  • options: ServiceWrapperSagaOptions - Optional configuration object
  • ...args: T - Arguments to pass to the service function

Options

interface ServiceWrapperSagaOptions {
  handleError?: Map<number, () => void>; // Status code error handlers
  timeout?: number;                      // Request timeout in milliseconds (default: 30000)
}

Returns

  • Success: AxiosResponse<R> - The complete Axios response
  • Cancellation: undefined - When request is cancelled
  • Error: Throws the original error for saga error handling

🛡️ Error Handling

The wrapper provides comprehensive error handling:

Automatic Error Logging

// Network errors
console.error('Network error - no response received:', error.message);

// Server errors  
console.error(`Service error: ${status} - ${statusText}`, responseData);

// Setup errors
console.error('Service setup error:', error.message);

Custom Error Handlers

const errorHandlers = new Map([
  [400, () => showToast('Invalid request')],
  [401, () => redirectToLogin()],
  [403, () => showAccessDenied()],
  [404, () => showNotFound()],
  [500, () => showServerError()]
]);

function* myServiceSaga() {
  try {
    const response = yield serviceWrapperSaga(
      myService, 
      { handleError: errorHandlers }
    );
  } catch (error) {
    // Additional error handling if needed
  }
}

⚡ Advanced Features

Request Cancellation

Automatic cancellation support when saga is cancelled:

function* cancellableServiceSaga() {
  try {
    // This will be cancelled if the saga is cancelled
    const response = yield serviceWrapperSaga(longRunningService);
  } finally {
    if (yield cancelled()) {
      console.log('Service call was cancelled');
    }
  }
}

Type Safety

Full TypeScript support with generic types:

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

function* typedServiceSaga() {
  // Response is typed as AxiosResponse<User[]>
  const response = yield serviceWrapperSaga<[], User[]>(getUsers);
  const users: User[] = response.data; // Fully typed
}

🔄 Migration from v1.0.6

If upgrading from previous versions:

  1. Function signature changed: handleError is now part of options object
  2. Better typing: Add type parameters for better IntelliSense
  3. New timeout option: Configure per-request timeouts
// Old (v1.0.6)
yield serviceWrapperSaga(myService, errorHandlers, ...args);

// New (v1.0.7+)
yield serviceWrapperSaga(myService, { handleError: errorHandlers }, ...args);

🤝 Contributing

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

📄 License

This project is licensed under the ISC License.

🔗 Links