codesnips

505 code snips · on codesnips 27 days
javascript
function escapeCell(value) {
  if (value === null || value === undefined) return '';
  const str = String(value);
  if (/[",\n\r]/.test(str)) {
    return '"' + str.replace(/"/g, '""') + '"';
  }

Stream a Large CSV Export in Express with Backpressure and an Async Row Generator

express streaming csv
by codesnips 3 tabs
python
import base64
import json
from datetime import datetime


class InvalidCursor(Exception):

Cursor-Based Pagination for a Flask JSON API Blueprint

flask pagination cursor
by codesnips 3 tabs
ruby
class Article < ApplicationRecord
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

  scope :published, -> { where.not(published_at: nil) }

Filtering a Listing by Tags with a has_many :through Scope and a Query Object

rails activerecord has-many-through
by codesnips 3 tabs
rust
use async_trait::async_trait;
use std::io;

#[async_trait]
pub trait ConnectionFactory: Send + Sync + 'static {
    type Connection: Send + 'static;

Building a Bounded Async Database Connection Pool With Tokio Semaphore

rust tokio async
by codesnips 3 tabs
typescript
import { SetMetadata } from '@nestjs/common';

export enum Role {
  User = 'user',
  Editor = 'editor',
  Admin = 'admin',

Role-Based Access Control in NestJS with a Custom Guard and @Roles Decorator

nestjs authorization rbac
by codesnips 3 tabs
go
package authtransport

import (
	"errors"
	"net/http"
)

Injecting Auth Headers via a Custom http.RoundTripper Decorator in Go

go http middleware
by codesnips 3 tabs
java
@RestController
@RequestMapping("/api/transactions")
public class TransactionExportController {

    private final CsvExportService exportService;

Stream a Large CSV Export to the HTTP Response with StreamingResponseBody in Spring Boot

spring-boot streaming csv
by codesnips 3 tabs
javascript
class ApiError extends Error {
  constructor(statusCode, message, code) {
    super(message);
    this.name = 'ApiError';
    this.statusCode = statusCode;
    this.code = code || null;

Centralized Async Error Handling in Express With asyncHandler and Error Middleware

express nodejs error-handling
by codesnips 4 tabs
python
import io
import os
from uuid import uuid4
from PIL import Image, UnidentifiedImageError

ALLOWED_FORMATS = {"JPEG", "PNG", "WEBP"}

Validate and Generate Image Thumbnails on a Flask Upload Endpoint With Pillow

flask pillow image-processing
by codesnips 3 tabs
javascript
const CACHE_NAME = 'swr-api-v1';
const STAMP_HEADER = 'x-cached-at';

async function open() {
  return caches.open(CACHE_NAME);
}

Stale-While-Revalidate Fetch Wrapper Using the Browser Cache API

cache-api stale-while-revalidate fetch
by codesnips 3 tabs
ruby
class Product < ApplicationRecord
  has_many :reviews, dependent: :destroy

  scope :published, -> { where(published: true) }

  def average_rating

Russian Doll Fragment Caching with Touch Propagation in Rails

rails caching fragment-caching
by codesnips 4 tabs
python
import base64
import json

from django.core.exceptions import BadRequest

Cursor-Paginated JSON Feed with a Django Class-Based ListView

django pagination cursor-pagination
by codesnips 3 tabs