@kubb/plugin-faker 5.0.3 → 5.0.5
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.cjs +13 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +13 -9
- package/dist/index.js.map +1 -1
- package/package.json +8 -4
package/dist/index.cjs
CHANGED
|
@@ -969,21 +969,24 @@ const fakerKeywordMapper = {
|
|
|
969
969
|
email: () => "faker.internet.email()",
|
|
970
970
|
blob: () => "faker.image.url() as unknown as Blob"
|
|
971
971
|
};
|
|
972
|
-
function getEnumValues(node) {
|
|
973
|
-
if (node.namedEnumValues?.length) return node.namedEnumValues.map((item) => item.value);
|
|
974
|
-
return node.enumValues ?? [];
|
|
975
|
-
}
|
|
976
972
|
function parseEnumValue(value) {
|
|
977
973
|
if (typeof value === "string") return stringify(value);
|
|
978
974
|
return value;
|
|
979
975
|
}
|
|
980
976
|
/**
|
|
981
977
|
* Reads the discriminator literal off a variant, or `undefined` when it can't be determined.
|
|
978
|
+
*
|
|
979
|
+
* A variant carries exactly one discriminator value, so a property resolving to several literals
|
|
980
|
+
* (a `ref` to the enum shared by every branch, say) identifies no branch and narrows nothing.
|
|
982
981
|
*/
|
|
983
982
|
function getDiscriminatorValue(member, discriminatorPropertyName) {
|
|
984
|
-
const
|
|
985
|
-
|
|
986
|
-
|
|
983
|
+
for (const property of kubb_kit.ast.resolveSchemaProperties({
|
|
984
|
+
node: member,
|
|
985
|
+
propertyName: discriminatorPropertyName
|
|
986
|
+
})) {
|
|
987
|
+
const values = kubb_kit.ast.getSchemaLiteralValues(property.schema);
|
|
988
|
+
if (values.length === 1) return values[0];
|
|
989
|
+
}
|
|
987
990
|
}
|
|
988
991
|
/**
|
|
989
992
|
* Type expression for an object property's value, indexed off the parent `typeName`.
|
|
@@ -1048,7 +1051,7 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
1048
1051
|
return `${resolvedName}${this.options.nestedInUnion && (node.schema?.type === "object" || node.schema?.type === "intersection") ? "<object>" : ""}()`;
|
|
1049
1052
|
},
|
|
1050
1053
|
enum(node) {
|
|
1051
|
-
return fakerKeywordMapper.enum(
|
|
1054
|
+
return fakerKeywordMapper.enum(kubb_kit.ast.getSchemaLiteralValues(node).map(parseEnumValue), this.options.typeName);
|
|
1052
1055
|
},
|
|
1053
1056
|
union(node) {
|
|
1054
1057
|
const { discriminatorPropertyName } = node;
|
|
@@ -1059,7 +1062,8 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
1059
1062
|
const typeName = `Extract<NonNullable<${baseTypeName}>, { ${JSON.stringify(discriminatorPropertyName)}: ${parseEnumValue(value)} }>`;
|
|
1060
1063
|
return printNested(member, {
|
|
1061
1064
|
typeName,
|
|
1062
|
-
nestedInObject: true
|
|
1065
|
+
nestedInObject: true,
|
|
1066
|
+
nestedInUnion: true
|
|
1063
1067
|
});
|
|
1064
1068
|
}
|
|
1065
1069
|
return printNested(member, {
|