Examples

Page 44 of 50

Browse code examples

typescriptSnippet

Typed Async Retry Helper

A generic retry helper that preserves the resolved value type, caps attempts, and backs off between failures without losing type information.

typescriptSnippet

Express Rate Limit Redis Store

Apply distributed rate limiting in Express with express-rate-limit backed by a Redis store, keying by user identity and API token.

typescriptSnippet

Generic API Response Envelope

Wrap API payloads in a generic envelope type carrying status, data, and optional error metadata so clients get full inference from a single shape.

typescriptSnippet

Next SSR Error Boundary Recovery

Contain server rendering failures in Next.js with route-level error boundaries, distinguishing retryable render errors from hard failures.

typescriptSnippet

Pino Multi-Transport Log Routing

Route Pino logs to multiple transports in parallel, sending errors to a dedicated sink while pretty console output stays development-only.

typescriptSnippet

Token Bucket Rate Limiter Async

Implement an async token bucket limiter with burst capacity, refill rate, per-key isolation, and wait semantics for queue-friendly callers.

typescriptSnippet

User-Defined Type Guards

Write predicate functions that narrow unknown values to concrete types, turning runtime checks into compile-time guarantees at the call site.

typescriptSnippet

Type-Safe Event Map Emitter

Bind event names to their payload types in one map so listeners and emitters are checked together and misspelled events fail to compile.

typescriptSnippet

Typed Result for Error Handling

Model success and failure as a discriminated Result type instead of throwing, forcing callers to handle the error branch explicitly at compile time.

typescriptSnippet

Vitest Module Mock Unit Test

Isolate a module under test with Vitest vi.mock hoisting, typed mock functions, and restore between tests so suites stay order-independent.

goSnippet

Env File Parser

Parse dotenv files with export prefixes, quoted values, inline comments, and strict key validation, returning a plain map.

goSnippet

DNS Lookup with Timeout

Resolve a hostname through the default resolver with a context deadline, sort the addresses, and surface lookup failures clearly.