agentleFS
ohcnetworkohcnetwork/care_fe/.cursor/rules/03-data-fetching.mdc

care_fe / rules

Cursor rule664 starsChanged 12 months agoMITView on GitHub

Routes are defined in dedicated API files (*Api.ts) within the corresponding type directory: Legacy routes are defined in src/Utils/api.tsx:

We split this 03-data-fetching.mdc into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use TanStack Query with the query and mutate utilities from @/Utils/request/

    line 11
  2. Distinctive:

    Use appropriate query keys following the resource pattern

    line 12
  3. Very specific:

    Use silent: true option to suppress error notifications

    line 124

≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
03-data-fetching.mdc
---
description:
globs:
alwaysApply: false
---

# Data Fetching and API Guidelines

## TanStack Query Usage

- Use TanStack Query with the `query` and `mutate` utilities from `@/Utils/request/`
- Use appropriate query keys following the resource pattern
- Leverage built-in features for pagination and debouncing
- Implement proper error handling using the global error handler

## API Route Definitions

### Modern Route Pattern

Routes are defined in dedicated API files (`*Api.ts`) within the corresponding type directory:

```typescript
// src/types/user/userApi.ts
export default {
  list: {
    path: "/api/v1/users/",
    method: HttpMethod.GET,
    TRes: Type<PaginatedResponse<UserReadMinimal>>(),
  },
  create: {
    path: "/api/v1/users/",
    method: HttpMethod.POST,
    TRes: Type<UserReadMinimal>(),
    TBody: Type<UserCreate>(),
  },
} as const;

// src/types/facility/facilityApi.ts
export default {
  getFacility: {
    path: "/api/v1/facility/{id}/",
    method: HttpMethod.GET,
    TRes: Type<FacilityRead>(),
  },
  updateMonetaryComponents: {
    path: "/api/v1/facility/{facilityId}/set_monetary_codes/",
    method: HttpMethod.POST,
    TRes: Type<FacilityRead>(),
    TBody: Type<{
      discount_codes: Code[];
      discount_monetary_components: MonetaryComponentRead[];
    }>(),
  },
} as const;
```

### Legacy Route Pattern

Legacy routes are defined in `src/Utils/api.tsx`:

```typescript
export const routes = {
  auth: {
    login: {
      path: "/api/v1/auth/login/",
      method: "POST",
    } as ApiRoute<LoginResponse, never, LoginData>,
    logout: {
      path: "/api/v1/auth/logout/",
      method: "POST",
    } as ApiRoute<void>,
  },
} as const;
```

## Query Patterns

### Basic Queries

```typescript
const { data } = useQuery({
  queryKey: ['users'],
  queryFn: query(userApi.list)
});
```

### With Parameters

```typescript
// Path parameters
const { data } = useQuery({
  queryKey: ['user', username],
  queryFn: query(userApi.get, {
    pathParams: { username }
  })
});

// Query parameters
const { data } = useQuery({
  queryKey: ['facilities', searchTerm],
  queryFn: query(facilityApi.getAllFacilities, {
    queryParams: { search: searchTerm }
  })
});
```

## Mutation Patterns

```typescript
const { mutate: createUser } = useMutation({
  mutationFn: mutate(userApi.create),
  onSuccess: () => {
    queryClient.invalidateQueries(["users"]);
  },
});
```

## Error Handling

- Errors are handled globally by default
- Common scenarios are automatically handled:
  - Session expiry → Redirects to /session-expired
  - Bad requests (400/406) → Shows error notification
- Use `silent: true` option to suppress error notifications
- Custom error handling can be implemented using `onError` callbacks

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

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.