General Request APIs from Browser Fetch Wrapper Axios, Tanstack Alternative

1 week ago 6


NPM badge for ai-research-agent

GitHub Discussions PRs Welcome

GRAB: General Request APIs from Browser

  1. Data Retrieval: Fetches data from server APIs using JSON parameters and returns JSON responses or error objects
  2. Request/Response Format: Standardized JSON communication for both input parameters and output data
  3. Automatic Loading States: Sets isLoading to true during data fetching operations and false upon completion
  4. Mock Server Support: Configure window.grabMockServer for development and testing environments
  5. Concurrent Request Handling: Cancels duplicate or overlapping requests automatically
  6. Timeout Configuration: Customizable request timeout settings
  7. Rate Limiting: Built-in rate limiting to prevent API abuse
  8. Debug Logging: Comprehensive logging system for request monitoring
  9. Request History: Stores all request and response data in global grabLog object
  10. Pagination Support: Built-in pagination handling for large datasets
  11. Environment Configuration: Configurable base URLs for development and production environments
  12. Frontend Caching: Intelligent caching system that prevents redundant API calls for repeat requests
  13. Modular Design: Single, flexible function that can be called from any part of your application.
  14. Framework Agnostic: No dependency on React hooks or component lifecycle - works with any JavaScript framework
  15. Universal Usage: More flexible than TanStack Query - works outside component initialization,
  16. Minimalist Single Function: Less boilerplate and complexity than axios, SuperAgent, Got
Parameter Type Description

path

string

The path in the API after base url

response

any

Pre-initialized object to store the response in, isLoading and error are also set on this object.

options?

baseURL: string; cache: boolean; cancelIfOngoing: boolean; cancelPrevious: boolean; debug: boolean; method: string; paginateKey: string; paginateResult: string; rateLimit: number; setDefaults: boolean; timeout: number;

Request params for GET or POST and more options

options.baseURL?

string

default='/api/' base url prefix, override with SERVER_API_URL env

options.cache?

boolean

default=false Whether to cache the request and from frontend cache

options.cancelIfOngoing?

boolean

default=false Cancel if a request to path is in progress

options.cancelPrevious?

boolean

default=true Cancel previous requests to same path

options.debug?

boolean

default=false Whether to log the request and response

options.method?

string

default="GET" The HTTP method to use

options.paginateKey?

string

default="page" The key to paginate the request by

options.paginateResult?

string

default=null The key to paginate result data by

options.rateLimit?

number

default=0 If set, how many seconds to wait between requests

options.setDefaults?

boolean

default=false Pass this with options to set those options as defaults for all requests.

options.timeout?

number

default=20 The timeout for the request in seconds

Promise<any>

The response from the server API

vtempest (2025)

import {grab} from "./grab-api"; let responseData = $state({}) as { results: Array<{title:string}>, isLoading: boolean, error: string, }; await grab('search', responseData, { query: "search words", method: 'POST' }) {#if responseData.results} {responseData.results} {:else if responseData.isLoading} ... {:else if responseData.error} {responseData.error} {/if} //Setup Mock testing server, response is object or function window.grabMockServer["search"] = { response: (params) => { return { results: [{title:`Result about ${params.query}`}] }; }, method: "POST", params: { query: "search words" }, delay : 1, }; //set defaults for all requests grab("", {}, { setDefaults: true, baseURL: "http://localhost:8080", timeout: 30, debug: true, rateLimit: 1, cache: true, cancelPrevious: true, cancelIfOngoing: true, });
Read Entire Article