@r6-data-js/runtime
v0.1.0
Published
Runtime layer for r6-data.js with caching, request deduplication, retry, timeout and local rate limiting.
Downloads
104
Maintainers
Readme
@r6-data-js/runtime
Optional runtime layer for r6-data.js with memory caching, request deduplication, retry, timeout and local rate limiting.
This package does not replace r6-data.js. You still create and configure the main R6Client yourself, then pass that instance to createR6Runtime.
It does not make direct API calls to R6Data and has no runtime dependencies.
Installation
npm install r6-data.js @r6-data-js/runtimeBasic Usage
import { R6Client } from 'r6-data.js';
import { createR6Runtime } from '@r6-data-js/runtime';
const client = new R6Client({ apiKey: process.env.R6_DATA_API_KEY! });
const r6 = createR6Runtime(client);
const stats = await r6.players.getPlayerStats({
nameOnPlatform: 'PlayerName',
platformType: 'uplay',
platform_families: 'pc',
board_id: 'ranked',
});Memory Cache
import { R6Client } from 'r6-data.js';
import { createR6Runtime, MemoryCacheAdapter } from '@r6-data-js/runtime';
const client = new R6Client({ apiKey: process.env.R6_DATA_API_KEY! });
const r6 = createR6Runtime(client, {
cache: new MemoryCacheAdapter(),
ttl: 60_000,
});MemoryCacheAdapter supports TTL, delete() and clear(). Expired entries are removed when they are read. It does not use a required setInterval.
Retry And Timeout
const r6 = createR6Runtime(client, {
timeout: 10_000,
retry: {
retries: 2,
delayMs: 500,
retryOn: [408, 429, 500, 502, 503, 504],
},
});Retry uses a simple backoff: delayMs * attempt. Timeout rejects with R6RuntimeTimeoutError.
Local Rate Limit
const r6 = createR6Runtime(client, {
rateLimit: {
maxRequests: 5,
intervalMs: 1000,
},
});The rate limiter is an in-memory queue. It limits local execution only; it does not coordinate across multiple processes or servers.
Custom Cache Adapter
import type { CacheAdapter } from '@r6-data-js/runtime';
class MyCache implements CacheAdapter {
private values = new Map<string, unknown>();
get(key: string) {
return this.values.get(key);
}
set(key: string, value: unknown) {
this.values.set(key, value);
}
delete(key: string) {
this.values.delete(key);
}
clear() {
this.values.clear();
}
}@r6-data-js/runtime intentionally does not include Redis, PostgreSQL, MongoDB or other databases. Use CacheAdapter if your app needs a custom cache backend.
Options
createR6Runtime(client, {
cache,
ttl,
dedupe,
timeout,
retry,
rateLimit,
cacheKey,
shouldCache,
onRequest,
onResponse,
onError,
});cache:CacheAdapterorfalse. Disabled by default.ttl: cache TTL in milliseconds.dedupe: avoids simultaneous duplicate calls with the same method path and args. Enabled by default.timeout: timeout in milliseconds.retry: retry settings for configured HTTP status codes.rateLimit: local queue settings.cacheKey: custom cache key function.shouldCache: decide whether a result should be cached.onRequest,onResponse,onError: lightweight lifecycle hooks.
License
MIT
