Skip to content

Commit 154c686

Browse files
authored
feat(fetch): make output.schemas.type exclusive and remove override.fetch.useSchemaResponse (orval-labs#2667)
* feat: make `output.schemas.type` exclusive and remove `override.fetch.useR Schema Response` * chore: update fetch test * feat: simplified fetch client import * chore: remove old test * fix: faild test
1 parent 56d1053 commit 154c686

File tree

7 files changed

+62
-125
lines changed

7 files changed

+62
-125
lines changed

packages/core/src/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ export type SchemaGenerationType = 'typescript' | 'zod';
202202

203203
export type SchemaOptions = {
204204
path: string;
205-
type: SchemaGenerationType | SchemaGenerationType[];
205+
type: SchemaGenerationType;
206206
};
207207

208208
export type NormalizedSchemaOptions = {
209209
path: string;
210-
type: SchemaGenerationType[];
210+
type: SchemaGenerationType;
211211
};
212212

213213
export type OutputOptions = {
@@ -629,15 +629,13 @@ export type NormalizedFetchOptions = {
629629
includeHttpResponseReturnType: boolean;
630630
forceSuccessResponse: boolean;
631631
jsonReviver?: Mutator;
632-
useZodSchemaResponse: boolean;
633632
runtimeValidation: boolean;
634633
};
635634

636635
export type FetchOptions = {
637636
includeHttpResponseReturnType?: boolean;
638637
forceSuccessResponse?: boolean;
639638
jsonReviver?: Mutator;
640-
useZodSchemaResponse?: boolean;
641639
runtimeValidation?: boolean;
642640
};
643641

packages/fetch/src/index.ts

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ const FETCH_DEPENDENCIES: GeneratorDependency[] = [
4545

4646
export const getFetchDependencies = () => FETCH_DEPENDENCIES;
4747

48-
const PRIMITIVE_TYPES = new Set([
49-
'string',
50-
'number',
51-
'boolean',
52-
'void',
53-
'unknown',
54-
]);
55-
5648
export const generateRequestFunction = (
5749
{
5850
queryParams,
@@ -169,7 +161,14 @@ ${
169161
);
170162

171163
const responseType = response.definition.success;
172-
const isPrimitiveType = PRIMITIVE_TYPES.has(responseType);
164+
165+
const isPrimitiveType = [
166+
'string',
167+
'number',
168+
'boolean',
169+
'void',
170+
'unknown',
171+
].includes(responseType);
173172
const hasSchema = response.imports.some((imp) => imp.name === responseType);
174173

175174
const isValidateResponse =
@@ -204,12 +203,7 @@ ${
204203
)
205204
.map((r) => {
206205
const name = `${responseTypeName}${pascal(r.key)}${'suffix' in r ? r.suffix : ''}`;
207-
208-
const hasValidZodSchema = r.value && !PRIMITIVE_TYPES.has(r.value);
209-
const dataType =
210-
override.fetch.useZodSchemaResponse && hasValidZodSchema
211-
? `zod.infer<typeof ${r.value}>`
212-
: r.value || 'unknown';
206+
const dataType = r.value || 'unknown';
213207

214208
return {
215209
name,
@@ -413,57 +407,24 @@ export const fetchResponseTypeName = (
413407
export const generateClient: ClientBuilder = (
414408
verbOptions,
415409
options,
416-
outputClient,
410+
_outputClient,
417411
output,
418412
) => {
419413
const imports = generateVerbImports(verbOptions);
420414
const functionImplementation = generateRequestFunction(verbOptions, options);
421415

422-
const isZodSchemaImportsRequired =
423-
verbOptions.override.fetch.useZodSchemaResponse ||
424-
verbOptions.override.fetch.runtimeValidation;
425-
426-
const responseImports = isZodSchemaImportsRequired
427-
? [
428-
...verbOptions.response.types.success,
429-
...verbOptions.response.types.errors,
430-
].flatMap((response) =>
431-
response.imports.map((imp) => ({
432-
name: imp.name,
433-
schemaName: imp.name,
434-
isZodSchema: true,
435-
values: true,
436-
})),
437-
)
438-
: [];
439-
440-
const requestImports = isZodSchemaImportsRequired
441-
? [
442-
...verbOptions.body.imports,
443-
...(verbOptions.queryParams
444-
? [{ name: `${pascal(verbOptions.operationName)}QueryParams` }]
445-
: []),
446-
...(verbOptions.headers
447-
? [{ name: `${pascal(verbOptions.operationName)}Header` }]
448-
: []),
449-
].map((imp) => ({
450-
name: imp.name,
451-
schemaName: imp.name,
452-
isZodSchema: true,
453-
values: true,
454-
}))
455-
: [];
456-
457-
const zodSchemaImports = [...responseImports, ...requestImports];
416+
const isZodSchema =
417+
isObject(output?.schemas) && output.schemas.type === 'zod';
458418

459-
const zodSchemaNames = new Set(zodSchemaImports.map((imp) => imp.name));
460-
const filteredImports = imports.filter(
461-
(imp) => !zodSchemaNames.has(imp.name),
462-
);
419+
const generateImports = imports.map((imp) => ({
420+
...imp,
421+
isZodSchema: isZodSchema,
422+
values: isZodSchema ? true : imp.values,
423+
}));
463424

464425
return {
465426
implementation: `${functionImplementation}\n`,
466-
imports: [...filteredImports, ...zodSchemaImports],
427+
imports: generateImports,
467428
};
468429
};
469430

packages/orval/src/client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,12 @@ export const generateOperations = (
246246
outputClient,
247247
output,
248248
);
249-
const client = await generatorClient(verbOption, options, outputClient);
249+
const client = await generatorClient(
250+
verbOption,
251+
options,
252+
outputClient,
253+
output,
254+
);
250255

251256
if (!client.implementation) {
252257
return acc;

packages/orval/src/utils/options.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,9 @@ function normalizeSchemasOption(
9696
return normalizePath(schemas, workspace);
9797
}
9898

99-
const types: SchemaGenerationType[] = Array.isArray(schemas.type)
100-
? schemas.type
101-
: [schemas.type];
102-
10399
return {
104100
path: normalizePath(schemas.path, workspace),
105-
type: types,
101+
type: schemas.type,
106102
};
107103
}
108104

@@ -370,8 +366,6 @@ export async function normalizeOptions(
370366
true,
371367
forceSuccessResponse:
372368
outputOptions.override?.fetch?.forceSuccessResponse ?? false,
373-
useZodSchemaResponse:
374-
outputOptions.override?.fetch?.useZodSchemaResponse ?? false,
375369
runtimeValidation:
376370
outputOptions.override?.fetch?.runtimeValidation ?? false,
377371
...outputOptions.override?.fetch,

packages/orval/src/write-specs.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,49 +65,45 @@ export async function writeSpecs(
6565
indexFiles: output.indexFiles,
6666
});
6767
} else {
68-
const types: SchemaGenerationType[] = Array.isArray(output.schemas.type)
69-
? output.schemas.type
70-
: [output.schemas.type];
68+
const schemaType = output.schemas.type;
7169

72-
for (const schemaType of types) {
73-
if (schemaType === 'typescript') {
74-
const fileExtension = output.fileExtension || '.ts';
70+
if (schemaType === 'typescript') {
71+
const fileExtension = output.fileExtension || '.ts';
7572

76-
await writeSchemas({
77-
schemaPath: output.schemas.path,
78-
schemas,
79-
target,
80-
namingConvention: output.namingConvention,
81-
fileExtension,
82-
header,
83-
indexFiles: output.indexFiles,
84-
});
85-
} else if (schemaType === 'zod') {
86-
const fileExtension = '.zod.ts';
73+
await writeSchemas({
74+
schemaPath: output.schemas.path,
75+
schemas,
76+
target,
77+
namingConvention: output.namingConvention,
78+
fileExtension,
79+
header,
80+
indexFiles: output.indexFiles,
81+
});
82+
} else if (schemaType === 'zod') {
83+
const fileExtension = '.zod.ts';
84+
85+
await writeZodSchemas(
86+
builder,
87+
output.schemas.path,
88+
fileExtension,
89+
header,
90+
output,
91+
);
8792

88-
await writeZodSchemas(
89-
builder,
93+
if (builder.verbOptions) {
94+
await writeZodSchemasFromVerbs(
95+
builder.verbOptions,
9096
output.schemas.path,
9197
fileExtension,
9298
header,
9399
output,
94-
);
95-
96-
if (builder.verbOptions) {
97-
await writeZodSchemasFromVerbs(
98-
builder.verbOptions,
99-
output.schemas.path,
100-
fileExtension,
101-
header,
100+
{
101+
spec: builder.spec,
102+
target: builder.target,
103+
workspace,
102104
output,
103-
{
104-
spec: builder.spec,
105-
target: builder.target,
106-
workspace,
107-
output,
108-
},
109-
);
110-
}
105+
},
106+
);
111107
}
112108
}
113109
}

tests/configs/default.config.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,4 @@ export default defineConfig({
530530
target: '../specifications/petstore.yaml',
531531
},
532532
},
533-
'schemas-zod-and-typescript': {
534-
output: {
535-
target: '../generated/default/schemas-zod-and-typescript/endpoints.ts',
536-
schemas: {
537-
path: '../generated/default/schemas-zod-and-typescript/model',
538-
type: ['typescript', 'zod'],
539-
},
540-
client: 'fetch',
541-
},
542-
input: {
543-
target: '../specifications/petstore.yaml',
544-
},
545-
},
546533
});

tests/configs/fetch.config.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,11 @@ export default defineConfig({
299299
target: '../generated/fetch/zod-schema-response-single/endpoints.ts',
300300
schemas: {
301301
path: '../generated/fetch/zod-schema-response-single/model',
302-
type: ['typescript', 'zod'],
302+
type: 'zod',
303303
},
304304
client: 'fetch',
305305
override: {
306306
fetch: {
307-
useZodSchemaResponse: true,
308307
runtimeValidation: true,
309308
},
310309
},
@@ -318,14 +317,13 @@ export default defineConfig({
318317
target: '../generated/fetch/zod-schema-response-split/endpoints.ts',
319318
schemas: {
320319
path: '../generated/fetch/zod-schema-response-split/model',
321-
type: ['typescript', 'zod'],
320+
type: 'zod',
322321
},
323322
mode: 'split',
324323
indexFiles: false,
325324
client: 'fetch',
326325
override: {
327326
fetch: {
328-
useZodSchemaResponse: true,
329327
runtimeValidation: true,
330328
},
331329
},
@@ -339,13 +337,12 @@ export default defineConfig({
339337
target: '../generated/fetch/zod-schema-response-tags/endpoints.ts',
340338
schemas: {
341339
path: '../generated/fetch/zod-schema-response-tags/model',
342-
type: ['typescript', 'zod'],
340+
type: 'zod',
343341
},
344342
mode: 'tags',
345343
client: 'fetch',
346344
override: {
347345
fetch: {
348-
useZodSchemaResponse: true,
349346
runtimeValidation: true,
350347
},
351348
},
@@ -359,13 +356,12 @@ export default defineConfig({
359356
target: '../generated/fetch/zod-schema-response-tags-split/endpoints.ts',
360357
schemas: {
361358
path: '../generated/fetch/zod-schema-response-tags-split/model',
362-
type: ['typescript', 'zod'],
359+
type: 'zod',
363360
},
364361
mode: 'tags-split',
365362
client: 'fetch',
366363
override: {
367364
fetch: {
368-
useZodSchemaResponse: true,
369365
runtimeValidation: true,
370366
},
371367
},

0 commit comments

Comments
 (0)