rest

python
from rest_framework import permissions


class IsOwner(permissions.BasePermission):
    """Allow only object owner to access."""

Django REST Framework permissions and authorization

django python rest
by Priya Sharma 2 tabs
python
from rest_framework import permissions


class IsOwnerOrReadOnly(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.

Django REST Framework viewset with custom permissions

django python rest
by Priya Sharma 2 tabs
typescript
import { OpenAPIRegistry, extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
import { z } from 'zod';

extendZodWithOpenApi(z);

export const registry = new OpenAPIRegistry();

OpenAPI generation for REST endpoints

typescript openapi zod
by codesnips 4 tabs
typescript
export class AppError extends Error {
  public readonly statusCode: number;
  public readonly code: string;
  public readonly isOperational: boolean;

  constructor(message: string, statusCode = 500, code = 'INTERNAL_ERROR', isOperational = true) {

Backend: normalize errors with a single Express handler

express error-handling typescript
by codesnips 4 tabs
python
import django_filters
from .models import Product


class ProductFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(lookup_expr='icontains')

Django REST Framework filtering with django-filter

django python rest
by Priya Sharma 2 tabs
python
from rest_framework import serializers
from .models import Author, Book


class BookSerializer(serializers.ModelSerializer):
    class Meta:

Django REST Framework nested serializers with writable fields

django python rest
by Priya Sharma 1 tab
python
from rest_framework.pagination import PageNumberPagination, CursorPagination


class StandardResultsSetPagination(PageNumberPagination):
    page_size = 25
    page_size_query_param = 'page_size'

Django REST Framework pagination with custom classes

django python rest
by Priya Sharma 2 tabs
python
from rest_framework.throttling import UserRateThrottle


class BurstRateThrottle(UserRateThrottle):
    """Allow short bursts of requests."""
    scope = 'burst'

Django REST Framework throttling for rate limiting

django python rest
by Priya Sharma 3 tabs
typescript
export interface Cursor {
  createdAt: string;
  id: string;
}

export function encodeCursor(c: Cursor): string {

Cursor Pagination for a REST List Endpoint with a Typed Fetch Client

typescript express rest
by codesnips 3 tabs
python
from rest_framework import routers
from rest_framework_nested import routers as nested_routers
from . import views

router = routers.DefaultRouter()
router.register(r'posts', views.PostViewSet, basename='post')

Django REST Framework nested routers

django python rest
by Priya Sharma 2 tabs
python
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response
from blog.models import Post
from .serializers import PostSerializer

Django REST Framework viewset actions

django python rest
by Priya Sharma 1 tab
python
from datetime import timedelta

INSTALLED_APPS += ['rest_framework_simplejwt']

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [

Django REST Framework authentication with JWT

django python rest
by Priya Sharma 2 tabs