@kubb/plugin-faker 3.0.0-alpha.3 → 3.0.0-alpha.31

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.
Files changed (55) hide show
  1. package/README.md +13 -4
  2. package/dist/chunk-HTZ2R6MK.js +305 -0
  3. package/dist/chunk-HTZ2R6MK.js.map +1 -0
  4. package/dist/chunk-KBSFZ5OP.cjs +311 -0
  5. package/dist/chunk-KBSFZ5OP.cjs.map +1 -0
  6. package/dist/chunk-QFJUM25Z.js +113 -0
  7. package/dist/chunk-QFJUM25Z.js.map +1 -0
  8. package/dist/chunk-U6LOSTOE.cjs +115 -0
  9. package/dist/chunk-U6LOSTOE.cjs.map +1 -0
  10. package/dist/components.cjs +6 -565
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +15 -18
  13. package/dist/components.d.ts +15 -18
  14. package/dist/components.js +2 -570
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +13 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +8 -0
  19. package/dist/generators.d.ts +8 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +48 -505
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +4 -125
  25. package/dist/index.d.ts +4 -125
  26. package/dist/index.js +35 -499
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-C1bqlbxh.d.cts +94 -0
  29. package/dist/types-C1bqlbxh.d.ts +94 -0
  30. package/package.json +20 -16
  31. package/src/components/Faker.tsx +80 -0
  32. package/src/components/index.ts +1 -2
  33. package/src/generators/__snapshots__/createPet.ts +26 -0
  34. package/src/generators/__snapshots__/createPetSeed.ts +30 -0
  35. package/src/generators/__snapshots__/createPetUnknownTypeAny.ts +26 -0
  36. package/src/generators/__snapshots__/deletePet.ts +3 -0
  37. package/src/generators/__snapshots__/enumNames.ts +5 -0
  38. package/src/generators/__snapshots__/enumVarNames.ts +5 -0
  39. package/src/generators/__snapshots__/getPets.ts +29 -0
  40. package/src/generators/__snapshots__/pet.ts +8 -0
  41. package/src/generators/__snapshots__/petWithDateString.ts +8 -0
  42. package/src/generators/__snapshots__/petWithDayjs.ts +9 -0
  43. package/src/generators/__snapshots__/petWithMapper.ts +8 -0
  44. package/src/generators/__snapshots__/petWithRandExp.ts +9 -0
  45. package/src/generators/__snapshots__/pets.ts +8 -0
  46. package/src/generators/__snapshots__/showPetById.ts +29 -0
  47. package/src/generators/fakerGenerator.tsx +140 -0
  48. package/src/generators/index.ts +1 -0
  49. package/src/parser/index.ts +25 -14
  50. package/src/plugin.ts +26 -43
  51. package/src/types.ts +28 -67
  52. package/src/OperationGenerator.tsx +0 -31
  53. package/src/SchemaGenerator.tsx +0 -31
  54. package/src/components/OperationSchema.tsx +0 -84
  55. package/src/components/Schema.tsx +0 -146
@@ -1,84 +0,0 @@
1
- import { Oas } from '@kubb/plugin-oas/components'
2
- import { useOas, useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
3
- import { File, Parser, useApp } from '@kubb/react'
4
- import { pluginTsName } from '@kubb/plugin-ts'
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
- const typePath = pluginManager.resolvePath({
55
- baseName: typeFileName,
56
- pluginKey: [pluginTsName],
57
- options: { tag: options.operation?.getTags()[0]?.name },
58
- })
59
-
60
- const tree = generator.parse({ schema, name })
61
-
62
- return (
63
- <Oas.Schema key={i} name={name} value={schema} tree={tree}>
64
- {typeName && typePath && <File.Import extName={plugin.options.extName} isTypeOnly root={file.path} path={typePath} name={[typeName]} />}
65
- {plugin.options.dateParser && <File.Import path={plugin.options.dateParser} name={plugin.options.dateParser} />}
66
-
67
- {mode === 'split' && <Oas.Schema.Imports extName={plugin.options.extName} />}
68
- <File.Source>
69
- <OperationSchema description={description} />
70
- </File.Source>
71
- </Oas.Schema>
72
- )
73
- }
74
-
75
- return (
76
- <Parser language="typescript">
77
- <File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
78
- <File.Import name={['faker']} path="@faker-js/faker" />
79
- {plugin.options.regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
80
- {items.map(mapItem)}
81
- </File>
82
- </Parser>
83
- )
84
- }
@@ -1,146 +0,0 @@
1
- import { Oas } from '@kubb/plugin-oas/components'
2
- import { File, Function, useApp, useFile } from '@kubb/react'
3
- import { pluginTsName } from '@kubb/plugin-ts'
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
- <>
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
- </>
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
- <File.Source>
105
- <Schema description={schema?.description} withData={withData} />
106
- </File.Source>
107
- </Oas.Schema.File>
108
- )
109
- }
110
- Schema.Imports = (): ReactNode => {
111
- const {
112
- pluginManager,
113
- plugin: {
114
- options: { extName, dateParser, regexGenerator },
115
- },
116
- } = useApp<PluginFaker>()
117
- const { path: root } = useFile()
118
- const { name, tree, schema } = useSchema()
119
-
120
- // used for this.options.typed
121
- const typeName = pluginManager.resolveName({
122
- name,
123
- pluginKey: [pluginTsName],
124
- type: 'type',
125
- })
126
-
127
- const typeFileName = pluginManager.resolveName({
128
- name: name,
129
- pluginKey: [pluginTsName],
130
- type: 'file',
131
- })
132
-
133
- const typePath = pluginManager.resolvePath({
134
- baseName: typeFileName,
135
- pluginKey: [pluginTsName],
136
- })
137
-
138
- return (
139
- <>
140
- <File.Import name={['faker']} path="@faker-js/faker" />
141
- {regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
142
- {dateParser && <File.Import path={dateParser} name={dateParser} />}
143
- {typeName && typePath && <File.Import extName={extName} isTypeOnly root={root} path={typePath} name={[typeName]} />}
144
- </>
145
- )
146
- }