rest-api

ruby
module Paginatable
  extend ActiveSupport::Concern

  MAX_PER_PAGE = 100
  DEFAULT_PER_PAGE = 25

API Pagination Headers (Link + Total)

rails pagination rest-api
by codesnips 3 tabs
typescript
export interface PageInfo {
  hasNextPage: boolean;
  hasPreviousPage: boolean;
  startCursor: string | null;
  endCursor: string | null;
}

API pagination response contract (page info)

typescript pagination cursor
by codesnips 4 tabs
java
package com.example.demo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;

Exception handling with @ControllerAdvice

java spring-boot exception-handling
by David Kumar 3 tabs
javascript
const express = require('express');
const { toCsv } = require('./csvSerializer');
const { fetchOrders } = require('./orderRepository');

const router = express.Router();

Content Negotiation in Express: Serve JSON or CSV From One Route

express content-negotiation rest-api
by codesnips 3 tabs
python
import datetime
import jwt
from flask import current_app


class TokenError(Exception):

Token-Based Auth Decorator That Loads current_user Into Flask's g

flask authentication decorators
by codesnips 3 tabs
python
from marshmallow import (
    Schema, fields, validates, validates_schema,
    ValidationError, RAISE,
)
from marshmallow.validate import Length, Email, Equal

Field-Level Signup Validation in Flask with Marshmallow Schemas

flask marshmallow validation
by codesnips 3 tabs
java
package com.shop.inventory;

import jakarta.persistence.*;

@Entity
@Table(name = "inventory_item")

Optimistic Locking on Inventory Updates with @Version and a Retrying Conflict Handler in Spring Boot

spring-boot jpa hibernate
by codesnips 3 tabs
java
package com.example.github;

import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

Declarative Typed HTTP Client With Retrofit and an OkHttp Auth Interceptor

retrofit okhttp http-client
by codesnips 3 tabs
java
@RestController
@RequestMapping("/users")
public class UserPatchController {

    private final UserRepository users;
    private final JsonPatchService patchService;

Applying RFC 6902 JSON Patch with Optimistic Locking in Spring Boot

spring-boot rest-api json-patch
by codesnips 3 tabs
typescript
import { Type } from 'class-transformer';
import { IsInt, IsOptional, IsString, Max, Min } from 'class-validator';

export class CursorPaginationQuery {
  @IsOptional()
  @IsString()

Cursor-Based Pagination for a TypeORM Repository in NestJS

nestjs typeorm pagination
by codesnips 4 tabs
python
from typing import Optional
from pydantic import BaseModel, EmailStr, Field, field_validator, model_validator


class CreateUserRequest(BaseModel):
    model_config = {"extra": "forbid"}

Validating JSON Payloads with Pydantic v2 and Returning Field-Level Errors in FastAPI

fastapi pydantic validation
by codesnips 3 tabs
java
package com.example.users.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

Mapping JPA Entities to DTOs with a MapStruct Mapper Injected into a Spring Service

spring-boot mapstruct dto
by codesnips 4 tabs