@kubb/plugin-faker
Version:
Faker.js data generator plugin for Kubb, creating realistic mock data from OpenAPI specifications for development and testing.
216 lines (214 loc) • 8.01 kB
JavaScript
const require_components = require('./components-BHsLXkVC.cjs');
let __kubb_plugin_oas = require("@kubb/plugin-oas");
__kubb_plugin_oas = require_components.__toESM(__kubb_plugin_oas);
let __kubb_plugin_ts = require("@kubb/plugin-ts");
__kubb_plugin_ts = require_components.__toESM(__kubb_plugin_ts);
let __kubb_core_hooks = require("@kubb/core/hooks");
__kubb_core_hooks = require_components.__toESM(__kubb_core_hooks);
let __kubb_plugin_oas_components = require("@kubb/plugin-oas/components");
__kubb_plugin_oas_components = require_components.__toESM(__kubb_plugin_oas_components);
let __kubb_plugin_oas_hooks = require("@kubb/plugin-oas/hooks");
__kubb_plugin_oas_hooks = require_components.__toESM(__kubb_plugin_oas_hooks);
let __kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils");
__kubb_plugin_oas_utils = require_components.__toESM(__kubb_plugin_oas_utils);
let __kubb_react = require("@kubb/react");
__kubb_react = require_components.__toESM(__kubb_react);
let __kubb_react_jsx_runtime = require("@kubb/react/jsx-runtime");
__kubb_react_jsx_runtime = require_components.__toESM(__kubb_react_jsx_runtime);
//#region src/generators/fakerGenerator.tsx
const fakerGenerator = (0, __kubb_plugin_oas.createReactGenerator)({
name: "faker",
Operation({ operation, options }) {
const { dateParser, regexGenerator, seed, mapper } = options;
const plugin = (0, __kubb_core_hooks.usePlugin)();
const mode = (0, __kubb_core_hooks.useMode)();
const pluginManager = (0, __kubb_core_hooks.usePluginManager)();
const oas = (0, __kubb_plugin_oas_hooks.useOas)();
const { getSchemas, getFile, getGroup } = (0, __kubb_plugin_oas_hooks.useOperationManager)();
const schemaManager = (0, __kubb_plugin_oas_hooks.useSchemaManager)();
const file = getFile(operation);
const schemas = getSchemas(operation);
const schemaGenerator = new __kubb_plugin_oas.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,...options$1 }, i) => {
const tree = schemaGenerator.parse({
schemaObject,
name
});
const imports = schemaManager.getImports(tree);
const group = options$1.operation ? getGroup(options$1.operation) : void 0;
const faker = {
name: schemaManager.getName(name, { type: "function" }),
file: schemaManager.getFile(name)
};
const type = {
name: schemaManager.getName(name, {
type: "type",
pluginKey: [__kubb_plugin_ts.pluginTsName]
}),
file: schemaManager.getFile(options$1.operationName || name, {
pluginKey: [__kubb_plugin_ts.pluginTsName],
group
})
};
const canOverride = tree.some(({ keyword }) => keyword === __kubb_plugin_oas.schemaKeywords.array || keyword === __kubb_plugin_oas.schemaKeywords.and || keyword === __kubb_plugin_oas.schemaKeywords.object || keyword === __kubb_plugin_oas.schemaKeywords.union || keyword === __kubb_plugin_oas.schemaKeywords.tuple);
return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_plugin_oas_components.Oas.Schema, {
name,
schemaObject,
tree,
children: [
canOverride && /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
isTypeOnly: true,
root: file.path,
path: type.file.path,
name: [type.name]
}),
imports.map((imp) => /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
root: file.path,
path: imp.path,
name: imp.name
}, [
imp.path,
imp.name,
imp.isTypeOnly
].join("-"))),
/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(require_components.Faker, {
name: faker.name,
typeName: type.name,
description,
tree,
regexGenerator,
dateParser,
mapper,
seed,
canOverride
})
]
}, i);
};
return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react.File, {
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: (0, __kubb_plugin_oas_utils.getBanner)({
oas,
output: plugin.options.output,
config: pluginManager.config
}),
footer: (0, __kubb_plugin_oas_utils.getFooter)({
oas,
output: plugin.options.output
}),
children: [
/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
name: ["faker"],
path: "@faker-js/faker"
}),
regexGenerator === "randexp" && /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
name: "RandExp",
path: "randexp"
}),
dateParser !== "faker" && /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
path: dateParser,
name: dateParser
}),
operationSchemas.map(mapOperationSchema)
]
});
},
Schema({ schema, options }) {
const { dateParser, regexGenerator, seed, mapper } = options;
const { getName, getFile, getImports } = (0, __kubb_plugin_oas_hooks.useSchemaManager)();
const { options: { output } } = (0, __kubb_core_hooks.usePlugin)();
const pluginManager = (0, __kubb_core_hooks.usePluginManager)();
const oas = (0, __kubb_plugin_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: [__kubb_plugin_ts.pluginTsName]
}),
file: getFile(schema.name, { pluginKey: [__kubb_plugin_ts.pluginTsName] })
};
const canOverride = schema.tree.some(({ keyword }) => keyword === __kubb_plugin_oas.schemaKeywords.array || keyword === __kubb_plugin_oas.schemaKeywords.and || keyword === __kubb_plugin_oas.schemaKeywords.object || keyword === __kubb_plugin_oas.schemaKeywords.union || keyword === __kubb_plugin_oas.schemaKeywords.tuple || keyword === __kubb_plugin_oas.schemaKeywords.string || keyword === __kubb_plugin_oas.schemaKeywords.integer || keyword === __kubb_plugin_oas.schemaKeywords.number);
return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react.File, {
baseName: faker.file.baseName,
path: faker.file.path,
meta: faker.file.meta,
banner: (0, __kubb_plugin_oas_utils.getBanner)({
oas,
output,
config: pluginManager.config
}),
footer: (0, __kubb_plugin_oas_utils.getFooter)({
oas,
output
}),
children: [
/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
name: ["faker"],
path: "@faker-js/faker"
}),
regexGenerator === "randexp" && /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
name: "RandExp",
path: "randexp"
}),
dateParser !== "faker" && /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
path: dateParser,
name: dateParser
}),
/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
isTypeOnly: true,
root: faker.file.path,
path: type.file.path,
name: [type.name]
}),
imports.map((imp) => /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, {
root: faker.file.path,
path: imp.path,
name: imp.name
}, [
imp.path,
imp.name,
imp.isTypeOnly
].join("-"))),
/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(require_components.Faker, {
name: faker.name,
typeName: type.name,
description: schema.value.description,
tree: schema.tree,
regexGenerator,
dateParser,
mapper,
seed,
canOverride
})
]
});
}
});
//#endregion
Object.defineProperty(exports, 'fakerGenerator', {
enumerable: true,
get: function () {
return fakerGenerator;
}
});
//# sourceMappingURL=fakerGenerator-BgUuFQPq.cjs.map