Skip to content

Commit

Permalink
Add Missing Else to enhanceEndpoints Function, Docs (#2386)
Browse files Browse the repository at this point in the history
* Add Missing Else to enhanceEndpoints Function

* Fix Docs Code Example

* Fix Typo

* complete example

Co-authored-by: Lenz Weber <mail@lenzw.de>
  • Loading branch information
kinson and phryneas authored Jun 10, 2022
1 parent 8b693fc commit 9cf6cc0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
48 changes: 48 additions & 0 deletions docs/rtk-query/api/created-api/code-splitting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,51 @@ Any provided tag types or endpoint definitions will be merged into the existing
Returns an updated and enhanced version of the API slice object, containing the combined endpoint definitions.

This is primarily useful for taking an API slice object that was code-generated from an API schema file like OpenAPI, and adding additional specific hand-written configuration for cache invalidation management on top of the generated endpoint definitions.

For example, `enhanceEndpoints` can be used to modify caching behavior by changing the values of `providesTags`, `invalidatesTags`, and `keepUnusedDataFor`:

```ts
// file: api.ts noEmit
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
endpoints: (builder) => ({
getUserByUserId: builder.query({
query() {
return ''
},
}),
patchUserByUserId: builder.mutation({
query() {
return ''
},
}),
getUsers: builder.query({
query() {
return ''
},
}),
}),
})

// file: enhanceEndpoints.ts
import { api } from './api'

const enhancedApi = api.enhanceEndpoints({
addTagTypes: ['User'],
endpoints: {
getUserByUserId: {
providesTags: ['User'],
},
patchUserByUserId: {
invalidatesTags: ['User'],
},
// alternatively, define a function which is called with the endpoint definition as an argument
getUsers(endpoint) {
endpoint.providesTags = ['User']
endpoint.keepUnusedDataFor = 120
},
},
})
```
9 changes: 5 additions & 4 deletions packages/toolkit/src/query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
)) {
if (typeof partialDefinition === 'function') {
partialDefinition(context.endpointDefinitions[endpointName])
} else {
Object.assign(
context.endpointDefinitions[endpointName] || {},
partialDefinition
)
}
Object.assign(
context.endpointDefinitions[endpointName] || {},
partialDefinition
)
}
}
return api
Expand Down

0 comments on commit 9cf6cc0

Please sign in to comment.