@kubb/plugin-faker 5.0.0-beta.94 → 5.0.0-beta.98
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 +93 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -16
- package/dist/index.js +93 -71
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -728,22 +728,79 @@ function getOperationParameters(node, options = {}) {
|
|
|
728
728
|
};
|
|
729
729
|
}
|
|
730
730
|
//#endregion
|
|
731
|
-
//#region ../../internals/shared/src/
|
|
731
|
+
//#region ../../internals/shared/src/resolver.ts
|
|
732
732
|
/**
|
|
733
|
-
*
|
|
734
|
-
*
|
|
733
|
+
* Resolves a single operation parameter name with the
|
|
734
|
+
* `<operationId> <in> <name>` template.
|
|
735
735
|
*
|
|
736
|
-
*
|
|
737
|
-
*
|
|
736
|
+
* @example
|
|
737
|
+
* `operationParamName.call(resolver, node, param) // → 'DeletePetPathPetId'`
|
|
738
|
+
*/
|
|
739
|
+
function operationParamName(node, param) {
|
|
740
|
+
return this.name(`${node.operationId} ${param.in} ${param.name}`);
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Builds the shared `param` namespace. Spread the result into `createResolver`
|
|
744
|
+
* and override individual methods next to it when a plugin deviates.
|
|
745
|
+
*
|
|
746
|
+
* @example
|
|
747
|
+
* ```ts
|
|
748
|
+
* createResolver<PluginTs>({ param: createOperationParamResolver(), ... })
|
|
749
|
+
* ```
|
|
750
|
+
*/
|
|
751
|
+
function createOperationParamResolver() {
|
|
752
|
+
return {
|
|
753
|
+
name: operationParamName,
|
|
754
|
+
path(node) {
|
|
755
|
+
return this.name(`${node.operationId} Path`);
|
|
756
|
+
},
|
|
757
|
+
query(node) {
|
|
758
|
+
return this.name(`${node.operationId} Query`);
|
|
759
|
+
},
|
|
760
|
+
headers(node) {
|
|
761
|
+
return this.name(`${node.operationId} Headers`);
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Builds the shared `response` namespace. Spread the result into
|
|
767
|
+
* `createResolver` and add plugin-specific methods (`options`, `error`) next
|
|
768
|
+
* to it.
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* ```ts
|
|
772
|
+
* createResolver<PluginTs>({ response: { ...createOperationResponseResolver(), options(node) {...} }, ... })
|
|
773
|
+
* ```
|
|
774
|
+
*/
|
|
775
|
+
function createOperationResponseResolver() {
|
|
776
|
+
return {
|
|
777
|
+
status(node, statusCode) {
|
|
778
|
+
return this.name(`${node.operationId} Status ${statusCode}`);
|
|
779
|
+
},
|
|
780
|
+
body(node) {
|
|
781
|
+
return this.name(`${node.operationId} Body`);
|
|
782
|
+
},
|
|
783
|
+
responses(node) {
|
|
784
|
+
return this.name(`${node.operationId} Responses`);
|
|
785
|
+
},
|
|
786
|
+
response(node) {
|
|
787
|
+
return this.name(`${node.operationId} Response`);
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Builds a resolver `file` override whose base name runs every path segment
|
|
793
|
+
* through `toFilePath`, casing the final segment with `caseLast`.
|
|
738
794
|
*
|
|
739
795
|
* @example
|
|
740
796
|
* ```ts
|
|
741
|
-
*
|
|
797
|
+
* createResolver<PluginTs>({ file: createCasedFile(pascalCase), ... })
|
|
742
798
|
* ```
|
|
743
799
|
*/
|
|
744
|
-
function
|
|
745
|
-
|
|
746
|
-
|
|
800
|
+
function createCasedFile(caseLast) {
|
|
801
|
+
return { baseName({ name, extname }) {
|
|
802
|
+
return `${toFilePath(name, caseLast)}${extname}`;
|
|
803
|
+
} };
|
|
747
804
|
}
|
|
748
805
|
//#endregion
|
|
749
806
|
//#region ../../internals/shared/src/group.ts
|
|
@@ -915,7 +972,7 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
915
972
|
return fakerKeywordMapper.time(node.representation ?? "string", this.options.dateParser);
|
|
916
973
|
},
|
|
917
974
|
ref(node) {
|
|
918
|
-
const refName =
|
|
975
|
+
const refName = kubb_kit.ast.resolveRefName(node);
|
|
919
976
|
if (!refName) throw new Error("Name not defined for ref node");
|
|
920
977
|
if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
|
|
921
978
|
const resolvedName = node.ref ? this.options.resolver.name(refName) : refName;
|
|
@@ -930,10 +987,13 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
930
987
|
const baseTypeName = this.options.typeName;
|
|
931
988
|
const items = kubb_kit.ast.mapSchemaMembers(node, (member) => {
|
|
932
989
|
const value = discriminatorPropertyName ? getDiscriminatorValue(member, discriminatorPropertyName) : void 0;
|
|
933
|
-
if (baseTypeName && value !== void 0)
|
|
934
|
-
typeName
|
|
935
|
-
|
|
936
|
-
|
|
990
|
+
if (baseTypeName && value !== void 0) {
|
|
991
|
+
const typeName = `Extract<NonNullable<${baseTypeName}>, { ${JSON.stringify(discriminatorPropertyName)}: ${parseEnumValue(value)} }>`;
|
|
992
|
+
return printNested(member, {
|
|
993
|
+
typeName,
|
|
994
|
+
nestedInObject: true
|
|
995
|
+
});
|
|
996
|
+
}
|
|
937
997
|
return printNested(member, {
|
|
938
998
|
typeName: baseTypeName,
|
|
939
999
|
nestedInObject: true,
|
|
@@ -993,7 +1053,7 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
993
1053
|
name: "faker",
|
|
994
1054
|
renderer: kubb_jsx.jsxRenderer,
|
|
995
1055
|
schema(node, ctx) {
|
|
996
|
-
const {
|
|
1056
|
+
const { config, resolver, root } = ctx;
|
|
997
1057
|
const { output, group, dateParser, regexGenerator, seed, locale, printer } = ctx.options;
|
|
998
1058
|
const pluginTs = ctx.driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
999
1059
|
if (!node.name || !pluginTs) return;
|
|
@@ -1030,8 +1090,7 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1030
1090
|
dateParser,
|
|
1031
1091
|
regexGenerator,
|
|
1032
1092
|
nodes: printer?.nodes,
|
|
1033
|
-
cyclicSchemas
|
|
1034
|
-
nameMapping: getOasAdapter(adapter).options.nameMapping
|
|
1093
|
+
cyclicSchemas
|
|
1035
1094
|
});
|
|
1036
1095
|
const fakerText = printerInstance.print(node) ?? "undefined";
|
|
1037
1096
|
const typeReference = resolveTypeReference({
|
|
@@ -1042,16 +1101,12 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1042
1101
|
filePath: meta.file.path,
|
|
1043
1102
|
typeFilePath: meta.typeFile.path
|
|
1044
1103
|
});
|
|
1045
|
-
const usedImports = filterUsedImports(
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
output,
|
|
1052
|
-
group: group ?? void 0
|
|
1053
|
-
}).path
|
|
1054
|
-
})), fakerText);
|
|
1104
|
+
const usedImports = filterUsedImports(resolver.imports({
|
|
1105
|
+
node,
|
|
1106
|
+
root,
|
|
1107
|
+
output,
|
|
1108
|
+
group: group ?? void 0
|
|
1109
|
+
}), fakerText);
|
|
1055
1110
|
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
1056
1111
|
baseName: meta.file.baseName,
|
|
1057
1112
|
path: meta.file.path,
|
|
@@ -1116,7 +1171,7 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1116
1171
|
});
|
|
1117
1172
|
},
|
|
1118
1173
|
operation(node, ctx) {
|
|
1119
|
-
const {
|
|
1174
|
+
const { config, resolver, root } = ctx;
|
|
1120
1175
|
const { output, group, dateParser, regexGenerator, seed, locale, printer } = ctx.options;
|
|
1121
1176
|
const pluginTs = ctx.driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1122
1177
|
if (!pluginTs) return;
|
|
@@ -1216,16 +1271,12 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1216
1271
|
})
|
|
1217
1272
|
};
|
|
1218
1273
|
function resolveMockImports(schema) {
|
|
1219
|
-
return
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
output,
|
|
1226
|
-
group: group ?? void 0
|
|
1227
|
-
}).path
|
|
1228
|
-
})).filter((entry) => entry.path !== meta.file.path);
|
|
1274
|
+
return resolver.imports({
|
|
1275
|
+
node: schema,
|
|
1276
|
+
root,
|
|
1277
|
+
output,
|
|
1278
|
+
group: group ?? void 0
|
|
1279
|
+
}).filter((entry) => entry.path !== meta.file.path);
|
|
1229
1280
|
}
|
|
1230
1281
|
function renderEntry({ schema, name, typeName, description, skipImportNames = [] }) {
|
|
1231
1282
|
if (!schema) return null;
|
|
@@ -1237,8 +1288,7 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1237
1288
|
dateParser,
|
|
1238
1289
|
regexGenerator,
|
|
1239
1290
|
nodes: printer?.nodes,
|
|
1240
|
-
cyclicSchemas
|
|
1241
|
-
nameMapping: getOasAdapter(adapter).options.nameMapping
|
|
1291
|
+
cyclicSchemas
|
|
1242
1292
|
});
|
|
1243
1293
|
const fakerText = printerInstance.print(schema) ?? "undefined";
|
|
1244
1294
|
const { imports, aliases } = aliasConflictingImports(filterUsedImports(resolveMockImports(schema), fakerText, skipImportNames), localHelperNames);
|
|
@@ -1361,37 +1411,9 @@ const resolverFaker = (0, kubb_kit.createResolver)({
|
|
|
1361
1411
|
name(name) {
|
|
1362
1412
|
return ensureValidVarName(camelCase(name, { prefix: "create" }));
|
|
1363
1413
|
},
|
|
1364
|
-
file:
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
param: {
|
|
1368
|
-
name(node, param) {
|
|
1369
|
-
return this.name(`${node.operationId} ${param.in} ${param.name}`);
|
|
1370
|
-
},
|
|
1371
|
-
path(node) {
|
|
1372
|
-
return this.name(`${node.operationId} Path`);
|
|
1373
|
-
},
|
|
1374
|
-
query(node) {
|
|
1375
|
-
return this.name(`${node.operationId} Query`);
|
|
1376
|
-
},
|
|
1377
|
-
headers(node) {
|
|
1378
|
-
return this.name(`${node.operationId} Headers`);
|
|
1379
|
-
}
|
|
1380
|
-
},
|
|
1381
|
-
response: {
|
|
1382
|
-
status(node, statusCode) {
|
|
1383
|
-
return this.name(`${node.operationId} Status ${statusCode}`);
|
|
1384
|
-
},
|
|
1385
|
-
body(node) {
|
|
1386
|
-
return this.name(`${node.operationId} Body`);
|
|
1387
|
-
},
|
|
1388
|
-
response(node) {
|
|
1389
|
-
return this.name(`${node.operationId} Response`);
|
|
1390
|
-
},
|
|
1391
|
-
responses(node) {
|
|
1392
|
-
return this.name(`${node.operationId} Responses`);
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1414
|
+
file: createCasedFile((part) => camelCase(part, { prefix: "create" })),
|
|
1415
|
+
param: createOperationParamResolver(),
|
|
1416
|
+
response: createOperationResponseResolver()
|
|
1395
1417
|
});
|
|
1396
1418
|
//#endregion
|
|
1397
1419
|
//#region src/plugin.ts
|