0.0.18 • Published 7 months ago

@radai/rad-service-client v0.0.18

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

Rad Service Client

Helper library for making service requests.

Quickstart

Install the package from npm:

npm install @radai/rad-service-client

Functions

useQueue

Custom hook to simplify managing, batching and flushing data from a queue in local storage.

This assumes that your data is JSON serializable (needed for localStorage).

FunctionType
useQueue({ flushCallback, batchSize, flushInterval, maxItems, localStorageKey }: LocalQueueOptions<JSONObject>) => { add: (item: JSONObject) => void; addBatch: (items: JSONObject[]) => void; forceFlush: () => void; clear: () => void; getItems: () => JSONObject[]; }

Parameters:

  • options: - Configuration options for the queue.
  • options.flushCallback: - Callback function to be called when the queue is flushed.
  • options.batchSize: - Number of items to batch before flushing.
  • options.flushInterval: - Interval in milliseconds to automatically flush the queue.
  • options.maxItems: - Maximum number of items allowed in the queue.
  • options.localStorageKey: - Key to use for storing the queue in localStorage.

Examples:

const { add, addBatch, forceFlush, clear, getItems } = useQueue({ flushCallback: (items) => console.log('Flushed items:', items), batchSize: 3, flushInterval: 10000, maxItems: 500, localStorageKey: 'myQueue' });

// add items to the queue add({ id: 1, name: 'Item 1' }); // add multiple items to the queue addBatch({ id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }); // force flush the queue immediately forceFlush(); // clear the queue without sending the items clear(); // peek at all items in the queue const items = getItems();

useRetry

Hook that wraps a function with retry logic.

The function will be retried up to retryMaxAttempts times with a backoff time of retryBackoffMs milliseconds between each attempt.

FunctionType
useRetry(retryMaxAttempts?: number, retryBackoffMs?: number) => { executeWithRetry: <T, P>(fn: (payload: P) => Promise<T>, payload: P) => Promise<T>; }

Parameters:

  • retryMaxAttempts: The maximum number of retry attempts.
  • retryBackoffMs: The backoff time in milliseconds between retry attempts.

LocalStorage

Methods

enqueue

MethodType
enqueue(item: T) => void

enqueueBatch

MethodType
enqueueBatch(items: T[]) => void

dequeue

MethodType
dequeue(numberOfItems: number) => T[]

peek

MethodType
peek(numberOfItems: number) => T[]

clear

MethodType
clear() => void

getQueueLength

MethodType
getQueueLength() => number

LocalQueue

Handle the queueing, batching, and flushing of items.

The flushTimer ensures that any items in the queue are flushed at the specified interval. If the batchSize is reached, the queue will flush immediately.

Methods

add

Add a single item to the queue.

The queue will flush if the batchSize is reached or after the flushInterval seconds has passed. If the queue is full, an error will be thrown and the item will not be added.

MethodType
add(item: T) => void

Parameters:

  • item: - The item to add to the queue

addBatch

Add a batch of items to the queue.

The queue will flush if the batchSize is reached or after the flushInterval seconds has passed. If the queue is full, an error will be thrown.

MethodType
addBatch(items: T[]) => void

Parameters:

  • items: - The items to add to the queue

forceFlush

MethodType
forceFlush() => void

clear

MethodType
clear() => void

getQueueLength

MethodType
getQueueLength() => number

getItems

MethodType
getItems() => T[]

RetryHandler

Given a function that accepts a single payload, try to execute it. On failure, retry with exponential backoff.

Interfaces

Storage

PropertyTypeDescription

LocalQueueOptions

PropertyTypeDescription
batchSizenumber or undefined
flushIntervalnumber or undefined
maxItemsnumber or undefined
localStorageKeystring or undefined
flushCallbackFlushCallback<T>
0.0.18

7 months ago

0.0.17

8 months ago

0.0.16

9 months ago

0.0.15

9 months ago

0.0.14

9 months ago

0.0.13

9 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.0.0

11 months ago