@kubb/plugin-faker 3.0.0-alpha.9 → 3.0.0-beta.1
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/README.md +14 -5
- package/dist/chunk-HTZ2R6MK.js +305 -0
- package/dist/chunk-HTZ2R6MK.js.map +1 -0
- package/dist/chunk-KBSFZ5OP.cjs +311 -0
- package/dist/chunk-KBSFZ5OP.cjs.map +1 -0
- package/dist/chunk-QFJUM25Z.js +113 -0
- package/dist/chunk-QFJUM25Z.js.map +1 -0
- package/dist/chunk-U6LOSTOE.cjs +115 -0
- package/dist/chunk-U6LOSTOE.cjs.map +1 -0
- package/dist/components.cjs +6 -556
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +15 -18
- package/dist/components.d.ts +15 -18
- package/dist/components.js +2 -561
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +13 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +8 -0
- package/dist/generators.d.ts +8 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +52 -500
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -125
- package/dist/index.d.ts +4 -125
- package/dist/index.js +39 -494
- package/dist/index.js.map +1 -1
- package/dist/types-C1bqlbxh.d.cts +94 -0
- package/dist/types-C1bqlbxh.d.ts +94 -0
- package/package.json +20 -16
- package/src/components/Faker.tsx +80 -0
- package/src/components/index.ts +1 -2
- package/src/generators/__snapshots__/createPet.ts +26 -0
- package/src/generators/__snapshots__/createPetSeed.ts +30 -0
- package/src/generators/__snapshots__/createPetUnknownTypeAny.ts +26 -0
- package/src/generators/__snapshots__/deletePet.ts +3 -0
- package/src/generators/__snapshots__/enumNames.ts +5 -0
- package/src/generators/__snapshots__/enumVarNames.ts +5 -0
- package/src/generators/__snapshots__/getPets.ts +29 -0
- package/src/generators/__snapshots__/pet.ts +8 -0
- package/src/generators/__snapshots__/petWithDateString.ts +8 -0
- package/src/generators/__snapshots__/petWithDayjs.ts +9 -0
- package/src/generators/__snapshots__/petWithMapper.ts +8 -0
- package/src/generators/__snapshots__/petWithRandExp.ts +9 -0
- package/src/generators/__snapshots__/pets.ts +8 -0
- package/src/generators/__snapshots__/showPetById.ts +29 -0
- package/src/generators/fakerGenerator.tsx +140 -0
- package/src/generators/index.ts +1 -0
- package/src/parser/index.ts +25 -14
- package/src/plugin.ts +29 -36
- package/src/types.ts +28 -67
- package/src/OperationGenerator.tsx +0 -31
- package/src/SchemaGenerator.tsx +0 -31
- package/src/components/OperationSchema.tsx +0 -82
- package/src/components/Schema.tsx +0 -144
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
2
|
-
import { useOas, useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
3
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, useApp } from '@kubb/react'
|
|
5
|
-
|
|
6
|
-
import { SchemaGenerator } from '../SchemaGenerator.tsx'
|
|
7
|
-
|
|
8
|
-
import type { OperationSchema as OperationSchemaType } from '@kubb/plugin-oas'
|
|
9
|
-
import type { ReactNode } from 'react'
|
|
10
|
-
import type { FileMeta, PluginFaker } from '../types.ts'
|
|
11
|
-
import { Schema } from './Schema.tsx'
|
|
12
|
-
|
|
13
|
-
type Props = {
|
|
14
|
-
description?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function OperationSchema({ description }: Props): ReactNode {
|
|
18
|
-
return <Schema withData={false} description={description} />
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type FileProps = {}
|
|
22
|
-
|
|
23
|
-
OperationSchema.File = function ({}: FileProps): ReactNode {
|
|
24
|
-
const { plugin, pluginManager, mode } = useApp<PluginFaker>()
|
|
25
|
-
|
|
26
|
-
const oas = useOas()
|
|
27
|
-
const { getSchemas, getFile } = useOperationManager()
|
|
28
|
-
const operation = useOperation()
|
|
29
|
-
|
|
30
|
-
const file = getFile(operation)
|
|
31
|
-
const schemas = getSchemas(operation)
|
|
32
|
-
const generator = new SchemaGenerator(plugin.options, {
|
|
33
|
-
oas,
|
|
34
|
-
plugin,
|
|
35
|
-
pluginManager,
|
|
36
|
-
mode,
|
|
37
|
-
override: plugin.options.override,
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean)
|
|
41
|
-
|
|
42
|
-
const mapItem = ({ name, schema, description, ...options }: OperationSchemaType, i: number) => {
|
|
43
|
-
// used for this.options.typed
|
|
44
|
-
const typeName = pluginManager.resolveName({
|
|
45
|
-
name,
|
|
46
|
-
pluginKey: [pluginTsName],
|
|
47
|
-
type: 'type',
|
|
48
|
-
})
|
|
49
|
-
const typeFileName = pluginManager.resolveName({
|
|
50
|
-
name: options.operationName || name,
|
|
51
|
-
pluginKey: [pluginTsName],
|
|
52
|
-
type: 'file',
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
// todo replace by getFile
|
|
56
|
-
const typePath = pluginManager.resolvePath({
|
|
57
|
-
baseName: typeFileName,
|
|
58
|
-
pluginKey: [pluginTsName],
|
|
59
|
-
options: { tag: options.operation?.getTags()[0]?.name },
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
const tree = generator.parse({ schema, name })
|
|
63
|
-
|
|
64
|
-
return (
|
|
65
|
-
<Oas.Schema key={i} name={name} value={schema} tree={tree}>
|
|
66
|
-
{typeName && typePath && <File.Import isTypeOnly root={file.path} path={typePath} name={[typeName]} />}
|
|
67
|
-
{plugin.options.dateParser && <File.Import path={plugin.options.dateParser} name={plugin.options.dateParser} />}
|
|
68
|
-
|
|
69
|
-
{mode === 'split' && <Oas.Schema.Imports />}
|
|
70
|
-
<OperationSchema description={description} />
|
|
71
|
-
</Oas.Schema>
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return (
|
|
76
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
77
|
-
<File.Import name={['faker']} path="@faker-js/faker" />
|
|
78
|
-
{plugin.options.regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
|
|
79
|
-
{items.map(mapItem)}
|
|
80
|
-
</File>
|
|
81
|
-
)
|
|
82
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
2
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
3
|
-
import { File, Function, useApp, useFile } from '@kubb/react'
|
|
4
|
-
|
|
5
|
-
import transformers from '@kubb/core/transformers'
|
|
6
|
-
import { schemaKeywords } from '@kubb/plugin-oas'
|
|
7
|
-
import { useSchema } from '@kubb/plugin-oas/hooks'
|
|
8
|
-
import type { ReactNode } from 'react'
|
|
9
|
-
import * as parserFaker from '../parser/index.ts'
|
|
10
|
-
import { pluginFakerName } from '../plugin.ts'
|
|
11
|
-
import type { PluginFaker } from '../types.ts'
|
|
12
|
-
|
|
13
|
-
type Props = {
|
|
14
|
-
description?: string
|
|
15
|
-
withData?: boolean
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function Schema(props: Props): ReactNode {
|
|
19
|
-
const { withData, description } = props
|
|
20
|
-
const { tree, name } = useSchema()
|
|
21
|
-
const {
|
|
22
|
-
pluginManager,
|
|
23
|
-
plugin: {
|
|
24
|
-
options: { dateParser, regexGenerator, mapper, seed },
|
|
25
|
-
},
|
|
26
|
-
} = useApp<PluginFaker>()
|
|
27
|
-
|
|
28
|
-
// all checks are also inside this.schema(React)
|
|
29
|
-
const resolvedName = pluginManager.resolveName({
|
|
30
|
-
name,
|
|
31
|
-
pluginKey: [pluginFakerName],
|
|
32
|
-
type: 'function',
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
const typeName = pluginManager.resolveName({
|
|
36
|
-
name,
|
|
37
|
-
pluginKey: [pluginTsName],
|
|
38
|
-
type: 'type',
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
const fakerText = parserFaker.joinItems(
|
|
42
|
-
tree
|
|
43
|
-
.map((schema) => parserFaker.parse(undefined, schema, { name: resolvedName, typeName, seed, regexGenerator, mapper, withData, dateParser }))
|
|
44
|
-
.filter(Boolean),
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
let fakerDefaultOverride: '' | '[]' | '{}' | undefined = undefined
|
|
48
|
-
let fakerTextWithOverride = fakerText
|
|
49
|
-
|
|
50
|
-
if (withData && fakerText.startsWith('{')) {
|
|
51
|
-
fakerDefaultOverride = '{}'
|
|
52
|
-
fakerTextWithOverride = `{
|
|
53
|
-
...${fakerText},
|
|
54
|
-
...data
|
|
55
|
-
}`
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (withData && fakerText.startsWith('faker.helpers.arrayElements')) {
|
|
59
|
-
fakerDefaultOverride = '[]'
|
|
60
|
-
fakerTextWithOverride = `[
|
|
61
|
-
...${fakerText},
|
|
62
|
-
...data
|
|
63
|
-
]`
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const params = fakerDefaultOverride ? `data: NonNullable<Partial<${typeName}>> = ${fakerDefaultOverride}` : `data?: NonNullable<Partial<${typeName}>>`
|
|
67
|
-
|
|
68
|
-
return (
|
|
69
|
-
<File.Source name={resolvedName} isExportable isIndexable>
|
|
70
|
-
<Function
|
|
71
|
-
export
|
|
72
|
-
name={resolvedName}
|
|
73
|
-
JSDoc={{ comments: [description ? `@description ${transformers.jsStringEscape(description)}` : undefined].filter(Boolean) }}
|
|
74
|
-
params={withData ? params : ''}
|
|
75
|
-
returnType={typeName ? `NonNullable<${typeName}>` : ''}
|
|
76
|
-
>
|
|
77
|
-
{seed ? `faker.seed(${JSON.stringify(seed)})` : ''}
|
|
78
|
-
<br />
|
|
79
|
-
<Function.Return>{fakerTextWithOverride}</Function.Return>
|
|
80
|
-
</Function>
|
|
81
|
-
<br />
|
|
82
|
-
</File.Source>
|
|
83
|
-
)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
type FileProps = {}
|
|
87
|
-
|
|
88
|
-
Schema.File = function ({}: FileProps): ReactNode {
|
|
89
|
-
const { pluginManager } = useApp<PluginFaker>()
|
|
90
|
-
const { tree, schema } = useSchema()
|
|
91
|
-
|
|
92
|
-
const withData = tree.some(
|
|
93
|
-
(schema) =>
|
|
94
|
-
schema.keyword === schemaKeywords.array ||
|
|
95
|
-
schema.keyword === schemaKeywords.and ||
|
|
96
|
-
schema.keyword === schemaKeywords.object ||
|
|
97
|
-
schema.keyword === schemaKeywords.union ||
|
|
98
|
-
schema.keyword === schemaKeywords.tuple,
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
return (
|
|
102
|
-
<Oas.Schema.File output={pluginManager.config.output.path}>
|
|
103
|
-
<Schema.Imports />
|
|
104
|
-
<Schema description={schema?.description} withData={withData} />
|
|
105
|
-
</Oas.Schema.File>
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
Schema.Imports = (): ReactNode => {
|
|
109
|
-
const {
|
|
110
|
-
pluginManager,
|
|
111
|
-
plugin: {
|
|
112
|
-
options: { extName, dateParser, regexGenerator },
|
|
113
|
-
},
|
|
114
|
-
} = useApp<PluginFaker>()
|
|
115
|
-
const { path: root } = useFile()
|
|
116
|
-
const { name, tree, schema } = useSchema()
|
|
117
|
-
|
|
118
|
-
// used for this.options.typed
|
|
119
|
-
const typeName = pluginManager.resolveName({
|
|
120
|
-
name,
|
|
121
|
-
pluginKey: [pluginTsName],
|
|
122
|
-
type: 'type',
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
const typeFileName = pluginManager.resolveName({
|
|
126
|
-
name: name,
|
|
127
|
-
pluginKey: [pluginTsName],
|
|
128
|
-
type: 'file',
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
const typePath = pluginManager.resolvePath({
|
|
132
|
-
baseName: typeFileName,
|
|
133
|
-
pluginKey: [pluginTsName],
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
return (
|
|
137
|
-
<>
|
|
138
|
-
<File.Import name={['faker']} path="@faker-js/faker" />
|
|
139
|
-
{regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
|
|
140
|
-
{dateParser && <File.Import path={dateParser} name={dateParser} />}
|
|
141
|
-
{typeName && typePath && <File.Import isTypeOnly root={root} path={typePath} name={[typeName]} />}
|
|
142
|
-
</>
|
|
143
|
-
)
|
|
144
|
-
}
|