11---
22description : Use when making HTTP requests. Covers native fetch API patterns and error handling. (project)
33alwaysApply : false
4- paths : " **/*.ts"
5- globs : " **/*.ts"
4+ paths : ' **/*.ts'
5+ globs : ' **/*.ts'
66---
77
88# HTTP Request Standards
@@ -17,17 +17,19 @@ Use the native fetch API for HTTP requests. Node.js now includes built-in fetch,
1717
1818``` typescript
1919async function fetchData(url : string ): Promise <unknown > {
20- try {
21- const response = await fetch (url );
22-
23- if (! response .ok ) {
24- throw new Error (` API error: ${response .status } for ${url } ` );
25- }
26-
27- return await response .json ();
28- } catch (error ) {
29- throw new Error (` Failed to fetch from ${url }: ${error instanceof Error ? error .message : String (error )} ` );
30- }
20+ try {
21+ const response = await fetch (url );
22+
23+ if (! response .ok ) {
24+ throw new Error (` API error: ${response .status } for ${url } ` );
25+ }
26+
27+ return await response .json ();
28+ } catch (error ) {
29+ throw new Error (
30+ ` Failed to fetch from ${url }: ${error instanceof Error ? error .message : String (error )} ` ,
31+ );
32+ }
3133}
3234```
3335
@@ -41,7 +43,7 @@ async function fetchData(url: string): Promise<unknown> {
4143
4244 ``` typescript
4345 if (! response .ok ) {
44- throw new Error (` API error: ${response .status } for ${url } ` );
46+ throw new Error (` API error: ${response .status } for ${url } ` );
4547 }
4648 ```
4749
@@ -65,63 +67,67 @@ async function fetchData(url: string): Promise<unknown> {
6567
6668``` typescript
6769async function getUser(userId : string ): Promise <User > {
68- try {
69- const response = await fetch (` https://api.example.com/users/${userId } ` );
70+ try {
71+ const response = await fetch (` https://api.example.com/users/${userId } ` );
7072
71- if (! response .ok ) {
72- throw new Error (` Failed to fetch user: ${response .status } ` );
73- }
73+ if (! response .ok ) {
74+ throw new Error (` Failed to fetch user: ${response .status } ` );
75+ }
7476
75- return await response .json () as User ;
76- } catch (error ) {
77- throw new Error (` User fetch failed: ${error instanceof Error ? error .message : String (error )} ` );
78- }
77+ return ( await response .json () ) as User ;
78+ } catch (error ) {
79+ throw new Error (` User fetch failed: ${error instanceof Error ? error .message : String (error )} ` );
80+ }
7981}
8082```
8183
8284** POST with JSON body** :
8385
8486``` typescript
8587async function createUser(data : CreateUserInput ): Promise <User > {
86- try {
87- const response = await fetch (' https://api.example.com/users' , {
88- method: ' POST' ,
89- headers: {
90- ' Content-Type' : ' application/json' ,
91- },
92- body: JSON .stringify (data ),
93- });
94-
95- if (! response .ok ) {
96- throw new Error (` Failed to create user: ${response .status } ` );
97- }
98-
99- return await response .json () as User ;
100- } catch (error ) {
101- throw new Error (` User creation failed: ${error instanceof Error ? error .message : String (error )} ` );
102- }
88+ try {
89+ const response = await fetch (' https://api.example.com/users' , {
90+ method: ' POST' ,
91+ headers: {
92+ ' Content-Type' : ' application/json' ,
93+ },
94+ body: JSON .stringify (data ),
95+ });
96+
97+ if (! response .ok ) {
98+ throw new Error (` Failed to create user: ${response .status } ` );
99+ }
100+
101+ return (await response .json ()) as User ;
102+ } catch (error ) {
103+ throw new Error (
104+ ` User creation failed: ${error instanceof Error ? error .message : String (error )} ` ,
105+ );
106+ }
103107}
104108```
105109
106110** With Authorization** :
107111
108112``` typescript
109113async function getProtectedData(token : string ): Promise <Data > {
110- try {
111- const response = await fetch (' https://api.example.com/protected' , {
112- headers: {
113- ' Authorization' : ` Bearer ${token } ` ,
114- },
115- });
116-
117- if (! response .ok ) {
118- throw new Error (` Auth failed: ${response .status } ` );
119- }
120-
121- return await response .json () as Data ;
122- } catch (error ) {
123- throw new Error (` Protected data fetch failed: ${error instanceof Error ? error .message : String (error )} ` );
124- }
114+ try {
115+ const response = await fetch (' https://api.example.com/protected' , {
116+ headers: {
117+ Authorization: ` Bearer ${token } ` ,
118+ },
119+ });
120+
121+ if (! response .ok ) {
122+ throw new Error (` Auth failed: ${response .status } ` );
123+ }
124+
125+ return (await response .json ()) as Data ;
126+ } catch (error ) {
127+ throw new Error (
128+ ` Protected data fetch failed: ${error instanceof Error ? error .message : String (error )} ` ,
129+ );
130+ }
125131}
126132```
127133
0 commit comments