Skip to content

Commit b907a3e

Browse files
authored
Merge pull request #2569 from hey-api/fix/tanstack-query-hooks
feat(tanstack-query): add hooks options
2 parents 32ed548 + 45138fc commit b907a3e

File tree

21 files changed

+343
-80
lines changed

21 files changed

+343
-80
lines changed

.changeset/changelog.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ export default {
2323

2424
return ['### Updated Dependencies:', ...list].join('\n');
2525
},
26-
getReleaseLine: async (changeset, type, changelogOpts) => {
27-
console.warn('----🐍🐍🐍🐍🐍🐍🐍');
28-
console.warn(changeset);
29-
console.warn(type);
30-
console.warn(changelogOpts);
31-
console.warn('----🐍🐍🐍🐍🐍🐍🐍');
26+
getReleaseLine: async (changeset) => {
3227
const repo = getRepo();
3328

3429
/** @type number | undefined */
@@ -73,10 +68,7 @@ export default {
7368
}
7469
const commitToFetchFrom = commitFromSummary || changeset.commit;
7570
if (commitToFetchFrom) {
76-
const { links } = await getInfo({
77-
commit: commitToFetchFrom,
78-
repo,
79-
});
71+
const { links } = await getInfo({ commit: commitToFetchFrom, repo });
8072
return links;
8173
}
8274
return {

packages/openapi-ts-tests/main/test/openapi-ts.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default defineConfig(() => {
8181
// deprecated: false,
8282
operations: {
8383
include: [
84-
'GET /event',
84+
// 'GET /event',
8585
// '/^[A-Z]+ /v1//',
8686
],
8787
},
@@ -212,9 +212,11 @@ export default defineConfig(() => {
212212
// queryKeys: {
213213
// name: '{{name}}QK',
214214
// },
215+
queryOptions: false,
215216
// queryOptions: {
216217
// name: '{{name}}QO',
217218
// },
219+
useQuery: true,
218220
'~hooks': {
219221
operations: {
220222
getKind: (op) => {

packages/openapi-ts/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ The above script can be executed by running `npm run openapi-ts` or equivalent c
205205

206206
### Node.js
207207

208-
You can also generate output programmatically by importing `@hey-api/openapi-ts` in a TypeScript file.
208+
You can also generate output programmatically by calling `createClient()` in a JavaScript/TypeScript file.
209+
210+
#### `script.ts`
209211

210212
```ts
211213
import { createClient } from '@hey-api/openapi-ts';

packages/openapi-ts/src/plugins/@pinia/colada/queryOptions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { IR } from '../../../ir/types';
55
import { tsc } from '../../../tsc';
66
import {
77
createOperationComment,
8+
hasOperationSse,
89
isOperationOptionsRequired,
910
} from '../../shared/utils/operation';
1011
import { handleMeta } from './meta';
@@ -35,7 +36,8 @@ export const createQueryOptions = ({
3536
}): void => {
3637
if (
3738
!plugin.config.queryOptions.enabled ||
38-
!plugin.hooks.operation.isQuery(operation)
39+
!plugin.hooks.operation.isQuery(operation) ||
40+
hasOperationSse({ operation })
3941
) {
4042
return;
4143
}

packages/openapi-ts/src/plugins/@tanstack/angular-query-experimental/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const defaultConfig: TanStackAngularQueryPlugin['Config'] = {
7777
defaultValue: {
7878
case: plugin.config.case ?? 'camelCase',
7979
enabled: true,
80+
exported: true,
8081
name: '{{name}}Options',
8182
},
8283
mappers: {

packages/openapi-ts/src/plugins/@tanstack/angular-query-experimental/types.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ export type UserConfig = Plugin.Name<'@tanstack/angular-query-experimental'> &
267267
* @default true
268268
*/
269269
enabled?: boolean;
270+
/**
271+
* Whether to export generated symbols.
272+
*
273+
* @default true
274+
*/
275+
exported?: boolean;
270276
/**
271277
* Custom function to generate metadata for the operation.
272278
* Can return any valid meta object that will be included in the generated query options.
@@ -510,6 +516,12 @@ export type Config = Plugin.Name<'@tanstack/angular-query-experimental'> &
510516
* @default true
511517
*/
512518
enabled: boolean;
519+
/**
520+
* Whether to export generated symbols.
521+
*
522+
* @default true
523+
*/
524+
exported: boolean;
513525
/**
514526
* Custom function to generate metadata for the operation.
515527
* Can return any valid meta object that will be included in the generated query options.

packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,14 @@ export const createInfiniteQueryOptions = ({
234234
plugin: PluginInstance;
235235
queryFn: string;
236236
state: PluginState;
237-
}) => {
238-
if (
239-
!plugin.config.infiniteQueryOptions.enabled ||
240-
!plugin.hooks.operation.isQuery(operation)
241-
) {
242-
return state;
243-
}
244-
237+
}): void => {
245238
const pagination = operationPagination({
246239
context: plugin.context,
247240
operation,
248241
});
249242

250243
if (!pagination) {
251-
return state;
244+
return;
252245
}
253246

254247
const file = plugin.context.file({ id: plugin.name })!;

packages/openapi-ts/src/plugins/@tanstack/query-core/mutationOptions.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ export const createMutationOptions = ({
1919
plugin: PluginInstance;
2020
queryFn: string;
2121
state: PluginState;
22-
}) => {
23-
if (
24-
!plugin.config.mutationOptions.enabled ||
25-
!plugin.hooks.operation.isMutation(operation)
26-
) {
27-
return state;
28-
}
29-
22+
}): void => {
3023
const mutationsType =
3124
plugin.name === '@tanstack/angular-query-experimental' ||
3225
plugin.name === '@tanstack/svelte-query' ||
@@ -165,6 +158,4 @@ export const createMutationOptions = ({
165158
name: identifier.name || '',
166159
});
167160
file.add(statement);
168-
169-
return state;
170161
};

packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createInfiniteQueryOptions } from './infiniteQueryOptions';
88
import { createMutationOptions } from './mutationOptions';
99
import { createQueryOptions } from './queryOptions';
1010
import type { PluginHandler, PluginState } from './types';
11+
import { createUseQuery } from './useQuery';
1112

1213
export const handler = ({ plugin }: Parameters<PluginHandler>[0]) => {
1314
const file = plugin.createFile({
@@ -68,26 +69,40 @@ export const handler = ({ plugin }: Parameters<PluginHandler>[0]) => {
6869
]
6970
).join('.');
7071

71-
createQueryOptions({
72-
operation,
73-
plugin,
74-
queryFn,
75-
state,
76-
});
72+
if (plugin.hooks.operation.isQuery(operation)) {
73+
if (plugin.config.queryOptions.enabled) {
74+
createQueryOptions({
75+
operation,
76+
plugin,
77+
queryFn,
78+
state,
79+
});
80+
}
7781

78-
createInfiniteQueryOptions({
79-
operation,
80-
plugin,
81-
queryFn,
82-
state,
83-
});
82+
if (plugin.config.infiniteQueryOptions.enabled) {
83+
createInfiniteQueryOptions({
84+
operation,
85+
plugin,
86+
queryFn,
87+
state,
88+
});
89+
}
8490

85-
createMutationOptions({
86-
operation,
87-
plugin,
88-
queryFn,
89-
state,
90-
});
91+
if ('useQuery' in plugin.config && plugin.config.useQuery.enabled) {
92+
createUseQuery({ operation, plugin, state });
93+
}
94+
}
95+
96+
if (plugin.hooks.operation.isMutation(operation)) {
97+
if (plugin.config.mutationOptions.enabled) {
98+
createMutationOptions({
99+
operation,
100+
plugin,
101+
queryFn,
102+
state,
103+
});
104+
}
105+
}
91106

92107
if (state.hasUsedQueryFn) {
93108
file.import({

packages/openapi-ts/src/plugins/@tanstack/query-core/queryOptions.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { IR } from '../../../ir/types';
44
import { tsc } from '../../../tsc';
55
import {
66
createOperationComment,
7+
hasOperationSse,
78
isOperationOptionsRequired,
89
} from '../../shared/utils/operation';
910
import { handleMeta } from './meta';
@@ -28,12 +29,9 @@ export const createQueryOptions = ({
2829
plugin: PluginInstance;
2930
queryFn: string;
3031
state: PluginState;
31-
}) => {
32-
if (
33-
!plugin.config.queryOptions.enabled ||
34-
!plugin.hooks.operation.isQuery(operation)
35-
) {
36-
return state;
32+
}): void => {
33+
if (hasOperationSse({ operation })) {
34+
return;
3735
}
3836

3937
const file = plugin.context.file({ id: plugin.name })!;
@@ -179,7 +177,7 @@ export const createQueryOptions = ({
179177
comment: plugin.config.comments
180178
? createOperationComment({ operation })
181179
: undefined,
182-
exportConst: true,
180+
exportConst: plugin.config.queryOptions.exported,
183181
expression: tsc.arrowFunction({
184182
parameters: [
185183
{
@@ -200,6 +198,4 @@ export const createQueryOptions = ({
200198
// TODO: AxiosError<PutSubmissionMetaError>
201199
});
202200
file.add(statement);
203-
204-
return state;
205201
};

0 commit comments

Comments
 (0)