typescript
export type EventMap = Record<string, unknown[]>;

type Listener<Args extends unknown[]> = (...args: Args) => void;

export class TypedEmitter<Events extends EventMap> {
  private listeners = new Map<keyof Events, Set<Listener<any>>>();

Type-Safe Event Emitter With Strongly-Typed Listener Payloads in TypeScript

typescript events event-emitter
by codesnips 3 tabs
javascript
const MAX_LIMIT = 100;
const DEFAULT_LIMIT = 20;
const ALLOWED_ORDER = new Set(['asc', 'desc']);

function decodeCursor(raw) {
  const json = Buffer.from(raw, 'base64').toString('utf8');

Cursor-Based Pagination in Express With Query-Parsing Middleware

express pagination cursor-pagination
by codesnips 3 tabs
java
package com.example.catalog.web;

import com.example.catalog.domain.Product;
import java.math.BigDecimal;

public record ProductResponse(

Paginated Product Search Endpoint with Spring Data JPA Specifications

spring-boot spring-data-jpa pagination
by codesnips 4 tabs
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_per_sec, ARGV[3] = now (float seconds)
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

Redis-Backed Token Bucket Rate Limiter as FastAPI Middleware

fastapi redis rate-limiting
by codesnips 4 tabs
ruby
require 'sinatra/base'
require_relative 'upload_store'

class UploadApp < Sinatra::Base
  MAX_BYTES = 50 * 1024 * 1024

Streaming Multipart File Uploads to Disk in Sinatra Without Buffering

sinatra rack file-upload
by codesnips 3 tabs
php
<?php

namespace App\Event;

use Symfony\Contracts\EventDispatcher\Event;

Symfony: Send a Welcome Email Asynchronously with Messenger and an Event Subscriber

symfony messenger async
by codesnips 4 tabs
go
package watch

import (
	"sync"
	"time"
)

Debounce Filesystem Change Events Before Triggering a Rebuild in Go

fsnotify debounce filewatcher
by codesnips 3 tabs
rust
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum LogLevel {
    Info,
    Warn,

Streaming Log File Aggregation With Buffered Line Iteration in Rust

rust streaming file-io
by codesnips 3 tabs
lua
-- KEYS[1] = current window key, KEYS[2] = previous window key
-- ARGV: limit, window_ms, elapsed_ms
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local elapsed = tonumber(ARGV[3])

Sliding-Window Rate Limiting for Express Routes Backed by Redis

express redis rate-limiting
by codesnips 4 tabs
ruby
Rails.application.routes.draw do
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :articles, only: [:index, :show, :create, :update, :destroy]
      resources :sessions, only: [:create]
    end

Versioned JSON API Namespace in Rails With a Shared Base Controller

rails api versioning
by codesnips 3 tabs
python
from sqlalchemy import select
from sqlalchemy.orm import Session

from .models import User

BATCH_SIZE = 1000

Stream a Large CSV Export in FastAPI With StreamingResponse and a Generator

fastapi streaming csv
by codesnips 3 tabs
rust
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use uuid::Uuid;

pub struct Id<T> {

Type-Safe Entity IDs in Rust with a Zero-Cost Id<T> Newtype

rust newtype type-safety
by codesnips 3 tabs