Skip to content

raburski/next-api-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@raburski/next-api-middleware

Generic middleware utilities and types for Next.js API routes.

Installation

npm install @raburski/next-api-middleware

Features

  • Generic TypeScript types for API handlers and middleware
  • Reusable middleware utilities
  • Type-safe API route composition

Types

APIContext<T>

Generic context type for API handlers that includes:

  • params: Route parameters
  • session: Optional session data
  • resource: Optional resource data
  • Additional custom properties

APIHandler<T>

Generic handler function type for API endpoints.

Middleware

Middleware function type that transforms a handler.

Middleware

withResourceExists

Generic middleware that ensures a resource exists and adds it to the context.

import { withResourceExists } from "@raburski/next-api-middleware"
import { db } from "@/server/db"

// Simple usage
const withBuildingExists = withResourceExists(
	(id: string) => db.building.findUnique({ where: { id } }),
	"Building"
)

// With custom error message
const withUserExists = withResourceExists(
	(id: string) => db.user.findUnique({ where: { id } }),
	"User",
	{ errorMessage: "User not found" }
)

// With custom param name
const withCommentExists = withResourceExists(
	(id: string) => db.comment.findUnique({ where: { id } }),
	"Comment",
	{ paramName: "commentId" }
)

// Usage in API route
export const GET = compose(
	withBuildingExists,
	withAuth
)(getBuilding)

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published