|
10 | 10 | * node scripts/generate-sdk.mjs |
11 | 11 | */ |
12 | 12 |
|
13 | | -import { spawn } from 'node:child_process' |
14 | 13 | import { readFileSync, writeFileSync } from 'node:fs' |
15 | 14 | import { resolve } from 'node:path' |
16 | 15 |
|
17 | 16 | import * as parser from '@babel/parser' |
18 | 17 | import traverse from '@babel/traverse' |
19 | 18 | import * as t from '@babel/types' |
20 | 19 | import MagicString from 'magic-string' |
| 20 | +import openapiTS from 'openapi-typescript' |
21 | 21 |
|
22 | 22 | import { getDefaultLogger } from '@socketsecurity/lib/logger' |
23 | 23 |
|
24 | 24 | import { getRootPath } from './utils/path-helpers.mjs' |
25 | 25 | import { runCommand } from './utils/run-command.mjs' |
26 | 26 |
|
27 | 27 | const rootPath = getRootPath(import.meta.url) |
| 28 | +const openApiJsonPath = resolve(rootPath, 'openapi.json') |
28 | 29 | const typesPath = resolve(rootPath, 'types/api.d.ts') |
29 | 30 |
|
30 | 31 | // Initialize logger |
31 | 32 | const logger = getDefaultLogger() |
32 | 33 |
|
33 | | -async function generateTypes() { |
34 | | - return new Promise((resolve, reject) => { |
35 | | - const child = spawn('node', ['scripts/generate-types.mjs'], { |
36 | | - cwd: rootPath, |
37 | | - stdio: ['inherit', 'pipe', 'inherit'], |
38 | | - }) |
39 | | - |
40 | | - let output = '' |
41 | | - |
42 | | - child.stdout.on('data', data => { |
43 | | - output += data.toString() |
44 | | - }) |
45 | | - |
46 | | - child.on('exit', code => { |
47 | | - if (code !== 0) { |
48 | | - reject(new Error(`Type generation failed with exit code ${code}`)) |
49 | | - return |
50 | | - } |
| 34 | +/** |
| 35 | + * Prettifies the OpenAPI JSON file. |
| 36 | + */ |
| 37 | +async function prettifyOpenApiJson() { |
| 38 | + const openApiData = readFileSync(openApiJsonPath, 'utf8') |
| 39 | + writeFileSync( |
| 40 | + openApiJsonPath, |
| 41 | + JSON.stringify(JSON.parse(openApiData), null, 2), |
| 42 | + ) |
| 43 | +} |
51 | 44 |
|
52 | | - try { |
53 | | - writeFileSync(typesPath, output, 'utf8') |
54 | | - // Fix array syntax after writing to disk |
55 | | - fixArraySyntax(typesPath) |
56 | | - // Add SDK v3 method name aliases |
57 | | - addSdkMethodAliases(typesPath) |
58 | | - resolve() |
59 | | - } catch (error) { |
60 | | - reject(error) |
| 45 | +/** |
| 46 | + * Generates TypeScript types from OpenAPI schema. |
| 47 | + */ |
| 48 | +async function generateTypes() { |
| 49 | + const output = await openapiTS(openApiJsonPath, { |
| 50 | + transform(schemaObject) { |
| 51 | + if ('format' in schemaObject && schemaObject.format === 'binary') { |
| 52 | + return 'never' |
61 | 53 | } |
62 | | - }) |
63 | | - |
64 | | - child.on('error', reject) |
| 54 | + }, |
65 | 55 | }) |
| 56 | + |
| 57 | + writeFileSync(typesPath, output, 'utf8') |
| 58 | + // Fix array syntax after writing to disk |
| 59 | + fixArraySyntax(typesPath) |
| 60 | + // Add SDK v3 method name aliases |
| 61 | + addSdkMethodAliases(typesPath) |
66 | 62 | } |
67 | 63 |
|
68 | 64 | /** |
@@ -213,19 +209,15 @@ async function main() { |
213 | 209 |
|
214 | 210 | // Step 1: Prettify OpenAPI JSON |
215 | 211 | logger.log(' 1. Prettifying OpenAPI JSON...') |
216 | | - let exitCode = await runCommand('node', ['scripts/prettify-base-json.mjs']) |
217 | | - if (exitCode !== 0) { |
218 | | - process.exitCode = exitCode |
219 | | - return |
220 | | - } |
| 212 | + await prettifyOpenApiJson() |
221 | 213 |
|
222 | 214 | // Step 2: Generate types |
223 | 215 | logger.log(' 2. Generating TypeScript types...') |
224 | 216 | await generateTypes() |
225 | 217 |
|
226 | 218 | // Step 3: Format generated files |
227 | 219 | logger.log(' 3. Formatting generated files...') |
228 | | - exitCode = await runCommand('pnpm', [ |
| 220 | + let exitCode = await runCommand('pnpm', [ |
229 | 221 | 'exec', |
230 | 222 | 'biome', |
231 | 223 | 'format', |
|
0 commit comments