Skip to content

Commit 70ab9a1

Browse files
authored
Merge pull request #93 from MattCCC/v4
V4
2 parents f22ebe9 + b4f39ab commit 70ab9a1

File tree

81 files changed

+19830
-4691
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+19830
-4691
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ${{ matrix.os }}
88
strategy:
99
matrix:
10-
node: ["18.x", "20.x", "22.x"]
10+
node: ['18.x', '20.x', '22.x', '24.x']
1111
os: [ubuntu-latest, macOS-latest]
1212

1313
steps:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@ package/
110110

111111
dist/*.ts
112112
dist/**/*.ts
113+
dist/**/*.mts
113114
!dist/*.map
114115
!dist/**/*.map

README.md

Lines changed: 2066 additions & 364 deletions
Large diffs are not rendered by default.
702 KB
Loading
759 KB
Loading

docs/examples/example-basic.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createApiFetcher } from 'fetchff';
2+
3+
const api = createApiFetcher({
4+
apiUrl: 'https://example.com/api',
5+
endpoints: {
6+
getUser: {
7+
url: '/user-details/:id',
8+
method: 'GET',
9+
},
10+
getBooks: {
11+
url: '/books/all',
12+
method: 'GET',
13+
},
14+
},
15+
});
16+
17+
async function main() {
18+
// Basic GET request with path param
19+
const { data: user } = await api.getUser({ urlPathParams: { id: 2 } });
20+
console.log('User:', user);
21+
22+
// Basic GET request to fetch all books
23+
const { data: books } = await api.getBooks();
24+
console.log('Books:', books);
25+
}
26+
27+
main();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createApiFetcher } from 'fetchff';
2+
3+
const api = createApiFetcher({
4+
apiUrl: 'https://api.example.com/',
5+
headers: {
6+
'Content-Type': 'application/json',
7+
Authorization: 'Bearer YOUR_TOKEN',
8+
},
9+
endpoints: {
10+
getProfile: {
11+
url: '/profile/:id',
12+
},
13+
},
14+
});
15+
16+
async function main() {
17+
// GET request with custom headers and path param
18+
const { data: profile } = await api.getProfile({
19+
urlPathParams: { id: 123 },
20+
});
21+
console.log('Profile:', profile);
22+
}
23+
24+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createApiFetcher } from 'fetchff';
2+
3+
const api = createApiFetcher({
4+
apiUrl: 'https://example.com/api',
5+
// strategy: 'defaultResponse',
6+
endpoints: {
7+
sendMessage: {
8+
method: 'post',
9+
url: '/send-message/:postId',
10+
// strategy: 'defaultResponse',
11+
},
12+
},
13+
});
14+
15+
async function sendMessage() {
16+
const { data, error } = await api.sendMessage({
17+
body: { message: 'Text' },
18+
urlPathParams: { postId: 1 },
19+
strategy: 'defaultResponse',
20+
defaultResponse: { status: 'failed', message: 'Default response' },
21+
onError(error) {
22+
console.error('API error:', error.message);
23+
},
24+
});
25+
26+
if (error) {
27+
console.warn('Message failed to send, using default response:', data);
28+
return;
29+
}
30+
31+
console.log('Message sent successfully:', data);
32+
}
33+
34+
sendMessage();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createApiFetcher } from 'fetchff';
2+
import type { ResponseError } from 'fetchff';
3+
4+
const api = createApiFetcher({
5+
apiUrl: 'https://example.com/api',
6+
endpoints: {
7+
sendMessage: {
8+
method: 'post',
9+
url: '/send-message/:postId',
10+
strategy: 'reject',
11+
},
12+
},
13+
});
14+
15+
async function sendMessage() {
16+
try {
17+
await api.sendMessage({
18+
body: { message: 'Text' },
19+
urlPathParams: { postId: 1 },
20+
});
21+
console.log('Message sent successfully');
22+
} catch (error) {
23+
console.error('Message failed to send:', (error as ResponseError).message);
24+
}
25+
}
26+
27+
sendMessage();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createApiFetcher } from 'fetchff';
2+
3+
const api = createApiFetcher({
4+
baseURL: 'https://example.com/api',
5+
endpoints: {
6+
sendMessage: {
7+
method: 'post',
8+
url: '/send-message/:postId',
9+
// strategy: 'silent',
10+
},
11+
},
12+
});
13+
14+
async function sendMessage() {
15+
await api.sendMessage({
16+
body: { message: 'Text' },
17+
urlPathParams: { postId: 1 },
18+
strategy: 'silent',
19+
onError(error) {
20+
console.error('Silent error logged:', error.message);
21+
},
22+
});
23+
24+
// Because of the strategy, if API call fails, it will never reach this point. Otherwise try/catch would need to be required.
25+
console.log('Message sent successfully');
26+
}
27+
28+
sendMessage();

0 commit comments

Comments
 (0)