@@ -3,7 +3,9 @@ import type { MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescr
33import { useCallback , useDebugValue , useMemo } from "react" ;
44import type { Fetcher , SWRHook } from "swr" ;
55import type { Exact } from "type-fest" ;
6- import type { TypesForGetRequest } from "./types.js" ;
6+ import type { TypesForRequest } from "./types.js" ;
7+
8+ type HttpMethod = "get" | "post" | "put" ;
79
810/**
911 * @private
@@ -16,27 +18,38 @@ export function configureBaseQueryHook(useHook: SWRHook) {
1618 FetcherError = never ,
1719 > ( client : Client < Paths , IMediaType > , prefix : Prefix ) {
1820 return function useQuery <
19- Path extends PathsWithMethod < Paths , "get" > ,
20- R extends TypesForGetRequest < Paths , Path > ,
21+ Path extends PathsWithMethod < Paths , M > ,
22+ R extends TypesForRequest < Paths , Extract < M , keyof Paths [ keyof Paths ] > , Path > ,
2123 Init extends Exact < R [ "Init" ] , Init > ,
2224 Data extends R [ "Data" ] ,
2325 Error extends R [ "Error" ] | FetcherError ,
2426 Config extends R [ "SWRConfig" ] ,
27+ M extends HttpMethod = "get" ,
2528 > (
2629 path : Path ,
27- ...[ init , config ] : RequiredKeysOf < Init > extends never ? [ ( Init | null ) ?, Config ?] : [ Init | null , Config ?]
30+ ...[ init , config ] : RequiredKeysOf < Init > extends never
31+ ? [ ( ( Init & { method ?: M } ) | null ) ?, Config ?]
32+ : [ ( Init & { method ?: M } ) | null , Config ?]
2833 ) {
2934 useDebugValue ( `${ prefix } - ${ path as string } ` ) ;
3035
31- const key = useMemo ( ( ) => ( init !== null ? ( [ prefix , path , init ] as const ) : null ) , [ prefix , path , init ] ) ;
36+ const key = useMemo (
37+ ( ) => ( init !== null ? ( [ prefix , path , init ] as const ) : null ) , // @ts -ignore
38+ [ prefix , path , init ] ,
39+ ) ;
3240
3341 type Key = typeof key ;
3442
35- // TODO: Lift up fetcher to and remove useCallback
3643 const fetcher : Fetcher < Data , Key > = useCallback (
37- async ( [ _ , path , init ] ) => {
38- // @ts -expect-error TODO: Improve internal init types
39- const res = await client . GET ( path , init ) ;
44+ async ( [ , path , init ] ) => {
45+ // runtime method default to 'get'
46+ const method = ( ( init as any ) ?. method ?? "get" ) as HttpMethod ;
47+ const fn = client [ method . toUpperCase ( ) as Uppercase < HttpMethod > ] as any ;
48+ if ( ! fn ) {
49+ throw new Error ( `Unsupported method: ${ method } ` ) ;
50+ }
51+
52+ const res = await fn ( path , init ) ;
4053 if ( res . error ) {
4154 throw res . error ;
4255 }
0 commit comments