UNPKG

@kubb/plugin-faker

Version:
137 lines (135 loc) 5.88 kB
import { Faker } from './chunk-R6YJBMLT.js'; import { createReactGenerator, schemaKeywords, SchemaGenerator } from '@kubb/plugin-oas'; import { Oas } from '@kubb/plugin-oas/components'; import { useSchemaManager, useOas, useOperationManager } from '@kubb/plugin-oas/hooks'; import { getFooter, getBanner } from '@kubb/plugin-oas/utils'; import { pluginTsName } from '@kubb/plugin-ts'; import { useApp, File } from '@kubb/react'; import { jsxs, jsx } from '@kubb/react/jsx-runtime'; var fakerGenerator = createReactGenerator({ name: "faker", Operation({ operation, options }) { const { dateParser, regexGenerator, seed, mapper } = options; const { plugin, pluginManager, mode } = useApp(); const oas = useOas(); const { getSchemas, getFile, getGroup } = useOperationManager(); const schemaManager = useSchemaManager(); const file = getFile(operation); const schemas = getSchemas(operation); const schemaGenerator = new 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: [pluginTsName] }), file: schemaManager.getFile(options2.operationName || name, { pluginKey: [pluginTsName], group }) }; const canOverride = tree.some( ({ keyword }) => keyword === schemaKeywords.array || keyword === schemaKeywords.and || keyword === schemaKeywords.object || keyword === schemaKeywords.union || keyword === schemaKeywords.tuple ); return /* @__PURE__ */ jsxs(Oas.Schema, { name, schemaObject, tree, children: [ canOverride && /* @__PURE__ */ jsx(File.Import, { isTypeOnly: true, root: file.path, path: type.file.path, name: [type.name] }), imports.map((imp) => /* @__PURE__ */ jsx(File.Import, { root: file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))), /* @__PURE__ */ jsx( Faker, { name: faker.name, typeName: type.name, description, tree, regexGenerator, dateParser, mapper, seed, canOverride } ) ] }, i); }; return /* @__PURE__ */ jsxs( File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: getBanner({ oas, output: plugin.options.output, config: pluginManager.config }), footer: getFooter({ oas, output: plugin.options.output }), children: [ /* @__PURE__ */ jsx(File.Import, { name: ["faker"], path: "@faker-js/faker" }), regexGenerator === "randexp" && /* @__PURE__ */ jsx(File.Import, { name: "RandExp", path: "randexp" }), dateParser !== "faker" && /* @__PURE__ */ jsx(File.Import, { path: dateParser, name: dateParser }), operationSchemas.map(mapOperationSchema) ] } ); }, Schema({ schema, options }) { const { dateParser, regexGenerator, seed, mapper } = options; const { getName, getFile, getImports } = useSchemaManager(); const { pluginManager, plugin: { options: { output } } } = useApp(); const oas = 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: [pluginTsName] }), file: getFile(schema.name, { pluginKey: [pluginTsName] }) }; const canOverride = schema.tree.some( ({ keyword }) => keyword === schemaKeywords.array || keyword === schemaKeywords.and || keyword === schemaKeywords.object || keyword === schemaKeywords.union || keyword === schemaKeywords.tuple ); return /* @__PURE__ */ jsxs( File, { baseName: faker.file.baseName, path: faker.file.path, meta: faker.file.meta, banner: getBanner({ oas, output, config: pluginManager.config }), footer: getFooter({ oas, output }), children: [ /* @__PURE__ */ jsx(File.Import, { name: ["faker"], path: "@faker-js/faker" }), regexGenerator === "randexp" && /* @__PURE__ */ jsx(File.Import, { name: "RandExp", path: "randexp" }), dateParser !== "faker" && /* @__PURE__ */ jsx(File.Import, { path: dateParser, name: dateParser }), /* @__PURE__ */ jsx(File.Import, { isTypeOnly: true, root: faker.file.path, path: type.file.path, name: [type.name] }), imports.map((imp) => /* @__PURE__ */ jsx(File.Import, { root: faker.file.path, path: imp.path, name: imp.name }, [imp.path, imp.name, imp.isTypeOnly].join("-"))), /* @__PURE__ */ jsx( Faker, { name: faker.name, typeName: type.name, description: schema.value.description, tree: schema.tree, regexGenerator, dateParser, mapper, seed, canOverride } ) ] } ); } }); export { fakerGenerator }; //# sourceMappingURL=chunk-V3URNWBK.js.map //# sourceMappingURL=chunk-V3URNWBK.js.map