-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Selleo/react/refactor
feat: add api sections to react docs
- Loading branch information
Showing
17 changed files
with
217 additions
and
13 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,3 +1,3 @@ | ||
# Architecture | ||
|
||
TODO: explain apporach to typical architecture of the project, discuss environments, CI/CD, monitoring | ||
In progress |
2 changes: 1 addition & 1 deletion
2
docs/docs/remix-recipes/01-remix.md → .../docs/react-recipes/01-react-framework.md
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Data mutations and refetches | ||
|
||
## Data revalidation | ||
|
||
With the queryOptions approach for queries data refetches are pretty simple. You simply import necesary queryOptions and refetch on them like: | ||
|
||
```tsx | ||
mutate( | ||
{ | ||
data: parsedValues, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: currentUserQueryOptions.queryKey, | ||
}); | ||
}, | ||
} | ||
); | ||
``` | ||
|
||
or | ||
|
||
```tsx | ||
mutate( | ||
{ | ||
data: parsedValues, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries(currentUserQueryOptions); | ||
}, | ||
} | ||
); | ||
``` |
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,60 @@ | ||
# Api client | ||
|
||
The common issue in Client <-> Server applications is the issue of typing the data during communcation. We can solve this by generating a OpenAPI schema on our backend and create an API Client on the frontend. | ||
|
||
## OpenAPI schema generation | ||
|
||
For this purpose on the backend we can use [@nestjs/Swagger](https://docs.nestjs.com/recipes/swagger) | ||
|
||
## Frontend consumption | ||
|
||
To generate a client on the frontend we have add a dev dependency called | ||
[swagger-typescript-api](https://www.npmjs.com/package/swagger-typescript-api). | ||
|
||
```bash | ||
pnpm add -D swagger-typescript-api | ||
``` | ||
|
||
after that we can create a script that will generate us a new client from a schema file. To do this add this script to you `package.json` | ||
|
||
You can configure the path to your BE api-schema.json and file/class names of the generated client. | ||
|
||
```json | ||
{ | ||
"scripts": { | ||
..., | ||
"generate:client": "swagger-typescript-api -p ../api/src/swagger/api-schema.json -o ./src/api --axios --name base-api.ts --api-class-name BaseApi", | ||
}, | ||
} | ||
``` | ||
|
||
Given command will create a `base.api.ts` file where our BaseApi client will be created. With it you can do anything you want that is availble in the axios client | ||
|
||
eg. use interceptors | ||
|
||
```ts | ||
export const API = new BaseApi({ | ||
baseURL, | ||
headers: { | ||
Authorization: getAuthorizationHeader(), | ||
}, | ||
}); | ||
|
||
API.instance.interceptors.response.use( | ||
(response) => { | ||
return response; | ||
}, | ||
(error: AxiosError) => { | ||
if (error.response?.status === 401) { | ||
useAuthStore.getState().setTokens(null, null); | ||
removeAuthHeader(); | ||
router.navigate("/login"); | ||
} | ||
return Promise.reject(error); | ||
} | ||
); | ||
``` | ||
|
||
Now all the methods on the API are fully typed and you can use them in the following way | ||
|
||
![Api structure](client.png) |
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
examples/common_nestjs_remix/apps/web/app/api/mutations/useUpdatePokemon.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
QueryClient, | ||
useMutation, | ||
useQueryClient, | ||
} from "@tanstack/react-query"; | ||
import { sleep } from "~/utils/sleep"; | ||
import { pokemonsOptions } from "../queries/usePokemons"; | ||
import { pokemonOptions } from "../queries/usePokemon"; | ||
|
||
export async function updatePokemon( | ||
_id: string, | ||
_options: { data: { name: string; weight: number } } | ||
) { | ||
await sleep(1000); | ||
return { id: 1, name: "bulbasaur", weight: 69, abilities: [] }; | ||
} | ||
|
||
export async function invalidatePokemonQueries( | ||
queryClient: QueryClient, | ||
id?: string | ||
) { | ||
await queryClient.invalidateQueries(pokemonsOptions); | ||
|
||
if (id) { | ||
await queryClient.invalidateQueries(pokemonOptions(id)); | ||
} | ||
} | ||
|
||
export function useUpdatePokemon() { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationKey: ["updatePokemon"], | ||
mutationFn: ({ | ||
id, | ||
options, | ||
}: { | ||
id: string; | ||
options: { data: { name: string; weight: number } }; | ||
}) => updatePokemon(id, options), | ||
onSettled: (_data, _error, variables) => { | ||
invalidatePokemonQueries(queryClient, variables.id); | ||
}, | ||
}); | ||
} |
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
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 @@ | ||
export function sleep(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |