Skip to content

Commit 4046196

Browse files
committed
feat: refactor API client creation to use new internal client functions and improve mutation cache handling
1 parent 78cae45 commit 4046196

2 files changed

Lines changed: 51 additions & 29 deletions

File tree

packages/react-client/redocly.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ apis:
7373
filename: create-internal-api-client
7474
services: all
7575
callbacks: all
76+
createMinimalAPIClient:
77+
services: none
78+
callbacks: none
79+
createNoCallbacksAPIClient:
80+
filename: createNoCallbacksAPIClient
81+
services: all
82+
callbacks: none
7683
createInternalReactAPIClient:
7784
services: none
7885
context: InternalReactAPIClientContext
Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
/* eslint-disable @typescript-eslint/no-unused-expressions */
2-
3-
import type { CreateAPIBasicQueryClientOptions } from '@openapi-qraft/react';
41
import { requestFn } from '@openapi-qraft/react';
5-
import * as callbacks from '@openapi-qraft/react/callbacks/index';
2+
import { getMutationCache } from '@openapi-qraft/react/callbacks/getMutationCache';
63
import { QueryClient } from '@tanstack/react-query';
74
import {
8-
createInternalReactAPIClient as createAPIOperationClient,
5+
createInternalReactAPIClient,
6+
createMinimalAPIClient,
7+
createNoCallbacksAPIClient,
98
services,
109
} from './fixtures/files-api/index.js';
1110

1211
const queryClient = new QueryClient();
1312

1413
// Default callbacks from redocly config should expose getMutationCache for mutation operations.
15-
const apiWithDefaultCallbacks = createAPIOperationClient(services, {
14+
const apiWithDefaultCallbacks = createInternalReactAPIClient(services, {
1615
queryClient,
1716
});
1817

@@ -21,35 +20,51 @@ apiWithDefaultCallbacks.files.deleteFiles.getMutationCache();
2120
apiWithDefaultCallbacks.files.getFileList.getMutationCache();
2221

2322
// For basic query options, provided callbacks should define the available methods.
24-
type MutationCacheOnlyCallbacks = {
25-
getMutationCache: typeof callbacks.getMutationCache;
26-
};
2723

28-
const customBasicQueryCallbacks: MutationCacheOnlyCallbacks = {
29-
getMutationCache: callbacks.getMutationCache,
30-
};
24+
const apiWithCustomBasicQueryCallbacksExplicit = createInternalReactAPIClient(
25+
services,
26+
{ queryClient },
27+
{
28+
getMutationCache,
29+
}
30+
);
3131

32-
const basicQueryOptions: CreateAPIBasicQueryClientOptions = { queryClient };
32+
apiWithCustomBasicQueryCallbacksExplicit.files.deleteFiles.getMutationCache();
3333

34-
const apiWithCustomBasicQueryCallbacksExplicit = createAPIOperationClient<
35-
typeof services,
36-
MutationCacheOnlyCallbacks
37-
>(services, basicQueryOptions, customBasicQueryCallbacks);
34+
createMinimalAPIClient(
35+
services,
36+
{ queryClient },
37+
{ getMutationCache }
38+
).files.deleteFiles.getMutationCache();
3839

39-
apiWithCustomBasicQueryCallbacksExplicit.files.deleteFiles.getMutationCache();
40+
createNoCallbacksAPIClient(
41+
{ queryClient },
42+
{ getMutationCache }
43+
).files.deleteFiles.getMutationCache();
4044

4145
// getMutationCache should remain unavailable without queryClient.
42-
const apiWithoutQueryClient = createAPIOperationClient<
43-
typeof services,
44-
MutationCacheOnlyCallbacks
45-
>(
46+
47+
createInternalReactAPIClient(
4648
services,
49+
{ requestFn, baseUrl: 'https://example.com' },
4750
{
48-
requestFn,
49-
baseUrl: 'https://example.com',
50-
},
51-
customBasicQueryCallbacks
52-
);
51+
getMutationCache,
52+
}
53+
)
54+
// @ts-expect-error - getMutationCache requires queryClient in options
55+
.files.deleteFiles.getMutationCache();
56+
57+
createMinimalAPIClient(
58+
services,
59+
{ requestFn, baseUrl: 'https://example.com' },
60+
{ getMutationCache }
61+
)
62+
// @ts-expect-error - getMutationCache requires queryClient in options
63+
.files.deleteFiles.getMutationCache();
5364

54-
// @ts-expect-error - getMutationCache requires queryClient in options
55-
apiWithoutQueryClient.files.deleteFiles.getMutationCache();
65+
createNoCallbacksAPIClient(
66+
{ requestFn, baseUrl: 'https://example.com' },
67+
{ getMutationCache }
68+
)
69+
// @ts-expect-error - getMutationCache requires queryClient in options
70+
.files.deleteFiles.getMutationCache();

0 commit comments

Comments
 (0)