typescript
import jwt, { JwtPayload, SignOptions } from 'jsonwebtoken';

const SECRET = process.env.JWT_SECRET as string;
const ISSUER = 'auth.example.com';
const AUDIENCE = 'api.example.com';

Signing and Verifying JWT Access Tokens with Express Middleware

jwt express authentication
by codesnips 3 tabs
php
<?php

namespace App\Entity;

trait RecordsEvents
{

Dispatch Domain Events After Doctrine Flush with a postFlush Subscriber in Symfony

symfony doctrine messenger
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
import asyncio
import uuid
from dataclasses import dataclass, field
from typing import Any, Dict, Optional

Long-Polling a Background Job's Status with a Shared Result Registry in FastAPI

fastapi asyncio long-polling
by codesnips 3 tabs
rust
use serde::{Deserialize, Deserializer};

#[derive(Debug, Deserialize)]
pub struct ListFilter {
    #[serde(default)]
    pub status: Status,

Parse URL Query Strings into a Typed Filter Struct with Defaults in Rust

rust serde query-string
by codesnips 3 tabs
java
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

import Transaction.Category;

Grouping and Summarizing Transactions with Java Stream Collectors

java streams collectors
by codesnips 3 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
php
<?php

namespace App\Validator;

use Symfony\Component\Validator\Constraint;

Validate a Symfony Signup DTO With a Custom Constraint and Validator

symfony validation dto
by codesnips 4 tabs
php
<?php

use App\Models\Conversation;
use App\Models\User;
use Illuminate\Support\Facades\Broadcast;

Broadcasting a Chat Message with Laravel Echo and Blade Components

laravel broadcasting websockets
by codesnips 4 tabs
java
package com.shop.orders.events;

import java.math.BigDecimal;
import java.time.Instant;

public record OrderPlacedEvent(

Transactional Domain Events With Spring's ApplicationEventPublisher and @TransactionalEventListener

spring spring-boot domain-events
by codesnips 4 tabs
typescript
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { FeatureFlagGuard } from './feature-flag.guard';
import { RequireFeature } from './feature-flag.decorator';

@Controller('experiments')
@UseGuards(FeatureFlagGuard)

Per-Request Feature Flags in NestJS with a Custom Decorator and Resolution Guard

nestjs feature-flags guards
by codesnips 4 tabs
ruby
class AddSoftDeleteToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :deleted_at, :datetime

    add_index :users, :deleted_at, where: "deleted_at IS NULL", name: "index_users_on_live"

Soft-Delete with a Default Scope, Restore Action, and Unique Index Guard in Rails

rails activerecord soft-delete
by codesnips 3 tabs