Skip to content

Commit

Permalink
feat(client): generate all schemas under single file
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Mar 27, 2024
1 parent 5d6802c commit 34bdd7b
Show file tree
Hide file tree
Showing 260 changed files with 4,362 additions and 5,895 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-years-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

generate all schemas in single `schemas.ts` file
2 changes: 1 addition & 1 deletion src/utils/write/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ describe('writeClientSchemas', () => {
write: true,
});

expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/$User.ts'), 'schema');
expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/schemas.ts'), 'schema');
});
});
2 changes: 1 addition & 1 deletion src/utils/write/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const writeClient = async (client: Client, templates: Templates, config:
fn: writeClientCore,
},
{
dir: 'schemas',
dir: '',
enabled: config.exportSchemas,
fn: writeClientSchemas,
},
Expand Down
15 changes: 6 additions & 9 deletions src/utils/write/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'node:path';
import type { Client } from '../../types/client';
import type { Config } from '../../types/config';
import type { Templates } from '../handlebars';
import { sortByName } from '../sort';

/**
* Generate Schemas using the Handlebar template and write to disk.
Expand All @@ -19,17 +18,15 @@ export const writeClientSchemas = async (
outputPath: string,
config: Config
): Promise<void> => {
// Generate file for each models schema.
// Generate file with all schemas
const results: string[] = [];
for (const model of client.models) {
const file = path.resolve(outputPath, `$${model.name}.ts`);
const templateResult = templates.exports.schema({
const result = templates.exports.schema({
$config: config,
...model,
});
await writeFileSync(file, templateResult);
results.push(result);
}
// Generate index file exporting all generated schema files.
const file = path.resolve(outputPath, 'index.ts');
const content = sortByName(client.models).map(model => `export { $${model.name} } from './$${model.name}';`);
await writeFileSync(file, content.join('\n'));
const file = path.resolve(outputPath, 'schemas.ts');
await writeFileSync(file, results.join('\n\n'));
};
Loading

0 comments on commit 34bdd7b

Please sign in to comment.