go
package api

import (
	"encoding/json"
	"encoding/xml"
	"net/http"

Content-Negotiating HTTP Handler for JSON and XML in Go

go http content-negotiation
by codesnips 2 tabs
ruby
require 'sinatra/base'
require 'rack/protection'

class TransferApp < Sinatra::Base
  enable :sessions
  set :session_secret, ENV.fetch('SESSION_SECRET', 'a' * 64)

CSRF Protection for Sinatra Forms with Rack::Protection AuthenticityToken

sinatra rack csrf
by codesnips 3 tabs
php
<?php

namespace App\Policies;

use App\Models\Article;
use App\Models\User;

Authorizing Article Edits with a Laravel Policy in Controller and Blade

laravel authorization policies
by codesnips 4 tabs
typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, shareReplay } from 'rxjs/operators';

export interface Project {

Cache and Deduplicate HTTP GET Requests with shareReplay in an Angular Service

angular rxjs caching
by codesnips 3 tabs
go
package httpx

import (
	"net/http"
	"strconv"
)

Enforce a Maximum Request Body Size in Go HTTP Handlers with MaxBytesReader

http middleware security
by codesnips 3 tabs
ruby
class FeatureFlag < ApplicationRecord
  validates :key, presence: true, uniqueness: true
  validates :percentage,
            numericality: { only_integer: true,
                            greater_than_or_equal_to: 0,
                            less_than_or_equal_to: 100 }

Percentage-Based Feature Flag Rollout With a Stable Bucketing Gate in Rails

rails feature-flags rollout
by codesnips 4 tabs
php
<?php

namespace App\Orders;

enum OrderStatus: string
{

Multi-Step Checkout Order State Machine in Laravel

laravel state-machine checkout
by codesnips 3 tabs
typescript
import { Injectable, NgZone } from '@angular/core';
import { Observable, Subject } from 'rxjs';

export interface ObserverOptions {
  rootMargin?: string;
  threshold?: number | number[];

Lazy-Load Images with an Angular IntersectionObserver Directive

angular directive intersectionobserver
by codesnips 4 tabs
javascript
const STORAGE_KEY = "auth.refreshToken";

let accessToken = null;
let refreshToken = localStorage.getItem(STORAGE_KEY);

const listeners = new Set();

Transparent JWT Refresh With a Single-Flight Axios Response Interceptor

axios interceptors jwt
by codesnips 3 tabs
php
<?php

namespace App\Message;

final class PurgeExpiredTokensMessage
{

Recurring Cleanup Task with Symfony Scheduler and Messenger Handler

symfony scheduler messenger
by codesnips 4 tabs
javascript
const { AsyncLocalStorage } = require('async_hooks');

const storage = new AsyncLocalStorage();

function runWithTenant(tenantId, userId, callback) {
  return storage.run({ tenantId, userId }, callback);

Per-Tenant Request Context in Express With AsyncLocalStorage and a Scoped Repository

express multi-tenancy middleware
by codesnips 4 tabs
typescript
import { useCallback, useState } from 'react';

type CopiedValue = string | null;
type CopyFn = (text: string) => Promise<boolean>;

function legacyCopy(text: string): boolean {

React useCopyToClipboard Hook With execCommand Fallback and Reset Timeout

react hooks clipboard
by codesnips 3 tabs