nuglabs
v1.3.4
Published
Local-first SDK for the NugLabs strain API.
Downloads
554
Maintainers
Readme
NugLabs JavaScript SDK
Local-first SDK for https://strains.nuglabs.co.
Current npm package version: 1.3.4.
Design
- Ships with a bundled
src/dataset.json - Ships with a bundled
src/rules.json - Loads bundled data on startup
- Uses persisted local data if a newer synced copy exists
- Performs all reads and searches against local data only
- Auto-syncs from the API every 12 hours
- Supports manual
forceResync()(dataset+rules) - Supports targeted
forceResyncDataset()andforceResyncRules() - Uses ETag conditional requests (
If-None-Match) for sync efficiency - Supports browser persistence with
useBrowserStorage: true - Falls back to memory-only mode if disk writes are not permitted
Install
npm install nuglabsUsage
import {
getStrain,
getAllStrains,
searchStrains,
forceResync,
forceResyncDataset,
forceResyncRules
} from "nuglabs";
const blueDream = await getStrain("Blue Dream");
const allStrains = await getAllStrains();
const matches = await searchStrains("dream");
const sync = await forceResync();
await forceResyncDataset();
await forceResyncRules();
console.log(sync.dataset.changed, sync.rules.changed);import { NugLabsClient } from "nuglabs";
const client = new NugLabsClient();
const strain = await client.getStrain("Blue Dream");
const strains = await client.getAllStrains();
const matches = await client.searchStrains("dream");
await client.forceResync(); // dataset + rules
await client.forceResyncDataset();
await client.forceResyncRules();
client.shutdown();Constructor Options
const client = new NugLabsClient({
cacheInMemory: true,
storageDir: "/tmp/nuglabs",
useBrowserStorage: false,
browserStorageKey: "nuglabs.dataset",
browserStorage: window.localStorage,
syncIntervalMs: 12 * 60 * 60 * 1000,
fetchImpl: fetch
});Sync always uses the canonical dataset URL (see NUGLABS_STRAINS_DATASET_URL in the package exports; matches Rust nuglabs_core::strains_dataset_url()).
Rules sync uses NUGLABS_RULES_URL (matches Rust nuglabs_core::rules_url()).
cacheInMemory: enables the in-memory read cachestorageDir: Node-only persistence directoryuseBrowserStorage: uses browser storage and ignoresstorageDirbrowserStorageKey: key used in browser storagebrowserStorage: custom storage adapter withgetItem()/setItem()syncIntervalMs: background sync interval in millisecondsfetchImpl: custom fetch implementation for sync
Return Shapes
getStrain(name): returns a singleStrain | nullgetAllStrains(): returnsStrain[]searchStrains(query): returnsStrain[]forceResync(): returns{ dataset: NugLabsArtifactSyncResult, rules: NugLabsArtifactSyncResult }forceResyncDataset(): returnsNugLabsArtifactSyncResultforceResyncRules(): returnsNugLabsArtifactSyncResult
Typical Strain fields include:
idnameakastypethcdescription- plus any additional dataset fields bundled with NugLabs
import { NugLabsClient } from "nuglabs";
const client = new NugLabsClient({
useBrowserStorage: true,
browserStorageKey: "nuglabs.dataset"
});Behavior
getStrain(name)does case-insensitive exact matching againstnameandakas[]searchStrains(query)does case-insensitive partial matching againstnameandakas[]getAllStrains()returns the full locally loaded dataset- Reads never call the API directly
- Sync failures keep the last good local artifacts
- Rules endpoint
404is treated asnot-modifiedfor backward-compatible deployments
