Skip to content

Commit 0e703ac

Browse files
committed
Support custom model name normalization in handler
Added an optional normalize function to collectSchemas and updated the handler to use a modelName provider from context if available. Extended types to support passing context with casing options to the handler.
1 parent 7ada415 commit 0e703ac

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hey-api-builders",
3-
"version": "0.8.1",
3+
"version": "1.0.1",
44
"description": "A custom plugin for @hey-api/openapi-ts that generates mock data builders with a lightweight custom runtime, Zod integration, or static mock generation.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -61,4 +61,4 @@
6161
"vitest": "^3.2.4",
6262
"zod": "^3.23.8"
6363
}
64-
}
64+
}

src/core/schema-transformer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,14 @@ export function sanitizeSchema(node: NormalizedSchemaNode): Schema {
360360
* @param all - All IR schemas
361361
* @returns Array of schema metadata
362362
*/
363-
export function collectSchemas(all: Record<string, IR.SchemaObject>): GeneratedSchemaMeta[] {
363+
export function collectSchemas(
364+
all: Record<string, IR.SchemaObject>,
365+
normalize?: (name: string) => string
366+
): GeneratedSchemaMeta[] {
364367
const metas: GeneratedSchemaMeta[] = [];
365368
for (const [name, irSchema] of Object.entries(all)) {
366369
let typeName = name.replace(/Schema$/, '');
367-
typeName = normalizeTypeName(typeName);
370+
typeName = normalize ? normalize(typeName) : normalizeTypeName(typeName);
368371

369372
const jsf = sanitizeSchema(normalizeSchema(irToSchema(irSchema as IR.SchemaObject, all)));
370373
const schemaWithType = jsf as ExtendedSchema;

src/plugin/handler.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ function resolveMockStrategy(config: {
3737
/**
3838
* Main plugin handler for generating builder classes
3939
*/
40-
export const handler: BuildersHandler = ({ plugin }) => {
40+
export const handler: BuildersHandler = ({ plugin, context }) => {
4141
const rawSchemas: Record<string, IR.SchemaObject> = {};
4242
plugin.forEach('schema', (event) => {
4343
rawSchemas[event.name] = event.schema;
4444
});
45-
const metas = collectSchemas(rawSchemas);
45+
46+
const modelNameProvider = context?.casing?.modelName;
47+
const normalize = modelNameProvider ? (name: string) => modelNameProvider(name) : undefined;
48+
49+
const metas = collectSchemas(rawSchemas, normalize);
4650

4751
const file = plugin.createFile({ id: plugin.name, path: plugin.output });
4852

src/types/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,22 @@ export interface Config {
111111
*/
112112
export type BuildersPlugin = DefinePlugin<Config>;
113113

114+
/**
115+
* Arguments for the plugin handler
116+
*/
117+
export interface HandlerArgs {
118+
plugin: Parameters<BuildersPlugin['Handler']>[0]['plugin'];
119+
context: IR.Context & {
120+
casing?: {
121+
modelName?: (name: string) => string;
122+
};
123+
};
124+
}
125+
114126
/**
115127
* Builder handler type
116128
*/
117-
export type BuildersHandler = BuildersPlugin['Handler'];
129+
export type BuildersHandler = (args: HandlerArgs) => void;
118130

119131
/**
120132
* Options for builder instances

0 commit comments

Comments
 (0)