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