-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create a react query hook plugin
- Loading branch information
1 parent
41d6e62
commit d474bc4
Showing
96 changed files
with
3,103 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
examples/src/ | ||
lib/ | ||
|
||
packages/typoas-react-query/src/__tests__/sample-client.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-9.07 KB
.yarn/cache/@babel-helper-validator-identifier-npm-7.16.7-8599fb00fc-42b9b56c35.zip
Binary file not shown.
Binary file added
BIN
+17.6 KB
.yarn/cache/@babel-helper-validator-identifier-npm-7.24.5-d1938535fe-38aaf6a64a.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+319 KB
.yarn/cache/@tanstack-react-query-npm-5.35.5-0fb0467ba0-b3c5c5528c.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+757 KB
.yarn/cache/@testing-library-react-npm-15.0.7-fc23780217-a9342ad09b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+89.3 KB
.yarn/cache/dom-accessibility-api-npm-0.5.16-d3e2310666-377b4a7f9e.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+4.85 KB
.yarn/cache/html-encoding-sniffer-npm-3.0.0-daac3dfe41-707a812ec2.zip
Binary file not shown.
Binary file added
BIN
+2.88 KB
.yarn/cache/is-potential-custom-element-name-npm-1.0.1-f352f606f8-ced7bbbb64.zip
Binary file not shown.
Binary file added
BIN
+4.55 KB
.yarn/cache/jest-environment-jsdom-npm-29.7.0-0b72dd0e0b-23bbfc9bca.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+12.4 KB
...ycode-npm-2.3.0-df4bdce06b-d4e7fbb96f.zip → ...ycode-npm-2.3.1-97543c420d-febdc4362b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
595 changes: 298 additions & 297 deletions
595
.yarn/releases/yarn-4.1.0.cjs → .yarn/releases/yarn-4.2.2.cjs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
releases: | ||
"@typoas/react-query": major | ||
"@typoas/runtime": patch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,5 @@ | |
"prettier": "^3.2.5", | ||
"typescript": "^5.3.3" | ||
}, | ||
"packageManager": "yarn@4.1.0" | ||
"packageManager": "yarn@4.2.2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# `@typoas/react-query` | ||
|
||
> This package is related to the [_Typoas_](https://github.com/Embraser01/typoas) project. | ||
## Install | ||
|
||
This dependency should be added into your dev dependencies. | ||
|
||
```shell | ||
yarn add @typoas/react-query -D | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
testMatch: ['**/__tests__/**/*.spec.[jt]s?(x)'], | ||
testPathIgnorePatterns: ['/node_modules/', '/lib/'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@typoas/react-query", | ||
"version": "3.0.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/embraser01/typoas", | ||
"directory": "packages/typoas-react-query" | ||
}, | ||
"main": "./src/index.ts", | ||
"scripts": { | ||
"prepack": "tsc", | ||
"start": "ts-node ./src/bin.ts", | ||
"test:types": "tsc --noEmit", | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"@tanstack/react-query": "^5.35.5", | ||
"@testing-library/react": "^15.0.7", | ||
"@types/node": "^20.11.24", | ||
"@types/react": "^18.3.2", | ||
"@types/react-dom": "^18.3.0", | ||
"@typoas/runtime": "workspace:^", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"ts-jest": "^29.1.2", | ||
"typescript": "^5.3.3" | ||
}, | ||
"peerDependencies": { | ||
"@tanstack/react-query": "^5.35.5", | ||
"@typoas/runtime": "workspace:^" | ||
}, | ||
"files": [ | ||
"/lib/**/*.js", | ||
"/lib/**/*.d.ts", | ||
"!/lib/**/__tests__/*" | ||
], | ||
"publishConfig": { | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts" | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/typoas-react-query/src/__tests__/infinite-query-factory.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { PropsWithChildren } from 'react'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { createContext, findPetsByStatus, Pet } from './sample-client'; | ||
import { ApiContextProvider } from '../api-context'; | ||
import { MockFetcher } from './mock-fetcher'; | ||
import { createInfiniteQueryHook } from '../infinite-query-factory'; | ||
|
||
describe('createInfiniteQueryHook', () => { | ||
const queryClient = new QueryClient(); | ||
const fetcher = new MockFetcher(); | ||
const ctx = createContext({ fetcher }); | ||
|
||
const wrapper = ({ children }: PropsWithChildren) => ( | ||
<ApiContextProvider context={ctx}> | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
</ApiContextProvider> | ||
); | ||
|
||
it('should work for simple cases', async () => { | ||
const useFindPetsByStatus = createInfiniteQueryHook(findPetsByStatus, { | ||
initialPageParam: { page: 1 }, | ||
getNextPageParam: (lastPage, allPages) => { | ||
if (allPages.length === 1) { | ||
return { page: 2 }; | ||
} | ||
return undefined; | ||
}, | ||
}); | ||
fetcher | ||
.mockResponseOnce<Pet[]>([ | ||
{ id: 3, name: 'Rocky', status: 'available', photoUrls: [] }, | ||
{ id: 4, name: 'Beethoven', status: 'available', photoUrls: [] }, | ||
]) | ||
.mockResponseOnce<Pet[]>([ | ||
{ id: 1, name: 'Bob', status: 'available', photoUrls: [] }, | ||
{ id: 2, name: 'Rufus', status: 'available', photoUrls: [] }, | ||
]); | ||
|
||
const { result } = renderHook( | ||
() => useFindPetsByStatus({ queryKey: [{ status: 'available' }] }), | ||
{ wrapper }, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(result.current.data?.pages.length).toEqual(1); | ||
expect(result.current.data?.pages[0].map((d) => d.id)).toEqual([1, 2]); | ||
}); | ||
|
||
result.current.fetchNextPage(); | ||
|
||
await waitFor(() => { | ||
expect(result.current.data?.pages.length).toEqual(2); | ||
expect(result.current.data?.pages[1].map((d) => d.id)).toEqual([3, 4]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Fetcher, RequestContext, ResponseContext } from '@typoas/runtime'; | ||
|
||
export class MockFetcher implements Fetcher { | ||
private data: unknown = undefined; | ||
|
||
private resQueue: unknown[] = []; | ||
|
||
mockResponse<T>(data: T): void { | ||
this.data = data; | ||
} | ||
|
||
mockResponseOnce<T>(data: T): MockFetcher { | ||
this.resQueue.push(data); | ||
return this; | ||
} | ||
|
||
async send( | ||
request: RequestContext, | ||
options?: string, | ||
): Promise<ResponseContext> { | ||
let data = this.resQueue.pop(); | ||
if (!data) { | ||
data = this.data; | ||
} | ||
|
||
return new ResponseContext( | ||
200, | ||
{ 'content-type': 'application/json' }, | ||
{ | ||
binary: async () => new Blob([]), | ||
text: async () => '', | ||
json: async () => data, | ||
}, | ||
); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/typoas-react-query/src/__tests__/query-factory.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { PropsWithChildren } from 'react'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { createQueryHook } from '../query-factory'; | ||
import { createContext, findPetsByStatus, Pet } from './sample-client'; | ||
import { ApiContextProvider } from '../api-context'; | ||
import { MockFetcher } from './mock-fetcher'; | ||
|
||
describe('createQueryHook', () => { | ||
const queryClient = new QueryClient(); | ||
const fetcher = new MockFetcher(); | ||
const ctx = createContext({ fetcher }); | ||
|
||
const wrapper = ({ children }: PropsWithChildren) => ( | ||
<ApiContextProvider context={ctx}> | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
</ApiContextProvider> | ||
); | ||
|
||
it('should work for simple cases', async () => { | ||
const useFindPetsByStatus = createQueryHook(findPetsByStatus, { | ||
successStatus: 200, | ||
}); | ||
fetcher.mockResponse<Pet[]>([ | ||
{ id: 1, name: 'Rufus', status: 'available', photoUrls: [] }, | ||
{ id: 2, name: 'Rocky', status: 'available', photoUrls: [] }, | ||
]); | ||
|
||
const { result } = renderHook( | ||
() => useFindPetsByStatus({ queryKey: [{ status: 'available' }] }), | ||
{ wrapper }, | ||
); | ||
|
||
await waitFor(() => | ||
expect(result.current.data?.map((d) => d.id)).toEqual([1, 2]), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.