@orval/mock 8.15.0 → 8.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +15 -4
- package/dist/index.mjs +178 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -31
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ClientMockGeneratorBuilder, ContextSpec, FakerMockOptions, GenerateMockImports, GeneratorImport, GeneratorOptions, GeneratorSchema, GeneratorVerbOptions, GlobalMockOptions, MswMockOptions } from "@orval/core";
|
|
1
|
+
import { ClientMockGeneratorBuilder, ContextSpec, FakerMockOptions, FinalizeMockImplementationOptions, GenerateMockImports, GeneratorImport, GeneratorOptions, GeneratorSchema, GeneratorVerbOptions, GlobalMockOptions, MswMockOptions } from "@orval/core";
|
|
3
2
|
|
|
4
3
|
//#region src/faker/index.d.ts
|
|
5
4
|
/**
|
|
@@ -18,6 +17,7 @@ declare function generateFaker(generatorVerbOptions: GeneratorVerbOptions, gener
|
|
|
18
17
|
interface GenerateFakerForSchemasResult {
|
|
19
18
|
implementation: string;
|
|
20
19
|
imports: GeneratorImport[];
|
|
20
|
+
strictMockSchemaTypeNames?: string[];
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Builds the contents of a consolidated faker mock file for every entry under
|
|
@@ -30,6 +30,17 @@ interface GenerateFakerForSchemasResult {
|
|
|
30
30
|
*/
|
|
31
31
|
declare function generateFakerForSchemas(schemas: GeneratorSchema[], context: ContextSpec, options: GlobalMockOptions): GenerateFakerForSchemasResult;
|
|
32
32
|
//#endregion
|
|
33
|
+
//#region src/mock-types.d.ts
|
|
34
|
+
declare function buildStrictMockTypeFileHeader(schemaTypeNames: Iterable<string>): string;
|
|
35
|
+
/**
|
|
36
|
+
* Prepends shared strict-mock helper types and each `{Schema}Mock` alias once at
|
|
37
|
+
* the top of a mock file. Generators pass `strictSchemaTypeNames`; no scraping.
|
|
38
|
+
*
|
|
39
|
+
* Not idempotent — callers must invoke this exactly once per aggregated mock
|
|
40
|
+
* file (writers and `writeFakerSchemaMocks`), not from import hooks.
|
|
41
|
+
*/
|
|
42
|
+
declare function dedupeStrictMockTypeDeclarations(implementation: string, options?: FinalizeMockImplementationOptions): string;
|
|
43
|
+
//#endregion
|
|
33
44
|
//#region src/msw/index.d.ts
|
|
34
45
|
declare const generateMSWImports: GenerateMockImports;
|
|
35
46
|
declare function generateMSW(generatorVerbOptions: GeneratorVerbOptions, generatorOptions: GeneratorOptions): ClientMockGeneratorBuilder;
|
|
@@ -55,7 +66,7 @@ declare const generateMockImports: GenerateMockImports;
|
|
|
55
66
|
*/
|
|
56
67
|
declare function generateMock(generatorVerbOptions: GeneratorVerbOptions, generatorOptions: Omit<GeneratorOptions, 'mock'> & {
|
|
57
68
|
mock: GlobalMockOptions;
|
|
58
|
-
}):
|
|
69
|
+
}): import("@orval/core").ClientMockGeneratorBuilder;
|
|
59
70
|
//#endregion
|
|
60
|
-
export { DEFAULT_FAKER_OPTIONS, DEFAULT_MSW_OPTIONS, type GenerateFakerForSchemasResult, generateFaker, generateFakerForSchemas, generateFakerImports, generateMSW, generateMSWImports, generateMock, generateMockImports, getDefaultMockOptionsForType };
|
|
71
|
+
export { DEFAULT_FAKER_OPTIONS, DEFAULT_MSW_OPTIONS, type GenerateFakerForSchemasResult, buildStrictMockTypeFileHeader, dedupeStrictMockTypeDeclarations, generateFaker, generateFakerForSchemas, generateFakerImports, generateMSW, generateMSWImports, generateMock, generateMockImports, getDefaultMockOptionsForType };
|
|
61
72
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,103 @@
|
|
|
1
1
|
import { DefaultTag, EnumGeneration, OutputMockType, OutputMode, PropertySortOrder, camel, compareVersions, escape, escapeRegExp, generalJSTypesWithArray, generateDependencyImports, getKey, getRefInfo, isBoolean, isFunction, isMswMock, isNumber, isObject, isReference, isSchema, isString, kebab, mergeDeep, pascal, resolveRef, sanitize, stringify } from "@orval/core";
|
|
2
2
|
import { prop } from "remeda";
|
|
3
|
+
//#region src/mock-types.ts
|
|
4
|
+
function isStrictMock(mockOptions) {
|
|
5
|
+
return Boolean(mockOptions && mockOptions.required && mockOptions.nonNullable);
|
|
6
|
+
}
|
|
7
|
+
function getStrictMockTypeName(typeName) {
|
|
8
|
+
return `${typeName}Mock`;
|
|
9
|
+
}
|
|
10
|
+
function getStrictMockHelperTypeDeclarations() {
|
|
11
|
+
return `export type KeysWithNull<O> = {
|
|
12
|
+
[K in keyof O]-?: null extends O[K] ? K : never;
|
|
13
|
+
}[keyof O];
|
|
14
|
+
|
|
15
|
+
export type MockWithNullableOverrides<
|
|
16
|
+
T,
|
|
17
|
+
O extends Partial<T>,
|
|
18
|
+
M extends Record<keyof T, unknown>,
|
|
19
|
+
> = Omit<M, Extract<KeysWithNull<O>, keyof T>> & {
|
|
20
|
+
[K in Extract<KeysWithNull<O>, keyof T>]: M[K] | null;
|
|
21
|
+
};`;
|
|
22
|
+
}
|
|
23
|
+
function getStrictMockTypeDeclaration(typeName) {
|
|
24
|
+
return `export type ${getStrictMockTypeName(typeName)} = {\n [K in keyof Required<${typeName}>]: NonNullable<Required<${typeName}>[K]>;\n};`;
|
|
25
|
+
}
|
|
26
|
+
function getStrictMockTypeDeclarations(typeNames) {
|
|
27
|
+
const unique = [...new Set(typeNames)];
|
|
28
|
+
if (unique.length === 0) return "";
|
|
29
|
+
return unique.map((typeName) => getStrictMockTypeDeclaration(typeName)).join("\n\n");
|
|
30
|
+
}
|
|
31
|
+
function getMockFactoryReturnType(typeName, mockOptions) {
|
|
32
|
+
return isStrictMock(mockOptions) ? getStrictMockTypeName(typeName) : typeName;
|
|
33
|
+
}
|
|
34
|
+
function getMockFactorySignatureParts(typeName, mockOptions, options = {}) {
|
|
35
|
+
const isOverridable = options.isOverridable ?? false;
|
|
36
|
+
const overrideType = options.overrideType ?? `Partial<${typeName}>`;
|
|
37
|
+
const mockTypeName = getStrictMockTypeName(typeName);
|
|
38
|
+
if (!isOverridable) return {
|
|
39
|
+
param: "",
|
|
40
|
+
returnType: getMockFactoryReturnType(typeName, mockOptions),
|
|
41
|
+
returnCast: ""
|
|
42
|
+
};
|
|
43
|
+
if (isStrictMock(mockOptions)) return {
|
|
44
|
+
param: `<O extends ${overrideType} = {}>(overrideResponse?: O)`,
|
|
45
|
+
returnType: `MockWithNullableOverrides<${typeName}, O, ${mockTypeName}>`,
|
|
46
|
+
returnCast: ` as MockWithNullableOverrides<${typeName}, O, ${mockTypeName}>`
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
param: `overrideResponse: ${overrideType} = {}`,
|
|
50
|
+
returnType: typeName,
|
|
51
|
+
returnCast: ""
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function getSimpleSchemaReturnType(returnType, schemaTypeNames) {
|
|
55
|
+
const trimmed = returnType.trim();
|
|
56
|
+
return schemaTypeNames.includes(trimmed) ? trimmed : void 0;
|
|
57
|
+
}
|
|
58
|
+
function formatMockFactoryDeclaration(factoryName, param, returnType, body, returnCast, options) {
|
|
59
|
+
return `${param ? param.startsWith("<") ? `export const ${factoryName} = ${param}` : `export const ${factoryName} = (${param})` : `export const ${factoryName} = ()`}${options?.omitReturnType || !returnType ? "" : `: ${returnType}`} => (${body})${returnCast}${returnCast || options?.terminateStatement ? ";" : ""}`;
|
|
60
|
+
}
|
|
61
|
+
function getSchemaTypeNamesFromResponses(responses) {
|
|
62
|
+
const names = /* @__PURE__ */ new Set();
|
|
63
|
+
for (const response of responses) {
|
|
64
|
+
for (const imp of response.imports) {
|
|
65
|
+
if (imp.values || imp.schemaFactory) continue;
|
|
66
|
+
const importName = imp.alias ?? imp.name;
|
|
67
|
+
if (/^[A-Z]\w*$/.test(importName)) names.add(importName);
|
|
68
|
+
}
|
|
69
|
+
const { value } = response;
|
|
70
|
+
if (!value) continue;
|
|
71
|
+
const baseType = value.endsWith("[]") ? value.slice(0, -2) : value;
|
|
72
|
+
if (/^[A-Z]\w*$/.test(baseType)) names.add(baseType);
|
|
73
|
+
}
|
|
74
|
+
return [...names];
|
|
75
|
+
}
|
|
76
|
+
function buildStrictMockTypeFileHeader(schemaTypeNames) {
|
|
77
|
+
const schemaBlock = getStrictMockTypeDeclarations([...new Set(schemaTypeNames)]);
|
|
78
|
+
return [getStrictMockHelperTypeDeclarations(), schemaBlock].filter(Boolean).join("\n\n");
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Prepends shared strict-mock helper types and each `{Schema}Mock` alias once at
|
|
82
|
+
* the top of a mock file. Generators pass `strictSchemaTypeNames`; no scraping.
|
|
83
|
+
*
|
|
84
|
+
* Not idempotent — callers must invoke this exactly once per aggregated mock
|
|
85
|
+
* file (writers and `writeFakerSchemaMocks`), not from import hooks.
|
|
86
|
+
*/
|
|
87
|
+
function dedupeStrictMockTypeDeclarations(implementation, options = {}) {
|
|
88
|
+
if (!isStrictMock(options.mockOptions)) return implementation;
|
|
89
|
+
const schemaTypeNames = options.strictSchemaTypeNames ? [...new Set(options.strictSchemaTypeNames)] : [];
|
|
90
|
+
if (schemaTypeNames.length === 0) return implementation;
|
|
91
|
+
return `${buildStrictMockTypeFileHeader(schemaTypeNames)}\n\n${implementation.trimStart()}`;
|
|
92
|
+
}
|
|
93
|
+
function applyStrictMockReturnType(returnType, schemaTypeNames) {
|
|
94
|
+
if (schemaTypeNames.length === 0) return returnType;
|
|
95
|
+
let result = returnType;
|
|
96
|
+
const sorted = [...schemaTypeNames].toSorted((a, b) => b.length - a.length);
|
|
97
|
+
for (const name of sorted) result = result.replaceAll(new RegExp(String.raw`\b${escapeRegExp(name)}\b`, "g"), getStrictMockTypeName(name));
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
3
101
|
//#region src/delay.ts
|
|
4
102
|
const getDelay = (override, options) => {
|
|
5
103
|
const mswOptions = options && isMswMock(options) ? options : void 0;
|
|
@@ -55,7 +153,7 @@ function getReferenceName$1(ref, context) {
|
|
|
55
153
|
if (!ref) return "";
|
|
56
154
|
return getRefInfo(ref, context).name;
|
|
57
155
|
}
|
|
58
|
-
function getMockObject({ item, mockOptions, operationId, tags, combine, context, imports, existingReferencedProperties, splitMockImplementations, allowOverride = false }) {
|
|
156
|
+
function getMockObject({ item, mockOptions, operationId, tags, combine, context, imports, existingReferencedProperties, existingReferencedAllOfRefs = [], splitMockImplementations, allowOverride = false }) {
|
|
59
157
|
if (isReference(item)) return resolveMockValue({
|
|
60
158
|
schema: {
|
|
61
159
|
...item,
|
|
@@ -68,6 +166,7 @@ function getMockObject({ item, mockOptions, operationId, tags, combine, context,
|
|
|
68
166
|
context,
|
|
69
167
|
imports,
|
|
70
168
|
existingReferencedProperties,
|
|
169
|
+
existingReferencedAllOfRefs,
|
|
71
170
|
splitMockImplementations
|
|
72
171
|
});
|
|
73
172
|
const schemaItem = item;
|
|
@@ -88,6 +187,7 @@ function getMockObject({ item, mockOptions, operationId, tags, combine, context,
|
|
|
88
187
|
context,
|
|
89
188
|
imports,
|
|
90
189
|
existingReferencedProperties,
|
|
190
|
+
existingReferencedAllOfRefs,
|
|
91
191
|
splitMockImplementations
|
|
92
192
|
});
|
|
93
193
|
if (Array.isArray(itemType)) {
|
|
@@ -108,6 +208,7 @@ function getMockObject({ item, mockOptions, operationId, tags, combine, context,
|
|
|
108
208
|
context,
|
|
109
209
|
imports,
|
|
110
210
|
existingReferencedProperties,
|
|
211
|
+
existingReferencedAllOfRefs,
|
|
111
212
|
splitMockImplementations
|
|
112
213
|
});
|
|
113
214
|
}
|
|
@@ -140,6 +241,7 @@ function getMockObject({ item, mockOptions, operationId, tags, combine, context,
|
|
|
140
241
|
context,
|
|
141
242
|
imports,
|
|
142
243
|
existingReferencedProperties,
|
|
244
|
+
existingReferencedAllOfRefs: [],
|
|
143
245
|
splitMockImplementations
|
|
144
246
|
});
|
|
145
247
|
imports.push(...resolvedValue.imports);
|
|
@@ -187,6 +289,7 @@ function getMockObject({ item, mockOptions, operationId, tags, combine, context,
|
|
|
187
289
|
context,
|
|
188
290
|
imports,
|
|
189
291
|
existingReferencedProperties,
|
|
292
|
+
existingReferencedAllOfRefs: [],
|
|
190
293
|
splitMockImplementations
|
|
191
294
|
});
|
|
192
295
|
return {
|
|
@@ -327,7 +430,12 @@ function extractArrayItemMock({ items, propertyName, parentName, operationId, ta
|
|
|
327
430
|
const { factoryName, typeName } = names;
|
|
328
431
|
const fileLevelFactories = getFileLevelExtractedFactories(context, getArrayItemMockFileScope(context, tags));
|
|
329
432
|
if (!(fileLevelFactories.has(factoryName) || splitMockImplementations.some((f) => f.includes(`export const ${factoryName}`)))) {
|
|
330
|
-
const
|
|
433
|
+
const mockOptions = context.output.override.mock;
|
|
434
|
+
const { param, returnType, returnCast } = getMockFactorySignatureParts(typeName, mockOptions, {
|
|
435
|
+
isOverridable: true,
|
|
436
|
+
overrideType: `Partial<${typeName}>`
|
|
437
|
+
});
|
|
438
|
+
const func = formatMockFactoryDeclaration(factoryName, param, returnType, `{${mapValue.startsWith("...") ? "" : "..."}${mapValue}, ...${overrideVarName}}`, returnCast, { terminateStatement: true });
|
|
331
439
|
splitMockImplementations.push(func);
|
|
332
440
|
fileLevelFactories.add(factoryName);
|
|
333
441
|
}
|
|
@@ -336,7 +444,7 @@ function extractArrayItemMock({ items, propertyName, parentName, operationId, ta
|
|
|
336
444
|
}
|
|
337
445
|
//#endregion
|
|
338
446
|
//#region src/faker/getters/scalar.ts
|
|
339
|
-
function getMockScalar({ item, imports, mockOptions, operationId, tags, combine, context, existingReferencedProperties, splitMockImplementations, allowOverride = false }) {
|
|
447
|
+
function getMockScalar({ item, imports, mockOptions, operationId, tags, combine, context, existingReferencedProperties, existingReferencedAllOfRefs = [], splitMockImplementations, allowOverride = false }) {
|
|
340
448
|
const safeMockOptions = mockOptions ?? {};
|
|
341
449
|
const nonNullableOption = safeMockOptions.nonNullable;
|
|
342
450
|
if (item.isRef) existingReferencedProperties = [...existingReferencedProperties, item.name];
|
|
@@ -458,6 +566,7 @@ function getMockScalar({ item, imports, mockOptions, operationId, tags, combine,
|
|
|
458
566
|
context,
|
|
459
567
|
imports,
|
|
460
568
|
existingReferencedProperties,
|
|
569
|
+
existingReferencedAllOfRefs,
|
|
461
570
|
splitMockImplementations
|
|
462
571
|
});
|
|
463
572
|
if (enums) return {
|
|
@@ -559,6 +668,7 @@ function getMockScalar({ item, imports, mockOptions, operationId, tags, combine,
|
|
|
559
668
|
context,
|
|
560
669
|
imports,
|
|
561
670
|
existingReferencedProperties,
|
|
671
|
+
existingReferencedAllOfRefs,
|
|
562
672
|
splitMockImplementations,
|
|
563
673
|
allowOverride
|
|
564
674
|
});
|
|
@@ -622,7 +732,8 @@ function stripArrayMarkerSegments(s) {
|
|
|
622
732
|
function resolveMockOverride(properties = {}, item, nonNullableOption) {
|
|
623
733
|
const path = item.path ?? `#.${item.name}`;
|
|
624
734
|
const normalizedPath = stripArrayMarkerSegments(path);
|
|
625
|
-
const
|
|
735
|
+
const entries = Object.entries(properties);
|
|
736
|
+
let property = entries.find(([key]) => {
|
|
626
737
|
if (isRegex(key)) {
|
|
627
738
|
const regex = new RegExp(key.slice(1, -1));
|
|
628
739
|
if (regex.test(item.name) || regex.test(path)) return true;
|
|
@@ -630,6 +741,7 @@ function resolveMockOverride(properties = {}, item, nonNullableOption) {
|
|
|
630
741
|
if (`#.${stripArrayMarkerSegments(key)}` === normalizedPath) return true;
|
|
631
742
|
return false;
|
|
632
743
|
});
|
|
744
|
+
if (!property) property = entries.find(([key]) => !isRegex(key) && !key.includes(".") && key === item.name);
|
|
633
745
|
if (!property) return;
|
|
634
746
|
return {
|
|
635
747
|
value: getNullable(property[1], isNullableSchema(item), nonNullableOption),
|
|
@@ -696,7 +808,7 @@ function hasOverrideTouchingSchema(schemaProperties, mockOptions, operationId, t
|
|
|
696
808
|
});
|
|
697
809
|
});
|
|
698
810
|
}
|
|
699
|
-
function resolveMockValue({ schema, mockOptions, operationId, tags, combine, context, imports, existingReferencedProperties, splitMockImplementations, allowOverride }) {
|
|
811
|
+
function resolveMockValue({ schema, mockOptions, operationId, tags, combine, context, imports, existingReferencedProperties, existingReferencedAllOfRefs = [], splitMockImplementations, allowOverride }) {
|
|
700
812
|
if (isReference(schema)) {
|
|
701
813
|
const schemaReference = schema;
|
|
702
814
|
const { name, refPaths } = getRefInfo(typeof schema.$ref === "string" ? schema.$ref : "", context);
|
|
@@ -757,6 +869,7 @@ function resolveMockValue({ schema, mockOptions, operationId, tags, combine, con
|
|
|
757
869
|
context,
|
|
758
870
|
imports,
|
|
759
871
|
existingReferencedProperties,
|
|
872
|
+
existingReferencedAllOfRefs,
|
|
760
873
|
splitMockImplementations,
|
|
761
874
|
allowOverride
|
|
762
875
|
});
|
|
@@ -764,9 +877,13 @@ function resolveMockValue({ schema, mockOptions, operationId, tags, combine, con
|
|
|
764
877
|
const funcName = `get${pascal(operationId)}Response${pascal(newSchema.name)}Mock`;
|
|
765
878
|
if (!splitMockImplementations.some((f) => f.includes(`export const ${funcName}`))) {
|
|
766
879
|
const discriminatedProperty = newSchema.discriminator?.propertyName;
|
|
767
|
-
let
|
|
768
|
-
if (discriminatedProperty)
|
|
769
|
-
const
|
|
880
|
+
let overrideType = `Partial<${newSchema.name}>`;
|
|
881
|
+
if (discriminatedProperty) overrideType = `Omit<${overrideType}, '${discriminatedProperty}'>`;
|
|
882
|
+
const { param, returnType, returnCast } = getMockFactorySignatureParts(newSchema.name, mockOptions, {
|
|
883
|
+
isOverridable: true,
|
|
884
|
+
overrideType
|
|
885
|
+
});
|
|
886
|
+
const func = formatMockFactoryDeclaration(funcName, param, returnType, `{${scalar.value.startsWith("...") ? "" : "..."}${scalar.value}, ...${overrideVarName}}`, returnCast, { terminateStatement: true });
|
|
770
887
|
splitMockImplementations.push(func);
|
|
771
888
|
}
|
|
772
889
|
scalar.value = newSchema.nullable ? `${funcName}()` : `{...${funcName}()}`;
|
|
@@ -787,6 +904,7 @@ function resolveMockValue({ schema, mockOptions, operationId, tags, combine, con
|
|
|
787
904
|
context,
|
|
788
905
|
imports,
|
|
789
906
|
existingReferencedProperties,
|
|
907
|
+
existingReferencedAllOfRefs,
|
|
790
908
|
splitMockImplementations,
|
|
791
909
|
allowOverride
|
|
792
910
|
}),
|
|
@@ -817,7 +935,7 @@ function getReferenceName(ref, context) {
|
|
|
817
935
|
if (!ref) return "";
|
|
818
936
|
return getRefInfo(ref, context).name;
|
|
819
937
|
}
|
|
820
|
-
function combineSchemasMock({ item, separator, mockOptions, operationId, tags, combine, context, imports, existingReferencedProperties, splitMockImplementations }) {
|
|
938
|
+
function combineSchemasMock({ item, separator, mockOptions, operationId, tags, combine, context, imports, existingReferencedProperties, existingReferencedAllOfRefs = [], splitMockImplementations }) {
|
|
821
939
|
const combineImports = [];
|
|
822
940
|
const includedProperties = [...combine?.includedProperties ?? []];
|
|
823
941
|
const separatorItems = item[separator] ?? [];
|
|
@@ -854,6 +972,7 @@ function combineSchemasMock({ item, separator, mockOptions, operationId, tags, c
|
|
|
854
972
|
context,
|
|
855
973
|
imports,
|
|
856
974
|
existingReferencedProperties,
|
|
975
|
+
existingReferencedAllOfRefs,
|
|
857
976
|
splitMockImplementations
|
|
858
977
|
}) : void 0;
|
|
859
978
|
includedProperties.push(...itemResolvedValue?.includedProperties ?? []);
|
|
@@ -867,7 +986,7 @@ function combineSchemasMock({ item, separator, mockOptions, operationId, tags, c
|
|
|
867
986
|
let value = separator === "allOf" ? "" : "faker.helpers.arrayElement([";
|
|
868
987
|
for (const val of separatorItems) {
|
|
869
988
|
const refName = isReference(val) ? getReferenceName(val.$ref, context) : "";
|
|
870
|
-
if (separator === "allOf" ? refName && (refName === item.name || existingReferencedProperties.includes(refName) && !item.isRef) : refName && existingReferencedProperties.includes(refName)) {
|
|
989
|
+
if (separator === "allOf" ? refName && (refName === item.name || existingReferencedProperties.includes(refName) && !item.isRef || existingReferencedAllOfRefs.includes(refName)) : refName && existingReferencedProperties.includes(refName)) {
|
|
871
990
|
if (separatorItems.length === 1) value = "undefined";
|
|
872
991
|
continue;
|
|
873
992
|
}
|
|
@@ -897,6 +1016,7 @@ function combineSchemasMock({ item, separator, mockOptions, operationId, tags, c
|
|
|
897
1016
|
context,
|
|
898
1017
|
imports,
|
|
899
1018
|
existingReferencedProperties,
|
|
1019
|
+
existingReferencedAllOfRefs: separator === "allOf" && refName ? [...existingReferencedAllOfRefs, refName] : [],
|
|
900
1020
|
splitMockImplementations
|
|
901
1021
|
});
|
|
902
1022
|
combineImports.push(...resolvedValue.imports);
|
|
@@ -992,6 +1112,7 @@ function getMockWithoutFunc(spec, override) {
|
|
|
992
1112
|
numberMin: override?.mock?.numberMin,
|
|
993
1113
|
numberMax: override?.mock?.numberMax,
|
|
994
1114
|
required: override?.mock?.required,
|
|
1115
|
+
nonNullable: override?.mock?.nonNullable,
|
|
995
1116
|
fractionDigits: override?.mock?.fractionDigits,
|
|
996
1117
|
...override?.mock?.properties ? { properties: getMockPropertiesWithoutFunc(override.mock.properties, spec) } : {},
|
|
997
1118
|
...override?.mock?.format ? { format: getMockPropertiesWithoutFunc(override.mock.format, spec) } : {},
|
|
@@ -1190,7 +1311,28 @@ function generateDefinition(name, route, getResponseMockFunctionNameBase, handle
|
|
|
1190
1311
|
const overrideResponseType = `Partial<Extract<${nonVoidMockReturnType}, object>>`;
|
|
1191
1312
|
const shouldPreferJsonResponse = hasJsonContentType && !hasStringReturnType;
|
|
1192
1313
|
const needsRuntimeContentTypeSwitch = isTextResponse && hasJsonContentType && hasStringReturnType && mockReturnType !== "string";
|
|
1193
|
-
const
|
|
1314
|
+
const mockOptionsFromOverride = override.mock;
|
|
1315
|
+
const strictMock = isStrictMock(mockOptionsFromOverride);
|
|
1316
|
+
const schemaTypeNames = strictMock ? getSchemaTypeNamesFromResponses(responses) : [];
|
|
1317
|
+
const strictMockReturnType = strictMock ? applyStrictMockReturnType(nonVoidMockReturnType, schemaTypeNames) : nonVoidMockReturnType;
|
|
1318
|
+
const simpleSchemaReturnType = strictMock ? getSimpleSchemaReturnType(nonVoidMockReturnType, schemaTypeNames) : void 0;
|
|
1319
|
+
let mockFactoryParam = "";
|
|
1320
|
+
let mockFactoryReturnType = nonVoidMockReturnType;
|
|
1321
|
+
let mockFactoryReturnCast = "";
|
|
1322
|
+
if (isResponseOverridable) if (strictMock && simpleSchemaReturnType) {
|
|
1323
|
+
const signature = getMockFactorySignatureParts(simpleSchemaReturnType, mockOptionsFromOverride, {
|
|
1324
|
+
isOverridable: true,
|
|
1325
|
+
overrideType: overrideResponseType
|
|
1326
|
+
});
|
|
1327
|
+
mockFactoryParam = signature.param;
|
|
1328
|
+
mockFactoryReturnType = signature.returnType;
|
|
1329
|
+
mockFactoryReturnCast = signature.returnCast;
|
|
1330
|
+
} else {
|
|
1331
|
+
mockFactoryParam = `overrideResponse: ${overrideResponseType} = {}`;
|
|
1332
|
+
mockFactoryReturnType = strictMock ? strictMockReturnType : nonVoidMockReturnType;
|
|
1333
|
+
}
|
|
1334
|
+
else if (strictMock) mockFactoryReturnType = strictMockReturnType;
|
|
1335
|
+
const mockImplementation = isReturnHttpResponse ? `${mockImplementations}${formatMockFactoryDeclaration(getResponseMockFunctionName, mockFactoryParam, mockFactoryReturnType, value, mockFactoryReturnCast, { omitReturnType: Boolean(mockData) })}\n\n` : mockImplementations;
|
|
1194
1336
|
const delay = getDelay(override, isFunction(mock) ? void 0 : mock);
|
|
1195
1337
|
const infoParam = "info";
|
|
1196
1338
|
const resolvedResponseExpr = `overrideResponse !== undefined
|
|
@@ -1260,35 +1402,40 @@ export const ${handlerName} = (overrideResponse?: ${mockReturnType} | ((${infoPa
|
|
|
1260
1402
|
handlerName,
|
|
1261
1403
|
handler: handlerImplementation
|
|
1262
1404
|
},
|
|
1263
|
-
imports: includeResponseImports
|
|
1405
|
+
imports: includeResponseImports,
|
|
1406
|
+
strictMockSchemaTypeNames: strictMock && schemaTypeNames.length > 0 ? schemaTypeNames : void 0
|
|
1264
1407
|
};
|
|
1265
1408
|
}
|
|
1266
1409
|
function generateMSW(generatorVerbOptions, generatorOptions) {
|
|
1267
1410
|
const { pathRoute, override, mock } = generatorOptions;
|
|
1268
|
-
const {
|
|
1411
|
+
const { operationName, response } = generatorVerbOptions;
|
|
1269
1412
|
const overrideBaseUrl = override.mock && "baseUrl" in override.mock ? override.mock.baseUrl : void 0;
|
|
1270
1413
|
const mockBaseUrl = mock && isMswMock(mock) ? mock.baseUrl : void 0;
|
|
1271
1414
|
const route = getRouteMSW(pathRoute, overrideBaseUrl ?? mockBaseUrl);
|
|
1272
|
-
const handlerName = `get${pascal(
|
|
1273
|
-
const getResponseMockFunctionName = `get${pascal(
|
|
1415
|
+
const handlerName = `get${pascal(operationName)}MockHandler`;
|
|
1416
|
+
const getResponseMockFunctionName = `get${pascal(operationName)}ResponseMock`;
|
|
1274
1417
|
const splitMockImplementations = [];
|
|
1275
1418
|
const baseDefinition = generateDefinition("", route, getResponseMockFunctionName, handlerName, generatorVerbOptions, generatorOptions, response.definition.success, response.types.success[0]?.key ?? "200", response.imports, response.types.success, response.contentTypes, splitMockImplementations);
|
|
1276
1419
|
const mockImplementations = [baseDefinition.implementation.function];
|
|
1277
1420
|
const handlerImplementations = [baseDefinition.implementation.handler];
|
|
1278
1421
|
const imports = [...baseDefinition.imports];
|
|
1422
|
+
const strictMockSchemaTypeNames = new Set(baseDefinition.strictMockSchemaTypeNames);
|
|
1279
1423
|
if (generatorOptions.mock && isObject(generatorOptions.mock) && generatorOptions.mock.generateEachHttpStatus) for (const statusResponse of [...response.types.success, ...response.types.errors]) {
|
|
1280
1424
|
const definition = generateDefinition(statusResponse.key, route, getResponseMockFunctionName, handlerName, generatorVerbOptions, generatorOptions, statusResponse.value, statusResponse.key, response.imports, [statusResponse], [statusResponse.contentType], splitMockImplementations);
|
|
1281
1425
|
mockImplementations.push(definition.implementation.function);
|
|
1282
1426
|
handlerImplementations.push(definition.implementation.handler);
|
|
1283
1427
|
imports.push(...definition.imports);
|
|
1428
|
+
for (const name of definition.strictMockSchemaTypeNames ?? []) strictMockSchemaTypeNames.add(name);
|
|
1284
1429
|
}
|
|
1430
|
+
const aggregatedStrictNames = [...strictMockSchemaTypeNames];
|
|
1285
1431
|
return {
|
|
1286
1432
|
implementation: {
|
|
1287
1433
|
function: mockImplementations.join("\n"),
|
|
1288
1434
|
handlerName,
|
|
1289
1435
|
handler: handlerImplementations.join("\n")
|
|
1290
1436
|
},
|
|
1291
|
-
imports
|
|
1437
|
+
imports,
|
|
1438
|
+
strictMockSchemaTypeNames: aggregatedStrictNames.length > 0 ? aggregatedStrictNames : void 0
|
|
1292
1439
|
};
|
|
1293
1440
|
}
|
|
1294
1441
|
//#endregion
|
|
@@ -1325,7 +1472,8 @@ function generateFaker(generatorVerbOptions, generatorOptions) {
|
|
|
1325
1472
|
handler: "",
|
|
1326
1473
|
handlerName: ""
|
|
1327
1474
|
},
|
|
1328
|
-
imports: result.imports
|
|
1475
|
+
imports: result.imports,
|
|
1476
|
+
strictMockSchemaTypeNames: result.strictMockSchemaTypeNames
|
|
1329
1477
|
};
|
|
1330
1478
|
}
|
|
1331
1479
|
/**
|
|
@@ -1339,6 +1487,7 @@ function generateFaker(generatorVerbOptions, generatorOptions) {
|
|
|
1339
1487
|
*/
|
|
1340
1488
|
function generateFakerForSchemas(schemas, context, options) {
|
|
1341
1489
|
const factories = [];
|
|
1490
|
+
const strictMockTypeNames = /* @__PURE__ */ new Set();
|
|
1342
1491
|
const allImports = [];
|
|
1343
1492
|
const splitMockImplementations = [];
|
|
1344
1493
|
const localFactoryNames = new Set(schemas.filter((s) => !!s.schema).map((s) => `get${pascal(s.name)}Mock`));
|
|
@@ -1365,7 +1514,12 @@ function generateFakerForSchemas(schemas, context, options) {
|
|
|
1365
1514
|
});
|
|
1366
1515
|
allImports.push(...result.imports, ...factoryImports);
|
|
1367
1516
|
const typeName = pascal(name);
|
|
1368
|
-
const
|
|
1517
|
+
const { param, returnType, returnCast } = getMockFactorySignatureParts(typeName, mockOptions, {
|
|
1518
|
+
isOverridable: result.value.includes("overrideResponse"),
|
|
1519
|
+
overrideType: `Partial<${typeName}>`
|
|
1520
|
+
});
|
|
1521
|
+
const factory = formatMockFactoryDeclaration(factoryName, param, returnType, result.value, returnCast);
|
|
1522
|
+
if (isStrictMock(mockOptions)) strictMockTypeNames.add(typeName);
|
|
1369
1523
|
factories.push(factory);
|
|
1370
1524
|
allImports.push({
|
|
1371
1525
|
name: pascal(name),
|
|
@@ -1384,9 +1538,12 @@ function generateFakerForSchemas(schemas, context, options) {
|
|
|
1384
1538
|
if (!existing.values && imp.values) mergedImports.set(key, imp);
|
|
1385
1539
|
}
|
|
1386
1540
|
const uniqueImports = [...mergedImports.values()];
|
|
1541
|
+
const implementation = [...splitMockImplementations, ...factories].filter(Boolean).join("\n\n");
|
|
1542
|
+
const aggregatedStrictNames = [...strictMockTypeNames];
|
|
1387
1543
|
return {
|
|
1388
|
-
implementation
|
|
1389
|
-
imports: uniqueImports
|
|
1544
|
+
implementation,
|
|
1545
|
+
imports: uniqueImports,
|
|
1546
|
+
strictMockSchemaTypeNames: aggregatedStrictNames.length > 0 ? aggregatedStrictNames : void 0
|
|
1390
1547
|
};
|
|
1391
1548
|
}
|
|
1392
1549
|
//#endregion
|
|
@@ -1434,6 +1591,6 @@ function generateMock(generatorVerbOptions, generatorOptions) {
|
|
|
1434
1591
|
}
|
|
1435
1592
|
}
|
|
1436
1593
|
//#endregion
|
|
1437
|
-
export { DEFAULT_FAKER_OPTIONS, DEFAULT_MSW_OPTIONS, generateFaker, generateFakerForSchemas, generateFakerImports, generateMSW, generateMSWImports, generateMock, generateMockImports, getDefaultMockOptionsForType };
|
|
1594
|
+
export { DEFAULT_FAKER_OPTIONS, DEFAULT_MSW_OPTIONS, buildStrictMockTypeFileHeader, dedupeStrictMockTypeDeclarations, generateFaker, generateFakerForSchemas, generateFakerImports, generateMSW, generateMSWImports, generateMock, generateMockImports, getDefaultMockOptionsForType };
|
|
1438
1595
|
|
|
1439
1596
|
//# sourceMappingURL=index.mjs.map
|