|
| 1 | +import path from 'node:path'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | + |
| 4 | +import { createClient } from '@hey-api/openapi-ts'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +const root = path.resolve(__dirname, '..'); |
| 10 | +const getSpecsPath = () => path.resolve(root, '..', 'specs'); |
| 11 | + |
| 12 | +const version = '3.1.x'; |
| 13 | +const namespace = 'clients'; |
| 14 | +const clientName = '@hey-api/client-ofetch'; |
| 15 | +const outputDir = path.join( |
| 16 | + root, |
| 17 | + 'test', |
| 18 | + 'generated', |
| 19 | + version, |
| 20 | + namespace, |
| 21 | + clientName, |
| 22 | +); |
| 23 | + |
| 24 | +const createConfig = (userConfig) => ({ |
| 25 | + ...userConfig, |
| 26 | + input: path.join(getSpecsPath(), version, 'full.yaml'), |
| 27 | + logs: { level: 'silent' }, |
| 28 | + output: |
| 29 | + typeof userConfig.output === 'string' |
| 30 | + ? path.join(outputDir, userConfig.output) |
| 31 | + : { |
| 32 | + ...userConfig.output, |
| 33 | + path: path.join(outputDir, userConfig.output.path), |
| 34 | + }, |
| 35 | +}); |
| 36 | + |
| 37 | +const scenarios = [ |
| 38 | + { |
| 39 | + config: createConfig({ output: 'default', plugins: [clientName] }), |
| 40 | + description: 'default output', |
| 41 | + }, |
| 42 | + { |
| 43 | + config: createConfig({ |
| 44 | + output: 'sdk-client-optional', |
| 45 | + plugins: [clientName, { client: true, name: '@hey-api/sdk' }], |
| 46 | + }), |
| 47 | + description: 'SDK with optional client option', |
| 48 | + }, |
| 49 | + { |
| 50 | + config: createConfig({ |
| 51 | + output: 'sdk-client-required', |
| 52 | + plugins: [clientName, { client: false, name: '@hey-api/sdk' }], |
| 53 | + }), |
| 54 | + description: 'SDK with required client option', |
| 55 | + }, |
| 56 | + { |
| 57 | + config: createConfig({ |
| 58 | + output: 'base-url-false', |
| 59 | + plugins: [{ baseUrl: false, name: clientName }, '@hey-api/typescript'], |
| 60 | + }), |
| 61 | + description: 'client without base URL', |
| 62 | + }, |
| 63 | + { |
| 64 | + config: createConfig({ |
| 65 | + output: 'base-url-number', |
| 66 | + plugins: [{ baseUrl: 0, name: clientName }, '@hey-api/typescript'], |
| 67 | + }), |
| 68 | + description: 'client with numeric base URL', |
| 69 | + }, |
| 70 | + { |
| 71 | + config: createConfig({ |
| 72 | + output: 'base-url-string', |
| 73 | + plugins: [ |
| 74 | + { baseUrl: 'https://foo.com', name: clientName }, |
| 75 | + '@hey-api/typescript', |
| 76 | + ], |
| 77 | + }), |
| 78 | + description: 'client with custom string base URL', |
| 79 | + }, |
| 80 | + { |
| 81 | + config: createConfig({ |
| 82 | + output: 'base-url-strict', |
| 83 | + plugins: [{ baseUrl: true, name: clientName }, '@hey-api/typescript'], |
| 84 | + }), |
| 85 | + description: 'client with strict base URL', |
| 86 | + }, |
| 87 | + { |
| 88 | + config: createConfig({ |
| 89 | + output: { |
| 90 | + path: 'tsconfig-nodenext-sdk', |
| 91 | + tsConfigPath: path.join( |
| 92 | + root, |
| 93 | + 'test', |
| 94 | + 'tsconfig', |
| 95 | + 'tsconfig.nodenext.json', |
| 96 | + ), |
| 97 | + }, |
| 98 | + plugins: [clientName, '@hey-api/sdk'], |
| 99 | + }), |
| 100 | + description: 'SDK with NodeNext tsconfig', |
| 101 | + }, |
| 102 | + { |
| 103 | + config: createConfig({ |
| 104 | + output: { clean: false, path: 'clean-false' }, |
| 105 | + plugins: [clientName, '@hey-api/sdk'], |
| 106 | + }), |
| 107 | + description: 'avoid appending extension multiple times | twice', |
| 108 | + }, |
| 109 | +]; |
| 110 | + |
| 111 | +async function main() { |
| 112 | + for (const { config, description } of scenarios) { |
| 113 | + console.log('Generating:', description); |
| 114 | + await createClient(config); |
| 115 | + if (description.endsWith('twice')) { |
| 116 | + await createClient(config); |
| 117 | + } |
| 118 | + } |
| 119 | + // Generate SSE for ofetch as well (mirrors 3.1.x.test.ts scenario) |
| 120 | + const sseOut = path.join(root, 'test', 'generated', version, 'sse-ofetch'); |
| 121 | + await createClient({ |
| 122 | + input: path.join(getSpecsPath(), version, 'opencode.yaml'), |
| 123 | + logs: { level: 'silent' }, |
| 124 | + output: sseOut, |
| 125 | + parser: { filters: { operations: { include: ['GET /event'] } } }, |
| 126 | + plugins: [clientName, '@hey-api/sdk'], |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +main().catch((err) => { |
| 131 | + console.error(err); |
| 132 | + process.exit(1); |
| 133 | +}); |
0 commit comments