Skip to content

Commit f6046ca

Browse files
committed
fix: types with providing optional method for mutate
TODO: fix type
1 parent 147b57d commit f6046ca

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

packages/swr-openapi/src/mutate.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { MediaType, PathsWithMethod } from "openapi-typescript-helpers";
33
import { useCallback, useDebugValue } from "react";
44
import { type MutatorCallback, type MutatorOptions, useSWRConfig } from "swr";
55
import type { Exact, PartialDeep } from "type-fest";
6-
import type { TypesForGetRequest } from "./types.js";
6+
import type { TypesForRequest } from "./types.js";
7+
import type { DataHttpMethod } from "./query-base.js";
78

89
// Types are loose here to support ecosystem utilities like `_.isMatch`
910
export type CompareFn = (init: any, partialInit: any) => boolean;
@@ -46,15 +47,17 @@ export function createMutateHook<Paths extends {}, IMediaType extends MediaType>
4647

4748
return useCallback(
4849
function mutate<
49-
Path extends PathsWithMethod<Paths, "get">,
50-
R extends TypesForGetRequest<Paths, Path>,
50+
Path extends PathsWithMethod<Paths, M>,
51+
R extends TypesForRequest<Paths, Extract<M, keyof Paths[keyof Paths]>, Path>,
5152
Init extends Exact<R["Init"], Init>,
53+
Data extends R["Data"],
54+
M extends DataHttpMethod = "get",
5255
>(
53-
[path, init]: [Path, PartialDeep<Init>?],
54-
data?: R["Data"] | Promise<R["Data"]> | MutatorCallback<R["Data"]>,
55-
opts?: boolean | MutatorOptions<R["Data"]>,
56+
[path, init]: [Path, (PartialDeep<Init> & { method?: M })?],
57+
data?: Data | Promise<Data> | MutatorCallback<Data>,
58+
opts?: boolean | (MutatorOptions<Data>),
5659
) {
57-
return swrMutate<R["Data"], R["Data"]>(
60+
return swrMutate<Data, Data>(
5861
(key) => {
5962
if (
6063
// Must be array

packages/swr-openapi/src/query-base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Client } from "openapi-fetch";
2-
import type { MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers";
2+
import type {HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf} from "openapi-typescript-helpers";
33
import { useCallback, useDebugValue, useMemo } from "react";
44
import type { Fetcher, SWRHook } from "swr";
55
import type { Exact } from "type-fest";
66
import type { TypesForRequest } from "./types.js";
77

8-
type HttpMethod = "get" | "post" | "put";
8+
export type DataHttpMethod = Extract<HttpMethod, "get" | "post" | "put">;
99

1010
/**
1111
* @private
@@ -24,7 +24,7 @@ export function configureBaseQueryHook(useHook: SWRHook) {
2424
Data extends R["Data"],
2525
Error extends R["Error"] | FetcherError,
2626
Config extends R["SWRConfig"],
27-
M extends HttpMethod = "get",
27+
M extends DataHttpMethod = "get",
2828
>(
2929
path: Path,
3030
...[init, config]: RequiredKeysOf<Init> extends never

0 commit comments

Comments
 (0)