netra-api-logger
v1.2.0
Published
## Overview The Netra API Library is a JavaScript library designed to simplify interactions with the NETRA backend. It provides a straightforward way to handle API calls, manage response statuses, and switch between different server endpoints seamlessly.
Maintainers
Readme
Netra API Library
Overview
The Netra API Library is a JavaScript library designed to simplify interactions with the NETRA backend. It provides a straightforward way to handle API calls, manage response statuses, and switch between different server endpoints seamlessly.
Features
- API Client: A robust client for making GET, POST, PUT, and DELETE requests.
- Status Handling: Automatic handling of common HTTP response statuses (200, 404, 500, etc.).
- Token Management: Functions for generating SAS tokens for secure API access.
- Server Switching: A round-robin mechanism for switching between multiple NETRA API endpoints.
Installation
To install the Netra API Library, use npm:
npm install netra-api-libUsage
Initialization
First, import the library and initialize the API client:
import { ApiClient } from 'netra-api-lib';
const apiClient = new ApiClient();Making Requests
You can make requests using the methods provided by the ApiClient class:
// Example of a GET request
apiClient.get('/endpoint')
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error);
});Handling Tokens
To generate a token for your requests, use the token management functions:
import { generateToken } from 'netra-api-lib/token';
const token = generateToken();Switching Servers
If you need to switch between different NETRA API servers, use the ServerSwitcher class:
import { ServerSwitcher } from 'netra-api-lib/serverSwitcher';
const serverSwitcher = new ServerSwitcher();
const nextServer = serverSwitcher.getNextServer();API Reference
ApiClient: Class for making API requests.
get(endpoint): Makes a GET request to the specified endpoint.post(endpoint, data): Makes a POST request to the specified endpoint with the provided data.put(endpoint, data): Makes a PUT request to the specified endpoint with the provided data.delete(endpoint): Makes a DELETE request to the specified endpoint.
Token Functions:
generateToken(): Generates a SAS token for authentication.
ServerSwitcher: Class for managing server endpoints.
getNextServer(): Returns the next server endpoint in the round-robin sequence.
Contributing
Contributions are welcome! Please submit a pull request or open an issue for any enhancements or bug fixes.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
This module provides functions to store, batch, and push log events to Netra using IndexedDB (via Dexie.js). It supports bulk sending, retry logic, and safe clearing of logs per day.
Functions initDb(customName?: string): Dexie Initializes the IndexedDB database with a custom name (default: "NetraLogsDB").
Parameters:
customName (optional): The name of the database to use. Usage:
initDb("NetraLogsDB-ttweb");
storeLogAndMaybePush(logEvent: object): Promise Stores a log event in IndexedDB. When the total size of pending logs reaches 600KB it pushes all pending logs to Netra in bulk. Uses a batch ID to avoid data loss in multi-tab scenarios.
Parameters:
logEvent: The log data object to store. Behavior:
Adds the log with status "pending". If size limit reached, marks all pending logs as "pushing" with a unique batch ID. Sends only logs with that batch ID. Clears only successfully pushed logs. If push fails, reverts status to "pending" for retry. Usage:
await storeLogAndMaybePush(logData);
clearDbOnNewDayLogin(): Promise Clears the database on a new day (based on date comparison). Deletes the DB and re-initializes it, then stores the new date in the meta table.
Behavior:
Checks the last login date in the meta table. If today is different, deletes the DB and re-initializes. Updates the meta table with the new date. Usage:
await clearDbOnNewDayLogin();
How It Works Batching: Logs are stored in IndexedDB until the size limit is reached, then sent in bulk. Multi-tab Safety: Uses status and batchId fields to avoid clearing logs added during a bulk push. Daily Reset: Clears the DB and resets log IDs on a new day. Recommended Usage Pattern Initialize DB at startup:
initDb("NetraLogsDB-ttweb");
Clear DB on new day (e.g., at login):
await clearDbOnNewDayLogin();
Log events:
await storeLogAndMaybePush(logData);
Notes The log table uses auto-incrementing IDs. Only logs for a successful bulk push are deleted. The meta table stores the last login date for daily resets.
