Skip to main content

grab-api

Documentation / grab-api

GrabResponse<T>

Defined in: grab-api.ts:540

Core response object that gets populated with API response data

Type Parameters

Type ParameterDefault type

T

any

Indexable

[key: string]: string | boolean | T

The actual response data - type depends on API endpoint

Properties

PropertyTypeDescriptionDefined in

isLoading?

boolean

Indicates if request is currently in progress

grab-api.ts:542

error?

string

Error message if request failed

grab-api.ts:544


GrabOptions<TResponse, TParams>

Defined in: grab-api.ts:550

Extended by

Type Parameters

Type ParameterDefault type

TResponse

any

TParams

Record<string, any>

Indexable

[key: string]: any

All other params become GET params, POST body, and other methods

Properties

PropertyTypeDescriptionDefined in

headers?

Record<string, string>

include headers and authorization in the request

grab-api.ts:552

response?

Record<string, any>

Pre-initialized object which becomes response JSON, no need for .data

grab-api.ts:554

method?

"GET" | "POST" | "PUT" | "PATCH" | "DELETE"

default="GET" The HTTP method to use

grab-api.ts:556

cache?

boolean

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

grab-api.ts:558

staleTime?

number

default=60 Seconds to consider data stale and invalidate cache

grab-api.ts:560

timeout?

number

default=20 The timeout for the request in seconds

grab-api.ts:562

baseURL?

string

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

grab-api.ts:564

cancelOngoingIfNew?

boolean

default=true Cancel previous requests to same path

grab-api.ts:566

cancelNewIfOngoing?

boolean

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

grab-api.ts:568

rateLimit?

number

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

grab-api.ts:570

debug?

boolean

default=false Whether to log the request and response

grab-api.ts:572

infiniteScroll?

[string, string, string]

default=null [page key, response field to concatenate, element with results]

grab-api.ts:574

setDefaults?

boolean

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

grab-api.ts:576

retryAttempts?

number

default=0 Retry failed requests this many times

grab-api.ts:578

logger?

(...args: any[]) => void

default=log Custom logger to override the built-in color JSON log()

grab-api.ts:580

onBeforeRequest?

(...args: any[]) => any

Set with defaults to modify each request data. Takes and returns in order: path, response, params, fetchParams

grab-api.ts:582

onAfterRequest?

(...args: any[]) => any

Set with defaults to modify each request data. Takes and returns in order: path, response, params, fetchParams

grab-api.ts:584

repeat?

number

default=0 Repeat request this many times

grab-api.ts:586

repeatEvery?

number

default=null Repeat request every seconds

grab-api.ts:588

debounce?

number

default=0 Seconds to debounce request, wait to execute so that other requests may override

grab-api.ts:590

regrabOnStale?

boolean

default=false Refetch when cache is past staleTime

grab-api.ts:592

regrabOnFocus?

boolean

default=false Refetch on window refocus

grab-api.ts:594

regrabOnNetwork?

boolean

default=false Refetch on network change

grab-api.ts:596

post?

boolean

shortcut for method: "POST"

grab-api.ts:598

put?

boolean

shortcut for method: "PUT"

grab-api.ts:600

patch?

boolean

shortcut for method: "PATCH"

grab-api.ts:602

body?

any

default=null The body of the POST/PUT/PATCH request (can be passed into main)

grab-api.ts:604


GrabRequestConfig<TResponse, TParams>

Defined in: grab-api.ts:611

Combined options and parameters interface

Extends

Type Parameters

Type ParameterDefault type

TResponse

any

TParams

Record<string, any>

Indexable

[key: string]: any

All other params become GET params, POST body, and other methods

Properties

PropertyTypeDescriptionInherited fromDefined in

headers?

Record<string, string>

include headers and authorization in the request

GrabOptions.headers

grab-api.ts:552

response?

Record<string, any>

Pre-initialized object which becomes response JSON, no need for .data

GrabOptions.response

grab-api.ts:554

method?

"GET" | "POST" | "PUT" | "PATCH" | "DELETE"

default="GET" The HTTP method to use

GrabOptions.method

grab-api.ts:556

cache?

boolean

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

GrabOptions.cache

grab-api.ts:558

staleTime?

number

default=60 Seconds to consider data stale and invalidate cache

GrabOptions.staleTime

grab-api.ts:560

timeout?

number

default=20 The timeout for the request in seconds

GrabOptions.timeout

grab-api.ts:562

baseURL?

string

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

GrabOptions.baseURL

grab-api.ts:564

cancelOngoingIfNew?

boolean

default=true Cancel previous requests to same path

GrabOptions.cancelOngoingIfNew

grab-api.ts:566

cancelNewIfOngoing?

boolean

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

GrabOptions.cancelNewIfOngoing

grab-api.ts:568

rateLimit?

number

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

GrabOptions.rateLimit

grab-api.ts:570

debug?

boolean

default=false Whether to log the request and response

GrabOptions.debug

grab-api.ts:572

infiniteScroll?

[string, string, string]

default=null [page key, response field to concatenate, element with results]

GrabOptions.infiniteScroll

grab-api.ts:574

setDefaults?

boolean

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

GrabOptions.setDefaults

grab-api.ts:576

retryAttempts?

number

default=0 Retry failed requests this many times

GrabOptions.retryAttempts

grab-api.ts:578

logger?

(...args: any[]) => void

default=log Custom logger to override the built-in color JSON log()

GrabOptions.logger

grab-api.ts:580

onBeforeRequest?

(...args: any[]) => any

Set with defaults to modify each request data. Takes and returns in order: path, response, params, fetchParams

GrabOptions.onBeforeRequest

grab-api.ts:582

onAfterRequest?

(...args: any[]) => any

Set with defaults to modify each request data. Takes and returns in order: path, response, params, fetchParams

GrabOptions.onAfterRequest

grab-api.ts:584

repeat?

number

default=0 Repeat request this many times

GrabOptions.repeat

grab-api.ts:586

repeatEvery?

number

default=null Repeat request every seconds

GrabOptions.repeatEvery

grab-api.ts:588

debounce?

number

default=0 Seconds to debounce request, wait to execute so that other requests may override

GrabOptions.debounce

grab-api.ts:590

regrabOnStale?

boolean

default=false Refetch when cache is past staleTime

GrabOptions.regrabOnStale

grab-api.ts:592

regrabOnFocus?

boolean

default=false Refetch on window refocus

GrabOptions.regrabOnFocus

grab-api.ts:594

regrabOnNetwork?

boolean

default=false Refetch on network change

GrabOptions.regrabOnNetwork

grab-api.ts:596

post?

boolean

shortcut for method: "POST"

GrabOptions.post

grab-api.ts:598

put?

boolean

shortcut for method: "PUT"

GrabOptions.put

grab-api.ts:600

patch?

boolean

shortcut for method: "PATCH"

GrabOptions.patch

grab-api.ts:602

body?

any

default=null The body of the POST/PUT/PATCH request (can be passed into main)

GrabOptions.body

grab-api.ts:604


GrabMockHandler<TParams, TResponse>

Defined in: grab-api.ts:617

Mock server configuration for testing

Type Parameters

Type ParameterDefault type

TParams

any

TResponse

any

Properties

PropertyTypeDescriptionDefined in

response

TResponse | (params: TParams) => TResponse

Mock response data or function that returns response

grab-api.ts:619

method?

string

HTTP method this mock should respond to

grab-api.ts:621

params?

TParams

Request parameters this mock should match

grab-api.ts:623

delay?

number

Delay in seconds before returning mock response

grab-api.ts:625


GrabLogEntry

Defined in: grab-api.ts:629

Request log entry for debugging and history

Properties

PropertyTypeDescriptionDefined in

path

string

API path that was requested

grab-api.ts:631

request

string

Stringified request parameters

grab-api.ts:633

response?

any

Response data (only present for successful requests)

grab-api.ts:635

error?

string

Error message (only present for failed requests)

grab-api.ts:637

lastFetchTime

number

Timestamp when request was made

grab-api.ts:639

controller?

AbortController

Abort controller for request cancellation

grab-api.ts:641

currentPage?

number

Current page number for paginated requests

grab-api.ts:643


GrabGlobal

Defined in: grab-api.ts:647

Global grab configuration and state

Properties

PropertyTypeDescriptionDefined in

defaults?

Partial<GrabOptions>

Default options applied to all requests

grab-api.ts:649

log?

GrabLogEntry[]

Request history and debugging info

grab-api.ts:651

mock?

Record<string, GrabMockHandler>

Mock server handlers for testing

grab-api.ts:653

instance?

(defaultOptions?: Partial<GrabOptions>) => GrabFunction

Create a separate instance of grab with separate default options

grab-api.ts:655


GrabFunction()

Defined in: grab-api.ts:659

Main grab function signature with overloads for different use cases

Call Signature

GrabFunction<TResponse>(path: string): Promise<GrabResponse<TResponse>>;

Defined in: grab-api.ts:668

Main grab function signature with overloads for different use cases

Type Parameters

Type ParameterDefault type

TResponse

any

Parameters

ParameterType

path

string

Returns

Promise<GrabResponse<TResponse>>

The response object with resulting data or .error if error.

Author

vtempest (2025)

See

🎯 Examples 📑 Docs

Call Signature

GrabFunction<TResponse, TParams>(path: string, config: GrabRequestConfig<TResponse, TParams>): Promise<GrabResponse<TResponse>>;

Defined in: grab-api.ts:678

Main grab function signature with overloads for different use cases

Type Parameters

Type ParameterDefault type

TResponse

any

TParams

Record<string, any>

Parameters

ParameterType

path

string

config

GrabRequestConfig<TResponse, TParams>

Returns

Promise<GrabResponse<TResponse>>

The response object with resulting data or .error if error.

Author

vtempest (2025)

See

🎯 Examples 📑 Docs

Properties

PropertyTypeDescriptionDefined in

defaults?

Partial<GrabOptions>

Default options applied to all requests

grab-api.ts:684

log?

GrabLogEntry[]

Request history and debugging info for all requests

grab-api.ts:687

mock?

Record<string, GrabMockHandler>

Mock server handlers for testing

grab-api.ts:690

instance?

(defaultOptions?: Partial<GrabOptions>) => GrabFunction

Create a separate instance of grab with separate default options

grab-api.ts:693


LogFunction()

Defined in: grab-api.ts:697

Log function for debugging

LogFunction(
message: string | object,
hideInProduction?: boolean,
style?: string): void;

Defined in: grab-api.ts:704

Log function for debugging

Parameters

ParameterTypeDescription

message

string | object

Message to log (string or object)

hideInProduction?

boolean

Whether to hide in production (auto-detected if undefined)

style?

string

CSS style string for console output

Returns

void


PrintStructureJSONFunction()

Defined in: grab-api.ts:708

Utility function to describe JSON structure

PrintStructureJSONFunction(obj: any): string;

Defined in: grab-api.ts:714

Utility function to describe JSON structure

Parameters

ParameterTypeDescription

obj

any

The JSON object to describe

Returns

string

String representation of object structure


GrabResponseWithData<T>

type GrabResponseWithData<T> = GrabResponse<T> & object;

Defined in: grab-api.ts:719

Type helpers for common use cases

Type declaration

NameTypeDefined in

data?

T

grab-api.ts:720

Type Parameters

Type Parameter

T


TypedGrabFunction()

type TypedGrabFunction = <TResponse, TParams>(path: string, config?: GrabRequestConfig<TResponse, TParams>) => Promise<GrabResponse<TResponse>>;

Defined in: grab-api.ts:725

Helper type for creating typed API clients

Type Parameters

Type ParameterDefault type

TResponse

any

TParams

Record<string, any>

Parameters

ParameterType

path

string

config?

GrabRequestConfig<TResponse, TParams>

Returns

Promise<GrabResponse<TResponse>>


grab()

function grab(path: string, options?: GrabOptions): Promise<
| Record<string, any>
| (...args: any[]) => Promise<void>>;

Defined in: grab-api.ts:81

GRAB: Generate Request to API from Browser

GrabAPILogo GRAB is the FBEST Request Manager: Functionally Brilliant, Elegantly Simple Tool

  1. One Function: 3Kb min, 0 dependencies, minimalist syntax, more features than top alternatives
  2. Auto-JSON Convert: Pass parameters and get response or error in JSON, handling other data types as is.
  3. isLoading Status: Sets .isLoading=true on the pre-initialized response object so you can show a "Loading..." in any framework
  4. Debug Logging: Adds global log() and prints colored JSON structure, response, timing for requests in test.
  5. Mock Server Support: Configure window.grab.mock for development and testing environments
  6. Cancel Duplicates: Prevent this request if one is ongoing to same path & params, or cancel the ongoing request.
  7. Timeout & Retry: Customizable request timeout, default 20s, and auto-retry on error
  8. DevTools: Ctrl+I overlays webpage with devtools showing all requests and responses, timing, and JSON structure.
  9. Request History: Stores all request and response data in global grab.log object
  10. Pagination Infinite Scroll: Built-in pagination for infinite scroll to auto-load and merge next result page, with scroll position recovery.
  11. Base URL Based on Environment: Configure grab.defaults.baseURL once at the top, overide with SERVER_API_URL in .env.
  12. Frontend Cache: Set cache headers and retrieve from frontend memory for repeat requests to static data.
  13. Regrab On Error: Regrab on timeout error, or on window refocus, or on network change, or on stale data.
  14. Framework Agnostic: Alternatives like TanStack work only in component initialization and depend on React & others.
  15. Globals: Adds to window in browser or global in Node.js so you only import once: grab(), log(), grab.log, grab.mock, grab.defaults
  16. TypeScript Tooltips: Developers can hover over option names and autocomplete TypeScript.
  17. Request Stategies: 🎯 Examples show common stategies like debounce, repeat, proxy, unit tests, interceptors, file upload, etc
  18. Rate Limiting: Built-in rate limiting to prevent multi-click cascading responses, require to wait seconds between requests.
  19. Repeat: Repeat request this many times, or repeat every X seconds to poll for updates.
  20. Loading Icons: Import from grab-api.js/icons to get enhanced animated loading icons.

Parameters

ParameterTypeDescription

path

string

The full URL path OR relative path on this server after grab.defaults.baseURL

options?

GrabOptions

Request params for GET or body for POST/PUT/PATCH and utility options

Returns

Promise< | Record<string, any> | (...args: any[]) => Promise<void>>

The response object with resulting data or .error if error.

Author

vtempest (2025)

See

🎯 Examples 📑 Docs

Example

import grab from 'grab-api.js';
let res = {};
await grab('search', {
response: res,
query: "search words"
})

default

Renames and re-exports grab


log

Re-exports log


showAlert

Re-exports showAlert


printStructureJSON

Re-exports printStructureJSON