@@ -5,7 +5,7 @@ const toStringRecord = (headers: FetchHeaders): Record<string, string> => {
55 const normalizedHeaders : Record < string , string > = { } ;
66 for ( const [ key , value ] of Object . entries ( headers ) ) {
77 if ( value !== undefined && value !== null ) {
8- normalizedHeaders [ key ] = String ( value ) ;
8+ normalizedHeaders [ key . toLowerCase ( ) ] = String ( value ) ;
99 }
1010 }
1111 return normalizedHeaders ;
@@ -24,15 +24,19 @@ const applyQueryParams = (url: string, params: FetchQueryParams): string => {
2424 return parsedUrl . toString ( ) ;
2525} ;
2626
27- export async function fetchWithTimeout < T = unknown > (
27+ async function baseFetch < T = unknown > (
2828 url : string ,
29- params : FetchQueryParams = { } ,
30- headers : FetchHeaders = { } ,
29+ method : string ,
30+ body : string | undefined ,
31+ params : FetchQueryParams ,
32+ headers : FetchHeaders ,
3133 timeout ?: number ,
3234 responseType : "json" | "text" = "json"
3335) : Promise < T > {
3436 const fullUrl = applyQueryParams ( url , params ) ;
3537 const response = await fetch ( fullUrl , {
38+ method,
39+ body,
3640 headers : toStringRecord ( headers ) ,
3741 ...( timeout && timeout > 0 && { signal : AbortSignal . timeout ( timeout ) } ) ,
3842 } ) ;
@@ -64,3 +68,32 @@ export async function fetchWithTimeout<T = unknown>(
6468 ) ;
6569 }
6670}
71+
72+ export function fetchWithTimeout < T = unknown > (
73+ url : string ,
74+ params : FetchQueryParams = { } ,
75+ headers : FetchHeaders = { } ,
76+ timeout ?: number ,
77+ responseType : "json" | "text" = "json"
78+ ) : Promise < T > {
79+ return baseFetch < T > ( url , "GET" , undefined , params , headers , timeout , responseType ) ;
80+ }
81+
82+ export function postWithTimeout < T = unknown > (
83+ url : string ,
84+ body : unknown ,
85+ params : FetchQueryParams = { } ,
86+ headers : FetchHeaders = { } ,
87+ timeout ?: number ,
88+ responseType : "json" | "text" = "json"
89+ ) : Promise < T > {
90+ return baseFetch < T > (
91+ url ,
92+ "POST" ,
93+ JSON . stringify ( body ) ,
94+ params ,
95+ { "Content-Type" : "application/json" , ...headers } ,
96+ timeout ,
97+ responseType
98+ ) ;
99+ }
0 commit comments