borghei/Claude-Skills / engineering/senior-frontend
senior-frontend
VerifiedScaffold Next.js 16 or React + Vite 8 apps, generate components with tests and stories, and flag heavy bundle dependencies. Use when starting a React project, adding components or hooks, or reviewing frontend code or performance.
assets/templates/scaffold/files/api_client.ts
View on GitHub// Typed fetch wrapper with JSON bodies and a typed error; no dependencies.
export class ApiError extends Error {
readonly status: number;
readonly body: unknown;
constructor(status: number, message: string, body: unknown = null) {
super(message);
this.name = 'ApiError';
this.status = status;
this.body = body;
}
}
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
function parseBody(text: string): unknown {
try {
return JSON.parse(text);
} catch {
return text;
}
}
export function createApiClient(baseUrl: string, defaultHeaders: Record<string, string> = {}) {
async function request<T>(
method: Method,
path: string,
body?: unknown,
init: RequestInit = {}
): Promise<T> {
const headers = new Headers(init.headers);
for (const [name, value] of Object.entries(defaultHeaders)) {
if (!headers.has(name)) headers.set(name, value);
}
if (!headers.has('Accept')) headers.set('Accept', 'application/json');
if (body !== undefined && !headers.has('Content-Type')) {
headers.set('Content-Type', 'application/json');
}
const response = await fetch(`${baseUrl}${path}`, {
credentials: 'same-origin',
...init,
method,
headers,
body: body === undefined ? undefined : JSON.stringify(body),
});
const text = await response.text();
const data = text ? parseBody(text) : null;
if (!response.ok) {
throw new ApiError(response.status, `${method} ${path} failed with ${response.status}`, data);
}
return data as T;
}
return {
get: <T>(path: string, init?: RequestInit) => request<T>('GET', path, undefined, init),
post: <T>(path: string, body?: unknown, init?: RequestInit) => request<T>('POST', path, body, init),
put: <T>(path: string, body?: unknown, init?: RequestInit) => request<T>('PUT', path, body, init),
patch: <T>(path: string, body?: unknown, init?: RequestInit) => request<T>('PATCH', path, body, init),
delete: <T>(path: string, init?: RequestInit) => request<T>('DELETE', path, undefined, init),
};
}
export const api = createApiClient('/api');
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |