Skip to content

Commit b65f6b5

Browse files
committed
update docs and remove enhanceEndpoints from typing (runtime fallback still exists)
1 parent 81f187e commit b65f6b5

File tree

6 files changed

+72
-78
lines changed

6 files changed

+72
-78
lines changed

docs/rtk-query/api/created-api/code-splitting.mdx

+38-26
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Each API slice allows [additional endpoint definitions to be injected at runtime
1313

1414
The individual API slice endpoint definitions can also be split across multiple files. This is primarily useful for working with API slices that were [code-generated from an API schema file](../../usage/code-generation.mdx), allowing you to add additional custom behavior and configuration to a set of automatically-generated endpoint definitions.
1515

16-
Each API slice object has `injectEndpoints` and `enhanceEndpoints` functions to support these use cases.
16+
Each API slice object has `injectEndpoints`, `addTagTypes` and `enhanceEndpoint` functions to support these use cases.
1717

1818
## `injectEndpoints`
1919

@@ -39,29 +39,44 @@ Endpoints will not be overridden unless `overrideExisting` is set to `true`. If
3939

4040
This method is primarily useful for code splitting and hot reloading.
4141

42-
## `enhanceEndpoints`
42+
## `addTagTypes`
4343

4444
#### Signature
4545

4646
```ts no-transpile
47-
const enhanceEndpoints = (endpointOptions: EnhanceEndpointsOptions) =>
48-
EnhancedApiSlice
47+
const addTagTypes = (...newTags: readonly string[]) => EnhancedApiSlice
48+
```
4949

50-
interface EnhanceEndpointsOptions {
51-
addTagTypes?: readonly string[]
52-
endpoints?: Record<string, Partial<EndpointDefinition>>
53-
}
50+
#### Description
51+
52+
Accepts a number of new tags to add to the API slice.
53+
54+
Returns an updated and enhanced version of the API slice object, with the new tags added.
55+
56+
This is primarily useful for chaining before `injectEndpoints` or `enhanceEndpoint`, to add tags which can then be used by new/enhanced endpoints.
57+
58+
## `enhanceEndpoint`
59+
60+
#### Signature
61+
62+
```ts no-transpile
63+
const enhanceEndpoint = (
64+
endpointName: string,
65+
partialDefinition:
66+
| Partial<EndpointDefinition>
67+
| ((definition: EndpointDefinition) => void)
68+
) => EnhancedApiSlice
5469
```
5570

5671
#### Description
5772

58-
Any provided tag types or endpoint definitions will be merged into the existing endpoint definitions for this API slice. Unlike `injectEndpoints`, the partial endpoint definitions will not _replace_ existing definitions, but are rather merged together on a per-definition basis (ie, `Object.assign(existingEndpoint, newPartialEndpoint)`).
73+
Provided partial definition will be merged into the existing endpoint definition for this API slice. Unlike `injectEndpoints`, the partial endpoint definition will not _replace_ the existing definition, but is rather merged together (i.e. `Object.assign(existingEndpoint, newPartialEndpoint)`).
5974

6075
Returns an updated and enhanced version of the API slice object, containing the combined endpoint definitions.
6176

6277
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.
6378

64-
For example, `enhanceEndpoints` can be used to modify caching behavior by changing the values of `providesTags`, `invalidatesTags`, and `keepUnusedDataFor`:
79+
For example, `enhanceEndpoint` can be used to modify caching behavior by changing the values of `providesTags`, `invalidatesTags`, and `keepUnusedDataFor`:
6580

6681
```ts
6782
// file: api.ts noEmit
@@ -91,20 +106,17 @@ export const api = createApi({
91106
// file: enhanceEndpoints.ts
92107
import { api } from './api'
93108

94-
const enhancedApi = api.enhanceEndpoints({
95-
addTagTypes: ['User'],
96-
endpoints: {
97-
getUserByUserId: {
98-
providesTags: ['User'],
99-
},
100-
patchUserByUserId: {
101-
invalidatesTags: ['User'],
102-
},
103-
// alternatively, define a function which is called with the endpoint definition as an argument
104-
getUsers(endpoint) {
105-
endpoint.providesTags = ['User']
106-
endpoint.keepUnusedDataFor = 120
107-
},
108-
},
109-
})
109+
const enhancedApi = api
110+
.addTagTypes('User')
111+
.enhanceEndpoint('getUserByUserId', {
112+
providesTags: ['User'],
113+
})
114+
.enhanceEndpoint('patchUserByUserId', {
115+
invalidatesTags: ['User'],
116+
})
117+
// alternatively, define a function which is called with the endpoint definition as an argument
118+
.enhanceEndpoint('getUsers', (endpoint) => {
119+
endpoint.providesTags = ['User']
120+
endpoint.keepUnusedDataFor = 120
121+
})
110122
```

docs/rtk-query/api/created-api/overview.mdx

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ type Api = {
4242

4343
// Code splitting and generation
4444
injectEndpoints: (options: InjectEndpointsOptions) => UpdatedApi
45-
enhanceEndpoints: (options: EnhanceEndpointsOptions) => UpdatedApi
45+
addTagTypes: (...newTags: readonly string[]) => UpdatedApi
46+
enhanceEndpoint: (
47+
endpointName: string,
48+
partialDefinition:
49+
| Partial<EndpointDefinition>
50+
| ((definition: EndpointDefinition) => void)
51+
) => UpdatedApi
4652

4753
// Utilities
4854
utils: {
@@ -114,7 +120,7 @@ Each API slice allows [additional endpoint definitions to be injected at runtime
114120
115121
The individual API slice endpoint definitions can also be split across multiple files. This is primarily useful for working with API slices that were [code-generated from an API schema file](../../usage/code-generation.mdx), allowing you to add additional custom behavior and configuration to a set of automatically-generated endpoint definitions.
116122
117-
Each API slice object has `injectEndpoints` and `enhanceEndpoints` functions to support these use cases.
123+
Each API slice object has `injectEndpoints`, `addTagTypes` and `enhanceEndpoint` functions to support these use cases.
118124
119125
:::info API Reference
120126

docs/rtk-query/usage/code-generation.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ That will result in all generated endpoints having `providesTags`/`invalidatesTa
6969

7070
Note that this will only result in string tags with no ids, so it might lead to scenarios where too much is invalidated and unneccessary requests are made on mutation.
7171

72-
In that case it is still recommended to manually specify tags by using [`enhanceEndpoints`](../api/created-api/code-splitting.mdx) on top of the generated api and manually declare `providesTags`/`invalidatesTags`.
72+
In that case it is still recommended to manually specify tags by using [`addTagTypes` and `enhanceEndpoint`](../api/created-api/code-splitting.mdx) on top of the generated api and manually declare `providesTags`/`invalidatesTags`.
7373

7474
### Programmatic usage
7575

examples/query/react/kitchen-sink/src/app/services/api.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const baseQueryWithRetry = retry(baseQuery, { maxRetries: 6 })
2020
* Create a base API to inject endpoints into elsewhere.
2121
* Components using this API should import from the injected site,
2222
* in order to get the appropriate types,
23-
* and to ensure that the file injecting the endpoints is loaded
23+
* and to ensure that the file injecting the endpoints is loaded
2424
*/
2525
export const api = createApi({
2626
/**
@@ -47,9 +47,3 @@ export const api = createApi({
4747
*/
4848
endpoints: () => ({}),
4949
})
50-
51-
export const enhancedApi = api.enhanceEndpoints({
52-
endpoints: () => ({
53-
getPost: () => 'test',
54-
}),
55-
})

packages/toolkit/src/query/apiTypes.ts

+2-29
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,7 @@ export type BaseApiMethods<
9696
Enhancers
9797
>
9898
/**
99-
* A function to enhance a generated API with additional information. Useful with code-generation.
100-
* @deprecated use addTagTypes and/or enhanceEndpoint instead
101-
*/
102-
enhanceEndpoints<
103-
NewTagTypes extends string = never,
104-
NewDefinitions extends EndpointDefinitions = never
105-
>(_: {
106-
addTagTypes?: readonly NewTagTypes[]
107-
endpoints?: UpdateDefinitions<
108-
Definitions,
109-
TagTypes | NoInfer<NewTagTypes>,
110-
NewDefinitions
111-
> extends infer NewDefinitions
112-
? {
113-
[K in keyof NewDefinitions]?:
114-
| Partial<NewDefinitions[K]>
115-
| ((definition: NewDefinitions[K]) => void)
116-
}
117-
: never
118-
}): Api<
119-
BaseQuery,
120-
UpdateDefinitions<Definitions, TagTypes | NewTagTypes, NewDefinitions>,
121-
ReducerPath,
122-
TagTypes | NewTagTypes,
123-
Enhancers
124-
>
125-
/**
126-
*A function to enhance a generated API with additional information. Useful with code-generation.
99+
*A function to add tag types to a generated API. Useful with code-generation.
127100
*/
128101
addTagTypes<NewTagTypes extends string = never>(
129102
...addTagTypes: readonly NewTagTypes[]
@@ -136,7 +109,7 @@ export type BaseApiMethods<
136109
>
137110

138111
/**
139-
*A function to enhance a generated API with additional information. Useful with code-generation.
112+
*A function to enhance a generated API endpoint with additional information. Useful with code-generation.
140113
*/
141114
enhanceEndpoint<
142115
QueryName extends QueryKeys<Definitions>,

packages/toolkit/src/query/createApi.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,30 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
319319
}
320320
return api
321321
},
322-
enhanceEndpoints({ addTagTypes, endpoints }) {
323-
if (addTagTypes) {
324-
api.addTagTypes(...addTagTypes)
325-
}
326-
if (endpoints) {
327-
for (const [endpointName, partialDefinition] of Object.entries(
328-
endpoints
329-
)) {
330-
;(api.enhanceEndpoint as any)(endpointName, partialDefinition)
331-
}
332-
}
333-
return api
334-
},
335322
} as Api<BaseQueryFn, {}, string, string, Modules[number]['name']>
336323

324+
// add fallback for runtime - undocumented in TS
325+
// @ts-ignore
326+
api.enhanceEndpoints = ({
327+
addTagTypes,
328+
endpoints,
329+
}: {
330+
addTagTypes?: string[]
331+
endpoints?: Record<string, object | Function>
332+
}) => {
333+
if (addTagTypes) {
334+
api.addTagTypes(...addTagTypes)
335+
}
336+
if (endpoints) {
337+
for (const [endpointName, partialDefinition] of Object.entries(
338+
endpoints
339+
)) {
340+
;(api.enhanceEndpoint as any)(endpointName, partialDefinition)
341+
}
342+
}
343+
return api
344+
}
345+
337346
const initializedModules = modules.map((m) =>
338347
m.init(api as any, optionsWithDefaults as any, context)
339348
)

0 commit comments

Comments
 (0)