fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 121 lines 3.4 kB view raw
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 ] as const; 56 57 // Generate scenarios for each framework 58 const scenarios = frameworks.map((framework) => ({ 59 config: createConfig({ 60 output: path.join(outputDir, '@tanstack', framework.output, 'meta-function'), 61 plugins: [ 62 { 63 infiniteQueryOptions: { 64 meta: (operation) => ({ 65 id: operation.id, 66 method: operation.method, 67 path: operation.path, 68 }), 69 }, 70 mutationOptions: { 71 meta: (operation) => ({ 72 id: operation.id, 73 method: operation.method, 74 path: operation.path, 75 }), 76 }, 77 name: framework.name, 78 queryOptions: { 79 meta: (operation) => ({ 80 id: operation.id, 81 method: operation.method, 82 path: operation.path, 83 }), 84 }, 85 }, 86 '@hey-api/client-fetch', 87 ], 88 }), 89 description: `generates ${framework.description} options with custom meta function`, 90 })); 91 92 it.each(scenarios)('$description', async ({ config }) => { 93 await createClient(config); 94 95 const outputPath = config.output as string; 96 const filePaths = getFilePaths(outputPath); 97 98 // Create snapshots for all generated files 99 await Promise.all( 100 filePaths.map(async (filePath) => { 101 const fileContent = fs.readFileSync(filePath, 'utf-8'); 102 const relativePath = filePath.slice(outputPath.length + 1); 103 const fileName = path.basename(relativePath); 104 const frameworkDir = path.dirname(relativePath).split(path.sep).pop()!; 105 await expect(fileContent).toMatchFileSnapshot( 106 path.join( 107 __dirname, 108 '..', 109 '..', 110 '__snapshots__', 111 'plugins', 112 '@tanstack', 113 'meta', 114 frameworkDir, 115 fileName, 116 ), 117 ); 118 }), 119 ); 120 }); 121});