1.0.0 • Published 6 months ago

get-set-action v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Action Management Library

This library provides a set of utilities for managing actions, async actions, and fetch operations, particularly useful for making AI calls. It is designed to simplify the handling of asynchronous operations and state management in JavaScript applications.

Features

  • Debounce and Throttle: Easily debounce or throttle your functions to control the rate of execution.
  • Async Actions: Manage asynchronous operations with built-in support for debouncing, throttling, and state management.
  • Fetch Utility: Simplified fetch operations with interceptors for request, response, and error handling.
  • State Management: Track and manage the state of your actions with ease.

Installation

To use this library, simply import the functions you need from the index.ts file.

Usage

Action

The action function allows you to create enhanced functions with interceptors and subscription capabilities.

API

  • intercept: Add an interceptor to modify the input before execution.
  • subscribe: Subscribe to the result of the action.
  • retry: Retry the last action with the same parameters.
  • getState: Get the current state of the action.

Example

import { action } from './index';

const myAction = action((props) => {
  console.log('Action executed with:', props);
  return props;
});

myAction.intercept((props) => {
  console.log('Intercepted:', props);
  return { ...props, intercepted: true };
});

myAction.subscribe((result) => {
  console.log('Result:', result);
});

myAction({ key: 'value' });

AsyncAction

The asyncAction function is used to manage asynchronous operations with support for debouncing and throttling.

API

  • run: Execute the async action.
  • abort: Abort the current async operation.
  • intercept: Add an interceptor to modify the input before execution.
  • interceptAfter: Add an interceptor to modify the result after execution.
  • interceptError: Add an interceptor to handle errors.

Example

import { asyncAction } from './index';

const myAsyncAction = asyncAction(async (payload) => {
  const response = await fetch(payload.url);
  return response.json();
}, { debounce: 500 });

myAsyncAction.run({ url: 'https://api.example.com/data' })
  .then(data => console.log('Data:', data))
  .catch(error => console.error('Error:', error));

Fetch

The Fetch function simplifies fetch operations with built-in interceptors for request, response, and error handling.

API

  • intercept: Add an interceptor to modify the request before execution.
  • interceptAfter: Add an interceptor to modify the response after execution.
  • interceptError: Add an interceptor to handle errors.

Example

import { Fetch } from './index';

const fetchAction = Fetch({ debounce: 1000 });

fetchAction.run({ url: 'https://api.example.com/data' })
  .then(response => console.log('Response:', response))
  .catch(error => console.error('Error:', error));

fetchAction.intercept((payload) => {
  console.log('Request Intercepted:', payload);
  return payload;
});

fetchAction.interceptAfter((data) => {
  console.log('Response Intercepted:', data);
  return data;
});

fetchAction.interceptError((error) => {
  console.error('Error Intercepted:', error);
  return error;
});

Conclusion

This library provides a comprehensive set of tools for managing actions and asynchronous operations in your applications. Whether you need to debounce a function, manage async actions, or perform fetch operations with custom interceptors, this library has you covered.

1.0.0

6 months ago