Production-ready implementations for common development challenges. Copy, paste, and ship with confidence.
Find solutions for every aspect of your application
Most trusted implementations used by thousands of developers
Complete JWT implementation with access/refresh token rotation, blacklist support, and secure cookie handling.
Distributed rate limiting using Redis with sliding window algorithm. Handles IP-based and user-based limiting.
Advanced Prisma patterns for complex queries, N+1 prevention, and connection pooling best practices.
Production rate limiter using Redis sliding window algorithm
Fluent SQL query builder with full TypeScript type inference
Secure OAuth 2.0 authorization with PKCE for SPA and mobile apps
Secure OAuth 2.0 authorization with PKCE
Distributed web scraper with URL queue and concurrent fetching
Compute deep differences and apply patches to objects
Readable test builder with fluent API chaining
Copy-paste ready utilities for everyday problems
async function retryWithBackoff(fn, options = {}) {
const {
maxAttempts = 3,
baseDelay = 1000,
maxDelay = 10000,
jitter = true
} = options;
let lastError;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (attempt === maxAttempts) {
throw error;
}
const delay = Math.min(
baseDelay * Math.pow(2, attempt - 1),
maxDelay
);
const finalDelay = jitter
? delay + Math.random() * 1000
: delay;
console.log(`Attempt ${attempt} failed, retrying in ${Math.round(finalDelay)}ms`);
await new Promise(resolve => setTimeout(resolve, finalDelay));
}
}
throw lastError;
}
class MemoryCache {
constructor(options = {}) {
this.cache = new Map();
this.ttl = options.ttl || 60000;
this.cleanupInterval = options.cleanupInterval || 60000;
this.startCleanup();
}
set(key, value, ttl = this.ttl) {
const expiresAt = Date.now() + ttl;
this.cache.set(key, { value, expiresAt });
return this;
}
get(key) {
const item = this.cache.get(key);
if (!item) return null;
if (Date.now() > item.expiresAt) {
this.delete(key);
return null;
}
return item.value;
}
has(key) {
return this.get(key) !== null;
}
delete(key) {
return this.cache.delete(key);
}
clear() {
this.cache.clear();
}
get size() {
return this.cache.size;
}
startCleanup() {
this.timer = setInterval(() => {
const now = Date.now();
for (const [key, item] of this.cache.entries()) {
if (now > item.expiresAt) {
this.cache.delete(key);
}
}
}, this.cleanupInterval);
}
stopCleanup() {
if (this.timer) clearInterval(this.timer);
}
getOrSet(key, fn, ttl) {
const value = this.get(key);
if (value !== null) return value;
const newValue = fn();
this.set(key, newValue, ttl);
return newValue;
}
}
function formatNaturalTime(date, now = new Date()) {
const then = new Date(date);
const diffMs = now - then;
const diffSecs = Math.floor(diffMs / 1000);
const diffMins = Math.floor(diffSecs / 60);
const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24);
const diffWeeks = Math.floor(diffDays / 7);
const diffMonths = Math.floor(diffDays / 30);
const diffYears = Math.floor(diffDays / 365);
if (diffSecs < 60) return "just now";
if (diffMins < 60) return `${diffMins} minute${diffMins !== 1 ? "s" : ""} ago`;
if (diffHours < 24) return `${diffHours} hour${diffHours !== 1 ? "s" : ""} ago`;
if (diffDays < 7) return `${diffDays} day${diffDays !== 1 ? "s" : ""} ago`;
if (diffWeeks < 4) return `${diffWeeks} week${diffWeeks !== 1 ? "s" : ""} ago`;
if (diffMonths < 12) return `${diffMonths} month${diffMonths !== 1 ? "s" : ""} ago`;
return `${diffYears} year${diffYears !== 1 ? "s" : ""} ago`;
}
class URLBuilder {
constructor(baseUrl = "") {
this.baseUrl = baseUrl;
this.queryParams = {};
this.hash = "";
}
setParam(key, value) {
if (value === null || value === undefined) {
delete this.queryParams[key];
} else if (Array.isArray(value)) {
this.queryParams[key] = value;
} else {
this.queryParams[key] = String(value);
}
return this;
}
setParams(params) {
for (const [key, value] of Object.entries(params)) {
this.setParam(key, value);
}
return this;
}
setHash(hash) {
this.hash = hash;
return this;
}
toString() {
let url = this.baseUrl;
const query = this.buildQuery();
if (query) url += "?" + query;
if (this.hash) url += "#" + encodeURIComponent(this.hash);
return url;
}
buildQuery() {
const params = [];
for (const [key, value] of Object.entries(this.queryParams)) {
if (Array.isArray(value)) {
for (const v of value) {
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(v)}`);
}
} else {
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
}
}
return params.join("&");
}
}
Step-by-step tutorials for complex integrations
Can't find what you're looking for? Request a solution and our community of experts will help you build it.