Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 61 additions & 55 deletions .claude/rules/file-operations.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
description: Use when making HTTP requests. Covers native fetch API patterns and error handling. (project)
alwaysApply: false
paths: "**/*.ts"
globs: "**/*.ts"
paths: '**/*.ts'
globs: '**/*.ts'
---

# HTTP Request Standards
Expand All @@ -17,17 +17,19 @@ Use the native fetch API for HTTP requests. Node.js now includes built-in fetch,

```typescript
async function fetchData(url: string): Promise<unknown> {
try {
const response = await fetch(url);

if (!response.ok) {
throw new Error(`API error: ${response.status} for ${url}`);
}

return await response.json();
} catch (error) {
throw new Error(`Failed to fetch from ${url}: ${error instanceof Error ? error.message : String(error)}`);
}
try {
const response = await fetch(url);

if (!response.ok) {
throw new Error(`API error: ${response.status} for ${url}`);
}

return await response.json();
} catch (error) {
throw new Error(
`Failed to fetch from ${url}: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
```

Expand All @@ -41,7 +43,7 @@ async function fetchData(url: string): Promise<unknown> {

```typescript
if (!response.ok) {
throw new Error(`API error: ${response.status} for ${url}`);
throw new Error(`API error: ${response.status} for ${url}`);
}
```

Expand All @@ -65,63 +67,67 @@ async function fetchData(url: string): Promise<unknown> {

```typescript
async function getUser(userId: string): Promise<User> {
try {
const response = await fetch(`https://api.example.com/users/${userId}`);
try {
const response = await fetch(`https://api.example.com/users/${userId}`);

if (!response.ok) {
throw new Error(`Failed to fetch user: ${response.status}`);
}
if (!response.ok) {
throw new Error(`Failed to fetch user: ${response.status}`);
}

return await response.json() as User;
} catch (error) {
throw new Error(`User fetch failed: ${error instanceof Error ? error.message : String(error)}`);
}
return (await response.json()) as User;
} catch (error) {
throw new Error(`User fetch failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
```

**POST with JSON body**:

```typescript
async function createUser(data: CreateUserInput): Promise<User> {
try {
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});

if (!response.ok) {
throw new Error(`Failed to create user: ${response.status}`);
}

return await response.json() as User;
} catch (error) {
throw new Error(`User creation failed: ${error instanceof Error ? error.message : String(error)}`);
}
try {
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});

if (!response.ok) {
throw new Error(`Failed to create user: ${response.status}`);
}

return (await response.json()) as User;
} catch (error) {
throw new Error(
`User creation failed: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
```

**With Authorization**:

```typescript
async function getProtectedData(token: string): Promise<Data> {
try {
const response = await fetch('https://api.example.com/protected', {
headers: {
'Authorization': `Bearer ${token}`,
},
});

if (!response.ok) {
throw new Error(`Auth failed: ${response.status}`);
}

return await response.json() as Data;
} catch (error) {
throw new Error(`Protected data fetch failed: ${error instanceof Error ? error.message : String(error)}`);
}
try {
const response = await fetch('https://api.example.com/protected', {
headers: {
Authorization: `Bearer ${token}`,
},
});

if (!response.ok) {
throw new Error(`Auth failed: ${response.status}`);
}

return (await response.json()) as Data;
} catch (error) {
throw new Error(
`Protected data fetch failed: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
```

Expand Down
Loading
Loading