lovable-boilerplate / rules
Cursor rule65 starsChanged 16 months agoMITView on GitHub
Service layer architecture, API patterns, and data management with TanStack Query
- Sends data out
In this repository8 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .cursor/rules/components.mdcCursor rule
- .cursor/rules/core.mdcCursor rule
- .cursor/rules/design.mdcCursor rule
- .cursor/rules/forms.mdcCursor rule
- .cursor/rules/hooks.mdcCursor rule
- .cursor/rules/quality.mdcCursor rule
- .cursor/rules/typescript.mdcCursor rule
The file
Files
---
description: Service layer architecture, API patterns, and data management with TanStack Query
globs: ["src/lib/**/*.ts", "src/services/**/*.ts", "src/hooks/use*.ts"]
alwaysApply: false
---
# Service Layer & Data Management Rules
## Service Layer Organization
### File Structure
```
src/lib/
├── utils.ts # General utilities (keep cn function)
├── types.ts # Shared TypeScript interfaces
├── constants.ts # App-wide constants
├── validations/ # Zod schemas
│ ├── user.ts
│ ├── product.ts
│ └── common.ts
└── services/ # API service layer
├── api.ts # Base API client
├── userService.ts
└── productService.ts
```
### Service Layer Pattern
```tsx
// services/userService.ts
export const userService = {
async getAll(): Promise<User[]> {
const response = await fetch('/api/users')
if (!response.ok) throw new Error('Failed to fetch users')
return response.json()
},
async getById(id: string): Promise<User> {
const response = await fetch(`/api/users/${id}`)
if (!response.ok) throw new Error('Failed to fetch user')
return response.json()
},
async create(data: CreateUserData): Promise<User> {
const response = await fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
if (!response.ok) throw new Error('Failed to create user')
return response.json()
},
async update(id: string, data: Partial<User>): Promise<User> {
const response = await fetch(`/api/users/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
if (!response.ok) throw new Error('Failed to update user')
return response.json()
}
}
```
### Shared Types (lib/types.ts)
```tsx
export interface User {
id: string
email: string
name: string
avatar?: string
role: UserRole
createdAt: string
updatedAt: string
}
export interface ApiResponse<T> {
data: T
message: string
success: boolean
meta?: {
total: number
page: number
limit: number
}
}
export type UserRole = 'admin' | 'user' | 'guest'
export type Theme = 'light' | 'dark' | 'system'
export type LoadingState = 'idle' | 'loading' | 'success' | 'error'
```
### Constants (lib/constants.ts)
```tsx
export const API_ENDPOINTS = {
USERS: '/api/users',
PRODUCTS: '/api/products',
AUTH: '/api/auth',
} as const
export const QUERY_KEYS = {
USERS: 'users',
PRODUCTS: 'products',
USER_PROFILE: 'user-profile',
} as const
export const ROUTES = {
HOME: '/',
DASHBOARD: '/dashboard',
USERS: '/users',
PROFILE: '/profile',
} as const
```
### Validation Schemas
```tsx
// lib/validations/common.ts
export const emailSchema = z.string().email('Invalid email address')
export const phoneSchema = z.string().regex(/^\+?[\d\s-()]+$/, 'Invalid phone number')
// lib/validations/user.ts
export const userSchema = z.object({
name: z.string().min(2, 'Name must be at least 2 characters'),
email: emailSchema,
phone: phoneSchema.optional(),
role: z.enum(['admin', 'user', 'guest'])
})
export const createUserSchema = userSchema.omit({ role: true })
export const updateUserSchema = userSchema.partial()
export type UserFormData = z.infer<typeof userSchema>
export type CreateUserData = z.infer<typeof createUserSchema>
export type UpdateUserData = z.infer<typeof updateUserSchema>
```
## TanStack Query Integration
### Query Hooks Pattern
```tsx
// hooks/useUsers.ts
export const useUsers = (params?: UserQueryParams) => {
return useQuery({
queryKey: [QUERY_KEYS.USERS, params],
queryFn: () => userService.getAll(params),
staleTime: 5 * 60 * 1000, // 5 minutes
})
}
export const useUser = (id: string) => {
return useQuery({
queryKey: [QUERY_KEYS.USERS, id],
queryFn: () => userService.getById(id),
enabled: !!id,
})
}
```
### Mutation Hooks Pattern
```tsx
// hooks/useUserMutations.ts
export const useCreateUser = () => {
const queryClient = useQueryClient()
return useMutation({
mutationFn: userService.create,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.USERS] })
toast.success('User created successfully')
},
onError: (error) => {
toast.error(error.message)
}
})
}
```
## NEVER in Components
- Direct API calls (`fetch`, `axios` inline)
- Business logic mixed with UI
- Hardcoded API URLs
- Duplicate validation schemas
## ALWAYS Use
- Service layer for all API calls
- Shared types for consistent interfaces
- Custom hooks for data fetching
- Centralized constants for URLs and keys
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

