@radai/rad-service-client v0.0.18
Rad Service Client
Helper library for making service requests.
Quickstart
Install the package from npm:
npm install @radai/rad-service-clientFunctions
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).
| Function | Type |
|---|---|
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.
| Function | Type |
|---|---|
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
| Method | Type |
|---|---|
enqueue | (item: T) => void |
enqueueBatch
| Method | Type |
|---|---|
enqueueBatch | (items: T[]) => void |
dequeue
| Method | Type |
|---|---|
dequeue | (numberOfItems: number) => T[] |
peek
| Method | Type |
|---|---|
peek | (numberOfItems: number) => T[] |
clear
| Method | Type |
|---|---|
clear | () => void |
getQueueLength
| Method | Type |
|---|---|
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.
| Method | Type |
|---|---|
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.
| Method | Type |
|---|---|
addBatch | (items: T[]) => void |
Parameters:
items: - The items to add to the queue
forceFlush
| Method | Type |
|---|---|
forceFlush | () => void |
clear
| Method | Type |
|---|---|
clear | () => void |
getQueueLength
| Method | Type |
|---|---|
getQueueLength | () => number |
getItems
| Method | Type |
|---|---|
getItems | () => T[] |
RetryHandler
Given a function that accepts a single payload, try to execute it. On failure, retry with exponential backoff.
Interfaces
Storage
| Property | Type | Description |
|---|
LocalQueueOptions
| Property | Type | Description |
|---|---|---|
batchSize | number or undefined | |
flushInterval | number or undefined | |
maxItems | number or undefined | |
localStorageKey | string or undefined | |
flushCallback | FlushCallback<T> |
7 months ago
8 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago