Documentation Index
Fetch the complete documentation index at: https://docs.shadcnkit.com/llms.txt
Use this file to discover all available pages before exploring further.
Let’s dive into the details of how it works and how you can extend it to fit your needs.
Overview
shadcnkit consists of several key components:- Subscription Plans: Defined in
constants/plans.ts, this file contains the available subscription plans and their associated resource limits. - Middleware: Located in
middlewares/subscriptionCheck.ts, the middleware checks the user’s subscription and enforces resource limits. - API Routes: The API routes in
app/apihandle CRUD operations for notes and tasks while utilizing the subscription middleware. - Database Service: The
utils/drizzle/notesTasksFoldersService.tsfile contains functions for interacting with the database, including usage tracking.
Adding New Subscription Plans
To add a new subscription plan, follow these steps:- Open
constants/plans.ts. - Add a new entry to the
PLANSobject with the desired plan name and resource limits. - If the plan is associated with a specific price ID, add a new entry to the
PRICE_ID_TO_PLANobject, mapping the price ID to the plan name. - Save the file.
Adding New Resource Limits
To add new resource limits, follow these steps:- Open
constants/plans.ts. - Add a new property to each plan in the
PLANSobject, specifying the limit for the new resource. - Open
middlewares/subscriptionCheck.ts. - Update the
SubscriptionLimitsinterface to include the new resource limit. - Update the
resourceKeyToPlanLimitKeyobject to map the new resource key to the corresponding plan limit key. - Save the files.
Adding New API Routes
To add a new API route, follow these steps:- Create a new file in
app/apifor your resource (e.g.,app/api/newResource/route.ts). - Implement the desired API route handlers (e.g.,
GET,POST,PUT,DELETE). - Import the
checkSubscriptionmiddleware and apply it to the route handlers that require subscription checks. - Define the necessary subscription limits for each route handler.
- Implement the corresponding database service functions in
utils/drizzle/notesTasksFoldersService.ts(or create a new service file for your resource). - Save the files.
How the Middleware Works
ThecheckSubscription middleware in middlewares/subscriptionCheck.ts performs the following steps:
- Authenticates the user using the
requireAuthfunction. - Retrieves the user’s subscription details from the database using the
getSubscriptionfunction. - Determines the user’s current subscription plan based on the price ID.
- Iterates over the provided
limitsobject, which specifies the resource limits to check. - For each resource type, retrieves the current usage count from the database using the
getUsageTrackingfunction. - Compares the current usage count plus the increment value (if provided) against the plan’s limit for that resource type.
- If the usage exceeds the limit, returns a
403 Forbiddenresponse indicating that the plan limit has been reached. - If all checks pass, calls the provided
handlerfunction, passing the user ID. - Returns the response from the handler function.