typescript
export class AppError extends Error {
  public readonly statusCode: number;
  public readonly code: string;
  public readonly isOperational: boolean;

  constructor(message: string, statusCode = 500, code = 'INTERNAL_ERROR', isOperational = true) {

Backend: normalize errors with a single Express handler

express error-handling typescript
by codesnips 4 tabs
typescript
import React from 'react';
import { useToasts } from './useToasts';

export function Toaster() {
  const { toasts, dismiss } = useToasts();

Frontend: toast notifications via a small event bus

react frontend typescript
by codesnips 3 tabs
typescript
import { useEffect, useRef, useState } from "react";

export type AsyncState<T> =
  | { status: "loading" }
  | { status: "error"; error: Error }
  | { status: "success"; data: T };

Frontend: skeleton loading instead of spinners

react ux typescript
by codesnips 4 tabs
sql
CREATE TABLE events (
    id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    source      text        NOT NULL,
    external_id text        NOT NULL,
    payload     jsonb       NOT NULL,
    occurred_at timestamptz NOT NULL,

Batched writes with COPY (conceptual)

postgres performance sql
by codesnips 3 tabs
typescript
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export async function getFeedNaive(limit = 20) {
  const posts = await prisma.post.findMany({

Prisma: avoid N+1 with include/select

prisma performance typescript
by codesnips 3 tabs
sql
-- Step 1: add the column nullable, no default.
-- Catalog-only change in Postgres 11+, returns instantly.
ALTER TABLE orders
  ADD COLUMN currency text;

-- Optional: keep the lock attempt bounded so a long-running

SQL migration safety: add column nullable, backfill, then constrain

postgres migrations reliability
by codesnips 3 tabs
typescript
import { createHash } from "crypto";
import { readFileSync } from "fs";

export function sha256(query: string): string {
  return createHash("sha256").update(query, "utf8").digest("hex");
}

GraphQL persisted queries (hash allowlist)

graphql security performance
by codesnips 3 tabs
typescript
import { initTRPC, TRPCError } from '@trpc/server';

export interface Context {
  user: { id: string; role: 'user' | 'admin' } | null;
  db: DatabaseClient;
}

tRPC router pattern for type-safe APIs

typescript trpc api
by codesnips 4 tabs
javascript
export const CACHE_VERSION = 'v7';

export const APP_SHELL = '/index.html';

export const PRECACHE_URLS = [
  '/',

Service worker: cache static assets safely

web performance pwa
by codesnips 3 tabs
typescript
import { onCLS, onINP, onLCP, onFCP, onTTFB, type Metric } from 'web-vitals';

export interface VitalPayload {
  id: string;
  name: Metric['name'];
  value: number;

Web Vitals reporting to an API endpoint

performance observability react
by codesnips 3 tabs
typescript
import { NextRequestWithAuth } from 'next/server';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { verifySession } from './lib/verify-session';
import { isProtected, isAuthPage } from './lib/route-matchers';

Next.js middleware for auth gating

nextjs auth security
by codesnips 3 tabs
typescript
import { z } from 'zod';

export const signupSchema = z
  .object({
    email: z.string().min(1, 'Email is required').email('Enter a valid email'),
    password: z

React Hook Form + Zod resolver

react forms react-hook-form
by codesnips 3 tabs