typescript
export type CircuitState = "closed" | "open" | "half-open";

export interface CircuitBreakerOptions {
  failureThreshold: number;
  resetTimeout: number; // ms to wait in "open" before probing
  onStateChange?: (from: CircuitState, to: CircuitState) => void;

Circuit breaker wrapper for flaky third-party APIs

circuit-breaker resilience http
by codesnips 3 tabs
typescript
import { QueryResultRow } from 'pg';
import { pool, Queryable } from './db';

export abstract class BaseRepository<T extends QueryResultRow> {
  protected abstract readonly table: string;

Repository pattern for DB access (small, pragmatic)

node postgres architecture
by codesnips 4 tabs
typescript
import { z } from "zod";

const coerceNumber = z.preprocess((v) => {
  if (v === "" || v === undefined) return undefined;
  if (typeof v !== "string") return v;
  const n = Number(v);

API input coercion for query params (Zod preprocess)

typescript validation api
by codesnips 3 tabs
typescript
import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

type TxClient = Prisma.TransactionClient;

Prisma transaction with retries for serialization errors

prisma postgres concurrency
by codesnips 3 tabs
typescript
import { defineConfig, devices } from '@playwright/test';
import path from 'node:path';

export const authFile = path.join(__dirname, '.auth/user.json');

export default defineConfig({

Playwright smoke test for auth flow

testing playwright e2e
by codesnips 4 tabs
typescript
import express, { Express, NextFunction, Request, Response } from 'express';
import { userRoutes } from './userRoutes';

function authenticate(req: Request, res: Response, next: NextFunction) {
  const header = req.header('authorization');
  if (!header || !header.startsWith('Bearer ')) {

Testing Express routes with Supertest + Jest

testing express supertest
by codesnips 4 tabs
typescript
export type CheckResult = { name: string; status: 'up' | 'down'; durationMs: number; error?: string };
export type Check = () => Promise<void>;

function withTimeout(fn: Check, ms: number): Promise<void> {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => reject(new Error(`timeout after ${ms}ms`)), ms);

Health checks with readiness + liveness

reliability fastify kubernetes
by codesnips 3 tabs
dockerfile
FROM node:20-alpine AS base
WORKDIR /app
RUN apk add --no-cache libc6-compat

FROM base AS deps
COPY package.json package-lock.json ./

Docker multi-stage build for Next.js

docker nextjs multi-stage
by codesnips 4 tabs
yaml
name: CI

on:
  push:
    branches: [main]
  pull_request:

GitHub Actions: cache + tests + build

ci github-actions node
by codesnips 3 tabs
typescript
export interface PageInfo {
  hasNextPage: boolean;
  hasPreviousPage: boolean;
  startCursor: string | null;
  endCursor: string | null;
}

API pagination response contract (page info)

typescript pagination cursor
by codesnips 4 tabs
typescript
export const FLAG_REGISTRY = {
  newDashboard: {
    default: false as boolean,
    description: 'Renders the redesigned dashboard shell',
  },
  maxUploadMb: {

Feature flags with a typed registry

typescript feature-flags react
by codesnips 3 tabs
typescript
import { Writable } from "node:stream";
import type { Pool } from "pg";

interface Row {
  email: string;
  name: string;

Streaming CSV import (Node streams)

streams postgres nodejs
by codesnips 3 tabs