Skip to content

Commit

Permalink
fix mutate param issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alijany committed Feb 12, 2024
1 parent 42a7a11 commit 17628ef
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
12 changes: 8 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function App() {


const mutateParams = useMemo(() => ({
pathParams: {
id: count
},
// pathParams: {
// id: count
// },
validator: z.object({
"userId": z.number(),
"id": z.number(),
Expand All @@ -39,7 +39,11 @@ function App() {
const [{ mutate }, mutateRes] = mutateHook(mutateParams)

useEffect(() => {
mutate(mutateParams)
mutate({
pathParams: {
id: count
}
})
}, [])

return (
Expand Down
8 changes: 4 additions & 4 deletions example/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMutateHook, createQueryHook } from '../../dist/index';
import { createMutateHook, createQueryHook } from '../../src/index'

const placeHolderUrl = 'https://jsonplaceholder.typicode.com/todos/:id';
export const queryHook = createQueryHook(placeHolderUrl);
const placeHolderUrl = 'https://jsonplaceholder.typicode.com/todos/:id'
export const queryHook = createQueryHook(placeHolderUrl)

export const mutateHook = createMutateHook(placeHolderUrl);
export const mutateHook = createMutateHook(placeHolderUrl)
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.3.0",
"version": "0.4.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -57,10 +57,16 @@
"tsup": "^8.0.2",
"typescript": "^5.3.3"
},
"keywords": [
"react",
"query",
"fetch",
"axios"
],
"dependencies": {
"axios": "^1.6.2",
"lodash": "^4.17.21",
"nanostores": "^0.9.5",
"zod": "^3.22.4"
}
}
}
11 changes: 6 additions & 5 deletions src/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ export function createMutateHook<

const mutate = useCallback(
async (
options: { req?: AxiosRequestConfig<Req> } & (T extends undefined
? { pathParams: Param }
: { pathParams?: Param })
options: AxiosRequestConfig<Req> & (T extends Param
? { pathParams?: Param }
: { pathParams: Param }
)
) => {
setState(
(state) =>
Expand All @@ -76,8 +77,8 @@ export function createMutateHook<
signal: controller.current.signal,
...defaultOptions,
...mutateOptions,
...options.req,
data: options.req?.data && reqInterceptor(options.req.data),
...options,
data: options?.data && reqInterceptor(options.data),
url: compiledUrl,
})
.then((result) => {
Expand Down

0 comments on commit 17628ef

Please sign in to comment.