Skip to content

Commit

Permalink
Merge branch 'reduxjs:master' into task/remove-all-settled
Browse files Browse the repository at this point in the history
  • Loading branch information
bever1337 authored Nov 28, 2023
2 parents 7b68805 + cf24419 commit d5f4a0b
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 13 deletions.
14 changes: 7 additions & 7 deletions docs/api/createSlice.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ Result's function `getInitialState` provides access to the initial state value g
## Examples

```ts
import { createSlice, createAction } from '@reduxjs/toolkit'
import { createSlice, createAction, configureStore } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import { createStore, combineReducers } from 'redux'
import { combineReducers } from 'redux'
const incrementBy = createAction<number>('incrementBy')
const decrementBy = createAction<number>('decrementBy')
Expand Down Expand Up @@ -282,13 +282,13 @@ const user = createSlice({
},
})
const reducer = combineReducers({
counter: counter.reducer,
user: user.reducer,
const store = configureStore({
reducer: {
counter: counter.reducer,
user: user.reducer,
},
})
const store = createStore(reducer)
store.dispatch(counter.actions.increment())
// -> { counter: 1, user: {name : '', age: 21} }
store.dispatch(counter.actions.increment())
Expand Down
14 changes: 13 additions & 1 deletion docs/rtk-query/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ RTK Query is **an optional addon included in the Redux Toolkit package**, and it

To learn how to use RTK Query, see the full ["Redux Essentials" tutorial](https://redux.js.org/tutorials/essentials/part-7-rtk-query-basics) on the Redux core docs site.

If you prefer a video course, you can [watch this RTK Query video course by Lenz Weber-Tronic, the creator of RTK Query, for free at Egghead](https://egghead.io/courses/rtk-query-basics-query-endpoints-data-flow-and-typescript-57ea3c43?af=7pnhj6) or take a look at the first lesson right here:

<div style={{position:"relative",paddingTop:"56.25%"}}>
<iframe
src="https://app.egghead.io/lessons/redux-course-introduction-and-application-walk-through-for-rtk-query-basics/embed?af=7pnhj6"
title="RTK Query Video course at Egghead: Course Introduction and Application Walk through for RTK Query Basics"
frameborder="0"
allowfullscreen
style={{position:"absolute",top:0,left:0,width:"100%",height:"100%"}}
></iframe>
</div>
:::

## Motivation
Expand Down Expand Up @@ -160,7 +172,7 @@ export default function App() {
const { data, error, isLoading } = useGetPokemonByNameQuery('bulbasaur')
// Individual hooks are also accessible under the generated endpoints:
// const { data, error, isLoading } = pokemonApi.endpoints.getPokemonByName.useQuery('bulbasaur')

// render UI based on data and loading state
}
```
Expand Down
14 changes: 14 additions & 0 deletions docs/tutorials/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ The RTK [**Usage Guide** docs page](../usage/usage-guide.md) explains the standa

The [Redux Essentials tutorial](https://redux.js.org/tutorials/essentials/part-1-overview-concepts) also shows how to use each of the APIs while building an application.

## RTK Query Video Course

If you prefer a video course, you can [watch this RTK Query video course by Lenz Weber-Tronic, the creator of RTK Query, for free at Egghead](https://egghead.io/courses/rtk-query-basics-query-endpoints-data-flow-and-typescript-57ea3c43?af=7pnhj6) or take a look at the first lesson right here:

<div style={{position:"relative",paddingTop:"56.25%"}}>
<iframe
src="https://app.egghead.io/lessons/redux-course-introduction-and-application-walk-through-for-rtk-query-basics/embed?af=7pnhj6"
title="RTK Query Video course at Egghead: Course Introduction and Application Walk through for RTK Query Basics"
frameborder="0"
allowfullscreen
style={{position:"absolute",top:0,left:0,width:"100%",height:"100%"}}
></iframe>
</div>
## Migrating Vanilla Redux to Redux Toolkit

If you already know Redux and just want to know how to migrate an existing application to use Redux Toolkit, the [**"Modern Redux with Redux Toolkit" page in the Redux Fundamentals tutorial**](https://redux.js.org/tutorials/fundamentals/part-8-modern-redux) shows how RTK's APIs simplify Redux usage patterns and how to handle that migration.
Expand Down
12 changes: 8 additions & 4 deletions packages/rtk-query-codegen-openapi/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ All notable changes to the `RTK Query - Code Generator` for `Open API` project w

The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.3 - 10-11-2023
## 1.2.0 - 2023-11-09

This version adds a new `mergeReadWriteOnly` configuration option (default to `false`) that, when set to `true` will not generate separate types for read-only and write-only properties.

## 1.1.3 - 2023-10-11

### Added
- Adds a temporary workaround for [4.9.0 and 4.10.0 generate circular types oazapfts/oazapfts#491](https://github.com/oazapfts/oazapfts/issues/491)

## 1.1.2 - 10-11-2023
## 1.1.2 - 2023-10-11

### Added
- Support for Read Only Properties in the Open API spec. Previously, this property was ignored.
- Now if the readOnly property is present and set to `true` in a schema, it will split the type into two types: one with the read only property suffixed as 'Read' and the other without the read only properties, using the same type name as before.
- This may cause issues if you had your OpenAPI spec properly typed/configured, as it will remove the read onyl types from your existing type. You will need to switch to the new type suffixed as 'Read' to avoid missing property names.

## 1.1.1 - 10-11-2023
## 1.1.1 - 2023-10-11

### Changed
- Codegen: better handling of duplicate param names ([Codegen: better handling of duplicate param names #3780](https://github.com/reduxjs/redux-toolkit/pull/3780))
- If a parameter name is both used in a query and a parameter, it will be prefixed with `query`/`param` now to avoid conflicts

## 1.1.0 - 10-11-2023
## 1.1.0 - 2023-10-11

### Added
- Option of generating real TS enums instead of string unions [Adds the option of generating real TS enums instead of string unions #2854](https://github.com/reduxjs/redux-toolkit/pull/2854)
Expand Down
2 changes: 1 addition & 1 deletion packages/rtk-query-codegen-openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rtk-query/codegen-openapi",
"version": "1.1.3",
"version": "1.2.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"author": "Lenz Weber",
Expand Down
2 changes: 2 additions & 0 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ export async function generateApi(
unionUndefined,
flattenArg = false,
useEnumType = false,
mergeReadWriteOnly = false,
}: GenerationOptions
) {
const v3Doc = await getV3Doc(spec);

const apiGen = new ApiGenerator(v3Doc, {
unionUndefined,
useEnumType,
mergeReadWriteOnly,
});

// temporary workaround for https://github.com/oazapfts/oazapfts/issues/491
Expand Down
5 changes: 5 additions & 0 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export interface CommonOptions {
* `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
*/
flattenArg?: boolean;
/**
* default to false
* `true` will not generate separate types for read-only and write-only properties.
*/
mergeReadWriteOnly?: boolean;
}

export type TextMatcher = string | RegExp | (string | RegExp)[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,38 @@ export const { useAddPetMutation, useGetPetByIdQuery } = injectedRtkApi;

`;

exports[`openapi spec readOnly / writeOnly are merged 1`] = `
import { api } from './fixtures/emptyApi';
const injectedRtkApi = api.injectEndpoints({
endpoints: (build) => ({
getExample: build.query<GetExampleApiResponse, GetExampleApiArg>({
query: () => ({ url: \`/example\` }),
}),
setExample: build.mutation<SetExampleApiResponse, SetExampleApiArg>({
query: (queryArg) => ({
url: \`/example\`,
method: 'POST',
body: queryArg.exampleSchema,
}),
}),
}),
overrideExisting: false,
});
export { injectedRtkApi as enhancedApi };
export type GetExampleApiResponse = /** status 200 OK */ ExampleSchema;
export type GetExampleApiArg = void;
export type SetExampleApiResponse = /** status 200 OK */ ExampleSchema;
export type SetExampleApiArg = {
exampleSchema: ExampleSchema;
};
export type ExampleSchema = {
always_present: string;
read_only_prop: string;
write_only_prop: string;
};

`;

exports[`openapi spec readOnly / writeOnly are respected 1`] = `
import { api } from './fixtures/emptyApi';
const injectedRtkApi = api.injectEndpoints({
Expand Down
12 changes: 12 additions & 0 deletions packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,15 @@ describe('openapi spec', () => {
expect(api).toMatchSnapshot();
});
});

describe('openapi spec', () => {
it('readOnly / writeOnly are merged', async () => {
const api = await generateEndpoints({
unionUndefined: true,
schemaFile: './fixtures/readOnlyWriteOnly.yaml',
apiFile: './fixtures/emptyApi.ts',
mergeReadWriteOnly: true
});
expect(api).toMatchSnapshot();
});
});
1 change: 1 addition & 0 deletions packages/toolkit/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type {
export type {
EndpointDefinitions,
EndpointDefinition,
EndpointBuilder,
QueryDefinition,
MutationDefinition,
TagDescription,
Expand Down

0 comments on commit d5f4a0b

Please sign in to comment.