fetch-architecture
# Fetch Architecture Skill
What this skill does
# Fetch Architecture Skill
Client and server-side fetch utilities for Next.js applications with two distinct data paths: direct backend calls (server) and API route proxying (client).
## When to Use This Skill
Use this skill when asked to:
- Set up fetch utilities for Next.js
- Configure client-side API calls with auth refresh
- Implement server-side data fetching
- Create API route proxies to backend services
- Handle authentication tokens across layers
## Architecture Overview
```
┌─────────────────────────────────────────────────────────────┐
│ Browser (Client) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Client Components │ │
│ │ • api.get/post/put/delete │ │
│ │ • CSRF via csrfManager │ │
│ │ • URL auto-prefix (/setting/x → /api/setting/x) │ │
│ └──────────────────────────┬──────────────────────────┘ │
└─────────────────────────────┼───────────────────────────────┘
│ HTTP (cookies)
▼
┌─────────────────────────────────────────────────────────────┐
│ Next.js Server │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ API Routes (app/api/...) ← client only │ │
│ │ • withAuth(token, forwardHeaders) │ │
│ │ • backendFetch() from backend.ts │ │
│ │ • Pre-emptive token refresh │ │
│ │ • CSRF forwarding │ │
│ └──────────────────────────┬──────────────────────────┘ │
│ │ HTTP (Bearer token) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Server Actions (lib/actions/) ← SSR path │ │
│ │ • directBackendFetch() — calls backend DIRECTLY │ │
│ │ • getAccessToken() + getBackendURL() │ │
│ │ • CSRF via getServerCsrfToken() for mutations │ │
│ └──────────────────────────┬──────────────────────────┘ │
│ │ HTTP (Bearer token) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Server Components (pages) │ │
│ │ • Call server actions for SSR data │ │
│ │ • Layout handles auth check │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────┼───────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ FastAPI Backend │
│ /backend/setting/... /backend/management/... │
└─────────────────────────────────────────────────────────────┘
```
## Anti-Patterns (NEVER DO)
Server actions MUST call the backend directly via `directBackendFetch()`.
Routing server actions through Next.js API routes is FORBIDDEN (unnecessary 2-hop overhead).
```typescript
// ❌ WRONG — server action routing through API route (2 hops)
import { serverGet } from '@/lib/fetch/server';
const data = await serverGet('/api/setting/users');
// ❌ WRONG — server action calling API route URL
const res = await fetch('http://localhost:3000/api/setting/users', {
headers: { Cookie: cookieHeader }
});
// ✅ CORRECT — server action calls backend directly (1 hop)
import { serverGet } from '@/lib/fetch/server';
const data = await serverGet<UsersResponse>('/backend/setting/users');
```
**Rule:** Server actions always use `/backend/...` URLs. API routes are only for client-side requests.
## Directory Structure
```
lib/
├── fetch/
│ ├── index.ts # Exports
│ ├── client.ts # Client-side fetch (browser) — exports `api`
│ ├── server.ts # Server-side fetch (direct backend calls)
│ ├── backend.ts # Backend fetch (used in API routes only)
│ ├── api-route-helper.ts # API route wrappers (withAuth)
│ ├── errors.ts # Error classes
│ └── types.ts # TypeScript types
└── auth/
├── server-auth.ts # Server authentication (getAccessToken, auth)
├── auth-cookies.ts # Cookie management (getBackendURL)
├── auth-service.ts # Client auth (token refresh)
├── csrf-manager.ts # Client CSRF token management
├── redirect-guard.ts # Redirect loop detection
└── auth-logger.ts # Auth event logging
```
## Core Files
### 1. Error Classes
```typescript
// lib/fetch/errors.ts
export class ApiError extends Error {
constructor(
message: string,
public status: number,
public data?: unknown
) {
super(message);
this.name = 'ApiError';
}
}
export function extractErrorMessage(data: unknown, status: number): string {
if (typeof data === 'string') return data;
if (typeof data === 'object' && data !== null) {
const obj = data as Record<string, unknown>;
if (typeof obj.detail === 'string') return obj.detail;
if (typeof obj.message === 'string') return obj.message;
if (typeof obj.error === 'string') return obj.error;
}
return 'An error occurred';
}
```
### 2. Type Definitions
```typescript
// lib/fetch/types.ts
export interface FetchOptions {
headers?: Record<string, string>;
timeout?: number;
cache?: RequestCache;
next?: NextFetchRequestConfig;
}
export interface FetchRequestOptions extends FetchOptions {
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
body?: unknown;
}
```
### 3. Server Fetch (Direct Backend Calls)
```typescript
// lib/fetch/server.ts
"use server";
/**
* Server-side fetch utilities for server actions.
* All requests call the backend directly (single hop).
* Mutations include a CSRF token fetched from the backend.
*/
import { getAccessToken } from '@/lib/auth/server-auth';
import { getBackendURL } from '@/lib/auth/auth-cookies';
import { ApiError, extractErrorMessage } from './errors';
import type { FetchOptions, FetchRequestOptions } from './types';
const DEFAULT_TIMEOUT = 30000;
async function getServerCsrfToken(accessToken: string): Promise<string | null> {
try {
const backendUrl = getBackendURL();
const response = await fetch(`${backendUrl}/backend/csrf-token`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
},
cache: 'no-store',
});
if (!response.ok) return null;
const data = await response.json();
return data.csrfToken ?? data.csrf_token ?? null;
} catch {
return null;
}
}
async function directBackendFetch<T>(
url: string,
options: FetchRequestOptions = {}
): Promise<T> {
const token = await getAccessToken();
if (!token) {
throw new ApiError('Not authenticated', 401);
}
const backendUrl = getBackendURL();
const fullUrl = `${backendUrl}${url}`;
const method = options.method || 'GET';
const controller = new AbortController();
const timeoutId = setTimeout(
() => controller.abort(),
options.timeout || DEFAULT_TIMEOUT
);
// Fetch CSRF token for state-changing requests
const csrfHeaders: Record<string, string> = {};
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method)) {
const csrfToken = await getServerCsrfToken(token);
if (csrfToken) {
csrfHeaders['X-CSRF-Token'] = csrfToken;
}
}
try {
const response = await fetch(fullUrl, {
method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'X-Request-ID': crypto.randomUUID(),
...csrfHeaders,
...options.headers,
},
body: options.body ? JSON.stringify(options.body) : undefined,
signal: controller.signal,
cRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.