-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample-request-chaining.ts
More file actions
32 lines (27 loc) · 819 Bytes
/
example-request-chaining.ts
File metadata and controls
32 lines (27 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { createApiFetcher } from 'fetchff';
const api = createApiFetcher({
baseURL: 'https://example.com/api',
endpoints: {
getUser: { url: '/user' },
createPost: { url: '/post', method: 'POST' },
},
});
interface PostData {
title: string;
content: string;
}
async function fetchUserAndCreatePost(userId: number, postData: PostData) {
// Fetch user data
const { data: userData } = await api.getUser({ params: { userId } });
// Create a new post with the fetched user data
return await api.createPost({
body: {
...postData,
userId: userData.id,
},
});
}
// Example usage
fetchUserAndCreatePost(1, { title: 'New Post', content: 'This is a new post.' })
.then((response) => console.log('Post created:', response))
.catch((error) => console.error('Error:', error));