@kubb/plugin-faker
Version:
Generator plugin-faker
139 lines (136 loc) • 6.32 kB
JavaScript
'use strict';
var chunkV3J6VCNW_cjs = require('./chunk-V3J6VCNW.cjs');
var pluginOas = require('@kubb/plugin-oas');
var components = require('@kubb/plugin-oas/components');
var hooks = require('@kubb/plugin-oas/hooks');
var utils = require('@kubb/plugin-oas/utils');
var pluginTs = require('@kubb/plugin-ts');
var react = require('@kubb/react');
var jsxRuntime = require('@kubb/react/jsx-runtime');
var fakerGenerator = pluginOas.createReactGenerator({
name: "faker",
Operation({ operation, options }) {
const { dateParser, regexGenerator, seed, mapper } = options;
const { plugin, pluginManager, mode } = react.useApp();
const oas = hooks.useOas();
const { getSchemas, getFile, getGroup } = hooks.useOperationManager();
const schemaManager = hooks.useSchemaManager();
const file = getFile(operation);
const schemas = getSchemas(operation);
const schemaGenerator = new pluginOas.SchemaGenerator(options, {
oas,
plugin,
pluginManager,
mode,
override: options.override
});
const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
const mapOperationSchema = ({ name, schema: schemaObject, description, ...options2 }, i) => {
const tree = schemaGenerator.parse({ schemaObject, name });
const imports = schemaManager.getImports(tree);
const group = options2.operation ? getGroup(options2.operation) : void 0;
const faker = {
name: schemaManager.getName(name, { type: "function" }),
file: schemaManager.getFile(name)
};
const type = {
name: schemaManager.getName(name, { type: "type", pluginKey: [pluginTs.pluginTsName] }),
file: schemaManager.getFile(options2.operationName || name, { pluginKey: [pluginTs.pluginTsName], group })
};
const canOverride = tree.some(
({ keyword }) => keyword === pluginOas.schemaKeywords.array || keyword === pluginOas.schemaKeywords.and || keyword === pluginOas.schemaKeywords.object || keyword === pluginOas.schemaKeywords.union || keyword === pluginOas.schemaKeywords.tuple
);
return /* @__PURE__ */ jsxRuntime.jsxs(components.Oas.Schema, { name, schemaObject, tree, children: [
canOverride && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { isTypeOnly: true, root: file.path, path: type.file.path, name: [type.name] }),
imports.map((imp) => /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { root: file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))),
/* @__PURE__ */ jsxRuntime.jsx(
chunkV3J6VCNW_cjs.Faker,
{
name: faker.name,
typeName: type.name,
description,
tree,
regexGenerator,
dateParser,
mapper,
seed,
canOverride
}
)
] }, i);
};
return /* @__PURE__ */ jsxRuntime.jsxs(
react.File,
{
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: utils.getBanner({ oas, output: plugin.options.output, config: pluginManager.config }),
footer: utils.getFooter({ oas, output: plugin.options.output }),
children: [
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["faker"], path: "@faker-js/faker" }),
regexGenerator === "randexp" && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: "RandExp", path: "randexp" }),
dateParser !== "faker" && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { path: dateParser, name: dateParser }),
operationSchemas.map(mapOperationSchema)
]
}
);
},
Schema({ schema, options }) {
const { dateParser, regexGenerator, seed, mapper } = options;
const { getName, getFile, getImports } = hooks.useSchemaManager();
const {
pluginManager,
plugin: {
options: { output }
}
} = react.useApp();
const oas = hooks.useOas();
const imports = getImports(schema.tree);
const faker = {
name: getName(schema.name, { type: "function" }),
file: getFile(schema.name)
};
const type = {
name: getName(schema.name, { type: "type", pluginKey: [pluginTs.pluginTsName] }),
file: getFile(schema.name, { pluginKey: [pluginTs.pluginTsName] })
};
const canOverride = schema.tree.some(
({ keyword }) => keyword === pluginOas.schemaKeywords.array || keyword === pluginOas.schemaKeywords.and || keyword === pluginOas.schemaKeywords.object || keyword === pluginOas.schemaKeywords.union || keyword === pluginOas.schemaKeywords.tuple
);
return /* @__PURE__ */ jsxRuntime.jsxs(
react.File,
{
baseName: faker.file.baseName,
path: faker.file.path,
meta: faker.file.meta,
banner: utils.getBanner({ oas, output, config: pluginManager.config }),
footer: utils.getFooter({ oas, output }),
children: [
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["faker"], path: "@faker-js/faker" }),
regexGenerator === "randexp" && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: "RandExp", path: "randexp" }),
dateParser !== "faker" && /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { path: dateParser, name: dateParser }),
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { isTypeOnly: true, root: faker.file.path, path: type.file.path, name: [type.name] }),
imports.map((imp) => /* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { root: faker.file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))),
/* @__PURE__ */ jsxRuntime.jsx(
chunkV3J6VCNW_cjs.Faker,
{
name: faker.name,
typeName: type.name,
description: schema.value.description,
tree: schema.tree,
regexGenerator,
dateParser,
mapper,
seed,
canOverride
}
)
]
}
);
}
});
exports.fakerGenerator = fakerGenerator;
//# sourceMappingURL=chunk-XJASW4QX.cjs.map
//# sourceMappingURL=chunk-XJASW4QX.cjs.map