@@ -13,7 +13,7 @@ import { MockRegistry } from "@foundatiofx/fetchclient/mocks";
1313## Basic Usage
1414
1515``` ts
16- import { FetchClientProvider } from " @foundatiofx/fetchclient" ;
16+ import { FetchClient } from " @foundatiofx/fetchclient" ;
1717import { MockRegistry } from " @foundatiofx/fetchclient/mocks" ;
1818
1919// Create mock registry
@@ -25,13 +25,11 @@ mocks.onGet("/api/users").reply(200, [
2525 { id: 2 , name: " Bob" },
2626]);
2727
28- // Install on provider
29- const provider = new FetchClientProvider ();
30- provider .setBaseUrl (" https://api.example.com" );
31- mocks .install (provider );
28+ // Install on client
29+ const client = new FetchClient ({ baseUrl: " https://api.example.com" });
30+ mocks .install (client );
3231
3332// Make requests - they're mocked!
34- const client = provider .getFetchClient ();
3533const response = await client .getJSON (" /api/users" );
3634// response.data = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]
3735
@@ -106,8 +104,7 @@ mocks.onDelete("/api/users/1").reply(204);
106104Use ` replyOnce ` for mocks that should only match once:
107105
108106``` ts
109- mocks .onPost (" /api/users" )
110- .replyOnce (201 , { id: 1 });
107+ mocks .onPost (" /api/users" ).replyOnce (201 , { id: 1 });
111108
112109await client .postJSON (" /api/users" , {}); // Returns 201, { id: 1 }
113110await client .postJSON (" /api/users" , {}); // Falls through to real fetch (or no match)
@@ -142,12 +139,12 @@ mocks.onGet("/api/data")
142139Match based on request headers:
143140
144141``` ts
145- mocks .onGet (" /api/data" )
146- .withHeaders ({ " Authorization" : " Bearer token123" })
142+ mocks
143+ .onGet (" /api/data" )
144+ .withHeaders ({ Authorization: " Bearer token123" })
147145 .reply (200 , { authorized: true });
148146
149- mocks .onGet (" /api/data" )
150- .reply (401 , { error: " Unauthorized" });
147+ mocks .onGet (" /api/data" ).reply (401 , { error: " Unauthorized" });
151148```
152149
153150## Request History
@@ -159,25 +156,25 @@ const mocks = new MockRegistry();
159156mocks .onGet (" /api/users" ).reply (200 , []);
160157mocks .onPost (" /api/users" ).reply (201 , {});
161158
162- mocks .install (provider );
159+ mocks .install (client );
163160
164161await client .getJSON (" /api/users" );
165162await client .postJSON (" /api/users" , { name: " Alice" });
166163
167164// Check history
168- console .log (mocks .history .all .length ); // 2
169- console .log (mocks .history .get .length ); // 1
165+ console .log (mocks .history .all .length ); // 2
166+ console .log (mocks .history .get .length ); // 1
170167console .log (mocks .history .post .length ); // 1
171168
172169// Access request details
173170const postRequest = mocks .history .post [0 ];
174- console .log (postRequest .url ); // "https://example.com/api/users"
171+ console .log (postRequest .url ); // "https://example.com/api/users"
175172console .log (postRequest .method ); // "POST"
176173```
177174
178175## Standalone Fetch Replacement
179176
180- Use ` mocks.fetch ` directly without installing on a provider :
177+ Use ` mocks.fetch ` directly without installing on a client :
181178
182179``` ts
183180const mocks = new MockRegistry ();
@@ -211,16 +208,13 @@ mocks.resetHistory();
211208
212209``` ts
213210describe (" User API" , () => {
214- let provider: FetchClientProvider ;
215211 let client: FetchClient ;
216212 let mocks: MockRegistry ;
217213
218214 beforeEach (() => {
219- provider = new FetchClientProvider ();
220- provider .setBaseUrl (" https://api.example.com" );
215+ client = new FetchClient ({ baseUrl: " https://api.example.com" });
221216 mocks = new MockRegistry ();
222- mocks .install (provider );
223- client = provider .getFetchClient ();
217+ mocks .install (client );
224218 });
225219
226220 afterEach (() => {
@@ -253,11 +247,13 @@ function setupMocks() {
253247}
254248
255249describe (" App" , () => {
250+ let client: FetchClient ;
256251 let mocks: MockRegistry ;
257252
258253 beforeEach (() => {
254+ client = new FetchClient ({ baseUrl: " https://api.example.com" });
259255 mocks = setupMocks ();
260- mocks .install (provider );
256+ mocks .install (client );
261257 });
262258
263259 afterEach (() => {
@@ -318,8 +314,9 @@ it("should handle 404", async () => {
318314it (" should handle network errors" , async () => {
319315 mocks .onGet (" /api/data" ).networkError (" Connection refused" );
320316
321- await expect (client .getJSON (" /api/data" ))
322- .rejects .toThrow (" Connection refused" );
317+ await expect (client .getJSON (" /api/data" )).rejects .toThrow (
318+ " Connection refused" ,
319+ );
323320});
324321
325322it (" should handle timeouts" , async () => {
@@ -334,14 +331,12 @@ it("should handle timeouts", async () => {
334331
335332``` ts
336333it (" should open circuit after failures" , async () => {
337- const provider = new FetchClientProvider ();
338- provider .useCircuitBreaker ({ failureThreshold: 3 });
334+ const client = new FetchClient ();
335+ client .useCircuitBreaker ({ failureThreshold: 3 });
339336
340337 const mocks = new MockRegistry ();
341338 mocks .onGet (" /api/data" ).reply (500 , { error: " Server error" });
342- mocks .install (provider );
343-
344- const client = provider .getFetchClient ();
339+ mocks .install (client );
345340
346341 // Trigger failures
347342 for (let i = 0 ; i < 3 ; i ++ ) {
@@ -361,17 +356,16 @@ it("should open circuit after failures", async () => {
361356
362357``` ts
363358import { assertEquals } from " @std/assert" ;
364- import { FetchClientProvider } from " @foundatiofx/fetchclient" ;
359+ import { FetchClient } from " @foundatiofx/fetchclient" ;
365360import { MockRegistry } from " @foundatiofx/fetchclient/mocks" ;
366361
367362Deno .test (" fetches users" , async () => {
368- const provider = new FetchClientProvider ();
363+ const client = new FetchClient ();
369364 const mocks = new MockRegistry ();
370365
371366 mocks .onGet (" /api/users" ).reply (200 , [{ id: 1 }]);
372- mocks .install (provider );
367+ mocks .install (client );
373368
374- const client = provider .getFetchClient ();
375369 const response = await client .getJSON (" /api/users" );
376370
377371 assertEquals (response .data , [{ id: 1 }]);
0 commit comments