Skip to content

Commit 88c65e2

Browse files
committed
feat: introduce operation metadata in TanStack options
1 parent 3e7eb18 commit 88c65e2

File tree

15 files changed

+922
-79
lines changed

15 files changed

+922
-79
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import { createClient, type UserConfig } from '@hey-api/openapi-ts';
6+
import { describe, expect, it } from 'vitest';
7+
8+
import { getFilePaths, getSpecsPath } from '../../utils';
9+
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = path.dirname(__filename);
12+
13+
describe('TanStack Query Meta Function Customization', () => {
14+
const version = '3.1.x';
15+
const namespace = 'plugins';
16+
17+
const createConfig = (
18+
userConfig: Omit<UserConfig, 'input'> & Pick<Partial<UserConfig>, 'input'>,
19+
): UserConfig => ({
20+
input: path.join(getSpecsPath(), version, 'security-api-key.json'),
21+
logs: {
22+
level: 'silent',
23+
},
24+
...userConfig,
25+
});
26+
27+
// Framework configurations
28+
const frameworks = [
29+
{
30+
description: 'React Query',
31+
name: '@tanstack/react-query',
32+
output: 'react-query',
33+
},
34+
{
35+
description: 'Vue Query',
36+
name: '@tanstack/vue-query',
37+
output: 'vue-query',
38+
},
39+
{
40+
description: 'Svelte Query',
41+
name: '@tanstack/svelte-query',
42+
output: 'svelte-query',
43+
},
44+
{
45+
description: 'Solid Query',
46+
name: '@tanstack/solid-query',
47+
output: 'solid-query',
48+
},
49+
{
50+
description: 'Angular Query',
51+
name: '@tanstack/angular-query-experimental',
52+
output: 'angular-query-experimental',
53+
},
54+
] as const;
55+
56+
// Custom meta function that returns the expected field names
57+
const customMetaFunction = (operation: any) => ({
58+
id: operation.id,
59+
method: operation.method,
60+
path: operation.path,
61+
});
62+
63+
const metaConfig = {
64+
infiniteQueryOptions: { meta: customMetaFunction },
65+
mutationOptions: { meta: customMetaFunction },
66+
queryOptions: { meta: customMetaFunction },
67+
};
68+
69+
// Generate scenarios for each framework
70+
const scenarios = frameworks.map((framework) => ({
71+
config: createConfig({
72+
output: path.join(
73+
__dirname,
74+
'generated',
75+
version,
76+
namespace,
77+
'@tanstack',
78+
`${framework.output}/meta-function`,
79+
),
80+
plugins: [
81+
{
82+
name: framework.name,
83+
...metaConfig,
84+
},
85+
'@hey-api/client-fetch',
86+
],
87+
}),
88+
description: `generates ${framework.description} options with custom meta function`,
89+
}));
90+
91+
it.each(scenarios)('$description', async ({ config }) => {
92+
await createClient(config);
93+
94+
const outputPath = config.output as string;
95+
const filePaths = getFilePaths(outputPath);
96+
97+
// Create snapshots for all generated files
98+
await Promise.all(
99+
filePaths.map(async (filePath) => {
100+
const fileContent = fs.readFileSync(filePath, 'utf-8');
101+
await expect(fileContent).toMatchFileSnapshot(
102+
path.join(
103+
__dirname,
104+
'__snapshots__',
105+
version,
106+
namespace,
107+
'@tanstack',
108+
filePath.slice(outputPath.length + 1),
109+
),
110+
);
111+
}),
112+
);
113+
});
114+
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const defaultConfig: TanStackAngularQueryPlugin['Config'] = {
3434
defaultValue: {
3535
case: plugin.config.case ?? 'camelCase',
3636
enabled: true,
37+
meta: false,
3738
name: '{{name}}InfiniteOptions',
3839
},
3940
mappers: {
@@ -48,6 +49,7 @@ export const defaultConfig: TanStackAngularQueryPlugin['Config'] = {
4849
defaultValue: {
4950
case: plugin.config.case ?? 'camelCase',
5051
enabled: true,
52+
meta: false,
5153
name: '{{name}}Mutation',
5254
},
5355
mappers: {
@@ -77,6 +79,7 @@ export const defaultConfig: TanStackAngularQueryPlugin['Config'] = {
7779
defaultValue: {
7880
case: plugin.config.case ?? 'camelCase',
7981
enabled: true,
82+
meta: false,
8083
name: '{{name}}Options',
8184
},
8285
mappers: {

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { IR } from '../../../ir/types';
12
import type { StringCase, StringName } from '../../../types/case';
23
import type { DefinePlugin, Plugin } from '../../types';
34

@@ -92,6 +93,28 @@ export type UserConfig = Plugin.Name<'@tanstack/angular-query-experimental'> & {
9293
* @default true
9394
*/
9495
enabled?: boolean;
96+
/**
97+
* Custom function to generate metadata for the operation.
98+
* Can return any valid meta object that will be included in the generated infinite query options.
99+
* @param operation - The operation object containing all available metadata
100+
* @returns A meta object with any properties you want to include
101+
*
102+
* @example
103+
* ```typescript
104+
* meta: (operation) => ({
105+
* customField: operation.id,
106+
* isDeprecated: operation.deprecated,
107+
* tags: operation.tags,
108+
* customObject: {
109+
* method: operation.method,
110+
* path: operation.path
111+
* }
112+
* })
113+
* ```
114+
*
115+
* @default false
116+
*/
117+
meta?: false | ((operation: IR.OperationObject) => Record<string, any>);
95118
/**
96119
* Custom naming pattern for generated infinite query options names. The name variable is
97120
* obtained from the SDK function name.
@@ -129,6 +152,28 @@ export type UserConfig = Plugin.Name<'@tanstack/angular-query-experimental'> & {
129152
* @default true
130153
*/
131154
enabled?: boolean;
155+
/**
156+
* Custom function to generate metadata for the operation.
157+
* Can return any valid meta object that will be included in the generated mutation options.
158+
* @param operation - The operation object containing all available metadata
159+
* @returns A meta object with any properties you want to include
160+
*
161+
* @example
162+
* ```typescript
163+
* meta: (operation) => ({
164+
* customField: operation.id,
165+
* isDeprecated: operation.deprecated,
166+
* tags: operation.tags,
167+
* customObject: {
168+
* method: operation.method,
169+
* path: operation.path
170+
* }
171+
* })
172+
* ```
173+
*
174+
* @default false
175+
*/
176+
meta?: false | ((operation: IR.OperationObject) => Record<string, any>);
132177
/**
133178
* Custom naming pattern for generated mutation options names. The name variable is
134179
* obtained from the SDK function name.
@@ -216,6 +261,28 @@ export type UserConfig = Plugin.Name<'@tanstack/angular-query-experimental'> & {
216261
* @default true
217262
*/
218263
enabled?: boolean;
264+
/**
265+
* Custom function to generate metadata for the operation.
266+
* Can return any valid meta object that will be included in the generated query options.
267+
* @param operation - The operation object containing all available metadata
268+
* @returns A meta object with any properties you want to include
269+
*
270+
* @example
271+
* ```typescript
272+
* meta: (operation) => ({
273+
* customField: operation.id,
274+
* isDeprecated: operation.deprecated,
275+
* tags: operation.tags,
276+
* customObject: {
277+
* method: operation.method,
278+
* path: operation.path
279+
* }
280+
* })
281+
* ```
282+
*
283+
* @default false
284+
*/
285+
meta?: false | ((operation: IR.OperationObject) => Record<string, any>);
219286
/**
220287
* Custom naming pattern for generated query options names. The name variable is
221288
* obtained from the SDK function name.
@@ -298,6 +365,28 @@ export type Config = Plugin.Name<'@tanstack/angular-query-experimental'> & {
298365
* @default true
299366
*/
300367
enabled: boolean;
368+
/**
369+
* Custom function to generate metadata for the operation.
370+
* Can return any valid meta object that will be included in the generated infinite query options.
371+
* @param operation - The operation object containing all available metadata
372+
* @returns A meta object with any properties you want to include
373+
*
374+
* @example
375+
* ```typescript
376+
* meta: (operation) => ({
377+
* customField: operation.id,
378+
* isDeprecated: operation.deprecated,
379+
* tags: operation.tags,
380+
* customObject: {
381+
* method: operation.method,
382+
* path: operation.path
383+
* }
384+
* })
385+
* ```
386+
*
387+
* @default false
388+
*/
389+
meta?: false | ((operation: IR.OperationObject) => Record<string, any>);
301390
/**
302391
* Custom naming pattern for generated infinite query options names. The name variable is
303392
* obtained from the SDK function name.
@@ -325,6 +414,28 @@ export type Config = Plugin.Name<'@tanstack/angular-query-experimental'> & {
325414
* @default true
326415
*/
327416
enabled: boolean;
417+
/**
418+
* Custom function to generate metadata for the operation.
419+
* Can return any valid meta object that will be included in the generated mutation options.
420+
* @param operation - The operation object containing all available metadata
421+
* @returns A meta object with any properties you want to include
422+
*
423+
* @example
424+
* ```typescript
425+
* meta: (operation) => ({
426+
* customField: operation.id,
427+
* isDeprecated: operation.deprecated,
428+
* tags: operation.tags,
429+
* customObject: {
430+
* method: operation.method,
431+
* path: operation.path
432+
* }
433+
* })
434+
* ```
435+
*
436+
* @default false
437+
*/
438+
meta?: false | ((operation: IR.OperationObject) => Record<string, any>);
328439
/**
329440
* Custom naming pattern for generated mutation options names. The name variable is
330441
* obtained from the SDK function name.
@@ -392,6 +503,28 @@ export type Config = Plugin.Name<'@tanstack/angular-query-experimental'> & {
392503
* @default true
393504
*/
394505
enabled: boolean;
506+
/**
507+
* Custom function to generate metadata for the operation.
508+
* Can return any valid meta object that will be included in the generated query options.
509+
* @param operation - The operation object containing all available metadata
510+
* @returns A meta object with any properties you want to include
511+
*
512+
* @example
513+
* ```typescript
514+
* meta: (operation) => ({
515+
* customField: operation.id,
516+
* isDeprecated: operation.deprecated,
517+
* tags: operation.tags,
518+
* customObject: {
519+
* method: operation.method,
520+
* path: operation.path
521+
* }
522+
* })
523+
* ```
524+
*
525+
* @default false
526+
*/
527+
meta?: false | ((operation: IR.OperationObject) => Record<string, any>);
395528
/**
396529
* Custom naming pattern for generated query options names. The name variable is
397530
* obtained from the SDK function name.

0 commit comments

Comments
 (0)