Skip to content

Commit

Permalink
docs: optimize router's section
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliao666 committed Dec 7, 2023
1 parent 9d56ed3 commit 2d7779e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
24 changes: 19 additions & 5 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ Options
- 可选
- 中间件函数数组 [(详情)](#中间件)

Returns
Expose Methods

- `getKey: () => MutationKey`
- `getOptions: () => UseMutationOptions`
Expand Down Expand Up @@ -425,10 +425,24 @@ post.add.getOptions()
post.add.mutationFn({ title: 'title', content: 'content' })

// infer types
type Data = inferData<typeof posts.list>
type FnData = inferFnData<typeof posts.list>
type Variables = inferVariables<typeof posts.list>
type Error = inferError<typeof posts.list>
type Data = inferData<typeof post.list>
type FnData = inferFnData<typeof post.list>
type Variables = inferVariables<typeof post.list>
type Error = inferError<typeof post.list>
```
### 合并路由
```ts
import { router } from 'react-query-kit'

const user = router(`user`, {})
const post = router(`post`, {})

const k = {
user,
post,
}
```

### API 文档
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ Options
- Optional
- array of middleware functions [(details)](#middleware)

Returns
Expose Methods

- `getKey: () => MutationKey`
- `getOptions: () => UseMutationOptions`
Expand Down Expand Up @@ -425,10 +425,24 @@ post.add.getOptions()
post.add.mutationFn({ title: 'title', content: 'content' })

// infer types
type Data = inferData<typeof posts.list>
type FnData = inferFnData<typeof posts.list>
type Variables = inferVariables<typeof posts.list>
type Error = inferError<typeof posts.list>
type Data = inferData<typeof post.list>
type FnData = inferFnData<typeof post.list>
type Variables = inferVariables<typeof post.list>
type Error = inferError<typeof post.list>
```
### Merging Routers
```ts
import { router } from 'react-query-kit'

const user = router(`user`, {})
const post = router(`post`, {})

const k = {
user,
post,
}
```

### API Reference
Expand Down
42 changes: 21 additions & 21 deletions tests/router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { router } from '../src'

describe('router', () => {
it('should return the correct shape', () => {
const posts = router(`posts`, {
const post = router(`post`, {
byId: router.query({
fetcher: (variables: { id: number }): Promise<{ id: 1 }> =>
fetch(`/posts/${variables.id}`).then(res => res.json()),
Expand All @@ -29,25 +29,25 @@ describe('router', () => {
}),
})

expect(posts.getKey()).toEqual(['posts'])
expect(posts.byId.getKey()).toEqual(['posts', 'byId'])
expect(posts.byId.getKey({ id: 1 })).toEqual(['posts', 'byId', { id: 1 }])
expect(posts.list.getKey()).toEqual(['posts', 'list'])
expect(posts.list.getKey()).toEqual(['posts', 'list'])
expect(posts.add.getKey()).toEqual(['posts', 'add'])
expect(typeof posts.byId.fetcher === 'function').toBe(true)
expect(typeof posts.byId.getFetchOptions === 'function').toBe(true)
expect(typeof posts.byId.getOptions === 'function').toBe(true)
expect(typeof posts.byId.useQuery === 'function').toBe(true)
expect(typeof posts.byId.useSuspenseQuery === 'function').toBe(true)
expect(typeof posts.list.fetcher === 'function').toBe(true)
expect(typeof posts.list.getFetchOptions === 'function').toBe(true)
expect(typeof posts.list.getOptions === 'function').toBe(true)
expect(typeof posts.list.useInfiniteQuery === 'function').toBe(true)
expect(typeof posts.list.useSuspenseInfiniteQuery === 'function').toBe(true)
expect(typeof posts.add.mutationFn === 'function').toBe(true)
expect(typeof posts.add.getKey === 'function').toBe(true)
expect(typeof posts.add.getOptions === 'function').toBe(true)
expect(typeof posts.add.useMutation === 'function').toBe(true)
expect(post.getKey()).toEqual(['posts'])
expect(post.byId.getKey()).toEqual(['posts', 'byId'])
expect(post.byId.getKey({ id: 1 })).toEqual(['posts', 'byId', { id: 1 }])
expect(post.list.getKey()).toEqual(['posts', 'list'])
expect(post.list.getKey()).toEqual(['posts', 'list'])
expect(post.add.getKey()).toEqual(['posts', 'add'])
expect(typeof post.byId.fetcher === 'function').toBe(true)
expect(typeof post.byId.getFetchOptions === 'function').toBe(true)
expect(typeof post.byId.getOptions === 'function').toBe(true)
expect(typeof post.byId.useQuery === 'function').toBe(true)
expect(typeof post.byId.useSuspenseQuery === 'function').toBe(true)
expect(typeof post.list.fetcher === 'function').toBe(true)
expect(typeof post.list.getFetchOptions === 'function').toBe(true)
expect(typeof post.list.getOptions === 'function').toBe(true)
expect(typeof post.list.useInfiniteQuery === 'function').toBe(true)
expect(typeof post.list.useSuspenseInfiniteQuery === 'function').toBe(true)
expect(typeof post.add.mutationFn === 'function').toBe(true)
expect(typeof post.add.getKey === 'function').toBe(true)
expect(typeof post.add.getOptions === 'function').toBe(true)
expect(typeof post.add.useMutation === 'function').toBe(true)
})
})

0 comments on commit 2d7779e

Please sign in to comment.