Skip to content

Commit 1feed2d

Browse files
committed
feat: support multiple OpenAPI specification sources
1 parent 9907427 commit 1feed2d

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

src/generator/apiGenerator.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,46 @@ async function loadOpenAPISpec(specSource: string): Promise<OpenAPIV3.Document>
3636
*/
3737
export async function generateApiClient(config: OpenAPIConfig): Promise<void> {
3838
try {
39-
// Load the OpenAPI specification
40-
const spec = await loadOpenAPISpec(config.specSource);
41-
console.log('Generated TypeScript interfaces successfully');
39+
// Load specs
40+
const specs = Array.isArray(config.specSource)
41+
? await Promise.all(config.specSource.map(loadOpenAPISpec))
42+
: [await loadOpenAPISpec(config.specSource)];
43+
// Generate for each spec...
44+
for (const spec of specs) {
45+
console.log('Generated TypeScript interfaces successfully');
46+
const title = spec.info.title.toLowerCase().replace(/\s+/g, '-');
4247

43-
// Create export directory if it doesn't exist
44-
await mkdir(config.exportDir, { recursive: true });
45-
46-
// Generate and write type definitions
47-
const typeDefinitions = generateTypeDefinitions(spec);
48-
await writeFile(
49-
resolve(config.exportDir, 'types.ts'),
50-
typeDefinitions,
51-
'utf-8'
52-
);
53-
54-
// Generate and write API client
55-
const clientCode = generateApiClientCode(spec);
56-
await writeFile(
57-
resolve(config.exportDir, 'client.ts'),
58-
clientCode,
59-
'utf-8'
60-
);
61-
62-
// Generate and write React Query options
63-
const queryCode = generateReactQuery(spec);
64-
await writeFile(
65-
resolve(config.exportDir, 'queries.ts'),
66-
queryCode,
67-
'utf-8'
68-
);
69-
70-
console.log('Generated API client successfully');
71-
console.log('Generated React Query options successfully');
72-
48+
// Create export directory if it doesn't exist
49+
await mkdir(config.exportDir, { recursive: true });
50+
51+
// Generate and write type definitions
52+
const typeDefinitions = generateTypeDefinitions(spec);
53+
await writeFile(
54+
resolve(config.exportDir, `${title}.schema.ts`),
55+
typeDefinitions,
56+
'utf-8'
57+
);
58+
59+
60+
// Generate and write API client
61+
const clientCode = generateApiClientCode(spec);
62+
await writeFile(
63+
resolve(config.exportDir, `${title}.client.ts`),
64+
clientCode,
65+
'utf-8'
66+
);
67+
68+
// Generate and write React Query options
69+
const queryCode = generateReactQuery(spec);
70+
await writeFile(
71+
resolve(config.exportDir, `${title}.queryOptions.ts`),
72+
queryCode,
73+
'utf-8'
74+
);
75+
76+
console.log('Generated API client successfully');
77+
console.log('Generated React Query options successfully');
78+
}
7379
} catch (error) {
7480
if (error instanceof Error) {
7581
throw new Error(`Failed to generate API client: ${error.message}`);

src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface OpenAPIConfig {
33
* OpenAPI specification source - can be a URL or local file path
44
* Supports JSON, YAML, or YML formats
55
*/
6-
specSource: string;
6+
specSource: string | string[];
77

88
/**
99
* Directory where generated files will be exported

test/configs/test-config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"specSource": "https://api.jaarbeurs.acc.lightbase.nl/_compas/structure.json?format=openapi",
2+
"specSource": ["https://api.diks.acc.lightbase.nl/_compas/structure.json?format=openapi",
3+
"https://api.jaarbeurs.acc.lightbase.nl/_compas/structure.json?format=openapi",
4+
"https://app.yourtravis.com/docs/api/location_api"
5+
],
36
"exportDir": "./test/generated",
47
"options": {
58
"generateMocks": true,

0 commit comments

Comments
 (0)