11import type { Fetch } from '../api' ;
22
3+ import type { ApiError } from './ApiError' ;
34import { createRequest } from './createRequest' ;
45import type { ApiResponse , Params , ParamsWithPayload } from './types' ;
56import { Method } from './types' ;
@@ -12,9 +13,16 @@ export interface HttpClient {
1213 delete < V = any > ( url : string , params ?: ParamsWithPayload ) : Promise < ApiResponse < V > > ;
1314}
1415
15- export function createHttpClient ( options : { baseUrl ?: string ; fetch ?: Fetch } = { } ) : HttpClient {
16+ interface Options {
17+ baseUrl ?: string ;
18+ fetch ?: Fetch ;
19+ onError ?: ( error : ApiError ) => void ;
20+ }
21+
22+ export function createHttpClient ( options : Options = { } ) : HttpClient {
1623 const baseUrl = options . baseUrl ?? null ;
1724 const fetchImpl = options . fetch ?? fetch ;
25+ const onError = options . onError ;
1826
1927 function resolveUrl ( url : string ) {
2028 if ( baseUrl ) {
@@ -25,47 +33,72 @@ export function createHttpClient(options: { baseUrl?: string; fetch?: Fetch } =
2533
2634 return {
2735 get ( url , { headers, query } = { } ) {
28- return createRequest ( fetchImpl , resolveUrl ( url ) , {
29- headers,
30- method : Method . GET ,
31- query,
32- } ) ;
36+ return createRequest (
37+ fetchImpl ,
38+ resolveUrl ( url ) ,
39+ {
40+ headers,
41+ method : Method . GET ,
42+ query,
43+ } ,
44+ onError ,
45+ ) ;
3346 } ,
3447
3548 post ( url : string , { headers, payload, query } = { } ) {
36- return createRequest ( fetchImpl , resolveUrl ( url ) , {
37- headers,
38- method : Method . POST ,
39- payload,
40- query,
41- } ) ;
49+ return createRequest (
50+ fetchImpl ,
51+ resolveUrl ( url ) ,
52+ {
53+ headers,
54+ method : Method . POST ,
55+ payload,
56+ query,
57+ } ,
58+ onError ,
59+ ) ;
4260 } ,
4361
4462 put ( url , { headers, payload, query } = { } ) {
45- return createRequest ( fetchImpl , resolveUrl ( url ) , {
46- headers,
47- method : Method . PUT ,
48- payload,
49- query,
50- } ) ;
63+ return createRequest (
64+ fetchImpl ,
65+ resolveUrl ( url ) ,
66+ {
67+ headers,
68+ method : Method . PUT ,
69+ payload,
70+ query,
71+ } ,
72+ onError ,
73+ ) ;
5174 } ,
5275
5376 patch ( url : string , { headers, payload, query } = { } ) {
54- return createRequest ( fetchImpl , resolveUrl ( url ) , {
55- headers,
56- method : Method . PATCH ,
57- payload,
58- query,
59- } ) ;
77+ return createRequest (
78+ fetchImpl ,
79+ resolveUrl ( url ) ,
80+ {
81+ headers,
82+ method : Method . PATCH ,
83+ payload,
84+ query,
85+ } ,
86+ onError ,
87+ ) ;
6088 } ,
6189
6290 delete ( url : string , { headers, payload, query } = { } ) {
63- return createRequest ( fetchImpl , resolveUrl ( url ) , {
64- headers,
65- method : Method . DELETE ,
66- payload,
67- query,
68- } ) ;
91+ return createRequest (
92+ fetchImpl ,
93+ resolveUrl ( url ) ,
94+ {
95+ headers,
96+ method : Method . DELETE ,
97+ payload,
98+ query,
99+ } ,
100+ onError ,
101+ ) ;
69102 } ,
70103 } ;
71104}
0 commit comments