@arxyf/fm
v1.2.0
Published
Zero-dependency TypeScript client for the Last.fm API with normalized responses and rate limiting.
Readme
@arxyf/fm
Zero-dependency TypeScript client for the Last.fm API with normalized responses and rate limiting.
Key Features
Zero-Dependency Core: Fully typed TypeScript client for the Last.fm API with no external runtime dependencies.
Normalized Responses: Every method returns parsed, consistently shaped, fully typed responses instead of raw API payloads.
Built-In Rate Limiting: Configurable token-bucket limiter with queueing and backoff for both rate-limit and service-outage scenarios.
Resilient Networking: Automatic retries with configurable strategies for timeouts, rate limits, and outages, backed by request abort timeouts.
Batch Scrobbling: Scrobble single tracks or submit batches in one call, with per-batch success counts on partial failure.
Event-Driven Monitoring: Emits events for failed requests and expired sessions so you can hook in your own logging or alerting.
Requirements
- Node.js:
≥ 18.0.0 - Last.fm API Key & Secret
Installation
npm install @arxyf/fmQuickstart
Initialization (Environment Variables)
If LASTFM_API_KEY, LASTFM_API_SECRET, and LASTFM_API_USER_AGENT are set in your environment, initialization is zero-config:
import { FMClient } from "@arxyf/fm";
const fm = new FMClient();Initialization (Custom Settings)
Alternatively, pass explicit configuration options:
import { FMClient } from "@arxyf/fm";
const fm = new FMClient({
api: {
key: "YOUR_KEY",
secret: "YOUR_SECRET",
userAgent: "YourApp/1.0.0 ([email protected])",
},
rateLimit: {
bucketMax: 3,
refillIntervalMs: 300,
maxQueueSize: 200,
backOffBaseMs: 5000,
backOffOutageBaseMs: 10_000,
},
network: {
retries: 1,
abortTimeoutMs: 4000,
retryStrategy: {
onRateLimit: true,
onServiceOutage: true,
onTimeout: true,
},
},
behavior: {
autoCorrectByDefault: true,
emitRequestFailedOnReject: true,
},
});Usage Examples
Fetching User Info
const res = await fm.user.getLovedTracks({ user: "username" });
if (!res.success) return console.warn(`(${res.errorCode}) ${res.errorMsg}`);
console.info(res.lovedTracks);const res = await fm.user.getNowPlaying("username");
if (!res.success) return console.warn(`User is currently offline.`);
console.info(`Listening to: ${res.track.name} by ${res.track.artist.name}`);Scrobbling
Scrobble a single track, or submit several at once with scrobbleBatch.
const res = await fm.track.scrobble({
sk: "SESSION_KEY",
track: "Aja",
artist: "Steely Dan",
timestamp: Math.floor(Date.now() / 1000),
meta: { userId }, // optional
});
if (!res.success) return console.warn(`(${res.errorCode}) ${res.errorMsg}`);const now = Math.floor(Date.now() / 1000);
const tracks = [
{ track: "Aja", artist: "Steely Dan", timestamp: now },
{ track: "Keith Don't Go", artist: "Nils Lofgren", timestamp: now - 120 },
];
const res = await fm.track.scrobbleBatch({
sk: "SESSION_KEY",
tracks,
meta: { userId }, // optional
});
if (!res.success) {
return console.warn(`(scrobbled: ${res.scrobbledCount}) (${res.errorCode}) ${res.errorMsg}`);
}Event Handling
Listen to internal client events to monitor network behavior or handle session states.
// Failed requests
fm.on("requestFailed", (payload) => {
const { apiMethod, attempt, message, queueSize, willRetry } = payload;
console.warn(`[${apiMethod}]: (Attempt: ${attempt}, Retry: ${willRetry}) (queueSize: ${queueSize}) ${message}`);
});
// Expired sessions
fm.on("sessionExpire", (message, meta) => {
console.info(`[sessionExpire]: ${message}`, meta);
});