mock-api
Create mock API endpoints that plug into the @finstreet/secure-fetch pattern. Use when the backend endpoint is not ready and you need to unblock frontend work.
What this skill does
# Mock API Route — Complete Guide
Mock API routes let the frontend work against realistic data before the backend is ready. They plug into the same `server.ts`/`client.ts` pattern as real endpoints — swapping to the real backend later is a one-line import change.
## Architecture
```
server.ts / client.ts
│
├── Mock: createMockServerFetchFunction(config) → /api/mock/... → mock registry
└── Real: createServerFetchFunction(config) → /api/proxy/... → Rails backend
```
The `EndpointConfig` is identical for mock and real. Only the import changes.
## Task Approach
1. **Gather input** — route path, HTTP method(s), response field descriptions or Zod schema reference. For list endpoints, check whether the feature has a `searchParams` definition — if it does, the mock needs pagination, search, and sort support.
2. **Generate mock handler** — see [mock-handler.md](mock-handler.md). For paginated list endpoints, see the "Paginated List Endpoints" section.
3. **Update barrel** — add the import to `src/shared/backend/mocks/handlers/index.ts`
## Using in server.ts
```typescript
// schema.ts stays IDENTICAL — same Zod schemas as the real endpoint
import { EndpointConfig } from "@finstreet/secure-fetch";
import { createMockServerFetchFunction } from "@/shared/backend/createMockServerFetchFunction";
import { ContractsResultSchema, ContractsPathVariablesSchema } from "./schema";
const getContractsConfig = {
protected: true,
method: "GET",
path: "/financial_service_providers/financing_cases/{product}/{financingCaseId}/contracts",
resultSchema: ContractsResultSchema,
pathVariablesSchema: ContractsPathVariablesSchema,
} satisfies EndpointConfig;
export const ContractsService = {
getContracts: createMockServerFetchFunction(getContractsConfig),
};
```
## Using in client.ts
```typescript
import { EndpointConfig } from "@finstreet/secure-fetch";
import { createMockClientFetchFunction } from "@/shared/backend/createMockClientFetchFunction";
import { ContractsResultSchema, ContractsPathVariablesSchema } from "./schema";
const getContractsConfig = {
protected: true,
method: "GET",
path: "/financial_service_providers/financing_cases/{product}/{financingCaseId}/contracts",
resultSchema: ContractsResultSchema,
pathVariablesSchema: ContractsPathVariablesSchema,
} satisfies EndpointConfig;
export const ContractsClientService = {
getContracts: createMockClientFetchFunction(getContractsConfig),
};
```
## Swapping to Real Backend
When the backend endpoint is ready, change the import in `server.ts` or `client.ts`:
```diff
- import { createMockServerFetchFunction } from "@/shared/backend/createMockServerFetchFunction";
+ import { createServerFetchFunction } from "@/shared/backend/createServerFetchFunction";
export const ContractsService = {
- getContracts: createMockServerFetchFunction(getContractsConfig),
+ getContracts: createServerFetchFunction(getContractsConfig),
};
```
Then optionally clean up: remove the mock handler file and its import from the barrel.
## Mock Delay Configuration
All mock requests pass through `src/app/api/mock/[...all]/route.ts`, which applies a configurable random delay to simulate network latency. This is controlled by a single config file:
**`src/shared/backend/mocks/mockConfig.ts`**
```typescript
export const mockConfig = {
delay: {
enabled: true,
min: 250, // ms
max: 750, // ms
},
};
```
| Scenario | min | max |
|-----------------------|------|------|
| Default (realistic) | 250 | 750 |
| Slow connection | 1000 | 3000 |
| No delay (fast iteration) | — | — (`enabled: false`) |
The delay is applied automatically to **every** registered mock handler — no per-handler changes needed. When creating new mock handlers, you do NOT need to touch the delay config.
## Step-by-Step Reference
- For **generating mock handlers** (registerMock calls, realistic data, barrel updates), see [mock-handler.md](mock-handler.md)
## Rules
1. Mock data must be **camelCase** — it matches Zod schemas directly, no snake_case transformation
2. ALWAYS check if infrastructure files exist before creating them
3. ALWAYS update the barrel file when adding a new mock handler
4. One handler file per feature area, placed inside the feature directory (e.g., `src/features/contracts/mock/contractsMock.ts`)
5. Use `{param}` syntax in path patterns to match path variables
6. Do NOT run any `tsc` or `pnpm` commands after implementation
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.