Skip to content

Commit 036441a

Browse files
authored
Merge pull request #2399 from ahmedrowaihi/main
feat(tanstack-query): introduce meta, and operation meta tags
2 parents a650513 + f89e478 commit 036441a

File tree

35 files changed

+2960
-96
lines changed

35 files changed

+2960
-96
lines changed

.changeset/eight-pillows-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
feat(tanstack-query): support generating `meta` fields

docs/openapi-ts/plugins/tanstack-query.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,49 @@ export default {
472472

473473
You can customize the naming and casing pattern for `mutationOptions` functions using the `.name` and `.case` options.
474474

475+
:::
476+
477+
## Meta Function Customization
478+
479+
The TanStack Query plugin supports custom meta functions that allow you to generate any metadata you want for your operations.
480+
481+
::: code-group
482+
483+
```ts [example]
484+
export default {
485+
input: 'https://get.heyapi.dev/hey-api/backend',
486+
output: 'src/client',
487+
plugins: [
488+
{
489+
name: '@tanstack/react-query',
490+
queryOptions: {
491+
meta: (operation) => ({
492+
// ... your meta object
493+
}),
494+
},
495+
},
496+
],
497+
};
498+
```
499+
500+
```js [config]
501+
export default {
502+
input: 'https://get.heyapi.dev/hey-api/backend',
503+
output: 'src/client',
504+
plugins: [
505+
// ...other plugins
506+
{
507+
name: '@tanstack/react-query',
508+
queryOptions: {
509+
meta: (operation) => ({
510+
// ... your meta object
511+
}),
512+
},
513+
},
514+
],
515+
};
516+
```
517+
475518
## API
476519

477520
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@tanstack/react-query/types.d.ts) interface.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { type Options, getFoo, getBar } from '../sdk.gen';
4+
import { queryOptions } from '@tanstack/angular-query-experimental';
5+
import type { GetFooData, GetBarData } from '../types.gen';
6+
import { client as _heyApiClient } from '../client.gen';
7+
8+
export type QueryKey<TOptions extends Options> = [
9+
Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & {
10+
_id: string;
11+
_infinite?: boolean;
12+
tags?: ReadonlyArray<string>;
13+
}
14+
];
15+
16+
const createQueryKey = <TOptions extends Options>(id: string, options?: TOptions, infinite?: boolean, tags?: ReadonlyArray<string>): [
17+
QueryKey<TOptions>[0]
18+
] => {
19+
const params: QueryKey<TOptions>[0] = { _id: id, baseUrl: options?.baseUrl || (options?.client ?? _heyApiClient).getConfig().baseUrl } as QueryKey<TOptions>[0];
20+
if (infinite) {
21+
params._infinite = infinite;
22+
}
23+
if (tags) {
24+
params.tags = tags;
25+
}
26+
if (options?.body) {
27+
params.body = options.body;
28+
}
29+
if (options?.headers) {
30+
params.headers = options.headers;
31+
}
32+
if (options?.path) {
33+
params.path = options.path;
34+
}
35+
if (options?.query) {
36+
params.query = options.query;
37+
}
38+
return [
39+
params
40+
];
41+
};
42+
43+
export const getFooQueryKey = (options?: Options<GetFooData>) => createQueryKey('getFoo', options);
44+
45+
export const getFooOptions = (options?: Options<GetFooData>) => {
46+
return queryOptions({
47+
queryFn: async ({ queryKey, signal }) => {
48+
const { data } = await getFoo({
49+
...options,
50+
...queryKey[0],
51+
signal,
52+
throwOnError: true
53+
});
54+
return data;
55+
},
56+
queryKey: getFooQueryKey(options),
57+
meta: {
58+
id: 'getFoo',
59+
method: 'get',
60+
path: '/foo'
61+
}
62+
});
63+
};
64+
65+
export const getBarQueryKey = (options?: Options<GetBarData>) => createQueryKey('getBar', options);
66+
67+
export const getBarOptions = (options?: Options<GetBarData>) => {
68+
return queryOptions({
69+
queryFn: async ({ queryKey, signal }) => {
70+
const { data } = await getBar({
71+
...options,
72+
...queryKey[0],
73+
signal,
74+
throwOnError: true
75+
});
76+
return data;
77+
},
78+
queryKey: getBarQueryKey(options),
79+
meta: {
80+
id: 'getBar',
81+
method: 'get',
82+
path: '/bar'
83+
}
84+
});
85+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { type Options, getFoo, getBar } from '../sdk.gen';
4+
import { queryOptions } from '@tanstack/react-query';
5+
import type { GetFooData, GetBarData } from '../types.gen';
6+
import { client as _heyApiClient } from '../client.gen';
7+
8+
export type QueryKey<TOptions extends Options> = [
9+
Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & {
10+
_id: string;
11+
_infinite?: boolean;
12+
tags?: ReadonlyArray<string>;
13+
}
14+
];
15+
16+
const createQueryKey = <TOptions extends Options>(id: string, options?: TOptions, infinite?: boolean, tags?: ReadonlyArray<string>): [
17+
QueryKey<TOptions>[0]
18+
] => {
19+
const params: QueryKey<TOptions>[0] = { _id: id, baseUrl: options?.baseUrl || (options?.client ?? _heyApiClient).getConfig().baseUrl } as QueryKey<TOptions>[0];
20+
if (infinite) {
21+
params._infinite = infinite;
22+
}
23+
if (tags) {
24+
params.tags = tags;
25+
}
26+
if (options?.body) {
27+
params.body = options.body;
28+
}
29+
if (options?.headers) {
30+
params.headers = options.headers;
31+
}
32+
if (options?.path) {
33+
params.path = options.path;
34+
}
35+
if (options?.query) {
36+
params.query = options.query;
37+
}
38+
return [
39+
params
40+
];
41+
};
42+
43+
export const getFooQueryKey = (options?: Options<GetFooData>) => createQueryKey('getFoo', options);
44+
45+
export const getFooOptions = (options?: Options<GetFooData>) => {
46+
return queryOptions({
47+
queryFn: async ({ queryKey, signal }) => {
48+
const { data } = await getFoo({
49+
...options,
50+
...queryKey[0],
51+
signal,
52+
throwOnError: true
53+
});
54+
return data;
55+
},
56+
queryKey: getFooQueryKey(options),
57+
meta: {
58+
id: 'getFoo',
59+
method: 'get',
60+
path: '/foo'
61+
}
62+
});
63+
};
64+
65+
export const getBarQueryKey = (options?: Options<GetBarData>) => createQueryKey('getBar', options);
66+
67+
export const getBarOptions = (options?: Options<GetBarData>) => {
68+
return queryOptions({
69+
queryFn: async ({ queryKey, signal }) => {
70+
const { data } = await getBar({
71+
...options,
72+
...queryKey[0],
73+
signal,
74+
throwOnError: true
75+
});
76+
return data;
77+
},
78+
queryKey: getBarQueryKey(options),
79+
meta: {
80+
id: 'getBar',
81+
method: 'get',
82+
path: '/bar'
83+
}
84+
});
85+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { type Options, getFoo, getBar } from '../sdk.gen';
4+
import { queryOptions } from '@tanstack/solid-query';
5+
import type { GetFooData, GetBarData } from '../types.gen';
6+
import { client as _heyApiClient } from '../client.gen';
7+
8+
export type QueryKey<TOptions extends Options> = [
9+
Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & {
10+
_id: string;
11+
_infinite?: boolean;
12+
tags?: ReadonlyArray<string>;
13+
}
14+
];
15+
16+
const createQueryKey = <TOptions extends Options>(id: string, options?: TOptions, infinite?: boolean, tags?: ReadonlyArray<string>): [
17+
QueryKey<TOptions>[0]
18+
] => {
19+
const params: QueryKey<TOptions>[0] = { _id: id, baseUrl: options?.baseUrl || (options?.client ?? _heyApiClient).getConfig().baseUrl } as QueryKey<TOptions>[0];
20+
if (infinite) {
21+
params._infinite = infinite;
22+
}
23+
if (tags) {
24+
params.tags = tags;
25+
}
26+
if (options?.body) {
27+
params.body = options.body;
28+
}
29+
if (options?.headers) {
30+
params.headers = options.headers;
31+
}
32+
if (options?.path) {
33+
params.path = options.path;
34+
}
35+
if (options?.query) {
36+
params.query = options.query;
37+
}
38+
return [
39+
params
40+
];
41+
};
42+
43+
export const getFooQueryKey = (options?: Options<GetFooData>) => createQueryKey('getFoo', options);
44+
45+
export const getFooOptions = (options?: Options<GetFooData>) => {
46+
return queryOptions({
47+
queryFn: async ({ queryKey, signal }) => {
48+
const { data } = await getFoo({
49+
...options,
50+
...queryKey[0],
51+
signal,
52+
throwOnError: true
53+
});
54+
return data;
55+
},
56+
queryKey: getFooQueryKey(options),
57+
meta: {
58+
id: 'getFoo',
59+
method: 'get',
60+
path: '/foo'
61+
}
62+
});
63+
};
64+
65+
export const getBarQueryKey = (options?: Options<GetBarData>) => createQueryKey('getBar', options);
66+
67+
export const getBarOptions = (options?: Options<GetBarData>) => {
68+
return queryOptions({
69+
queryFn: async ({ queryKey, signal }) => {
70+
const { data } = await getBar({
71+
...options,
72+
...queryKey[0],
73+
signal,
74+
throwOnError: true
75+
});
76+
return data;
77+
},
78+
queryKey: getBarQueryKey(options),
79+
meta: {
80+
id: 'getBar',
81+
method: 'get',
82+
path: '/bar'
83+
}
84+
});
85+
};

0 commit comments

Comments
 (0)