Skip to content

Commit 3054b76

Browse files
committed
feat: make naming in generated types configurable
1 parent d7038ac commit 3054b76

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@kickstartds/jsonschema2types",
5+
"comment": "Add configurable naming for schemas",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@kickstartds/jsonschema2types"
10+
}

tools/jsonschema2types/src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,35 @@ import { pascalCase } from 'pascal-case';
55

66
declare type MyAjv = import('ajv').default;
77

8+
export function defaultTitleFunction(schema: JSONSchema4): string {
9+
return `${schema.title}Props`;
10+
}
11+
12+
export function defaultFilenameFunction(schema: JSONSchema4): string {
13+
return `${pascalCase(getSchemaName(schema.$id))}Props`;
14+
}
15+
816
export async function createTypes(
917
schemaIds: string[],
1018
renderImportName: (schemaId: string) => string,
1119
renderImportStatement: (schemaId: string) => string,
1220
ajv: MyAjv,
13-
options?: Partial<Options>
21+
options?: Partial<Options>,
22+
titleFunction: (schema: JSONSchema4) => string = defaultTitleFunction,
23+
filenameFunction: (schema: JSONSchema4) => string = defaultFilenameFunction
1424
): Promise<Record<string, string>> {
1525
const generatedTypings: Record<string, string> = {};
1626
const schemas = getSchemasForIds(schemaIds, ajv);
1727

18-
for (const schema of schemas) {
28+
for (const schema of schemas as JSONSchema4[]) {
1929
if (!schema.$id) throw new Error("Can't process a schema without $id");
2030

2131
const typings = await compile(
2232
{
23-
...(schema as JSONSchema4),
24-
title: `${schema.title}Props`
33+
...schema,
34+
title: titleFunction(schema)
2535
},
26-
`${pascalCase(getSchemaName(schema.$id))}Props`,
36+
filenameFunction(schema),
2737
{
2838
declareExternallyReferenced: true,
2939
$refOptions: {

0 commit comments

Comments
 (0)