@kubb/plugin-faker 5.0.0-alpha.9 → 5.0.0-beta.100

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.
@@ -1,107 +0,0 @@
1
- import { jsStringEscape } from '@internals/utils'
2
- import type { Schema } from '@kubb/plugin-oas'
3
- import { isKeyword, schemaKeywords } from '@kubb/plugin-oas'
4
- import { File, Function, FunctionParams } from '@kubb/react-fabric'
5
- import type { FabricReactNode } from '@kubb/react-fabric/types'
6
- import * as parserFaker from '../parser.ts'
7
- import type { PluginFaker } from '../types.ts'
8
-
9
- type Props = {
10
- name: string
11
- typeName: string
12
- tree: Array<Schema>
13
- seed?: PluginFaker['options']['seed']
14
- description?: string
15
- regexGenerator?: PluginFaker['options']['regexGenerator']
16
- mapper?: PluginFaker['options']['mapper']
17
- dateParser?: PluginFaker['options']['dateParser']
18
- canOverride: boolean
19
- }
20
-
21
- export function Faker({ tree, description, name, typeName, seed, regexGenerator, canOverride, mapper, dateParser }: Props): FabricReactNode {
22
- const fakerText = parserFaker.joinItems(
23
- tree
24
- .map((schema, _index, siblings) =>
25
- parserFaker.parse(
26
- { name, schema, parent: undefined, current: schema, siblings },
27
- {
28
- typeName,
29
- rootTypeName: name,
30
- regexGenerator,
31
- mapper,
32
- canOverride,
33
- dateParser,
34
- },
35
- ),
36
- )
37
- .filter((x): x is string => Boolean(x)),
38
- )
39
-
40
- const isArray = fakerText.startsWith('faker.helpers.arrayElements') || fakerText.startsWith('faker.helpers.multiple')
41
- const isRefToArray = tree.some((s) => isKeyword(s, schemaKeywords.schema) && s.args.type === 'array')
42
- const isObject = fakerText.startsWith('{')
43
- const isTuple = fakerText.startsWith('faker.helpers.arrayElement')
44
-
45
- const isSimpleString = name === 'string'
46
- const isSimpleInt = name === 'integer'
47
- const isSimpleFloat = name === 'float'
48
-
49
- let fakerTextWithOverride = fakerText
50
-
51
- if (canOverride && isObject) {
52
- fakerTextWithOverride = `{
53
- ...${fakerText},
54
- ...data || {}
55
- }`
56
- }
57
-
58
- if (canOverride && isTuple) fakerTextWithOverride = `data || ${fakerText}`
59
-
60
- if (canOverride && isArray) {
61
- fakerTextWithOverride = `[
62
- ...${fakerText},
63
- ...data || []
64
- ]`
65
- }
66
-
67
- if (canOverride && isSimpleString) fakerTextWithOverride = 'data ?? faker.string.alpha()'
68
-
69
- if (canOverride && isSimpleInt) fakerTextWithOverride = 'data ?? faker.number.int()'
70
-
71
- if (canOverride && isSimpleFloat) fakerTextWithOverride = 'data ?? faker.number.float()'
72
-
73
- let type = `Partial<${typeName}>`
74
-
75
- if (isArray) type = typeName
76
- if (isRefToArray) type = typeName
77
- if (isSimpleString) type = name
78
- if (isSimpleInt || isSimpleFloat) type = 'number'
79
-
80
- const params = FunctionParams.factory({
81
- data: {
82
- // making a partial out of an array does not make sense
83
- type,
84
- optional: true,
85
- },
86
- })
87
-
88
- let returnType = canOverride ? typeName : undefined
89
-
90
- if (isSimpleString || isSimpleInt || isSimpleFloat) returnType = type
91
-
92
- return (
93
- <File.Source name={name} isExportable isIndexable>
94
- <Function
95
- export
96
- name={name}
97
- JSDoc={{ comments: [description ? `@description ${jsStringEscape(description)}` : undefined].filter(Boolean) }}
98
- params={canOverride ? params.toConstructor() : undefined}
99
- returnType={returnType}
100
- >
101
- {seed ? `faker.seed(${JSON.stringify(seed)})` : undefined}
102
- <br />
103
- {`return ${fakerTextWithOverride}`}
104
- </Function>
105
- </File.Source>
106
- )
107
- }
@@ -1 +0,0 @@
1
- export { Faker } from './Faker.tsx'
@@ -1,170 +0,0 @@
1
- import { useMode, usePluginDriver } from '@kubb/core/hooks'
2
- import { type OperationSchema as OperationSchemaType, SchemaGenerator, schemaKeywords } from '@kubb/plugin-oas'
3
- import { createReactGenerator } from '@kubb/plugin-oas/generators'
4
- import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
5
- import { applyParamsCasing, getBanner, getFooter, getImports, isParameterSchema } from '@kubb/plugin-oas/utils'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { File } from '@kubb/react-fabric'
8
- import { Faker } from '../components'
9
- import type { PluginFaker } from '../types'
10
-
11
- export const fakerGenerator = createReactGenerator<PluginFaker>({
12
- name: 'faker',
13
- Operation({ operation, generator, plugin }) {
14
- const {
15
- options,
16
- options: { dateParser, regexGenerator, seed, mapper },
17
- } = plugin
18
- const mode = useMode()
19
- const driver = usePluginDriver()
20
-
21
- const oas = useOas()
22
- const { getSchemas, getFile, getGroup } = useOperationManager(generator)
23
- const schemaManager = useSchemaManager()
24
-
25
- const file = getFile(operation)
26
- const schemas = getSchemas(operation)
27
- const schemaGenerator = new SchemaGenerator(options, {
28
- fabric: generator.context.fabric,
29
- oas,
30
- plugin,
31
- events: generator.context.events,
32
- driver,
33
- mode,
34
- override: options.override,
35
- })
36
-
37
- const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
38
- .flat()
39
- .filter((x): x is OperationSchemaType => Boolean(x))
40
-
41
- const mapOperationSchema = ({ name, schema, description, ...options }: OperationSchemaType) => {
42
- // Apply paramsCasing transformation if enabled and this is a parameter schema
43
- const shouldTransform = isParameterSchema(name) && plugin.options.paramsCasing
44
- const transformedSchema = shouldTransform ? applyParamsCasing(schema, plugin.options.paramsCasing) : schema
45
-
46
- const tree = schemaGenerator.parse({ schema: transformedSchema, name, parentName: null })
47
- const imports = getImports(tree)
48
- const group = options.operation ? getGroup(options.operation) : undefined
49
-
50
- const faker = {
51
- name: schemaManager.getName(name, { type: 'function' }),
52
- file: schemaManager.getFile(name),
53
- }
54
-
55
- const type = {
56
- name: schemaManager.getName(name, { type: 'type', pluginName: pluginTsName }),
57
- file: schemaManager.getFile(options.operationName || name, { pluginName: pluginTsName, group }),
58
- }
59
-
60
- const canOverride = tree.some(
61
- ({ keyword }) =>
62
- keyword === schemaKeywords.array ||
63
- keyword === schemaKeywords.and ||
64
- keyword === schemaKeywords.object ||
65
- keyword === schemaKeywords.union ||
66
- keyword === schemaKeywords.tuple ||
67
- keyword === schemaKeywords.enum ||
68
- keyword === schemaKeywords.ref,
69
- )
70
-
71
- return (
72
- <>
73
- {canOverride && <File.Import isTypeOnly root={file.path} path={type.file.path} name={[type.name]} />}
74
- {imports.map((imp) => (
75
- <File.Import key={[imp.path, imp.name, imp.isTypeOnly].join('-')} root={file.path} path={imp.path} name={imp.name} />
76
- ))}
77
- <Faker
78
- name={faker.name}
79
- typeName={type.name}
80
- description={description}
81
- tree={tree}
82
- regexGenerator={regexGenerator}
83
- dateParser={dateParser}
84
- mapper={mapper}
85
- seed={seed}
86
- canOverride={canOverride}
87
- />
88
- </>
89
- )
90
- }
91
-
92
- return (
93
- <File
94
- baseName={file.baseName}
95
- path={file.path}
96
- meta={file.meta}
97
- banner={getBanner({ oas, output: plugin.options.output, config: driver.config })}
98
- footer={getFooter({ oas, output: plugin.options.output })}
99
- >
100
- <File.Import name={['faker']} path="@faker-js/faker" />
101
- {regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
102
- {dateParser !== 'faker' && <File.Import path={dateParser} name={dateParser} />}
103
- {operationSchemas.map(mapOperationSchema)}
104
- </File>
105
- )
106
- },
107
- Schema({ schema, plugin }) {
108
- const { getName, getFile } = useSchemaManager()
109
- const {
110
- options: { output, dateParser, regexGenerator, seed, mapper },
111
- } = plugin
112
- const driver = usePluginDriver()
113
- const oas = useOas()
114
- const imports = getImports(schema.tree)
115
-
116
- const faker = {
117
- name: getName(schema.name, { type: 'function' }),
118
- file: getFile(schema.name),
119
- }
120
-
121
- const type = {
122
- name: getName(schema.name, { type: 'type', pluginName: pluginTsName }),
123
- file: getFile(schema.name, { pluginName: pluginTsName }),
124
- }
125
-
126
- const canOverride = schema.tree.some(
127
- ({ keyword }) =>
128
- keyword === schemaKeywords.array ||
129
- keyword === schemaKeywords.and ||
130
- keyword === schemaKeywords.object ||
131
- keyword === schemaKeywords.union ||
132
- keyword === schemaKeywords.tuple ||
133
- keyword === schemaKeywords.ref ||
134
- keyword === schemaKeywords.enum ||
135
- keyword === schemaKeywords.string ||
136
- keyword === schemaKeywords.integer ||
137
- keyword === schemaKeywords.number,
138
- )
139
-
140
- return (
141
- <File
142
- baseName={faker.file.baseName}
143
- path={faker.file.path}
144
- meta={faker.file.meta}
145
- banner={getBanner({ oas, output, config: driver.config })}
146
- footer={getFooter({ oas, output })}
147
- >
148
- <File.Import name={['faker']} path="@faker-js/faker" />
149
- {regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
150
- {dateParser !== 'faker' && <File.Import path={dateParser} name={dateParser} />}
151
- <File.Import isTypeOnly root={faker.file.path} path={type.file.path} name={[type.name]} />
152
- {imports.map((imp) => (
153
- <File.Import key={[imp.path, imp.name, imp.isTypeOnly].join('-')} root={faker.file.path} path={imp.path} name={imp.name} />
154
- ))}
155
-
156
- <Faker
157
- name={faker.name}
158
- typeName={type.name}
159
- description={schema.value.description}
160
- tree={schema.tree}
161
- regexGenerator={regexGenerator}
162
- dateParser={dateParser}
163
- mapper={mapper}
164
- seed={seed}
165
- canOverride={canOverride}
166
- />
167
- </File>
168
- )
169
- },
170
- })
@@ -1 +0,0 @@
1
- export { fakerGenerator } from './fakerGenerator.tsx'
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { pluginFaker, pluginFakerName } from './plugin.ts'
2
- export type { PluginFaker } from './types.ts'