Skip to content

Commit 685f480

Browse files
committed
feat: first tentative solution for recording boot http networks
1 parent 8567449 commit 685f480

File tree

13 files changed

+688
-528
lines changed

13 files changed

+688
-528
lines changed

apps/playground/src/app/screens/NetworkTestScreen.tsx

Lines changed: 2 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -14,191 +14,7 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
1414
import EventSource from 'react-native-sse';
1515
import { NavigationProp, useNavigation } from '@react-navigation/native';
1616
import { RootStackParamList } from '../navigation/types';
17-
18-
// Real API service using JSONPlaceholder
19-
const api = {
20-
getUsers: async (): Promise<User[]> => {
21-
const response = await fetch('https://jsonplaceholder.typicode.com/users', {
22-
headers: {
23-
'X-Rozenite-Test': 'true',
24-
Cookie: 'sessionid=abc123; theme=dark; user=testuser',
25-
},
26-
});
27-
28-
if (!response.ok) {
29-
throw new Error(`HTTP error! status: ${response.status}`);
30-
}
31-
32-
return response.json();
33-
},
34-
35-
getPosts: async (): Promise<Post[]> => {
36-
const response = await fetch(
37-
'https://jsonplaceholder.typicode.com/posts?_limit=10&userId=1&sort=desc',
38-
{
39-
headers: {
40-
'X-Rozenite-Test': 'true',
41-
},
42-
}
43-
);
44-
45-
if (!response.ok) {
46-
throw new Error(`HTTP error! status: ${response.status}`);
47-
}
48-
49-
return response.json();
50-
},
51-
52-
getTodos: async (): Promise<Todo[]> => {
53-
const response = await fetch(
54-
'https://jsonplaceholder.typicode.com/todos?_limit=15',
55-
{
56-
headers: {
57-
'X-Rozenite-Test': 'true',
58-
},
59-
}
60-
);
61-
62-
if (!response.ok) {
63-
throw new Error(`HTTP error! status: ${response.status}`);
64-
}
65-
66-
return response.json();
67-
},
68-
69-
// Simulate a slow API call
70-
getSlowData: async (): Promise<User[]> => {
71-
// Add artificial delay to simulate slow network
72-
await new Promise((resolve) => setTimeout(resolve, 3000));
73-
const response = await fetch(
74-
'https://jsonplaceholder.typicode.com/users?_limit=5',
75-
{
76-
headers: {
77-
'X-Rozenite-Test': 'true',
78-
},
79-
}
80-
);
81-
82-
if (!response.ok) {
83-
throw new Error(`HTTP error! status: ${response.status}`);
84-
}
85-
86-
return response.json();
87-
},
88-
89-
// Simulate an API that sometimes fails
90-
getUnreliableData: async (): Promise<Post[]> => {
91-
// 20% chance of failure
92-
if (Math.random() < 0.2) {
93-
throw new Error('Random API failure - please try again');
94-
}
95-
const response = await fetch(
96-
'https://jsonplaceholder.typicode.com/posts?_limit=8',
97-
{
98-
headers: {
99-
'X-Rozenite-Test': 'true',
100-
},
101-
}
102-
);
103-
104-
if (!response.ok) {
105-
throw new Error(`HTTP error! status: ${response.status}`);
106-
}
107-
108-
return response.json();
109-
},
110-
111-
// Create a new post
112-
createPost: async (postData: Omit<Post, 'id'>): Promise<Post> => {
113-
const response = await fetch(
114-
'https://jsonplaceholder.typicode.com/posts?someParam=value',
115-
{
116-
method: 'POST',
117-
headers: {
118-
'Content-Type': 'application/json',
119-
'X-Rozenite-Test': 'true',
120-
},
121-
body: JSON.stringify(postData),
122-
}
123-
);
124-
125-
if (!response.ok) {
126-
throw new Error(`HTTP error! status: ${response.status}`);
127-
}
128-
129-
return response.json();
130-
},
131-
132-
// Create a new post with FormData
133-
createPostWithFormData: async (postData: Omit<Post, 'id'>): Promise<Post> => {
134-
const formData = new FormData();
135-
formData.append('title', postData.title);
136-
formData.append('body', postData.body);
137-
formData.append('userId', postData.userId.toString());
138-
139-
const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
140-
method: 'POST',
141-
headers: {
142-
'X-Rozenite-Test': 'true',
143-
},
144-
body: formData,
145-
});
146-
147-
if (!response.ok) {
148-
throw new Error(`HTTP error! status: ${response.status}`);
149-
}
150-
151-
return response.json();
152-
},
153-
154-
getLargeFile: async (): Promise<ArrayBuffer> => {
155-
const cacheBuster = Date.now();
156-
const response = await fetch(
157-
`https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson?cb=${cacheBuster}`,
158-
{
159-
headers: {
160-
'X-Rozenite-Test': 'large-download',
161-
'Cache-Control': 'no-cache, no-store, must-revalidate',
162-
'Pragma': 'no-cache',
163-
'Expires': '0',
164-
},
165-
}
166-
);
167-
168-
if (!response.ok) {
169-
throw new Error(`HTTP error! status: ${response.status}`);
170-
}
171-
172-
return response.arrayBuffer();
173-
},
174-
};
175-
176-
interface User {
177-
id: number;
178-
name: string;
179-
email: string;
180-
username: string;
181-
phone: string;
182-
website: string;
183-
company: {
184-
name: string;
185-
catchPhrase: string;
186-
};
187-
}
188-
189-
interface Post {
190-
id: number;
191-
title: string;
192-
body: string;
193-
userId: number;
194-
}
195-
196-
interface Todo {
197-
id: number;
198-
title: string;
199-
completed: boolean;
200-
userId: number;
201-
}
17+
import { api, User, Post, Todo } from '../utils/network-activity/api';
20218

20319
const useUsersQuery = () => {
20420
return useQuery({
@@ -615,7 +431,7 @@ const HTTPTestComponent: React.FC = () => {
615431
return (
616432
<View style={styles.container}>
617433
<FlatList
618-
data={data}
434+
data={data as User[] | Post[] | Todo[]}
619435
renderItem={renderItem}
620436
keyExtractor={(item) => item.id.toString()}
621437
ListHeaderComponent={renderHeader()}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
export interface User {
2+
id: number;
3+
name: string;
4+
email: string;
5+
username: string;
6+
phone: string;
7+
website: string;
8+
company: {
9+
name: string;
10+
catchPhrase: string;
11+
};
12+
}
13+
14+
export interface Post {
15+
id: number;
16+
title: string;
17+
body: string;
18+
userId: number;
19+
}
20+
21+
export interface Todo {
22+
id: number;
23+
title: string;
24+
completed: boolean;
25+
userId: number;
26+
}
27+
28+
export const api = {
29+
getUsers: async (): Promise<User[]> => {
30+
const response = await fetch('https://jsonplaceholder.typicode.com/users', {
31+
headers: {
32+
'X-Rozenite-Test': 'true',
33+
Cookie: 'sessionid=abc123; theme=dark; user=testuser',
34+
},
35+
});
36+
37+
if (!response.ok) {
38+
throw new Error(`HTTP error! status: ${response.status}`);
39+
}
40+
41+
return response.json();
42+
},
43+
44+
getPosts: async (): Promise<Post[]> => {
45+
const response = await fetch(
46+
'https://jsonplaceholder.typicode.com/posts?_limit=10&userId=1&sort=desc',
47+
{
48+
headers: {
49+
'X-Rozenite-Test': 'true',
50+
},
51+
},
52+
);
53+
54+
if (!response.ok) {
55+
throw new Error(`HTTP error! status: ${response.status}`);
56+
}
57+
58+
return response.json();
59+
},
60+
61+
getTodos: async (): Promise<Todo[]> => {
62+
const response = await fetch(
63+
'https://jsonplaceholder.typicode.com/todos?_limit=15',
64+
{
65+
headers: {
66+
'X-Rozenite-Test': 'true',
67+
},
68+
},
69+
);
70+
71+
if (!response.ok) {
72+
throw new Error(`HTTP error! status: ${response.status}`);
73+
}
74+
75+
return response.json();
76+
},
77+
78+
getSlowData: async (): Promise<User[]> => {
79+
// Add artificial delay to simulate slow network
80+
await new Promise((resolve) => setTimeout(resolve, 3000));
81+
const response = await fetch(
82+
'https://jsonplaceholder.typicode.com/users?_limit=5',
83+
{
84+
headers: {
85+
'X-Rozenite-Test': 'true',
86+
},
87+
},
88+
);
89+
90+
if (!response.ok) {
91+
throw new Error(`HTTP error! status: ${response.status}`);
92+
}
93+
94+
return response.json();
95+
},
96+
97+
getUnreliableData: async (): Promise<Post[]> => {
98+
// 20% chance of failure
99+
if (Math.random() < 0.2) {
100+
throw new Error('Random API failure - please try again');
101+
}
102+
const response = await fetch(
103+
'https://jsonplaceholder.typicode.com/posts?_limit=8',
104+
{
105+
headers: {
106+
'X-Rozenite-Test': 'true',
107+
},
108+
},
109+
);
110+
111+
if (!response.ok) {
112+
throw new Error(`HTTP error! status: ${response.status}`);
113+
}
114+
115+
return response.json();
116+
},
117+
118+
get404: async (): Promise<Post[]> => {
119+
const response = await fetch('https://www.google.com/test');
120+
121+
if (!response.ok) {
122+
throw new Error(`HTTP error! status: ${response.status}`);
123+
}
124+
125+
return response.json();
126+
},
127+
128+
post404: async (): Promise<unknown> => {
129+
const response = await fetch('https://www.google.com/test', {
130+
method: 'POST',
131+
headers: {
132+
'Content-Type': 'application/json',
133+
'X-Rozenite-Test': 'true',
134+
},
135+
body: JSON.stringify({ test: 'data' }),
136+
});
137+
138+
if (!response.ok) {
139+
throw new Error(`HTTP error! status: ${response.status}`);
140+
}
141+
142+
return response.json();
143+
},
144+
145+
createPost: async (postData: Omit<Post, 'id'>): Promise<Post> => {
146+
const response = await fetch(
147+
'https://jsonplaceholder.typicode.com/posts?someParam=value',
148+
{
149+
method: 'POST',
150+
headers: {
151+
'Content-Type': 'application/json',
152+
'X-Rozenite-Test': 'true',
153+
},
154+
body: JSON.stringify(postData),
155+
},
156+
);
157+
158+
if (!response.ok) {
159+
throw new Error(`HTTP error! status: ${response.status}`);
160+
}
161+
162+
return response.json();
163+
},
164+
165+
createPostWithFormData: async (postData: Omit<Post, 'id'>): Promise<Post> => {
166+
const formData = new FormData();
167+
formData.append('title', postData.title);
168+
formData.append('body', postData.body);
169+
formData.append('userId', postData.userId.toString());
170+
171+
const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
172+
method: 'POST',
173+
headers: {
174+
'X-Rozenite-Test': 'true',
175+
},
176+
body: formData,
177+
});
178+
179+
if (!response.ok) {
180+
throw new Error(`HTTP error! status: ${response.status}`);
181+
}
182+
183+
return response.json();
184+
},
185+
};

0 commit comments

Comments
 (0)