Skip to content

Commit 7ada415

Browse files
authored
Merge pull request #3 from JeffreyNijs/refactor/mock-generation-config-to-use-mockStrategy
Refactor mock generation config to use mockStrategy
2 parents 5e816be + 475f1ee commit 7ada415

File tree

11 files changed

+241
-107
lines changed

11 files changed

+241
-107
lines changed

README.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- **Automatic Reference Resolution:** Handles `$ref` and schema composition, so your builders reflect your OpenAPI definitions accurately.
1919
- **Seamless Integration:** Designed to work with Hey API's plugin system and TypeScript type generation.
2020
- **Zero External Dependencies:** No need for faker.js or other heavy dependencies - the custom runtime is lightweight and fast.
21-
- **Configurable Output:** Choose between custom runtime, Zod, or static mock generation.
21+
- **Flexible Configuration:** Choose between custom runtime, Zod, or static mock generation using a simple `mockStrategy` option.
2222

2323
## Installation
2424

@@ -56,17 +56,32 @@ export default defineConfig({
5656
defineBuildersConfig({
5757
// Optional: Generate Zod schemas for validation
5858
generateZod: true,
59-
// Optional: Use Zod for mock generation (requires zod-mock library)
60-
useZodForMocks: false,
61-
// Optional: Use static mock generation (no runtime dependencies)
62-
useStaticMocks: false,
59+
// Optional: Mock generation strategy (default: 'runtime')
60+
// Options: 'runtime' | 'zod' | 'static'
61+
mockStrategy: 'runtime',
6362
// Optional: Custom output filename
6463
output: 'builders.gen.ts'
6564
}),
6665
],
6766
});
6867
```
6968

69+
### Configuration Options
70+
71+
- **generateZod** (boolean): Generate Zod schemas alongside builders for validation. Default: `false`.
72+
- **mockStrategy** (string): Strategy for generating mock data. Default: `'runtime'`.
73+
- `'runtime'`: Use custom lightweight runtime mock generation
74+
- `'zod'`: Use Zod for mock generation
75+
- `'static'`: Generate static mock builders without runtime dependencies
76+
- **output** (string): Output filename (without extension) for the generated builders. Default: `'builders'`.
77+
78+
### Deprecated Options
79+
80+
The following options are deprecated but still supported for backward compatibility:
81+
82+
- **useZodForMocks** (boolean): Use `mockStrategy: 'zod'` instead.
83+
- **useStaticMocks** (boolean): Use `mockStrategy: 'static'` instead.
84+
7085
## Usage
7186

7287
After running the Hey API code generation, you'll get a file containing builder classes for each schema.
@@ -99,20 +114,19 @@ if (result.success) {
99114
}
100115
```
101116

102-
## Configuration Options
103-
104-
- **generateZod** (boolean): Generate Zod schemas alongside builders for validation. Default: `false`.
105-
- **useZodForMocks** (boolean): Use Zod for mock generation instead of the custom runtime. Default: `false`.
106-
- **useStaticMocks** (boolean): Generate static mock builders without runtime dependencies. When enabled, generates hardcoded mock values based on schema types. Default: `false`.
107-
- **output** (string): Output filename (without extension) for the generated builders. Default: `'builders'`.
108-
109117
## Mock Generation Strategies
110118

111119
### Custom Runtime (Default)
112120

113121
The default strategy uses a lightweight custom mock generator that supports all JSON Schema features without external dependencies. This provides fast, predictable mock data generation.
114122

115123
```typescript
124+
// Configuration
125+
defineBuildersConfig({
126+
mockStrategy: 'runtime', // or omit for default
127+
})
128+
129+
// Usage
116130
import { UserBuilder } from './client/builders';
117131

118132
const user = new UserBuilder()
@@ -129,7 +143,7 @@ Features:
129143

130144
### Static Mocks
131145

132-
When `useStaticMocks: true` is enabled, the plugin generates hardcoded mock values directly in the builder classes. This approach:
146+
When `mockStrategy: 'static'` is configured, the plugin generates hardcoded mock values directly in the builder classes. This approach:
133147

134148
- **No runtime generation** - All values are pre-computed at build time
135149
- **Predictable values** - generates consistent, type-appropriate default values
@@ -139,7 +153,7 @@ When `useStaticMocks: true` is enabled, the plugin generates hardcoded mock valu
139153
```typescript
140154
// Configuration
141155
defineBuildersConfig({
142-
useStaticMocks: true,
156+
mockStrategy: 'static',
143157
})
144158

145159
// Usage - same API, but mocks are statically generated
@@ -159,7 +173,19 @@ Static mocks generate appropriate defaults based on OpenAPI types and formats:
159173

160174
### Zod Integration
161175

162-
When `useZodForMocks: true` is enabled, mock generation uses Zod schemas. This is experimental and provides runtime validation.
176+
When `mockStrategy: 'zod'` is configured, mock generation uses Zod schemas. This is experimental and provides runtime validation.
177+
178+
```typescript
179+
// Configuration
180+
defineBuildersConfig({
181+
mockStrategy: 'zod',
182+
})
183+
184+
// Usage
185+
const user = new UserBuilder()
186+
.withName('Alice')
187+
.build(); // Uses Zod for mock generation
188+
```
163189

164190
## Format Support
165191

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hey-api-builders",
3-
"version": "0.7.0",
3+
"version": "0.8.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",

src/core/code-generator.test.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ describe('Code Generator', () => {
7878
});
7979

8080
describe('generateImports', () => {
81-
it('generates JSF imports when useStaticMocks is false', () => {
81+
it('generates runtime imports when mockStrategy is runtime', () => {
8282
const result = generateImports({
83-
useStaticMocks: false,
84-
useZodForMocks: false,
83+
mockStrategy: 'runtime',
8584
generateZod: false,
8685
});
8786

@@ -91,10 +90,9 @@ describe('Code Generator', () => {
9190
expect(result).not.toContain('import { z }');
9291
});
9392

94-
it('generates Zod imports when useZodForMocks is true', () => {
93+
it('generates Zod imports when mockStrategy is zod', () => {
9594
const result = generateImports({
96-
useStaticMocks: false,
97-
useZodForMocks: true,
95+
mockStrategy: 'zod',
9896
generateZod: false,
9997
});
10098

@@ -103,10 +101,9 @@ describe('Code Generator', () => {
103101
expect(result).not.toContain('import { generateMock }');
104102
});
105103

106-
it('generates no library imports when useStaticMocks is true', () => {
104+
it('generates no library imports when mockStrategy is static', () => {
107105
const result = generateImports({
108-
useStaticMocks: true,
109-
useZodForMocks: false,
106+
mockStrategy: 'static',
110107
generateZod: false,
111108
});
112109

@@ -117,8 +114,7 @@ describe('Code Generator', () => {
117114

118115
it('includes Zod import when generateZod is true', () => {
119116
const result = generateImports({
120-
useStaticMocks: false,
121-
useZodForMocks: false,
117+
mockStrategy: 'runtime',
122118
generateZod: true,
123119
});
124120

@@ -127,8 +123,7 @@ describe('Code Generator', () => {
127123

128124
it('includes Zod import only once when both options use it', () => {
129125
const result = generateImports({
130-
useStaticMocks: false,
131-
useZodForMocks: true,
126+
mockStrategy: 'zod',
132127
generateZod: true,
133128
});
134129

@@ -212,8 +207,7 @@ describe('Code Generator', () => {
212207
describe('generateImports edge cases', () => {
213208
it('includes all imports when all options are enabled', () => {
214209
const result = generateImports({
215-
useStaticMocks: false,
216-
useZodForMocks: true,
210+
mockStrategy: 'zod',
217211
generateZod: true,
218212
});
219213

@@ -224,8 +218,7 @@ describe('Code Generator', () => {
224218

225219
it('includes only base imports when no options are enabled', () => {
226220
const result = generateImports({
227-
useStaticMocks: false,
228-
useZodForMocks: false,
221+
mockStrategy: 'runtime',
229222
generateZod: false,
230223
});
231224

@@ -235,8 +228,7 @@ describe('Code Generator', () => {
235228

236229
it('handles static mocks correctly', () => {
237230
const result = generateImports({
238-
useStaticMocks: true,
239-
useZodForMocks: false,
231+
mockStrategy: 'static',
240232
generateZod: false,
241233
});
242234

src/core/code-generator.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Schema } from '../types';
1+
import type { Schema, MockStrategy } from '../types';
22
import type { ExtendedSchema } from '../types';
33
import { toPascal } from './string-utils';
44

@@ -31,20 +31,19 @@ export function generateWithMethods(schema: Schema, typeName: string): string {
3131
* @returns Import statements
3232
*/
3333
export function generateImports(options: {
34-
useStaticMocks: boolean;
35-
useZodForMocks: boolean;
34+
mockStrategy: MockStrategy;
3635
generateZod: boolean;
3736
}): string {
38-
const { useStaticMocks, useZodForMocks, generateZod } = options;
37+
const { mockStrategy, generateZod } = options;
3938
let imports = '';
4039

41-
const needsZodImport = generateZod || useZodForMocks;
40+
const needsZodImport = generateZod || mockStrategy === 'zod';
4241

43-
if (useStaticMocks) {
42+
if (mockStrategy === 'static') {
4443
return 'import type * as types from "./types.gen"\n\n';
4544
}
4645

47-
if (useZodForMocks) {
46+
if (mockStrategy === 'zod') {
4847
imports += 'import { generateMockFromZodSchema } from "hey-api-builders"\n';
4948
} else {
5049
imports += 'import { generateMock } from "hey-api-builders"\n';

0 commit comments

Comments
 (0)